bt2: add integer range set support
[babeltrace.git] / tests / bindings / python / bt2 / test_integer_range_set.py
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
19 import bt2
20 import unittest
21
22
23 class _IntegerRangeTestCase:
24 def setUp(self):
25 self._rg = self._CLS(self._def_lower, self._def_upper)
26
27 def test_create(self):
28 self.assertEqual(self._rg.lower, self._def_lower)
29 self.assertEqual(self._rg.upper, self._def_upper)
30
31 def test_create_same(self):
32 rg = self._CLS(self._def_lower, self._def_lower)
33 self.assertEqual(rg.lower, self._def_lower)
34 self.assertEqual(rg.upper, self._def_lower)
35
36 def test_create_wrong_type_lower(self):
37 with self.assertRaises(TypeError):
38 rg = self._CLS(19.3, self._def_upper)
39
40 def test_create_wrong_type_lower(self):
41 with self.assertRaises(TypeError):
42 rg = self._CLS(self._def_lower, 19.3)
43
44 def test_create_out_of_bound_lower(self):
45 with self.assertRaises(ValueError):
46 rg = self._CLS(self._oob_lower, self._def_upper)
47
48 def test_create_out_of_bound_upper(self):
49 with self.assertRaises(ValueError):
50 rg = self._CLS(self._def_lower, self._oob_upper)
51
52 def test_create_lower_gt_upper(self):
53 with self.assertRaises(ValueError):
54 rg = self._CLS(self._def_lower, self._def_lower - 1)
55
56 def test_contains_lower(self):
57 self.assertTrue(self._rg.contains(self._def_lower))
58
59 def test_contains_upper(self):
60 self.assertTrue(self._rg.contains(self._def_upper))
61
62 def test_contains_avg(self):
63 avg = (self._def_lower + self._def_upper) // 2
64 self.assertTrue(self._rg.contains(avg))
65
66 def test_contains_wrong_type(self):
67 with self.assertRaises(TypeError):
68 self._rg.contains('allo')
69
70 def test_contains_out_of_bound(self):
71 with self.assertRaises(ValueError):
72 self._rg.contains(self._oob_upper)
73
74 def test_eq(self):
75 rg = self._CLS(self._def_lower, self._def_upper)
76 self.assertEqual(rg, self._rg)
77
78 def test_ne(self):
79 rg = self._CLS(self._def_lower, self._def_upper - 1)
80 self.assertNotEqual(rg, self._rg)
81
82 def test_ne_other_type(self):
83 self.assertNotEqual(self._rg, 48)
84
85
86 class UnsignedIntegerRangeTestCase(_IntegerRangeTestCase, unittest.TestCase):
87 _CLS = bt2.UnsignedIntegerRange
88 _def_lower = 23
89 _def_upper = 18293
90 _oob_lower = -1
91 _oob_upper = 1 << 64
92
93
94 class SignedIntegerRangeTestCase(_IntegerRangeTestCase, unittest.TestCase):
95 _CLS = bt2.SignedIntegerRange
96 _def_lower = -184
97 _def_upper = 11547
98 _oob_lower = -(1 << 63) - 1
99 _oob_upper = 1 << 63
100
101
102 class _IntegerRangeSetTestCase:
103 def setUp(self):
104 self._rs = self._CLS((self._range1, self._range2, self._range3))
105
106 def test_create(self):
107 self.assertEqual(len(self._rs), 3)
108 self.assertIn(self._range1, self._rs)
109 self.assertIn(self._range2, self._rs)
110 self.assertIn(self._range3, self._rs)
111
112 def test_create_tuples(self):
113 rs = self._CLS((
114 (self._range1.lower, self._range1.upper),
115 (self._range2.lower, self._range2.upper),
116 (self._range3.lower, self._range3.upper),
117 ))
118 self.assertEqual(len(rs), 3)
119 self.assertIn(self._range1, rs)
120 self.assertIn(self._range2, rs)
121 self.assertIn(self._range3, rs)
122
123 def test_create_non_iter(self):
124 with self.assertRaises(TypeError):
125 self._rs = self._CLS(23)
126
127 def test_create_wrong_elem_type(self):
128 with self.assertRaises(TypeError):
129 self._rs = self._CLS((self._range1, self._range2, 17))
130
131 def test_len(self):
132 self.assertEqual(len(self._rs), 3)
133
134 def test_contains(self):
135 rs = self._CLS((self._range1, self._range2))
136 self.assertIn(self._range1, rs)
137 self.assertIn(self._range2, rs)
138 self.assertNotIn(self._range3, rs)
139
140 def test_contains_value(self):
141 rs = self._CLS((self._range1, self._range2))
142 self.assertTrue(rs.contains_value(self._range1.upper))
143 self.assertTrue(rs.contains_value(self._range2.lower))
144 self.assertFalse(rs.contains_value(self._range3.upper))
145
146 def test_contains_value_wrong_type(self):
147 with self.assertRaises(TypeError):
148 self._rs.contains_value('meow')
149
150 def test_iter(self):
151 range_list = list(self._rs)
152 self.assertIn(self._range1, range_list)
153 self.assertIn(self._range2, range_list)
154 self.assertIn(self._range3, range_list)
155
156 def test_empty(self):
157 rs = self._CLS()
158 self.assertEqual(len(rs), 0)
159 self.assertEqual(len(list(rs)), 0)
160
161 def test_add_range_obj(self):
162 rs = self._CLS((self._range1,))
163 self.assertEqual(len(rs), 1)
164 rs.add(self._range2)
165 self.assertEqual(len(rs), 2)
166 self.assertIn(self._range2, rs)
167
168 def test_discard_not_implemented(self):
169 with self.assertRaises(NotImplementedError):
170 self._rs.discard(self._range2)
171
172 def test_eq_same_order(self):
173 rs = self._CLS((self._range1, self._range2, self._range3))
174 self.assertEqual(self._rs, rs)
175
176 def test_eq_diff_order(self):
177 rs = self._CLS((self._range1, self._range3, self._range2))
178 self.assertEqual(self._rs, rs)
179
180 def test_eq_same_addr(self):
181 self.assertEqual(self._rs, self._rs)
182
183 def test_ne_diff_len(self):
184 rs = self._CLS((self._range1, self._range2))
185 self.assertNotEqual(self._rs, rs)
186
187 def test_ne_diff_values(self):
188 rs1 = self._CLS((self._range1, self._range2))
189 rs2 = self._CLS((self._range1, self._range3))
190 self.assertNotEqual(rs1, rs2)
191
192 def test_ne_incompatible_type(self):
193 self.assertNotEqual(self._rs, object())
194
195
196 class UnsignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase, unittest.TestCase):
197 _CLS = bt2.UnsignedIntegerRangeSet
198
199 def setUp(self):
200 self._range1 = bt2.UnsignedIntegerRange(4, 192)
201 self._range2 = bt2.UnsignedIntegerRange(17, 228)
202 self._range3 = bt2.UnsignedIntegerRange(1000, 2000)
203 super().setUp()
204
205
206 class SignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase, unittest.TestCase):
207 _CLS = bt2.SignedIntegerRangeSet
208
209 def setUp(self):
210 self._range1 = bt2.SignedIntegerRange(-1484, -17)
211 self._range2 = bt2.SignedIntegerRange(-101, 1500)
212 self._range3 = bt2.SignedIntegerRange(1948, 2019)
213 super().setUp()
This page took 0.034038 seconds and 4 git commands to generate.