v / vlib / datatypes
Raw file | 193 loc (177 sloc) | 2.85 KB | Latest commit hash 7d8c38672
1module datatypes
2
3fn test_is_empty() {
4 mut list := LinkedList[int]{}
5 assert list.is_empty() == true
6 list.push(1)
7 assert list.is_empty() == false
8}
9
10fn test_len() {
11 mut list := LinkedList[int]{}
12 assert list.len() == 0
13 list.push(1)
14 assert list.len() == 1
15 list.pop()!
16 assert list.len() == 0
17}
18
19fn test_first() {
20 mut list := LinkedList[int]{}
21 list.push(1)
22 assert list.first()! == 1
23 list.push(2)
24 assert list.first()! == 1
25 list = LinkedList[int]{}
26 list.first() or { return }
27 assert false
28}
29
30fn test_last() {
31 mut list := LinkedList[int]{}
32 list.push(1)
33 assert list.last()! == 1
34 list.push(2)
35 assert list.last()! == 2
36 list = LinkedList[int]{}
37 list.last() or { return }
38 assert false
39}
40
41fn test_index() {
42 mut list := LinkedList[int]{}
43 list.push(1)
44 assert list.index(0)! == 1
45 list.push(2)
46 assert list.index(1)! == 2
47 list.pop()!
48 list.index(1) or { return }
49 assert false
50}
51
52fn test_push() {
53 mut list := LinkedList[int]{}
54 list.push(1)
55 assert list.last()! == 1
56 list.push(2)
57 assert list.last()! == 2
58 list.push(3)
59 assert list.last()! == 3
60}
61
62fn test_pop() {
63 mut list := LinkedList[int]{}
64 list.push(1)
65 list.push(2)
66 list.push(3)
67 assert list.pop()! == 3
68 list.push(4)
69 assert list.pop()! == 4
70 assert list.pop()! == 2
71 list = LinkedList[int]{}
72 list.pop() or { return }
73 assert false
74}
75
76fn test_shift() {
77 mut list := LinkedList[int]{}
78 list.push(1)
79 list.push(2)
80 list.push(3)
81 assert list.shift()! == 1
82 list.push(4)
83 assert list.shift()! == 2
84 assert list.shift()! == 3
85 list = LinkedList[int]{}
86 list.shift() or { return }
87 assert false
88}
89
90fn test_insert() {
91 mut list := LinkedList[int]{}
92 list.push(1)
93 list.push(2)
94 list.push(3)
95 list.insert(1, 111) or { return }
96 assert true
97}
98
99fn test_prepend() {
100 mut list := LinkedList[int]{}
101 list.push(1)
102 list.push(2)
103 list.push(3)
104 list.prepend(111)
105 assert list.first()! == 111
106}
107
108fn test_str() {
109 mut list := LinkedList[int]{}
110 list.push(1)
111 list.push(2)
112 list.push(3)
113 assert list.str() == '[1, 2, 3]'
114}
115
116fn test_array() {
117 mut list := LinkedList[int]{}
118 list.push(1)
119 list.push(2)
120 list.push(3)
121 assert list.array() == [1, 2, 3]
122}
123
124fn test_linked_list_iterating_with_for() {
125 mut list := LinkedList[int]{}
126 list.push(1)
127 list.push(2)
128 list.push(3)
129 mut res := []int{}
130 for x in list {
131 res << x
132 }
133 assert res == [1, 2, 3]
134}
135
136fn test_linked_list_separate_iterators() {
137 mut list := LinkedList[int]{}
138 list.push(1)
139 list.push(2)
140 list.push(3)
141 mut it1 := list.iterator()
142 mut it2 := list.iterator()
143 mut it3 := list.iterator()
144 assert it1.next()! == 1
145 assert it1.next()! == 2
146 assert it1.next()! == 3
147 assert it2.next()! == 1
148 if _ := it1.next() {
149 assert false
150 } else {
151 assert true
152 }
153 if _ := it1.next() {
154 assert false
155 } else {
156 assert true
157 }
158 assert it2.next()! == 2
159 assert it2.next()! == 3
160 if _ := it2.next() {
161 assert false
162 } else {
163 assert true
164 }
165 mut res := []int{}
166 for x in it3 {
167 res << x
168 }
169 assert res == [1, 2, 3]
170}
171
172struct Foo {
173mut:
174 field LinkedList[map[string]int]
175}
176
177fn test_linked_list_map() {
178 mut foo := Foo{}
179 foo.field.push({
180 'one': 1
181 })
182 foo.field.push({
183 'two': 2
184 })
185 println(foo)
186 mut iter := foo.field.iterator()
187 assert iter.next()! == {
188 'one': 1
189 }
190 assert iter.next()! == {
191 'two': 2
192 }
193}