How to use TestLCSString method of diff_test Package

Best Got code snippet using diff_test.TestLCSString

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

lcs_test.go

Source: lcs_test.go Github

copy
1package diff_test
2
3import (
4	"bytes"
5	"context"
6	"strings"
7	"testing"
8	"time"
9
10	"github.com/ysmood/got"
11	"github.com/ysmood/got/lib/diff"
12)
13
14func TestReduce(t *testing.T) {
15	eq := func(x, y string, e string) {
16		t.Helper()
17
18		out := diff.NewWords(diff.Split(x)).Reduce(diff.NewWords(diff.Split(y))).String()
19		if out != e {
20			t.Error(out, "!=", e)
21		}
22	}
23
24	eq("", "", "")
25	eq("", "a", "")
26	eq("a", "", "")
27	eq("abc", "abc", "abc")
28	eq("abc", "acb", "abc")
29	eq("abc", "acbc", "abc")
30	eq("abc", "xxx", "")
31	eq("ac", "bc", "c")
32}
33
34func TestCommon(t *testing.T) {
35	eq := func(x, y string, el, er int) {
36		t.Helper()
37
38		l, r := diff.NewWords(diff.Split(x)).Common(diff.NewWords(diff.Split(y)))
39
40		if l != el || r != er {
41			t.Error(l, r, "!=", el, er)
42		}
43	}
44
45	eq("", "", 0, 0)
46	eq("", "a", 0, 0)
47	eq("a", "", 0, 0)
48	eq("abc", "abc", 3, 0)
49	eq("abc", "acb", 1, 0)
50	eq("abc", "acbc", 1, 2)
51	eq("abc", "xxx", 0, 0)
52	eq("ac", "bc", 0, 1)
53}
54
55func TestLCSString(t *testing.T) {
56	eq := func(x, y, expected string) {
57		t.Helper()
58
59		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
60		defer cancel()
61		lcs := diff.NewWords(split(x)).LCS(ctx, diff.NewWords(split(y)))
62		out := lcs.String()
63
64		if out != expected {
65			t.Error(out, "!=", expected)
66		}
67	}
68
69	eq("", "", "")
70	eq("abc", "acb", "ac")
71	eq("abc", "acbc", "abc")
72	eq("abc", "xxx", "")
73	eq("ac", "bc", "c")
74	eq("gac", "agcat", "gc")
75	eq("agcat", "gac", "ga")
76
77	x := bytes.Repeat([]byte("x"), 100000)
78	y := bytes.Repeat([]byte("y"), 100000)
79	eq(string(x), string(y), "")
80
81	x[len(x)/2] = byte('a')
82	y[len(y)/2] = byte('a')
83	eq(string(x), string(y), "a")
84
85	x[len(x)/2] = byte('y')
86	y[len(y)/2] = byte('x')
87	eq(string(x), string(y), "yx")
88}
89
90func TestLCSRandomString(t *testing.T) {
91	g := got.T(t)
92
93	x := g.ReadFile("fixtures/rand_x.txt").String()
94	y := g.ReadFile("fixtures/rand_y.txt").String()
95
96	res := diff.NewWords(split(x)).LCS(g.Context(), diff.NewWords(split(y)))
97	g.Eq(
98		res.String(),
99		g.ReadFile("fixtures/rand_lcs.txt").String(),
100	)
101}
102
103func TestText(t *testing.T) {
104	g := setup(t)
105	g.Len(diff.NewLines("a"), 1)
106	g.Len(diff.NewLines("a\n"), 2)
107	g.Len(diff.NewLines("a\n\n"), 3)
108	g.Len(diff.NewLines("\na"), 2)
109}
110
111func TestLCSText(t *testing.T) {
112	g := setup(t)
113	eq := func(x, y, expected string) {
114		t.Helper()
115
116		x = strings.Join(strings.Split(x, ""), "\n")
117		y = strings.Join(strings.Split(y, ""), "\n")
118		expected = strings.Join(strings.Split(expected, ""), "\n")
119
120		lcs := diff.NewLines(x).LCS(context.Background(), diff.NewLines(y))
121		g.Eq(lcs.String(), expected)
122	}
123
124	eq("", "", "")
125	eq("abc", "acb", "ac")
126	eq("abc", "acbc", "abc")
127	eq("abc", "xxx", "")
128}
129
130func TestIsIn(t *testing.T) {
131	g := got.T(t)
132
133	y := diff.NewWords(split("abc"))
134
135	g.True(diff.NewWords(split("ab")).IsSubsequenceOf(y))
136	g.True(diff.NewWords(split("ac")).IsSubsequenceOf(y))
137	g.True(diff.NewWords(split("bc")).IsSubsequenceOf(y))
138	g.False(diff.NewWords(split("cb")).IsSubsequenceOf(y))
139	g.False(diff.NewWords(split("ba")).IsSubsequenceOf(y))
140	g.False(diff.NewWords(split("ca")).IsSubsequenceOf(y))
141}
142
143func TestBTreeFindGreater(t *testing.T) {
144	g := got.T(t)
145
146	check := func(x int, ey int, ef bool) {
147		g.Helper()
148
149		y, f := diff.BTreeFindGreater([]int{1, 3, 5, 7, 9, 11}, x)
150		g.Eq(y, ey)
151		g.Eq(f, ef)
152	}
153
154	check(4, 5, true)
155	check(5, 7, true)
156	check(1, 3, true)
157	check(8, 9, true)
158	check(11, 0, false)
159}
160
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger TestLCSString code on LambdaTest Cloud Grid

Execute automation tests with TestLCSString on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)