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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
from util.generic.maybe cimport TMaybe, Nothing
import pytest
import unittest
def _check_from_py(TMaybe[int] x):
return x.Defined()
def _check_to_py_value():
cdef TMaybe[int] tmp = TMaybe[int](42)
return tmp
def _check_to_py_nothing():
cdef TMaybe[int] tmp = Nothing()
return tmp
class TestMaybe(unittest.TestCase):
def test_ctor1(self):
cdef TMaybe[int] tmp = TMaybe[int]()
self.assertFalse(tmp.Defined())
def test_ctor2(self):
cdef TMaybe[int] tmp = TMaybe[int](42)
self.assertTrue(tmp.Defined())
self.assertEquals(tmp.GetRef(), 42)
def test_ctor3(self):
cdef TMaybe[int] tmp = Nothing()
self.assertFalse(tmp.Defined())
def test_operator_assign(self):
cdef TMaybe[int] tmp
tmp = 42
self.assertTrue(tmp.Defined())
self.assertEquals(tmp.GetRef(), 42)
def test_compare(self):
cdef TMaybe[int] tmp1 = 17
cdef TMaybe[int] tmp2 = 42
cdef TMaybe[int] nothing
# ==
self.assertTrue(tmp1 == 17)
self.assertTrue(tmp1 == tmp1)
self.assertTrue(nothing == nothing)
self.assertFalse(tmp1 == 16)
self.assertFalse(tmp1 == tmp2)
self.assertFalse(tmp1 == nothing)
# !=
self.assertTrue(tmp1 != 16)
self.assertTrue(tmp1 != tmp2)
self.assertTrue(tmp1 != nothing)
self.assertFalse(tmp1 != 17)
self.assertFalse(tmp1 != tmp1)
self.assertFalse(nothing != nothing)
# <
self.assertTrue(nothing < tmp1)
self.assertTrue(nothing < tmp2)
self.assertTrue(tmp1 < tmp2)
self.assertTrue(nothing < 0)
self.assertTrue(tmp1 < 18)
self.assertFalse(nothing < nothing)
self.assertFalse(tmp1 < tmp1)
self.assertFalse(tmp2 < tmp1)
self.assertFalse(tmp1 < 16)
# <=
self.assertTrue(nothing <= nothing)
self.assertTrue(nothing <= tmp1)
self.assertTrue(nothing <= tmp2)
self.assertTrue(tmp1 <= tmp1)
self.assertTrue(tmp1 <= tmp2)
self.assertTrue(nothing <= 0)
self.assertTrue(tmp1 <= 18)
self.assertFalse(tmp2 <= tmp1)
self.assertFalse(tmp1 <= 16)
# >
self.assertTrue(tmp1 > nothing)
self.assertTrue(tmp2 > nothing)
self.assertTrue(tmp2 > tmp1)
self.assertTrue(tmp1 > 16)
self.assertFalse(nothing > nothing)
self.assertFalse(nothing > 0)
self.assertFalse(tmp1 > tmp1)
self.assertFalse(tmp1 > tmp2)
self.assertFalse(tmp1 > 18)
# >=
self.assertTrue(nothing >= nothing)
self.assertTrue(tmp1 >= nothing)
self.assertTrue(tmp2 >= nothing)
self.assertTrue(tmp2 >= tmp1)
self.assertTrue(tmp1 >= tmp1)
self.assertTrue(tmp1 >= 16)
self.assertFalse(nothing >= 0)
self.assertFalse(tmp1 >= tmp2)
self.assertFalse(tmp1 >= 18)
def test_construct_in_place(self):
cdef TMaybe[int] tmp
tmp.ConstructInPlace(42)
self.assertTrue(tmp.Defined())
self.assertEquals(tmp.GetRef(), 42)
def test_clear(self):
cdef TMaybe[int] tmp = 42
tmp.Clear()
self.assertFalse(tmp.Defined())
def test_defined(self):
cdef TMaybe[int] tmp
self.assertFalse(tmp.Defined())
self.assertTrue(tmp.Empty())
tmp = 42
self.assertTrue(tmp.Defined())
self.assertFalse(tmp.Empty())
def test_check_defined(self):
cdef TMaybe[int] tmp
with pytest.raises(RuntimeError):
tmp.CheckDefined()
tmp = 42
tmp.CheckDefined()
def test_get(self):
cdef TMaybe[int] tmp = 42
cdef int* p = tmp.Get()
self.assertTrue(p != NULL)
self.assertEquals(p[0], 42)
def test_get_ref(self):
cdef TMaybe[int] tmp = 42
self.assertTrue(tmp.Defined())
self.assertEquals(tmp.GetRef(), 42)
def test_get_or_else(self):
cdef TMaybe[int] tmp = 42
self.assertEquals(tmp.GetOrElse(13), 42)
tmp.Clear()
self.assertEquals(tmp.GetOrElse(13), 13)
def test_or_else(self):
cdef TMaybe[int] tmp = 42
cdef TMaybe[int] nothing
self.assertFalse(nothing.OrElse(nothing).Defined())
self.assertEquals(tmp.OrElse(nothing).GetRef(), 42)
self.assertEquals(nothing.OrElse(tmp).GetRef(), 42)
self.assertEquals(tmp.OrElse(tmp).GetRef(), 42)
def test_cast(self):
cdef TMaybe[int] tmp = 42
cdef TMaybe[char] tmp2 = tmp.Cast[char]()
self.assertEquals(tmp2.GetRef(), 42)
def test_swap(self):
cdef TMaybe[int] tmp1 = 42
cdef TMaybe[int] tmp2
tmp2.Swap(tmp1)
self.assertFalse(tmp1.Defined())
self.assertEquals(tmp2.GetRef(), 42)
def test_from_py(self):
self.assertTrue(_check_from_py(42))
self.assertFalse(_check_from_py(None))
with self.assertRaises(TypeError):
_check_from_py("ttt")
def test_to_py(self):
self.assertEquals(_check_to_py_value(), 42)
self.assertEquals(_check_to_py_nothing(), None)
|