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
):
115 (self
._range
1.lower
, self
._range
1.upper
),
116 (self
._range
2.lower
, self
._range
2.upper
),
117 (self
._range
3.lower
, self
._range
3.upper
),
120 self
.assertEqual(len(rs
), 3)
121 self
.assertIn(self
._range
1, rs
)
122 self
.assertIn(self
._range
2, rs
)
123 self
.assertIn(self
._range
3, rs
)
125 def test_create_non_iter(self
):
126 with self
.assertRaises(TypeError):
127 self
._rs
= self
._CLS
(23)
129 def test_create_wrong_elem_type(self
):
130 with self
.assertRaises(TypeError):
131 self
._rs
= self
._CLS
((self
._range
1, self
._range
2, 17))
134 self
.assertEqual(len(self
._rs
), 3)
136 def test_contains(self
):
137 rs
= self
._CLS
((self
._range
1, self
._range
2))
138 self
.assertIn(self
._range
1, rs
)
139 self
.assertIn(self
._range
2, rs
)
140 self
.assertNotIn(self
._range
3, rs
)
142 def test_contains_value(self
):
143 rs
= self
._CLS
((self
._range
1, self
._range
2))
144 self
.assertTrue(rs
.contains_value(self
._range
1.upper
))
145 self
.assertTrue(rs
.contains_value(self
._range
2.lower
))
146 self
.assertFalse(rs
.contains_value(self
._range
3.upper
))
148 def test_contains_value_wrong_type(self
):
149 with self
.assertRaises(TypeError):
150 self
._rs
.contains_value('meow')
153 range_list
= list(self
._rs
)
154 self
.assertIn(self
._range
1, range_list
)
155 self
.assertIn(self
._range
2, range_list
)
156 self
.assertIn(self
._range
3, range_list
)
158 def test_empty(self
):
160 self
.assertEqual(len(rs
), 0)
161 self
.assertEqual(len(list(rs
)), 0)
163 def test_add_range_obj(self
):
164 rs
= self
._CLS
((self
._range
1,))
165 self
.assertEqual(len(rs
), 1)
167 self
.assertEqual(len(rs
), 2)
168 self
.assertIn(self
._range
2, rs
)
170 def test_discard_not_implemented(self
):
171 with self
.assertRaises(NotImplementedError):
172 self
._rs
.discard(self
._range
2)
174 def test_eq_same_order(self
):
175 rs
= self
._CLS
((self
._range
1, self
._range
2, self
._range
3))
176 self
.assertEqual(self
._rs
, rs
)
178 def test_eq_diff_order(self
):
179 rs
= self
._CLS
((self
._range
1, self
._range
3, self
._range
2))
180 self
.assertEqual(self
._rs
, rs
)
182 def test_eq_same_addr(self
):
183 self
.assertEqual(self
._rs
, self
._rs
)
185 def test_ne_diff_len(self
):
186 rs
= self
._CLS
((self
._range
1, self
._range
2))
187 self
.assertNotEqual(self
._rs
, rs
)
189 def test_ne_diff_values(self
):
190 rs1
= self
._CLS
((self
._range
1, self
._range
2))
191 rs2
= self
._CLS
((self
._range
1, self
._range
3))
192 self
.assertNotEqual(rs1
, rs2
)
194 def test_ne_incompatible_type(self
):
195 self
.assertNotEqual(self
._rs
, object())
198 class UnsignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase
, unittest
.TestCase
):
199 _CLS
= bt2
.UnsignedIntegerRangeSet
202 self
._range
1 = bt2
.UnsignedIntegerRange(4, 192)
203 self
._range
2 = bt2
.UnsignedIntegerRange(17, 228)
204 self
._range
3 = bt2
.UnsignedIntegerRange(1000, 2000)
208 class SignedIntegerRangeSetTestCase(_IntegerRangeSetTestCase
, unittest
.TestCase
):
209 _CLS
= bt2
.SignedIntegerRangeSet
212 self
._range
1 = bt2
.SignedIntegerRange(-1484, -17)
213 self
._range
2 = bt2
.SignedIntegerRange(-101, 1500)
214 self
._range
3 = bt2
.SignedIntegerRange(1948, 2019)
This page took 0.034905 seconds and 5 git commands to generate.