-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_simplelist.mojo
146 lines (104 loc) · 3.88 KB
/
test_simplelist.mojo
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from simpletools.simplelist import SimpleList
from simpletools.simpletest import MojoTest
@value
@register_passable
struct MyStruct:
var name: StringRef
var x: Int
var y: Int
fn test_simplelist_append() raises:
let mojo_test = MojoTest("simplelist_append")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
mojo_test.assert_equal(storage[0].name, "valor")
mojo_test.assert_equal(storage[0].x, 0)
mojo_test.assert_equal(storage[0].y, 1)
fn test_simplelist_index_error():
let mojo_test = MojoTest("simplelist_index_error")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
try:
_ = storage[5]
mojo_test.assert_true(False, "This should raise error")
except:
mojo_test.assert_true(True, "Raised error")
var result = SimpleList[StringRef]()
fn test_foreach() raises:
let mojo_test = MojoTest("foreach")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
storage.append(MyStruct("valor_2", 9, 10))
fn print_name(value: MyStruct) -> None:
print(value.name)
result.append(value.name)
storage.foreach(print_name)
mojo_test.assert_equal(result[0], "valor")
mojo_test.assert_equal(result[1], "valor_2")
fn test_map() raises:
let mojo_test = MojoTest("map")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
storage.append(MyStruct("valor_2", 9, 10))
fn get_name(value: MyStruct) -> StringRef:
return value.name
var name_storage = storage.map[StringRef](get_name)
mojo_test.assert_equal(name_storage[0], "valor")
mojo_test.assert_equal(name_storage[1], "valor_2")
fn test_all_any() raises:
let mojo_test = MojoTest("all_any")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
storage.append(MyStruct("valor_2", 9, 10))
fn is_true(value: MyStruct) -> Bool:
return True
fn is_false(value: MyStruct) -> Bool:
return False
fn is_some(value: MyStruct) -> Bool:
return value.name == "valor_2"
mojo_test.assert_true(storage.all(is_true))
mojo_test.assert_false(storage.all(is_false))
mojo_test.assert_false(storage.all(is_some))
mojo_test.assert_true(storage.any(is_true))
mojo_test.assert_false(storage.any(is_false))
mojo_test.assert_true(storage.any(is_some))
fn test_range() raises:
let mojo_test = MojoTest("range")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
storage.append(MyStruct("valor_2", 9, 10))
var range_storage = storage.range(0, 1)
mojo_test.assert_equal(range_storage[0].name, "valor")
mojo_test.assert_equal(range_storage.__len__(), 1)
fn test_reduce() raises:
let mojo_test = MojoTest("reduce")
var storage = SimpleList[MyStruct]()
storage.append(MyStruct("valor", 0, 1))
storage.append(MyStruct("valor_2", 9, 10))
fn add_values(accumulator: Int, value: MyStruct) -> Int:
return accumulator + value.x
var reduced_storage = storage.reduce[Int, 0](add_values)
mojo_test.assert_equal(reduced_storage, 9)
fn test_bool():
let mojo_test = MojoTest("bool")
var storage = SimpleList[MyStruct]()
if storage:
mojo_test.assert_true(False, "It is empty and it should be False")
storage.append(MyStruct("valor", 0, 1))
if not storage:
mojo_test.assert_true(False, "It has an element and it should be True")
fn test_size():
let mojo_test = MojoTest("size")
var storage = SimpleList[MyStruct]()
mojo_test.assert_equal(storage.size(), 0)
storage.append(MyStruct("valor", 0, 1))
mojo_test.assert_equal(storage.size(), 1)
fn main() raises:
test_simplelist_append()
test_simplelist_index_error()
test_foreach()
test_map()
test_all_any()
test_range()
test_reduce()
test_bool()
test_size()