1 // Copyright 2017, The Go Authors. 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.
14 "github.com/google/go-cmp/cmp/internal/flags"
18 flags.Deterministic = true
21 func TestDifference(t *testing.T) {
23 // Before passing x and y to Difference, we strip all spaces so that
24 // they can be used by the test author to indicate a missing symbol
25 // in one of the lists.
61 x: "##########0123456789",
63 want: "XXXXXXXXXX..........",
66 y: "##########0123456789",
67 want: "YYYYYYYYYY..........",
69 x: "#####0123456789#####",
71 want: "XXXXX..........XXXXX",
74 y: "#####0123456789#####",
75 want: "YYYYY..........YYYYY",
77 x: "01234##########56789",
79 want: ".....XXXXXXXXXX.....",
82 y: "01234##########56789",
83 want: ".....YYYYYYYYYY.....",
85 x: "0123456789##########",
87 want: "..........XXXXXXXXXX",
90 y: "0123456789##########",
91 want: "..........YYYYYYYYYY",
93 x: "abcdefghij0123456789",
94 y: "ABCDEFGHIJ0123456789",
95 want: "MMMMMMMMMM..........",
97 x: "ABCDEFGHIJ0123456789",
98 y: "abcdefghij0123456789",
99 want: "MMMMMMMMMM..........",
101 x: "01234abcdefghij56789",
102 y: "01234ABCDEFGHIJ56789",
103 want: ".....MMMMMMMMMM.....",
105 x: "01234ABCDEFGHIJ56789",
106 y: "01234abcdefghij56789",
107 want: ".....MMMMMMMMMM.....",
109 x: "0123456789abcdefghij",
110 y: "0123456789ABCDEFGHIJ",
111 want: "..........MMMMMMMMMM",
113 x: "0123456789ABCDEFGHIJ",
114 y: "0123456789abcdefghij",
115 want: "..........MMMMMMMMMM",
117 x: "ABCDEFGHIJ0123456789 ",
118 y: " 0123456789abcdefghij",
119 want: "XXXXXXXXXX..........YYYYYYYYYY",
121 x: " 0123456789abcdefghij",
122 y: "ABCDEFGHIJ0123456789 ",
123 want: "YYYYYYYYYY..........XXXXXXXXXX",
125 x: "ABCDE0123456789 FGHIJ",
126 y: " 0123456789abcdefghij",
127 want: "XXXXX..........YYYYYMMMMM",
129 x: " 0123456789abcdefghij",
130 y: "ABCDE0123456789 FGHIJ",
131 want: "YYYYY..........XXXXXMMMMM",
133 x: "ABCDE01234F G H I J 56789 ",
134 y: " 01234 a b c d e56789fghij",
135 want: "XXXXX.....XYXYXYXYXY.....YYYYY",
137 x: " 01234a b c d e 56789fghij",
138 y: "ABCDE01234 F G H I J56789 ",
139 want: "YYYYY.....XYXYXYXYXY.....XXXXX",
141 x: "FGHIJ01234ABCDE56789 ",
142 y: " 01234abcde56789fghij",
143 want: "XXXXX.....MMMMM.....YYYYY",
145 x: " 01234abcde56789fghij",
146 y: "FGHIJ01234ABCDE56789 ",
147 want: "YYYYY.....MMMMM.....XXXXX",
159 want: ".Y....YXY..X.X",
161 x: "###z#z###### x #",
162 y: "#y##Z#Z###### yy#",
163 want: ".Y..M.M......XYY.",
165 x: "0 12z3x 456789 x x 0",
166 y: "0y12Z3 y456789y y y0",
167 want: ".Y..M.XY......YXYXY.",
169 x: "0 2 4 6 8 ..................abXXcdEXF.ghXi",
170 y: " 1 3 5 7 9..................AB CDE F.GH I",
171 want: "XYXYXYXYXY..................MMXXMM.X..MMXM",
173 x: "I HG.F EDC BA..................9 7 5 3 1 ",
174 y: "iXhg.FXEdcXXba.................. 8 6 4 2 0",
175 want: "MYMM..Y.MMYYMM..................XYXYXYXYXY",
207 want: "YY........XX",
211 want: "YYY.......XXX",
215 want: "YYYY......XXXX",
219 want: "XXXXX.....YYYYY",
223 want: "XXXX......YYYY",
227 want: "XXX.......YYY",
231 want: "XX........YY",
237 x: "0 1 2 3 45 6 7 8 9 ",
238 y: " 9 8 7 6 54 3 2 1 0",
239 want: "XYXYXYXYX.YXYXYXYXY",
243 want: "XYXY.XX.XX.Y.YYY",
245 x: "F B Q M O I G T L N72X90 E 4S P 651HKRJU DA 83CVZW",
246 y: " 5 W H XO10R9IV K ZLCTAJ8P3N SEQM4 7 2G6 UBD F ",
247 want: "XYXYXYXY.YYYY.YXYXY.YYYYYYY.XXXXXY.YY.XYXYY.XXXXXX.Y.XYXXXXXX",
250 for _, tt := range tests {
251 t.Run("", func(t *testing.T) {
252 x := strings.Replace(tt.x, " ", "", -1)
253 y := strings.Replace(tt.y, " ", "", -1)
254 es := testStrings(t, x, y)
255 if got := es.String(); got != tt.want {
256 t.Errorf("Difference(%s, %s):\ngot %s\nwant %s", x, y, got, tt.want)
262 func TestDifferenceFuzz(t *testing.T) {
263 tests := []struct{ px, py, pm float32 }{
264 {px: 0.0, py: 0.0, pm: 0.1},
265 {px: 0.0, py: 0.1, pm: 0.0},
266 {px: 0.1, py: 0.0, pm: 0.0},
267 {px: 0.0, py: 0.1, pm: 0.1},
268 {px: 0.1, py: 0.0, pm: 0.1},
269 {px: 0.2, py: 0.2, pm: 0.2},
270 {px: 0.3, py: 0.1, pm: 0.2},
271 {px: 0.1, py: 0.3, pm: 0.2},
272 {px: 0.2, py: 0.2, pm: 0.2},
273 {px: 0.3, py: 0.3, pm: 0.3},
274 {px: 0.1, py: 0.1, pm: 0.5},
275 {px: 0.4, py: 0.1, pm: 0.5},
276 {px: 0.3, py: 0.2, pm: 0.5},
277 {px: 0.2, py: 0.3, pm: 0.5},
278 {px: 0.1, py: 0.4, pm: 0.5},
281 for i, tt := range tests {
282 t.Run(fmt.Sprintf("P%d", i), func(t *testing.T) {
283 // Sweep from 1B to 1KiB.
284 for n := 1; n <= 1024; n <<= 1 {
285 t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) {
286 for j := 0; j < 10; j++ {
287 x, y := generateStrings(n, tt.px, tt.py, tt.pm, int64(j))
296 func BenchmarkDifference(b *testing.B) {
297 for n := 1 << 10; n <= 1<<20; n <<= 2 {
298 b.Run(fmt.Sprintf("N%d", n), func(b *testing.B) {
299 x, y := generateStrings(n, 0.05, 0.05, 0.10, 0)
301 b.SetBytes(int64(len(x) + len(y)))
302 for i := 0; i < b.N; i++ {
303 Difference(len(x), len(y), func(ix, iy int) Result {
304 return compareByte(x[ix], y[iy])
311 func generateStrings(n int, px, py, pm float32, seed int64) (string, string) {
313 panic("invalid probabilities")
319 r := rand.New(rand.NewSource(seed))
324 switch p := r.Float32(); {
325 case p < px: // UniqueX
327 case p < py: // UniqueY
329 case p < pm: // Modified
330 x = append(x, 'A'+(b[0]%26))
331 y = append(y, 'a'+(b[0]%26))
338 return string(x), string(y)
341 func testStrings(t *testing.T, x, y string) EditScript {
342 es := Difference(len(x), len(y), func(ix, iy int) Result {
343 return compareByte(x[ix], y[iy])
345 if es.LenX() != len(x) {
346 t.Errorf("es.LenX = %d, want %d", es.LenX(), len(x))
348 if es.LenY() != len(y) {
349 t.Errorf("es.LenY = %d, want %d", es.LenY(), len(y))
351 if !validateScript(x, y, es) {
352 t.Errorf("invalid edit script: %v", es)
357 func validateScript(x, y string, es EditScript) bool {
359 for _, e := range es {
362 if !compareByte(x[len(bx)], y[len(by)]).Equal() {
365 bx = append(bx, x[len(bx)])
366 by = append(by, y[len(by)])
368 bx = append(bx, x[len(bx)])
370 by = append(by, y[len(by)])
372 if !compareByte(x[len(bx)], y[len(by)]).Similar() {
375 bx = append(bx, x[len(bx)])
376 by = append(by, y[len(by)])
379 return string(bx) == x && string(by) == y
382 // compareByte returns a Result where the result is Equal if x == y,
383 // similar if x and y differ only in casing, and different otherwise.
384 func compareByte(x, y byte) (r Result) {
387 return equalResult // Identity
388 case unicode.ToUpper(rune(x)) == unicode.ToUpper(rune(y)):
389 return similarResult // Modified
391 return differentResult // UniqueX or UniqueY
396 equalResult = Result{NumDiff: 0}
397 similarResult = Result{NumDiff: 1}
398 differentResult = Result{NumDiff: 2}
401 func TestResult(t *testing.T) {
407 // equalResult is equal since NumDiff == 0, by definition of Equal method.
408 {equalResult, true, true},
409 // similarResult is similar since it is a binary result where only one
410 // element was compared (i.e., Either NumSame==1 or NumDiff==1).
411 {similarResult, false, true},
412 // differentResult is different since there are enough differences that
413 // it isn't even considered similar.
414 {differentResult, false, false},
416 // Zero value is always equal.
417 {Result{NumSame: 0, NumDiff: 0}, true, true},
419 // Binary comparisons (where NumSame+NumDiff == 1) are always similar.
420 {Result{NumSame: 1, NumDiff: 0}, true, true},
421 {Result{NumSame: 0, NumDiff: 1}, false, true},
423 // More complex ratios. The exact ratio for similarity may change,
424 // and may require updates to these test cases.
425 {Result{NumSame: 1, NumDiff: 1}, false, true},
426 {Result{NumSame: 1, NumDiff: 2}, false, true},
427 {Result{NumSame: 1, NumDiff: 3}, false, false},
428 {Result{NumSame: 2, NumDiff: 1}, false, true},
429 {Result{NumSame: 2, NumDiff: 2}, false, true},
430 {Result{NumSame: 2, NumDiff: 3}, false, true},
431 {Result{NumSame: 3, NumDiff: 1}, false, true},
432 {Result{NumSame: 3, NumDiff: 2}, false, true},
433 {Result{NumSame: 3, NumDiff: 3}, false, true},
434 {Result{NumSame: 1000, NumDiff: 0}, true, true},
435 {Result{NumSame: 1000, NumDiff: 1}, false, true},
436 {Result{NumSame: 1000, NumDiff: 2}, false, true},
437 {Result{NumSame: 0, NumDiff: 1000}, false, false},
438 {Result{NumSame: 1, NumDiff: 1000}, false, false},
439 {Result{NumSame: 2, NumDiff: 1000}, false, false},
442 for _, tt := range tests {
443 if got := tt.result.Equal(); got != tt.wantEqual {
444 t.Errorf("%#v.Equal() = %v, want %v", tt.result, got, tt.wantEqual)
446 if got := tt.result.Similar(); got != tt.wantSimilar {
447 t.Errorf("%#v.Similar() = %v, want %v", tt.result, got, tt.wantSimilar)