aboutsummaryrefslogtreecommitdiff
path: root/levenshtein/generic_test.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-01 21:50:12 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2022-05-01 21:50:12 +0300
commit9fcf8e29214210612d545bed50d7f889800ac639 (patch)
tree3a99d2cd37fb8158d49abc1de6298758d205c9dd /levenshtein/generic_test.go
Initial
Diffstat (limited to 'levenshtein/generic_test.go')
-rw-r--r--levenshtein/generic_test.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/levenshtein/generic_test.go b/levenshtein/generic_test.go
new file mode 100644
index 0000000..11c9611
--- /dev/null
+++ b/levenshtein/generic_test.go
@@ -0,0 +1,115 @@
+package levenshtein
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestLevenshtein2(t *testing.T) {
+ type args struct {
+ s1 []myType
+ s2 []myType
+ cost EditCost[myType]
+ }
+ tests := []struct {
+ name string
+ args args
+ want []Edit
+ }{
+ {
+ name: "test1",
+ args: args{
+ s1: []myType{
+ {
+ a: "a",
+ }, {
+ a: "b",
+ }, {
+ a: "c",
+ },
+ },
+ s2: []myType{
+ {
+ a: "a",
+ }, {
+ a: "d",
+ }, {
+ a: "c",
+ },
+ },
+ cost: EditCost[myType](func(t EditType, from, to *myType) int {
+ return 1
+ }),
+ },
+ want: []Edit{
+ {
+ Type: Replace,
+ Idx1: 1,
+ Idx2: 1,
+ },
+ },
+ },
+ {
+ name: "test2 - costly replace",
+ args: args{
+ s1: []myType{
+ {
+ a: "a",
+ }, {
+ a: "b",
+ }, {
+ a: "c",
+ },
+ },
+ s2: []myType{
+ {
+ a: "a",
+ }, {
+ a: "d",
+ }, {
+ a: "c",
+ },
+ },
+ cost: EditCost[myType](func(t EditType, from, to *myType) int {
+ if t == Replace {
+ return 100
+ }
+ return 1
+ }),
+ },
+ want: []Edit{
+ {
+ Type: Delete,
+ Idx1: 2,
+ Idx2: 0,
+ },
+ {
+ Type: Insert,
+ Idx1: 1,
+ Idx2: 2,
+ },
+ {
+ Type: Delete,
+ Idx1: 1,
+ Idx2: 0,
+ },
+ {
+ Type: Insert,
+ Idx1: 0,
+ Idx2: 1,
+ },
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := Levenshtein(tt.args.s1, tt.args.s2, tt.args.cost); !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("Levenshtein() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
+type myType struct {
+ a string
+}