v2 / vlib / datatypes / set_test.v
139 lines · 125 sloc · 3.05 KB · 880a9873a48ff1d6fd48fcc61f232dcf4c870ccd
Raw
1module datatypes
2
3fn test_exists() {
4 mut set := Set[string]{}
5 set.add('foo')
6 assert set.exists('foo')
7 assert set.exists('bar') == false
8}
9
10fn test_remove() {
11 mut set := Set[string]{}
12 set.remove('foo')
13 set.add('foo')
14 assert set.exists('foo')
15 set.remove('foo')
16 assert set.exists('foo') == false
17}
18
19fn test_size() {
20 mut set := Set[string]{}
21 set.add('foo')
22 set.add('foo')
23 assert set.size() == 1
24}
25
26fn test_pop() {
27 mut set := Set[string]{}
28 set.add('foo')
29 set.pop() or { return }
30 assert set.exists('foo') == false
31}
32
33fn test_clear() {
34 mut set := Set[string]{}
35 set.add('foo')
36 set.clear()
37 assert set.size() == 0
38}
39
40fn test_rest() {
41 mut set := Set[string]{}
42 set.add('foo')
43 set.add('bar')
44 array := set.rest() or { return }
45 assert array.len == 1
46}
47
48fn test_equal() {
49 mut first_set := Set[string]{}
50 mut second_set := Set[string]{}
51 first_set.add('foo')
52 assert second_set != first_set
53 second_set.add('foo')
54 assert second_set == first_set
55}
56
57fn test_is_empty() {
58 mut set := Set[string]{}
59 assert set.is_empty()
60 set.add('foo')
61 assert set.is_empty() == false
62}
63
64fn test_union() {
65 mut first_set := Set[string]{}
66 mut second_set := Set[string]{}
67 first_set.add_all(['b', 'c', 'd'])
68 second_set.add_all(['a', 'e'])
69 mut third_set := first_set.@union(second_set)
70 assert first_set.elements.len == 3
71 assert third_set.exists('a')
72 assert third_set.exists('b')
73 assert third_set.exists('c')
74 assert third_set.exists('d')
75 assert third_set.exists('e')
76}
77
78fn test_intersection() {
79 mut first_set := Set[string]{}
80 first_set.add_all(['foo', 'bar', 'baz'])
81 mut second_set := Set[string]{}
82 second_set.add_all(['bar', 'baz', 'boo'])
83 mut third_set := first_set.intersection(second_set)
84 assert first_set.exists('foo') == true
85 assert third_set.exists('foo') == false
86 assert third_set.exists('bar')
87 assert third_set.exists('baz')
88 assert third_set.exists('boo') == false
89}
90
91fn test_difference() {
92 mut first_set := Set[string]{}
93 mut second_set := Set[string]{}
94 first_set.add_all(['foo', 'bar', 'baz'])
95 second_set.add_all(['bar', 'baz', 'boo'])
96 mut third_set := first_set - second_set
97 assert third_set.exists('foo')
98 assert third_set.exists('bar') == false
99 assert third_set.exists('baz') == false
100 assert third_set.exists('boo') == false
101 first_set.clear()
102 second_set.clear()
103 third_set.clear()
104 first_set.add_all(['bar', 'baz', 'boo'])
105 second_set.add_all(['foo', 'bar', 'baz'])
106 third_set = first_set - second_set
107 assert third_set.exists('foo') == false
108 assert third_set.exists('bar') == false
109 assert third_set.exists('baz') == false
110 assert third_set.exists('boo')
111}
112
113fn test_diff() {
114 m := ['a', 'b', 'c']
115 n := ['a', 'b', 'c']
116 assert m == n
117 mut set_m := Set[string]{}
118 mut set_n := Set[string]{}
119 set_m.add_all(m)
120 set_n.add_all(n)
121 assert set_m == set_n
122 diff1 := set_m - set_n
123 diff2 := set_n - set_m
124 assert diff1 == diff2
125}
126
127fn test_subset() {
128 mut set := Set[string]{}
129 set.add_all(['a', 'b', 'c'])
130 mut subset := Set[string]{}
131 subset.add_all(['b', 'c'])
132 assert set.subset(subset)
133}
134
135fn test_array() {
136 mut set := Set[string]{}
137 set.add_all(['a', 'b', 'c'])
138 assert set.array().len == 3
139}
140