Enable dev build with the latest repo
[platform/framework/web/chromium-efl.git] / courgette / bsdiff_memory_unittest.cc
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "courgette/third_party/bsdiff/bsdiff.h"
6
7 #include <stddef.h>
8
9 #include "courgette/base_test_unittest.h"
10 #include "courgette/courgette.h"
11 #include "courgette/streams.h"
12
13 class BSDiffMemoryTest : public BaseTest {
14  public:
15   void GenerateAndTestPatch(const std::string& a, const std::string& b) const;
16
17   std::string GenerateSyntheticInput(size_t length, int seed) const;
18 };
19
20 void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text,
21                                             const std::string& new_text) const {
22   courgette::SourceStream old1;
23   courgette::SourceStream new1;
24   old1.Init(old_text.c_str(), old_text.length());
25   new1.Init(new_text.c_str(), new_text.length());
26
27   courgette::SinkStream patch1;
28   bsdiff::BSDiffStatus status =
29       bsdiff::CreateBinaryPatch(&old1, &new1, &patch1);
30   EXPECT_EQ(bsdiff::OK, status);
31
32   courgette::SourceStream old2;
33   courgette::SourceStream patch2;
34   old2.Init(old_text.c_str(), old_text.length());
35   patch2.Init(patch1);
36
37   courgette::SinkStream new2;
38   status = bsdiff::ApplyBinaryPatch(&old2, &patch2, &new2);
39   EXPECT_EQ(bsdiff::OK, status);
40   EXPECT_EQ(new_text.length(), new2.Length());
41   EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length()));
42 }
43
44 std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
45   const {
46   static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
47   std::string result;
48   while (result.length() < length) {
49     seed = (seed + 17) * 1049 + (seed >> 27);
50     result.append(a[seed & 7]);
51   }
52   result.resize(length);
53   return result;
54 }
55
56 TEST_F(BSDiffMemoryTest, TestEmpty) {
57   GenerateAndTestPatch(std::string(), std::string());
58 }
59
60 TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
61   GenerateAndTestPatch(std::string(), "xxx");
62 }
63
64 TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
65   GenerateAndTestPatch("xxx", std::string());
66 }
67
68 TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
69   std::string file1 =
70       "I would not, could not, in a box.\n"
71       "I could not, would not, with a fox.\n"
72       "I will not eat them with a mouse.\n"
73       "I will not eat them in a house.\n"
74       "I will not eat them here or there.\n"
75       "I will not eat them anywhere.\n"
76       "I do not eat green eggs and ham.\n"
77       "I do not like them, Sam-I-am.\n";
78   std::string file2 =
79       "I would not, could not, in a BOX.\n"
80       "I could not, would not, with a FOX.\n"
81       "I will not eat them with a MOUSE.\n"
82       "I will not eat them in a HOUSE.\n"
83       "I will not eat them in a HOUSE.\n"     // Extra line.
84       "I will not eat them here or THERE.\n"
85       "I will not eat them ANYWHERE.\n"
86       "I do not eat green eggs and HAM.\n"
87       "I do not like them, Sam-I-am.\n";
88   GenerateAndTestPatch(file1, file2);
89 }
90
91 TEST_F(BSDiffMemoryTest, TestNearPageArrayPageSize) {
92   // This magic number is the size of one block of the PageArray in
93   // third_party/bsdiff_create.cc.
94   size_t critical_size = 1 << 18;
95
96   // Test first-inputs with sizes that straddle the magic size to test this
97   // PageArray's internal boundary condition.
98
99   std::string file1 = GenerateSyntheticInput(critical_size, 0);
100   std::string file2 = GenerateSyntheticInput(critical_size, 1);
101   GenerateAndTestPatch(file1, file2);
102
103   std::string file1a = file1.substr(0, critical_size - 1);
104   GenerateAndTestPatch(file1a, file2);
105
106   std::string file1b = file1.substr(0, critical_size - 2);
107   GenerateAndTestPatch(file1b, file2);
108
109   std::string file1c = file1 + file1.substr(0, 1);
110   GenerateAndTestPatch(file1c, file2);
111 }
112
113 TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
114   std::string file1 = FileContents("en-US.dll");
115   GenerateAndTestPatch(file1, file1);
116 }
117
118 TEST_F(BSDiffMemoryTest, TestDifferentExes) {
119   std::string file1 = FileContents("setup1.exe");
120   std::string file2 = FileContents("setup2.exe");
121   GenerateAndTestPatch(file1, file2);
122 }
123
124 TEST_F(BSDiffMemoryTest, TestDifferentElfs) {
125   std::string file1 = FileContents("elf-32-1");
126   std::string file2 = FileContents("elf-32-2");
127   GenerateAndTestPatch(file1, file2);
128 }