File: golcs_test.go

package info (click to toggle)
golang-github-yudai-golcs 0.0~git20170316.ecda9a5-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 64 kB
  • sloc: makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download
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
package lcs

import (
	"context"
	"reflect"
	"testing"
	"time"
)

func TestLCS(t *testing.T) {
	cases := []struct {
		left       []interface{}
		right      []interface{}
		indexPairs []IndexPair
		values     []interface{}
		length     int
	}{
		{
			left:       []interface{}{1, 2, 3},
			right:      []interface{}{2, 3},
			indexPairs: []IndexPair{{1, 0}, {2, 1}},
			values:     []interface{}{2, 3},
			length:     2,
		},
		{
			left:       []interface{}{2, 3},
			right:      []interface{}{1, 2, 3},
			indexPairs: []IndexPair{{0, 1}, {1, 2}},
			values:     []interface{}{2, 3},
			length:     2,
		},
		{
			left:       []interface{}{2, 3},
			right:      []interface{}{2, 5, 3},
			indexPairs: []IndexPair{{0, 0}, {1, 2}},
			values:     []interface{}{2, 3},
			length:     2,
		},
		{
			left:       []interface{}{2, 3, 3},
			right:      []interface{}{2, 5, 3},
			indexPairs: []IndexPair{{0, 0}, {2, 2}},
			values:     []interface{}{2, 3},
			length:     2,
		},
		{
			left:       []interface{}{1, 2, 5, 3, 1, 1, 5, 8, 3},
			right:      []interface{}{1, 2, 3, 3, 4, 4, 5, 1, 6},
			indexPairs: []IndexPair{{0, 0}, {1, 1}, {2, 6}, {4, 7}},
			values:     []interface{}{1, 2, 5, 1},
			length:     4,
		},
		{
			left:       []interface{}{},
			right:      []interface{}{2, 5, 3},
			indexPairs: []IndexPair{},
			values:     []interface{}{},
			length:     0,
		},
		{
			left:       []interface{}{3, 4},
			right:      []interface{}{},
			indexPairs: []IndexPair{},
			values:     []interface{}{},
			length:     0,
		},
		{
			left:       []interface{}{"foo"},
			right:      []interface{}{"baz", "foo"},
			indexPairs: []IndexPair{{0, 1}},
			values:     []interface{}{"foo"},
			length:     1,
		},
		{
			left:       []interface{}{byte('T'), byte('G'), byte('A'), byte('G'), byte('T'), byte('A')},
			right:      []interface{}{byte('G'), byte('A'), byte('T'), byte('A')},
			indexPairs: []IndexPair{{1, 0}, {2, 1}, {4, 2}, {5, 3}},
			values:     []interface{}{byte('G'), byte('A'), byte('T'), byte('A')},
			length:     4,
		},
	}

	for i, c := range cases {
		lcs := New(c.left, c.right)

		actualPairs := lcs.IndexPairs()
		if !reflect.DeepEqual(actualPairs, c.indexPairs) {
			t.Errorf("test case %d failed at index pair, actual: %#v, expected: %#v", i, actualPairs, c.indexPairs)
		}

		actualValues := lcs.Values()
		if !reflect.DeepEqual(actualValues, c.values) {
			t.Errorf("test case %d failed at values, actual: %#v, expected: %#v", i, actualValues, c.values)
		}

		actualLength := lcs.Length()
		if actualLength != c.length {
			t.Errorf("test case %d failed at length, actual: %d, expected: %d", i, actualLength, c.length)
		}
	}
}

func TestContextCancel(t *testing.T) {
	left := make([]interface{}, 100000) // takes over 1 sec
	right := make([]interface{}, 100000)
	right[0] = 1
	right[len(right)-1] = 1
	lcs := New(left, right)

	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		time.Sleep(time.Second)
		cancel()
	}()

	_, err := lcs.LengthContext(ctx)
	if err != context.Canceled {
		t.Fatalf("unexpected err: %s", err)
	}
}