Fix: log.h: missing defines of `_ERRNO()` macros to `_UNUSED()`
[babeltrace.git] / tests / bindings / python / bt2 / test_integer_range_set.py
CommitLineData
19bb8b5a
PP
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
19import bt2
20import unittest
21
1abe38eb 22from utils import get_default_trace_class, create_const_field
fc866000
FD
23
24
25def get_const_signed_integer_range(int_ranges):
26 def range_setter(field):
27 field.value = 12
28
29 tc = get_default_trace_class()
30 fc = tc.create_signed_enumeration_field_class(32)
31 fc.add_mapping('something', bt2.SignedIntegerRangeSet(int_ranges))
32
33 return create_const_field(tc, fc, range_setter).cls['something'].ranges
34
35
36def get_const_unsigned_integer_range(int_ranges):
37 def range_setter(field):
38 field.value = 12
39
40 tc = get_default_trace_class()
41 fc = tc.create_unsigned_enumeration_field_class(32)
42 fc.add_mapping('something', bt2.UnsignedIntegerRangeSet(int_ranges))
43
44 return create_const_field(tc, fc, range_setter).cls['something'].ranges
45
19bb8b5a
PP
46
47class _IntegerRangeTestCase:
48 def setUp(self):
49 self._rg = self._CLS(self._def_lower, self._def_upper)
fc866000
FD
50 self._const_rg = list(
51 self._GET_CONST_RANGE_SET([(self._def_lower, self._def_upper)])
52 )[0]
19bb8b5a
PP
53
54 def test_create(self):
55 self.assertEqual(self._rg.lower, self._def_lower)
56 self.assertEqual(self._rg.upper, self._def_upper)
fc866000
FD
57 self.assertIs(type(self._rg), self._CLS)
58
59 def test_const_create(self):
60 self.assertEqual(self._const_rg.lower, self._def_lower)
61 self.assertEqual(self._const_rg.upper, self._def_upper)
62 self.assertIs(type(self._const_rg), self._CONST_CLS)
19bb8b5a
PP
63
64 def test_create_same(self):
65 rg = self._CLS(self._def_lower, self._def_lower)
66 self.assertEqual(rg.lower, self._def_lower)
67 self.assertEqual(rg.upper, self._def_lower)
68
7a2a2328
PP
69 def test_create_single(self):
70 rg = self._CLS(self._def_lower)
71 self.assertEqual(rg.lower, self._def_lower)
72 self.assertEqual(rg.upper, self._def_lower)
73
19bb8b5a
PP
74 def test_create_wrong_type_lower(self):
75 with self.assertRaises(TypeError):
082db648 76 self._CLS(19.3, self._def_upper)
19bb8b5a 77
43ab0346 78 def test_create_wrong_type_upper(self):
19bb8b5a 79 with self.assertRaises(TypeError):
082db648 80 self._CLS(self._def_lower, 19.3)
19bb8b5a
PP
81
82 def test_create_out_of_bound_lower(self):
83 with self.assertRaises(ValueError):
082db648 84 self._CLS(self._oob_lower, self._def_upper)
19bb8b5a
PP
85
86 def test_create_out_of_bound_upper(self):
87 with self.assertRaises(ValueError):
082db648 88 self._CLS(self._def_lower, self._oob_upper)
19bb8b5a
PP
89
90 def test_create_lower_gt_upper(self):
91 with self.assertRaises(ValueError):
082db648 92 self._CLS(self._def_lower, self._def_lower - 1)
19bb8b5a
PP
93
94 def test_contains_lower(self):
95 self.assertTrue(self._rg.contains(self._def_lower))
96
97 def test_contains_upper(self):
98 self.assertTrue(self._rg.contains(self._def_upper))
99
100 def test_contains_avg(self):
101 avg = (self._def_lower + self._def_upper) // 2
102 self.assertTrue(self._rg.contains(avg))
103
104 def test_contains_wrong_type(self):
105 with self.assertRaises(TypeError):
106 self._rg.contains('allo')
107
108 def test_contains_out_of_bound(self):
109 with self.assertRaises(ValueError):
110 self._rg.contains(self._oob_upper)
111
112 def test_eq(self):
113 rg = self._CLS(self._def_lower, self._def_upper)
114 self.assertEqual(rg, self._rg)
115
fc866000
FD
116 def test_const_eq(self):
117 rg = list(self._GET_CONST_RANGE_SET([(self._def_lower, self._def_upper)]))[0]
118 self.assertEqual(rg, self._const_rg)
119
120 def test_const_nonconst_eq(self):
121 self.assertEqual(self._rg, self._const_rg)
122
19bb8b5a
PP
123 def test_ne(self):
124 rg = self._CLS(self._def_lower, self._def_upper - 1)
125 self.assertNotEqual(rg, self._rg)
126
fc866000
FD
127 def test_const_ne(self):
128 rg = list(self._GET_CONST_RANGE_SET([(self._def_lower, self._def_upper - 1)]))[
129 0
130 ]
131 self.assertNotEqual(rg, self._const_rg)
132
19bb8b5a
PP
133 def test_ne_other_type(self):
134 self.assertNotEqual(self._rg, 48)
135
136
137class UnsignedIntegerRangeTestCase(_IntegerRangeTestCase, unittest.TestCase):
138 _CLS = bt2.UnsignedIntegerRange
fc866000
FD
139 _CONST_CLS = bt2._UnsignedIntegerRangeConst
140 _GET_CONST_RANGE_SET = staticmethod(get_const_unsigned_integer_range)
19bb8b5a
PP
141 _def_lower = 23
142 _def_upper = 18293
143 _oob_lower = -1
144 _oob_upper = 1 << 64
145
146
147class SignedIntegerRangeTestCase(_IntegerRangeTestCase, unittest.TestCase):
148 _CLS = bt2.SignedIntegerRange
fc866000
FD
149 _CONST_CLS = bt2._SignedIntegerRangeConst
150 _GET_CONST_RANGE_SET = staticmethod(get_const_signed_integer_range)
19bb8b5a
PP
151 _def_lower = -184
152 _def_upper = 11547
153 _oob_lower = -(1 << 63) - 1
154 _oob_upper = 1 << 63
155
156
157class _IntegerRangeSetTestCase:
158 def setUp(self):
159 self._rs = self._CLS((self._range1, self._range2, self._range3))
fc866000
FD
160 self._const_rs = self._GET_CONST_RANGE_SET(
161 [self._range1, self._range2, self._range3]
162 )
19bb8b5a
PP
163
164 def test_create(self):
165 self.assertEqual(len(self._rs), 3)
166 self.assertIn(self._range1, self._rs)
167 self.assertIn(self._range2, self._rs)
168 self.assertIn(self._range3, self._rs)
fc866000
FD
169 self.assertIs(type(self._range1), self._RANGE_CLS)
170
171 def test_const_create(self):
172 self.assertEqual(len(self._const_rs), 3)
173 self.assertIn(self._range1, self._const_rs)
174 self.assertIn(self._range2, self._const_rs)
175 self.assertIn(self._range3, self._const_rs)
176 self.assertIs(type(self._range1), self._RANGE_CLS)
19bb8b5a
PP
177
178 def test_create_tuples(self):
cfbd7cf3
FD
179 rs = self._CLS(
180 (
181 (self._range1.lower, self._range1.upper),
182 (self._range2.lower, self._range2.upper),
183 (self._range3.lower, self._range3.upper),
184 )
185 )
19bb8b5a
PP
186 self.assertEqual(len(rs), 3)
187 self.assertIn(self._range1, rs)
188 self.assertIn(self._range2, rs)
189 self.assertIn(self._range3, rs)
190
7a2a2328
PP
191 def test_create_single(self):
192 rs = self._CLS((self._range_same.lower,))
193 self.assertEqual(len(rs), 1)
194 self.assertIn(self._range_same, rs)
195
19bb8b5a
PP
196 def test_create_non_iter(self):
197 with self.assertRaises(TypeError):
198 self._rs = self._CLS(23)
199
200 def test_create_wrong_elem_type(self):
201 with self.assertRaises(TypeError):
7a2a2328 202 self._rs = self._CLS((self._range1, self._range2, 'lel'))
19bb8b5a
PP
203
204 def test_len(self):
205 self.assertEqual(len(self._rs), 3)
206
207 def test_contains(self):
208 rs = self._CLS((self._range1, self._range2))
209 self.assertIn(self._range1, rs)
210 self.assertIn(self._range2, rs)
211 self.assertNotIn(self._range3, rs)
212
213 def test_contains_value(self):
214 rs = self._CLS((self._range1, self._range2))
215 self.assertTrue(rs.contains_value(self._range1.upper))
216 self.assertTrue(rs.contains_value(self._range2.lower))
217 self.assertFalse(rs.contains_value(self._range3.upper))
218
219 def test_contains_value_wrong_type(self):
220 with self.assertRaises(TypeError):
221 self._rs.contains_value('meow')
222
223 def test_iter(self):
224 range_list = list(self._rs)
225 self.assertIn(self._range1, range_list)
226 self.assertIn(self._range2, range_list)
227 self.assertIn(self._range3, range_list)
228
fc866000
FD
229 for rg in range_list:
230 self.assertIs(type(rg), self._RANGE_CLS)
231
232 def test_const_iter(self):
233 range_list = list(self._const_rs)
234 self.assertIn(self._range1, range_list)
235 self.assertIn(self._range2, range_list)
236 self.assertIn(self._range3, range_list)
237
238 for rg in range_list:
239 self.assertIs(type(rg), self._CONST_RANGE_CLS)
240
19bb8b5a
PP
241 def test_empty(self):
242 rs = self._CLS()
243 self.assertEqual(len(rs), 0)
244 self.assertEqual(len(list(rs)), 0)
245
246 def test_add_range_obj(self):
247 rs = self._CLS((self._range1,))
248 self.assertEqual(len(rs), 1)
249 rs.add(self._range2)
250 self.assertEqual(len(rs), 2)
251 self.assertIn(self._range2, rs)
252
fc866000
FD
253 def test_const_add_range_obj(self):
254 with self.assertRaises(AttributeError):
255 self._const_rs.add((12, 4434))
256
19bb8b5a
PP
257 def test_discard_not_implemented(self):
258 with self.assertRaises(NotImplementedError):
259 self._rs.discard(self._range2)
260
261 def test_eq_same_order(self):
262 rs = self._CLS((self._range1, self._range2, self._range3))
263 self.assertEqual(self._rs, rs)
264
265 def test_eq_diff_order(self):
266 rs = self._CLS((self._range1, self._range3, self._range2))
267 self.assertEqual(self._rs, rs)
268
269 def test_eq_same_addr(self):
270 self.assertEqual(self._rs, self._rs)
271
272 def test_ne_diff_len(self):
273 rs = self._CLS((self._range1, self._range2))
274 self.assertNotEqual(self._rs, rs)
275
276 def test_ne_diff_values(self):
277 rs1 = self._CLS((self._range1, self._range2))
278 rs2 = self._CLS((self._range1, self._range3))
279 self.assertNotEqual(rs1, rs2)
280
281 def test_ne_incompatible_type(self):
282 self.assertNotEqual(self._rs, object())
283
284
285class UnsignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase, unittest.TestCase):
286 _CLS = bt2.UnsignedIntegerRangeSet
fc866000
FD
287 _CONST_CLS = bt2._UnsignedIntegerRangeSetConst
288 _RANGE_CLS = bt2.UnsignedIntegerRange
289 _CONST_RANGE_CLS = bt2._UnsignedIntegerRangeConst
290 _GET_CONST_RANGE_SET = staticmethod(get_const_unsigned_integer_range)
19bb8b5a
PP
291
292 def setUp(self):
293 self._range1 = bt2.UnsignedIntegerRange(4, 192)
294 self._range2 = bt2.UnsignedIntegerRange(17, 228)
295 self._range3 = bt2.UnsignedIntegerRange(1000, 2000)
7a2a2328 296 self._range_same = bt2.UnsignedIntegerRange(1300, 1300)
19bb8b5a
PP
297 super().setUp()
298
299
300class SignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase, unittest.TestCase):
301 _CLS = bt2.SignedIntegerRangeSet
fc866000
FD
302 _CONST_CLS = bt2._SignedIntegerRangeSetConst
303 _RANGE_CLS = bt2.SignedIntegerRange
304 _CONST_RANGE_CLS = bt2._SignedIntegerRangeConst
305 _GET_CONST_RANGE_SET = staticmethod(get_const_signed_integer_range)
19bb8b5a
PP
306
307 def setUp(self):
308 self._range1 = bt2.SignedIntegerRange(-1484, -17)
309 self._range2 = bt2.SignedIntegerRange(-101, 1500)
310 self._range3 = bt2.SignedIntegerRange(1948, 2019)
7a2a2328 311 self._range_same = bt2.SignedIntegerRange(-1300, -1300)
19bb8b5a 312 super().setUp()
d14ddbba
SM
313
314
315if __name__ == '__main__':
316 unittest.main()
This page took 0.04292 seconds and 4 git commands to generate.