File: match_test.go

package info (click to toggle)
golang-github-sergi-go-diff 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,324 kB
  • sloc: makefile: 38; sh: 15
file content (174 lines) | stat: -rw-r--r-- 4,519 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
// https://github.com/sergi/go-diff
// See the included LICENSE file for license details.
//
// go-diff is a Go implementation of Google's Diff, Match, and Patch library
// Original library is Copyright (c) 2006 Google Inc.
// http://code.google.com/p/google-diff-match-patch/

package diffmatchpatch

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMatchAlphabet(t *testing.T) {
	type TestCase struct {
		Pattern string

		Expected map[byte]int
	}

	dmp := New()

	for i, tc := range []TestCase{
		{
			Pattern: "abc",

			Expected: map[byte]int{
				'a': 4,
				'b': 2,
				'c': 1,
			},
		},
		{
			Pattern: "abcaba",

			Expected: map[byte]int{
				'a': 37,
				'b': 18,
				'c': 8,
			},
		},
	} {
		actual := dmp.MatchAlphabet(tc.Pattern)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
	}
}

func TestMatchBitap(t *testing.T) {
	type TestCase struct {
		Name string

		Text     string
		Pattern  string
		Location int

		Expected int
	}

	dmp := New()
	dmp.MatchDistance = 100
	dmp.MatchThreshold = 0.5

	for i, tc := range []TestCase{
		{"Exact match #1", "abcdefghijk", "fgh", 5, 5},
		{"Exact match #2", "abcdefghijk", "fgh", 0, 5},
		{"Fuzzy match #1", "abcdefghijk", "efxhi", 0, 4},
		{"Fuzzy match #2", "abcdefghijk", "cdefxyhijk", 5, 2},
		{"Fuzzy match #3", "abcdefghijk", "bxy", 1, -1},
		{"Overflow", "123456789xx0", "3456789x0", 2, 2},
		{"Before start match", "abcdef", "xxabc", 4, 0},
		{"Beyond end match", "abcdef", "defyy", 4, 3},
		{"Oversized pattern", "abcdef", "xabcdefy", 0, 0},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	dmp.MatchThreshold = 0.4

	for i, tc := range []TestCase{
		{"Threshold #1", "abcdefghijk", "efxyhi", 1, 4},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	dmp.MatchThreshold = 0.3

	for i, tc := range []TestCase{
		{"Threshold #2", "abcdefghijk", "efxyhi", 1, -1},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	dmp.MatchThreshold = 0.0

	for i, tc := range []TestCase{
		{"Threshold #3", "abcdefghijk", "bcdef", 1, 1},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	dmp.MatchThreshold = 0.5

	for i, tc := range []TestCase{
		{"Multiple select #1", "abcdexyzabcde", "abccde", 3, 0},
		{"Multiple select #2", "abcdexyzabcde", "abccde", 5, 8},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	// Strict location.
	dmp.MatchDistance = 10

	for i, tc := range []TestCase{
		{"Distance test #1", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, -1},
		{"Distance test #2", "abcdefghijklmnopqrstuvwxyz", "abcdxxefg", 1, 0},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	// Loose location.
	dmp.MatchDistance = 1000

	for i, tc := range []TestCase{
		{"Distance test #3", "abcdefghijklmnopqrstuvwxyz", "abcdefg", 24, 0},
	} {
		actual := dmp.MatchBitap(tc.Text, tc.Pattern, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}
}

func TestMatchMain(t *testing.T) {
	type TestCase struct {
		Name string

		Text1    string
		Text2    string
		Location int

		Expected int
	}

	dmp := New()

	for i, tc := range []TestCase{
		{"Equality", "abcdef", "abcdef", 1000, 0},
		{"Null text", "", "abcdef", 1, -1},
		{"Null pattern", "abcdef", "", 3, 3},
		{"Exact match", "abcdef", "de", 3, 3},
		{"Beyond end match", "abcdef", "defy", 4, 3},
		{"Oversized pattern", "abcdef", "abcdefy", 0, 0},
	} {
		actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
	}

	dmp.MatchThreshold = 0.7

	for i, tc := range []TestCase{
		{"Complex match", "I am the very model of a modern major general.", " that berry ", 5, 4},
	} {
		actual := dmp.MatchMain(tc.Text1, tc.Text2, tc.Location)
		assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
	}
}