-
Notifications
You must be signed in to change notification settings - Fork 0
/
axl-duplicates_test.go
120 lines (106 loc) · 3.32 KB
/
axl-duplicates_test.go
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
package main
import (
"math/rand"
"testing"
)
type anyListTables = struct {
name string
pkid string
addId string
duplicate bool
}
type uniqueTables struct {
t *UniqueList
}
func processAddTest(t *testing.T, table anyListTables) {
ul := NewUniqueList(table.name, "desc "+table.name, table.pkid)
ul.Add(table.addId)
if table.duplicate {
if len(ul.pkid) != 1 {
t.Errorf("duplicate pkid added [%s / %d]\r\n%s\r\n%s", table.name, len(ul.pkid), table.pkid, table.addId)
}
} else {
if len(ul.pkid) != 2 {
t.Errorf("unique pkid not added [%s / %d]\r\n%s\r\n%s", table.name, len(ul.pkid), table.pkid, table.addId)
}
}
}
func TestUniqueList_Add(t *testing.T) {
t.Parallel()
for _, table := range userListData {
processAddTest(t, table)
}
for _, table := range deviceListData {
processAddTest(t, table)
}
for _, table := range lineListData {
processAddTest(t, table)
}
}
func TestUniqueList_UserListString(t *testing.T) {
}
func TestContainsString(t *testing.T) {
t.Parallel()
tables := []struct {
t []string
a string
e bool
name string
}{
{t: []string{"aaa", "bbb", "aba"}, a: "ccc", e: false, name: "new last"},
{t: []string{"ddd", "bbb", "aba"}, a: "aaa", e: false, name: "new first"},
{t: []string{"aaa", "bbb", "ccc"}, a: "aba", e: false, name: "new middle"},
{t: []string{"ddd", "bbb", "ccc"}, a: "ddd", e: true, name: "contains first"},
{t: []string{"ddd", "bbb", "ccc"}, a: "ccc", e: true, name: "contains last"},
{t: []string{"ddd", "bbb", "ccc"}, a: "bbb", e: true, name: "contains middle"},
{t: []string{"ddd", "bbb", "ccc"}, a: "", e: false, name: "empty string"},
}
for _, table := range tables {
if table.e != ContainsString(table.t, table.a) {
t.Errorf("contains test fail [%s]", table.name)
}
}
}
func BenchmarkContainsString(b *testing.B) {
benchmarkContainsString(100, false, b)
}
func BenchmarkContainsString2(b *testing.B) {
benchmarkContainsString(100, true, b)
}
func benchmarkContainsString(size int, exist bool, b *testing.B) {
var list []string
test := RandomString()
list = []string{}
for i := 0; i < size; i++ {
list = append(list, RandomString())
}
if exist {
test = list[rand.Intn(size)]
}
for n := 0; n < b.N; n++ {
_ = ContainsString(list, test)
}
}
func generateUniqueTables() (tbl []uniqueTables) {
var ut []uniqueTables
ut = []uniqueTables{}
for _, table := range userListData {
ut = append(ut, uniqueTables{t: NewUniqueList(table.name, "desc "+table.name, table.pkid)})
}
return
}
var userListData = []anyListTables{
{name: "agent01", pkid: "aaaaa-aaa-01", duplicate: false, addId: "baaaa-aaa-01"},
{name: "agent02", pkid: "aaaaa-aaa-02", duplicate: false, addId: "baaaa-aaa-02"},
{name: "agent03", pkid: "aaaaa-aaa-03", duplicate: true, addId: "aaaaa-aaa-03"},
}
var deviceListData = []anyListTables{
{name: "device01", pkid: "ddddd-ddd-01", duplicate: false, addId: "bdddd-ddd-01"},
{name: "device02", pkid: "ddddd-ddd-02", duplicate: false, addId: "bdddd-ddd-02"},
{name: "device03", pkid: "ddddd-ddd-03", duplicate: true, addId: "ddddd-ddd-03"},
}
var lineListData = []anyListTables{
{name: "line01", pkid: "lllll-lll-01", duplicate: false, addId: "bllll-lll-01"},
{name: "line02", pkid: "lllll-lll-02", duplicate: false, addId: "bllll-lll-02"},
{name: "line03", pkid: "lllll-lll-03", duplicate: true, addId: "lllll-lll-03"},
}