-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_list_test.go
More file actions
92 lines (79 loc) · 1.79 KB
/
Copy pathstring_list_test.go
File metadata and controls
92 lines (79 loc) · 1.79 KB
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
package main
import (
"fmt"
"sort"
"strings"
"sync"
"testing"
)
func TestStringList(t *testing.T) {
original := []string{"foo", "bar", "buzz"}
l := NewStringListFromSlice(original...)
i := 0
for s := range l.Iter() {
if original[i] != s {
t.Errorf("Expected %s at %d in %v", s, i, original)
}
i++
}
if !l.Any(func(s string) bool { return s == "foo" }) {
t.Errorf("Any() must find 'foo'")
}
if l.Any(func(s string) bool { return s == "foobar" }) {
t.Errorf("Any() must NOT find 'foobar'")
}
filtered := l.Filter(func(s string) bool { return strings.HasPrefix(s, "b") })
if filtered.Get(0) != "bar" {
t.Errorf("Expected 'bar' at 0 in %v", filtered)
}
if filtered.Get(1) != "buzz" {
t.Errorf("Expected 'buzz' at 1 in %v", filtered)
}
l.Append("brr")
if l.Get(3) != "brr" {
t.Errorf("Expected 'brr' at 3 in %v", l)
}
if l.Pop("default") != "brr" {
t.Errorf("Expected pop 'brr' out %v", l)
}
if l.Len() != 3 {
t.Errorf("Expected have 3 elements after pop %v", l)
}
mustBeSorted := []string{}
l.Sort(func(s1, s2 string) bool { return s1 < s2 })
for s := range l.Iter() {
mustBeSorted = append(mustBeSorted, s)
}
if !sort.StringsAreSorted(mustBeSorted) {
t.Errorf("%v must be sorted", mustBeSorted)
}
}
func TestStringListConcurrency(t *testing.T) {
l := NewSyncronizedStringList()
for i := 0; i < 1000; i++ {
l.Append(fmt.Sprintf("%d", i))
}
wg := sync.WaitGroup{}
wg.Add(1)
n := 100
done := make(chan bool, n)
for i := 0; i < n; i++ {
go func() {
wg.Wait()
l.Sort(func(s1, s2 string) bool { return s1 < s2 })
done <- true
}()
go func(j int) {
wg.Wait()
l.Pop("")
done <- true
}(i)
}
wg.Done()
for i := 0; i < 2*n; i++ {
<-done
}
if l.Len() != 900 {
t.Errorf("Expected to have 900 elements instead of %d", l.Len())
}
}