2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
23 class _IntegerRangeTestCase
:
25 self
._rg
= self
._CLS
(self
._def
_lower
, self
._def
_upper
)
27 def test_create(self
):
28 self
.assertEqual(self
._rg
.lower
, self
._def
_lower
)
29 self
.assertEqual(self
._rg
.upper
, self
._def
_upper
)
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
)
36 def test_create_wrong_type_lower(self
):
37 with self
.assertRaises(TypeError):
38 rg
= self
._CLS
(19.3, self
._def
_upper
)
40 def test_create_wrong_type_lower(self
):
41 with self
.assertRaises(TypeError):
42 rg
= self
._CLS
(self
._def
_lower
, 19.3)
44 def test_create_out_of_bound_lower(self
):
45 with self
.assertRaises(ValueError):
46 rg
= self
._CLS
(self
._oob
_lower
, self
._def
_upper
)
48 def test_create_out_of_bound_upper(self
):
49 with self
.assertRaises(ValueError):
50 rg
= self
._CLS
(self
._def
_lower
, self
._oob
_upper
)
52 def test_create_lower_gt_upper(self
):
53 with self
.assertRaises(ValueError):
54 rg
= self
._CLS
(self
._def
_lower
, self
._def
_lower
- 1)
56 def test_contains_lower(self
):
57 self
.assertTrue(self
._rg
.contains(self
._def
_lower
))
59 def test_contains_upper(self
):
60 self
.assertTrue(self
._rg
.contains(self
._def
_upper
))
62 def test_contains_avg(self
):
63 avg
= (self
._def
_lower
+ self
._def
_upper
) // 2
64 self
.assertTrue(self
._rg
.contains(avg
))
66 def test_contains_wrong_type(self
):
67 with self
.assertRaises(TypeError):
68 self
._rg
.contains('allo')
70 def test_contains_out_of_bound(self
):
71 with self
.assertRaises(ValueError):
72 self
._rg
.contains(self
._oob
_upper
)
75 rg
= self
._CLS
(self
._def
_lower
, self
._def
_upper
)
76 self
.assertEqual(rg
, self
._rg
)
79 rg
= self
._CLS
(self
._def
_lower
, self
._def
_upper
- 1)
80 self
.assertNotEqual(rg
, self
._rg
)
82 def test_ne_other_type(self
):
83 self
.assertNotEqual(self
._rg
, 48)
86 class UnsignedIntegerRangeTestCase(_IntegerRangeTestCase
, unittest
.TestCase
):
87 _CLS
= bt2
.UnsignedIntegerRange
94 class SignedIntegerRangeTestCase(_IntegerRangeTestCase
, unittest
.TestCase
):
95 _CLS
= bt2
.SignedIntegerRange
98 _oob_lower
= -(1 << 63) - 1
102 class _IntegerRangeSetTestCase
:
104 self
._rs
= self
._CLS
((self
._range
1, self
._range
2, self
._range
3))
106 def test_create(self
):
107 self
.assertEqual(len(self
._rs
), 3)
108 self
.assertIn(self
._range
1, self
._rs
)
109 self
.assertIn(self
._range
2, self
._rs
)
110 self
.assertIn(self
._range
3, self
._rs
)
112 def test_create_tuples(self
):
114 (self
._range
1.lower
, self
._range
1.upper
),
115 (self
._range
2.lower
, self
._range
2.upper
),
116 (self
._range
3.lower
, self
._range
3.upper
),
118 self
.assertEqual(len(rs
), 3)
119 self
.assertIn(self
._range
1, rs
)
120 self
.assertIn(self
._range
2, rs
)
121 self
.assertIn(self
._range
3, rs
)
123 def test_create_non_iter(self
):
124 with self
.assertRaises(TypeError):
125 self
._rs
= self
._CLS
(23)
127 def test_create_wrong_elem_type(self
):
128 with self
.assertRaises(TypeError):
129 self
._rs
= self
._CLS
((self
._range
1, self
._range
2, 17))
132 self
.assertEqual(len(self
._rs
), 3)
134 def test_contains(self
):
135 rs
= self
._CLS
((self
._range
1, self
._range
2))
136 self
.assertIn(self
._range
1, rs
)
137 self
.assertIn(self
._range
2, rs
)
138 self
.assertNotIn(self
._range
3, rs
)
140 def test_contains_value(self
):
141 rs
= self
._CLS
((self
._range
1, self
._range
2))
142 self
.assertTrue(rs
.contains_value(self
._range
1.upper
))
143 self
.assertTrue(rs
.contains_value(self
._range
2.lower
))
144 self
.assertFalse(rs
.contains_value(self
._range
3.upper
))
146 def test_contains_value_wrong_type(self
):
147 with self
.assertRaises(TypeError):
148 self
._rs
.contains_value('meow')
151 range_list
= list(self
._rs
)
152 self
.assertIn(self
._range
1, range_list
)
153 self
.assertIn(self
._range
2, range_list
)
154 self
.assertIn(self
._range
3, range_list
)
156 def test_empty(self
):
158 self
.assertEqual(len(rs
), 0)
159 self
.assertEqual(len(list(rs
)), 0)
161 def test_add_range_obj(self
):
162 rs
= self
._CLS
((self
._range
1,))
163 self
.assertEqual(len(rs
), 1)
165 self
.assertEqual(len(rs
), 2)
166 self
.assertIn(self
._range
2, rs
)
168 def test_discard_not_implemented(self
):
169 with self
.assertRaises(NotImplementedError):
170 self
._rs
.discard(self
._range
2)
172 def test_eq_same_order(self
):
173 rs
= self
._CLS
((self
._range
1, self
._range
2, self
._range
3))
174 self
.assertEqual(self
._rs
, rs
)
176 def test_eq_diff_order(self
):
177 rs
= self
._CLS
((self
._range
1, self
._range
3, self
._range
2))
178 self
.assertEqual(self
._rs
, rs
)
180 def test_eq_same_addr(self
):
181 self
.assertEqual(self
._rs
, self
._rs
)
183 def test_ne_diff_len(self
):
184 rs
= self
._CLS
((self
._range
1, self
._range
2))
185 self
.assertNotEqual(self
._rs
, rs
)
187 def test_ne_diff_values(self
):
188 rs1
= self
._CLS
((self
._range
1, self
._range
2))
189 rs2
= self
._CLS
((self
._range
1, self
._range
3))
190 self
.assertNotEqual(rs1
, rs2
)
192 def test_ne_incompatible_type(self
):
193 self
.assertNotEqual(self
._rs
, object())
196 class UnsignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase
, unittest
.TestCase
):
197 _CLS
= bt2
.UnsignedIntegerRangeSet
200 self
._range
1 = bt2
.UnsignedIntegerRange(4, 192)
201 self
._range
2 = bt2
.UnsignedIntegerRange(17, 228)
202 self
._range
3 = bt2
.UnsignedIntegerRange(1000, 2000)
206 class SignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase
, unittest
.TestCase
):
207 _CLS
= bt2
.SignedIntegerRangeSet
210 self
._range
1 = bt2
.SignedIntegerRange(-1484, -17)
211 self
._range
2 = bt2
.SignedIntegerRange(-101, 1500)
212 self
._range
3 = bt2
.SignedIntegerRange(1948, 2019)
This page took 0.034038 seconds and 4 git commands to generate.