Debian Codegraph @

hashmerge/combine_test.go

raw
1 // Copyright 2015, Joe Tsai. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.md file.
4
5 package hashmerge
6
7 import (
8 "hash/adler32"
9 "hash/crc32"
10 "hash/crc64"
11 "strings"
12 "testing"
13 )
14
15 // shortString truncates long strings into something more human readable.
16 func shortString(s string) string {
17 if len(s) > 220 {
18 s = s[:100] + "..." + s[len(s)-100:]
19 }
20 return s
21 }
22
23 func TestCombineAdler32(t *testing.T) {
24 var golden = []struct {
25 out uint32
26 in string
27 }{
28 {0x00000001, ""},
29 {0x00620062, "a"},
30 {0x012600c4, "ab"},
31 {0x024d0127, "abc"},
32 {0x03d8018b, "abcd"},
33 {0x05c801f0, "abcde"},
34 {0x081e0256, "abcdef"},
35 {0x0adb02bd, "abcdefg"},
36 {0x0e000325, "abcdefgh"},
37 {0x118e038e, "abcdefghi"},
38 {0x158603f8, "abcdefghij"},
39 {0x3f090f02, "Discard medicine more than two years old."},
40 {0x46d81477, "He who has a shady past knows that nice guys finish last."},
41 {0x40ee0ee1, "I wouldn't marry him with a ten foot pole."},
42 {0x16661315, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
43 {0x5b2e1480, "The days of the digital watch are numbered. -Tom Stoppard"},
44 {0x8c3c09ea, "Nepal premier won't resign."},
45 {0x45ac18fd, "For every action there is an equal and opposite government program."},
46 {0x53c61462, "His money is twice tainted: 'taint yours and 'taint mine."},
47 {0x7e511e63, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
48 {0xe4801a6a, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
49 {0x61b507df, "size: a.out: bad magic"},
50 {0xb8631171, "The major problem is with sendmail. -Mark Horton"},
51 {0x8b5e1904, "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
52 {0x7cc6102b, "If the enemy is within range, then so are you."},
53 {0x700318e7, "It's well we cannot hear the screams/That we create in others' dreams."},
54 {0x1e601747, "You remind me of a TV show, but that's all right: I watch it anyway."},
55 {0xb55b0b09, "C is as portable as Stonehedge!!"},
56 {0x39111dd0, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
57 {0x91dd304f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
58 {0x2e5d1316, "How can you write a big system without C++? -Paul Glick"},
59 {0xd0201df6, "'Invariant assertions' is the most elegant programming technique! -Tom Szymanski"},
60 {0x211297c8, strings.Repeat("\xff", 5548) + "8"},
61 {0xbaa198c8, strings.Repeat("\xff", 5549) + "9"},
62 {0x553499be, strings.Repeat("\xff", 5550) + "0"},
63 {0xf0c19abe, strings.Repeat("\xff", 5551) + "1"},
64 {0x8d5c9bbe, strings.Repeat("\xff", 5552) + "2"},
65 {0x2af69cbe, strings.Repeat("\xff", 5553) + "3"},
66 {0xc9809dbe, strings.Repeat("\xff", 5554) + "4"},
67 {0x69189ebe, strings.Repeat("\xff", 5555) + "5"},
68 {0x86af0001, strings.Repeat("\x00", 1e5)},
69 {0x79660b4d, strings.Repeat("a", 1e5)},
70 {0x110588ee, strings.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1e4)},
71 }
72
73 for _, g := range golden {
74 var splits = []int{
75 0 * (len(g.in) / 1),
76 1 * (len(g.in) / 4),
77 2 * (len(g.in) / 4),
78 3 * (len(g.in) / 4),
79 1 * (len(g.in) / 1),
80 }
81
82 for _, i := range splits {
83 p1, p2 := []byte(g.in[:i]), []byte(g.in[i:])
84 in1, in2 := shortString(g.in[:i]), shortString(g.in[i:])
85 len2 := int64(len(p2))
86 if got := CombineAdler32(adler32.Checksum(p1), adler32.Checksum(p2), len2); got != g.out {
87 t.Errorf("CombineAdler32(Checksum(%q), Checksum(%q), %d) = 0x%x, want 0x%x",
88 in1, in2, len2, got, g.out)
89 }
90 }
91 }
92 }
93
94 func TestCombineCRC32(t *testing.T) {
95 var golden = []struct {
96 ieee, castagnoli, koopman uint32
97 in string
98 }{
99 {0x00000000, 0x00000000, 0x00000000, ""},
100 {0xe8b7be43, 0xc1d04330, 0x0da2aa8a, "a"},
101 {0x9e83486d, 0xe2a22936, 0x31ec935a, "ab"},
102 {0x352441c2, 0x364b3fb7, 0xba2322ac, "abc"},
103 {0xed82cd11, 0x92c80a31, 0xe0a6bcf7, "abcd"},
104 {0x8587d865, 0xc450d697, 0xac046415, "abcde"},
105 {0x4b8e39ef, 0x53bceff1, 0x7589981b, "abcdef"},
106 {0x312a6aa6, 0xe627f441, 0x7999acb5, "abcdefg"},
107 {0xaeef2a50, 0x0a9421b7, 0xd5cc0e40, "abcdefgh"},
108 {0x8da988af, 0x2ddc99fc, 0x39080d0d, "abcdefghi"},
109 {0x3981703a, 0xe6599437, 0xd6205881, "abcdefghij"},
110 {0x6b9cdfe7, 0xb2cc01fe, 0x418f6bac, "Discard medicine more than two years old."},
111 {0xc90ef73f, 0x0e28207f, 0x847e1e04, "He who has a shady past knows that nice guys finish last."},
112 {0xb902341f, 0xbe93f964, 0x606bf5a6, "I wouldn't marry him with a ten foot pole."},
113 {0x042080e8, 0x9e3be0c3, 0x1521d7b7, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
114 {0x154c6d11, 0xf505ef04, 0xe238d024, "The days of the digital watch are numbered. -Tom Stoppard"},
115 {0x4c418325, 0x85d3dc82, 0x5423e28a, "Nepal premier won't resign."},
116 {0x33955150, 0xc5142380, 0x97f7c3a6, "For every action there is an equal and opposite government program."},
117 {0x26216a4b, 0x75eb77dd, 0xe4543ac6, "His money is twice tainted: 'taint yours and 'taint mine."},
118 {0x1abbe45e, 0x91ebe9f7, 0x48ec4d9a, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
119 {0xc89a94f7, 0xf0b1168e, 0xc75afda4, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
120 {0xab3abe14, 0x572b74e2, 0x6db40154, "size: a.out: bad magic"},
121 {0xbab102b6, 0x8a58a6d5, 0x4c148ba0, "The major problem is with sendmail. -Mark Horton"},
122 {0x999149d7, 0x9c426c50, 0x9be6c237, "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
123 {0x6d52a33c, 0x735400a4, 0x52f8abfc, "If the enemy is within range, then so are you."},
124 {0x90631e8d, 0xbec49c95, 0xf98e0b1d, "It's well we cannot hear the screams/That we create in others' dreams."},
125 {0x78309130, 0xa95a2079, 0x6a1d5514, "You remind me of a TV show, but that's all right: I watch it anyway."},
126 {0x7d0a377f, 0xde2e65c5, 0xd88bc947, "C is as portable as Stonehedge!!"},
127 {0x8c79fd79, 0x297a88ed, 0x5e625378, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
128 {0xa20b7167, 0x66ed1d8b, 0xbd1004ed, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
129 {0x8e0bb443, 0xdcded527, 0xd4575591, "How can you write a big system without C++? -Paul Glick"},
130 }
131
132 var ChecksumIEEE = func(data []byte) uint32 {
133 return crc32.ChecksumIEEE(data)
134 }
135 var ChecksumCastagnoli = func(data []byte) uint32 {
136 return crc32.Checksum(data, crc32.MakeTable(crc32.Castagnoli))
137 }
138 var ChecksumKoopman = func(data []byte) uint32 {
139 return crc32.Checksum(data, crc32.MakeTable(crc32.Koopman))
140 }
141
142 for _, g := range golden {
143 var splits = []int{
144 0 * (len(g.in) / 1),
145 1 * (len(g.in) / 4),
146 2 * (len(g.in) / 4),
147 3 * (len(g.in) / 4),
148 1 * (len(g.in) / 1),
149 }
150
151 for _, i := range splits {
152 p1, p2 := []byte(g.in[:i]), []byte(g.in[i:])
153 in1, in2 := g.in[:i], g.in[i:]
154 len2 := int64(len(p2))
155 if got := CombineCRC32(crc32.IEEE, ChecksumIEEE(p1), ChecksumIEEE(p2), len2); got != g.ieee {
156 t.Errorf("CombineCRC32(IEEE, ChecksumIEEE(%q), ChecksumIEEE(%q), %d) = 0x%x, want 0x%x",
157 in1, in2, len2, got, g.ieee)
158 }
159 if got := CombineCRC32(crc32.Castagnoli, ChecksumCastagnoli(p1), ChecksumCastagnoli(p2), len2); got != g.castagnoli {
160 t.Errorf("CombineCRC32(Castagnoli, ChecksumCastagnoli(%q), ChecksumCastagnoli(%q), %d) = 0x%x, want 0x%x",
161 in1, in2, len2, got, g.castagnoli)
162 }
163 if got := CombineCRC32(crc32.Koopman, ChecksumKoopman(p1), ChecksumKoopman(p2), len2); got != g.koopman {
164 t.Errorf("CombineCRC32(Koopman, ChecksumKoopman(%q), ChecksumKoopman(%q), %d) = 0x%x, want 0x%x",
165 in1, in2, len2, got, g.koopman)
166 }
167 }
168 }
169 }
170
171 func TestCombineCRC64(t *testing.T) {
172 var golden = []struct {
173 iso, ecma uint64
174 in string
175 }{
176 {0x0000000000000000, 0x0000000000000000, ""},
177 {0x3420000000000000, 0x330284772e652b05, "a"},
178 {0x36c4200000000000, 0xbc6573200e84b046, "ab"},
179 {0x3776c42000000000, 0x2cd8094a1a277627, "abc"},
180 {0x336776c420000000, 0x3c9d28596e5960ba, "abcd"},
181 {0x32d36776c4200000, 0x040bdf58fb0895f2, "abcde"},
182 {0x3002d36776c42000, 0xd08e9f8545a700f4, "abcdef"},
183 {0x31b002d36776c420, 0xec20a3a8cc710e66, "abcdefg"},
184 {0x0e21b002d36776c4, 0x67b4f30a647a0c59, "abcdefgh"},
185 {0x8b6e21b002d36776, 0x9966f6c89d56ef8e, "abcdefghi"},
186 {0x7f5b6e21b002d367, 0x32093a2ecd5773f4, "abcdefghij"},
187 {0x8ec0e7c835bf9cdf, 0x8a0825223ea6d221, "Discard medicine more than two years old."},
188 {0xc7db1759e2be5ab4, 0x8562c0ac2ab9a00d, "He who has a shady past knows that nice guys finish last."},
189 {0xfbf9d9603a6fa020, 0x3ee2a39c083f38b4, "I wouldn't marry him with a ten foot pole."},
190 {0xeafc4211a6daa0ef, 0x1f603830353e518a, "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
191 {0x3e05b21c7a4dc4da, 0x02fd681d7b2421fd, "The days of the digital watch are numbered. -Tom Stoppard"},
192 {0x5255866ad6ef28a6, 0x790ef2b16a745a41, "Nepal premier won't resign."},
193 {0x8a79895be1e9c361, 0x3ef8f06daccdcddf, "For every action there is an equal and opposite government program."},
194 {0x8878963a649d4916, 0x049e41b2660b106d, "His money is twice tainted: 'taint yours and 'taint mine."},
195 {0xa7b9d53ea87eb82f, 0x561cc0cfa235ac68, "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
196 {0xdb6805c0966a2f9c, 0xd4fe9ef082e69f59, "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
197 {0xf3553c65dacdadd2, 0xe3b5e46cd8d63a4d, "size: a.out: bad magic"},
198 {0x9d5e034087a676b9, 0x865aaf6b94f2a051, "The major problem is with sendmail. -Mark Horton"},
199 {0xa6db2d7f8da96417, 0x7eca10d2f8136eb4, "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
200 {0x325e00cd2fe819f9, 0xd7dd118c98e98727, "If the enemy is within range, then so are you."},
201 {0x88c6600ce58ae4c6, 0x70fb33c119c29318, "It's well we cannot hear the screams/That we create in others' dreams."},
202 {0x28c4a3f3b769e078, 0x57c891e39a97d9b7, "You remind me of a TV show, but that's all right: I watch it anyway."},
203 {0xa698a34c9d9f1dca, 0xa1f46ba20ad06eb7, "C is as portable as Stonehedge!!"},
204 {0xf6c1e2a8c26c5cfc, 0x7ad25fafa1710407, "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
205 {0x0d402559dfe9b70c, 0x73cef1666185c13f, "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
206 {0xdb6efff26aa94946, 0xb41858f73c389602, "How can you write a big system without C++? -Paul Glick"},
207 }
208
209 var ChecksumISO = func(data []byte) uint64 {
210 return crc64.Checksum(data, crc64.MakeTable(crc64.ISO))
211 }
212 var ChecksumECMA = func(data []byte) uint64 {
213 return crc64.Checksum(data, crc64.MakeTable(crc64.ECMA))
214 }
215
216 for _, g := range golden {
217 var splits = []int{
218 0 * (len(g.in) / 1),
219 1 * (len(g.in) / 4),
220 2 * (len(g.in) / 4),
221 3 * (len(g.in) / 4),
222 1 * (len(g.in) / 1),
223 }
224
225 for _, i := range splits {
226 p1, p2 := []byte(g.in[:i]), []byte(g.in[i:])
227 in1, in2 := g.in[:i], g.in[i:]
228 len2 := int64(len(p2))
229 if got := CombineCRC64(crc64.ISO, ChecksumISO(p1), ChecksumISO(p2), len2); got != g.iso {
230 t.Errorf("CombineCRC64(ISO, ChecksumISO(%q), ChecksumISO(%q), %d) = 0x%x, want 0x%x",
231 in1, in2, len2, got, g.iso)
232 }
233 if got := CombineCRC64(crc64.ECMA, ChecksumECMA(p1), ChecksumECMA(p2), len2); got != g.ecma {
234 t.Errorf("CombineCRC64(ECMA, ChecksumECMA(%q), ChecksumECMA(%q), %d) = 0x%x, want 0x%x",
235 in1, in2, len2, got, g.ecma)
236 }
237 }
238 }
239 }