roll skia 1111->1115
[chromium-blink-merge.git] / courgette / bsdiff_memory_unittest.cc
blob75de082c7ae5e81038103020a197640a6ab71a47
1 // Copyright (c) 2009 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.
5 #include "courgette/third_party/bsdiff.h"
7 #include <string>
9 #include "base/file_util.h"
10 #include "base/path_service.h"
11 #include "base/string_util.h"
13 #include "courgette/courgette.h"
14 #include "courgette/streams.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 class BSDiffMemoryTest : public testing::Test {
19 public:
20 std::string FileContents(const char* file_name) const;
21 void GenerateAndTestPatch(const std::string& a, const std::string& b) const;
23 std::string GenerateSyntheticInput(size_t length, int seed) const;
25 private:
26 void SetUp() {
27 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir_);
28 test_dir_ = test_dir_.AppendASCII("courgette");
29 test_dir_ = test_dir_.AppendASCII("testdata");
32 FilePath test_dir_;
35 // Reads a test file into a string.
36 std::string BSDiffMemoryTest::FileContents(const char* file_name) const {
37 FilePath file_path = test_dir_;
38 file_path = file_path.AppendASCII(file_name);
39 std::string file_bytes;
40 if (!file_util::ReadFileToString(file_path, &file_bytes)) {
41 EXPECT_TRUE(!"Could not read test data");
43 return file_bytes;
46 void BSDiffMemoryTest::GenerateAndTestPatch(const std::string& old_text,
47 const std::string& new_text) const {
48 courgette::SourceStream old1;
49 courgette::SourceStream new1;
50 old1.Init(old_text.c_str(), old_text.length());
51 new1.Init(new_text.c_str(), new_text.length());
53 courgette::SinkStream patch1;
54 courgette::BSDiffStatus status = CreateBinaryPatch(&old1, &new1, &patch1);
55 EXPECT_EQ(courgette::OK, status);
57 courgette::SourceStream old2;
58 courgette::SourceStream patch2;
59 old2.Init(old_text.c_str(), old_text.length());
60 patch2.Init(patch1);
62 courgette::SinkStream new2;
63 status = ApplyBinaryPatch(&old2, &patch2, &new2);
64 EXPECT_EQ(courgette::OK, status);
65 EXPECT_EQ(new_text.length(), new2.Length());
66 EXPECT_EQ(0, memcmp(new_text.c_str(), new2.Buffer(), new_text.length()));
69 std::string BSDiffMemoryTest::GenerateSyntheticInput(size_t length, int seed)
70 const {
71 static const char* a[8] = {"O", "A", "x", "-", "y", ".", "|", ":"};
72 std::string result;
73 while (result.length() < length) {
74 seed = (seed + 17) * 1049 + (seed >> 27);
75 result.append(a[seed & 7]);
77 result.resize(length);
78 return result;
81 TEST_F(BSDiffMemoryTest, TestEmpty) {
82 GenerateAndTestPatch("", "");
85 TEST_F(BSDiffMemoryTest, TestEmptyVsNonempty) {
86 GenerateAndTestPatch("", "xxx");
89 TEST_F(BSDiffMemoryTest, TestNonemptyVsEmpty) {
90 GenerateAndTestPatch("xxx", "");
93 TEST_F(BSDiffMemoryTest, TestSmallInputsWithSmallChanges) {
94 std::string file1 =
95 "I would not, could not, in a box.\n"
96 "I could not, would not, with a fox.\n"
97 "I will not eat them with a mouse.\n"
98 "I will not eat them in a house.\n"
99 "I will not eat them here or there.\n"
100 "I will not eat them anywhere.\n"
101 "I do not eat green eggs and ham.\n"
102 "I do not like them, Sam-I-am.\n";
103 std::string file2 =
104 "I would not, could not, in a BOX.\n"
105 "I could not, would not, with a FOX.\n"
106 "I will not eat them with a MOUSE.\n"
107 "I will not eat them in a HOUSE.\n"
108 "I will not eat them in a HOUSE.\n" // Extra line.
109 "I will not eat them here or THERE.\n"
110 "I will not eat them ANYWHERE.\n"
111 "I do not eat green eggs and HAM.\n"
112 "I do not like them, Sam-I-am.\n";
113 GenerateAndTestPatch(file1, file2);
116 TEST_F(BSDiffMemoryTest, TestNearPageArrayPageSize) {
117 // This magic number is the size of one block of the PageArray in
118 // third_party/bsdiff_create.cc.
119 size_t critical_size = 1 << 18;
121 // Test first-inputs with sizes that straddle the magic size to test this
122 // PageArray's internal boundary condition.
124 std::string file1 = GenerateSyntheticInput(critical_size, 0);
125 std::string file2 = GenerateSyntheticInput(critical_size, 1);
126 GenerateAndTestPatch(file1, file2);
128 std::string file1a = file1.substr(0, critical_size - 1);
129 GenerateAndTestPatch(file1a, file2);
131 std::string file1b = file1.substr(0, critical_size - 2);
132 GenerateAndTestPatch(file1b, file2);
134 std::string file1c = file1 + file1.substr(0, 1);
135 GenerateAndTestPatch(file1c, file2);
138 TEST_F(BSDiffMemoryTest, TestIndenticalDlls) {
139 std::string file1 = FileContents("en-US.dll");
140 GenerateAndTestPatch(file1, file1);
143 TEST_F(BSDiffMemoryTest, TestDifferentExes) {
144 std::string file1 = FileContents("setup1.exe");
145 std::string file2 = FileContents("setup2.exe");
146 GenerateAndTestPatch(file1, file2);