v / vlib / datatypes
Raw file | 117 loc (105 sloc) | 2.61 KB | Latest commit hash ef5be22f8
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 third_set.exists('a')
71 assert third_set.exists('b')
72 assert third_set.exists('c')
73 assert third_set.exists('d')
74 assert third_set.exists('e')
75}
76
77fn test_intersection() {
78 mut first_set := Set[string]{}
79 first_set.add_all(['foo', 'bar', 'baz'])
80 mut second_set := Set[string]{}
81 second_set.add_all(['bar', 'baz', 'boo'])
82 mut third_set := first_set.intersection(second_set)
83 assert third_set.exists('foo') == false
84 assert third_set.exists('bar')
85 assert third_set.exists('baz')
86 assert third_set.exists('boo') == false
87}
88
89fn test_difference() {
90 mut first_set := Set[string]{}
91 mut second_set := Set[string]{}
92 first_set.add_all(['foo', 'bar', 'baz'])
93 second_set.add_all(['bar', 'baz', 'boo'])
94 mut third_set := first_set - second_set
95 assert third_set.exists('foo')
96 assert third_set.exists('bar') == false
97 assert third_set.exists('baz') == false
98 assert third_set.exists('boo') == false
99 first_set.clear()
100 second_set.clear()
101 third_set.clear()
102 first_set.add_all(['bar', 'baz', 'boo'])
103 second_set.add_all(['foo', 'bar', 'baz'])
104 third_set = first_set - second_set
105 assert third_set.exists('foo') == false
106 assert third_set.exists('bar') == false
107 assert third_set.exists('baz') == false
108 assert third_set.exists('boo')
109}
110
111fn test_subset() {
112 mut set := Set[string]{}
113 set.add_all(['a', 'b', 'c'])
114 mut subset := Set[string]{}
115 subset.add_all(['b', 'c'])
116 assert set.subset(subset)
117}