OSDN Git Service

Reduce the number of #include <Windows.h> and <tchar.h>.
[winmerge-jp/winmerge-jp.git] / Testing / GoogleTest / StringDiffs / stringdiffs_test_bytelevel.cpp
1 #include "pch.h"
2 #include <gtest/gtest.h>
3 #include <vector>
4 #include "stringdiffs.h"
5
6 using std::vector;
7
8 namespace
9 {
10         // The fixture for testing stringdiff.
11         class StringDiffsTestByte : public testing::Test
12         {
13         protected:
14                 // You can remove any or all of the following functions if its body
15                 // is   empty.
16
17                 StringDiffsTestByte()
18                 {
19                         // You can do set-up work for each test here.
20                         strdiff::Init();
21                 }
22
23                 virtual ~StringDiffsTestByte()
24                 {
25                         // You can do clean-up work     that doesn't throw exceptions here.
26                         strdiff::Close();
27                 }
28
29                 // If   the     constructor     and     destructor are not enough for setting up
30                 // and cleaning up each test, you can define the following methods:
31
32                 virtual void SetUp()
33                 {
34                         // Code here will be called     immediately     after the constructor (right
35                         // before each test).
36                 }
37
38                 virtual void TearDown()
39                 {
40                         // Code here will be called     immediately     after each test (right
41                         // before the destructor).
42                 }
43
44                 // Objects declared here can be used by all tests in the test case for Foo.
45         };
46
47         // strdiff::ComputeWordDiffs() parameters are:
48         // String & str1 - the first string to compare
49         // String & str2 - the second string to compare
50         // bool case_sensitive - is the compare case-sensitive?
51         // int whitespace - do we ignore whitespace and how
52         // int breakType - Space (0) or punctuations (1) break
53         // bool byte_level - are we word (false) or byte-level (true) diffing
54         // std::vector<strdiff::wdiff> * pDiffs - resultting diff list
55
56
57         TEST_F(StringDiffsTestByte, ByteLevel19b3)
58         {
59                 strdiff::SetBreakChars(_T(",;"));
60                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(
61                         //  0   1       2       3       4567890123456789012345678901234567890123456789
62                         _T("                            wsprintf(buf, _T(left=  %s,   %d,%d, right=  %s,   %d,%d ),"),
63                         _T("                                    if (len2 < 50)"),
64                         true, true, 0, 1, true);
65                 EXPECT_EQ(4, diffs.size());
66         }
67
68         // Identical strings, case sensitivity, no whitespace, punctuations, byte-level
69         // Second word is different
70         TEST_F(StringDiffsTestByte, ByteLevel19b2)
71         {
72                 strdiff::SetBreakChars(_T(",;"));
73                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(
74                         //  0   1234567890123456789012345678901234567890123456789
75                         _T("    while (1)"),
76                         _T("    for (;;)"), true, true, 0, 1, true);
77                 EXPECT_EQ(2, diffs.size());
78                 strdiff::wdiff *pDiff;
79                 if (diffs.size() >= 1 )
80                 {
81                         pDiff = &diffs[0];
82                         EXPECT_EQ(1, pDiff->begin[0]);
83                         EXPECT_EQ(5, pDiff->end[0]);
84                         EXPECT_EQ(1, pDiff->begin[1]);
85                         EXPECT_EQ(3, pDiff->end[1]);
86                 }       
87                 if (diffs.size() >=2 )
88                 {
89                         pDiff = &diffs[1];
90                         EXPECT_EQ(7, pDiff->begin[0]);
91                         EXPECT_EQ(8, pDiff->end[0]);
92                         EXPECT_EQ(5, pDiff->begin[1]);
93                         EXPECT_EQ(7, pDiff->end[1]);
94                 }       
95                 if (diffs.size() >=3 )
96                 {
97                         pDiff = &diffs[2];
98                         EXPECT_EQ(10, pDiff->begin[0]);
99                         EXPECT_EQ(9, pDiff->end[0]);
100                         EXPECT_EQ(6, pDiff->begin[1]);
101                         EXPECT_EQ(8, pDiff->end[1]);
102                 }       
103         }
104         // Identical strings, case sensitivity, no whitespace, punctuations, byte-level
105         // Second word is different
106         TEST_F(StringDiffsTestByte, ByteLevel19b1)
107         {
108                 strdiff::SetBreakChars(_T(","));
109                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(
110                         //  01234567890123456789012345678901234567890123456789
111                         _T("abcdef,abccef,abcdef,"),
112                         _T("abcdef,abcdef,abcdef,"),
113                         true, true, 0, 1, true);
114                 EXPECT_EQ(1, diffs.size());
115                 strdiff::wdiff *pDiff;
116                 if (diffs.size() >= 1 )
117                 {
118                         pDiff = &diffs[0];
119                         EXPECT_EQ(10, pDiff->begin[0]);
120                         EXPECT_EQ(10, pDiff->end[0]);
121                         EXPECT_EQ(10, pDiff->begin[1]);
122                         EXPECT_EQ(10, pDiff->end[1]);
123                 }       
124         }
125         // NoneIdentical strings, case sensitivity, no whitespace, punctuations, byte-level
126         // Second word is different
127         TEST_F(StringDiffsTestByte, ByteLevel20c)
128         {
129                 strdiff::SetBreakChars(_T(".,;:()[]{}!@#\"$%^&*~+-=<>\'/\\|"));
130                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(
131                         //  01234567890123456789012345678901234567890123456789
132                         _T(""),
133                         _T("            // remove empty records on both side"),
134                         true, true, 0, 1, true);
135                 EXPECT_EQ(1, diffs.size());
136                 strdiff::wdiff *pDiff;
137                 if (diffs.size() >= 1 )
138                 {
139                         pDiff = &diffs[0];
140                         EXPECT_EQ(0, pDiff->begin[0]);
141                         EXPECT_EQ(-1, pDiff->end[0]);
142                         EXPECT_EQ(0, pDiff->begin[1]);
143                         EXPECT_EQ(37, pDiff->end[1]);
144                 }       
145         }
146         // NoneIdentical strings, case sensitivity, no whitespace, punctuations, byte-level
147         // Second word is different
148         TEST_F(StringDiffsTestByte, ByteLevel20a)
149         {
150                 strdiff::SetBreakChars(_T(".,;:()[]{}!@#\"$%^&*~+-=<>\'/\\|"));
151                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(
152                         //  01234567890123456789012345678901234567890123456789
153                         _T(",;+ der abcdef,der,Thomas,abcdef,abcdef,;"),
154                         _T(",;+ der abcdef,Thomas,accdgf,abcdef,-+"),
155                         true, true, 0, 1, true);
156                 EXPECT_EQ(3, diffs.size());
157                 strdiff::wdiff *pDiff;
158                 if (diffs.size() >= 1 )
159                 {
160                         pDiff = &diffs[0];
161                         EXPECT_EQ(15, pDiff->begin[0]);
162                         EXPECT_EQ(18, pDiff->end[0]);
163                         EXPECT_EQ(15, pDiff->begin[1]);
164                         EXPECT_EQ(14, pDiff->end[1]);
165                 }       
166                 if (diffs.size() >=2 )
167                 {
168                         pDiff = &diffs[1];
169                         EXPECT_EQ(27, pDiff->begin[0]);
170                         EXPECT_EQ(30, pDiff->end[0]);
171                         EXPECT_EQ(23, pDiff->begin[1]);
172                         EXPECT_EQ(26, pDiff->end[1]);
173                 }       
174                 if (diffs.size() >=3 )
175                 {
176                         pDiff = &diffs[2];
177                         EXPECT_EQ(40, pDiff->begin[0]);
178                         EXPECT_EQ(40, pDiff->end[0]);
179                         EXPECT_EQ(36, pDiff->begin[1]);
180                         EXPECT_EQ(37, pDiff->end[1]);
181                 }       
182                 if (diffs.size() >=4 )
183                 {
184                         pDiff = &diffs[3];
185                         EXPECT_EQ(40, pDiff->begin[0]);
186                         EXPECT_EQ(40, pDiff->end[0]);
187                         EXPECT_EQ(36, pDiff->begin[1]);
188                         EXPECT_EQ(36, pDiff->end[1]);
189                 }       
190                 if (diffs.size() >=5 )
191                 {
192                         pDiff = &diffs[4];
193                         EXPECT_EQ(41, pDiff->begin[0]);
194                         EXPECT_EQ(40, pDiff->end[0]);
195                         EXPECT_EQ(37, pDiff->begin[1]);
196                         EXPECT_EQ(37, pDiff->end[1]);
197                 }       
198         }
199
200         // NoneIdentical strings, case sensitivity, no whitespace, punctuations, word-level
201         // Second word is different
202         TEST_F(StringDiffsTestByte, ByteLevel19a)
203         {
204                 strdiff::SetBreakChars(_T(","));
205                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(_T("abcdef,abcdef,abcdef,"), _T("abcdef,abccef,abcdef,"),
206                                 true, true, 0, 1, false);
207                 EXPECT_EQ(1, diffs.size());
208                 strdiff::wdiff *pDiff;
209                 if (diffs.size() >= 1 )
210                 {
211                         pDiff = &diffs[0];
212                         EXPECT_EQ(7, pDiff->begin[0]);
213                         EXPECT_EQ(12, pDiff->end[0]);
214                         EXPECT_EQ(7, pDiff->begin[1]);
215                         EXPECT_EQ(12, pDiff->end[1]);
216                 }       
217         }
218         // Identical strings, case sensitivity, no whitespace, punctuations, byte-level
219         // Second word is different
220
221         TEST_F(StringDiffsTestByte, ByteLevel19b)
222         {
223                 strdiff::SetBreakChars(_T(","));
224                 std::vector<strdiff::wdiff> diffs = strdiff::ComputeWordDiffs(_T("abcdef,abccef,abcdef,"), _T("abcdef,abcdef,abcdef,"),
225                                 true, true, 0, 1, true);
226                 EXPECT_EQ(1, diffs.size());
227                 strdiff::wdiff *pDiff;
228                 if (diffs.size() >= 1 )
229                 {
230                         pDiff = &diffs[0];
231                         EXPECT_EQ(10, pDiff->begin[0]);
232                         EXPECT_EQ(10, pDiff->end[0]);
233                         EXPECT_EQ(10, pDiff->begin[1]);
234                         EXPECT_EQ(10, pDiff->end[1]);
235                 }       
236         }
237
238 }  // namespace