(self)
| 259 | sample('x', 1, counts=[0]) |
| 260 | |
| 261 | def test_choices(self): |
| 262 | choices = self.gen.choices |
| 263 | data = ['red', 'green', 'blue', 'yellow'] |
| 264 | str_data = 'abcd' |
| 265 | range_data = range(4) |
| 266 | set_data = set(range(4)) |
| 267 | |
| 268 | # basic functionality |
| 269 | for sample in [ |
| 270 | choices(data, k=5), |
| 271 | choices(data, range(4), k=5), |
| 272 | choices(k=5, population=data, weights=range(4)), |
| 273 | choices(k=5, population=data, cum_weights=range(4)), |
| 274 | choices(data, k=MyIndex(5)), |
| 275 | ]: |
| 276 | self.assertEqual(len(sample), 5) |
| 277 | self.assertEqual(type(sample), list) |
| 278 | self.assertTrue(set(sample) <= set(data)) |
| 279 | |
| 280 | # test argument handling |
| 281 | with self.assertRaises(TypeError): # missing arguments |
| 282 | choices(2) |
| 283 | |
| 284 | self.assertEqual(choices(data, k=0), []) # k == 0 |
| 285 | self.assertEqual(choices(data, k=-1), []) # negative k behaves like ``[0] * -1`` |
| 286 | with self.assertRaises(TypeError): |
| 287 | choices(data, k=2.5) # k is a float |
| 288 | |
| 289 | self.assertTrue(set(choices(str_data, k=5)) <= set(str_data)) # population is a string sequence |
| 290 | self.assertTrue(set(choices(range_data, k=5)) <= set(range_data)) # population is a range |
| 291 | with self.assertRaises(TypeError): |
| 292 | choices(set_data, k=2) # population is not a sequence |
| 293 | |
| 294 | self.assertTrue(set(choices(data, None, k=5)) <= set(data)) # weights is None |
| 295 | self.assertTrue(set(choices(data, weights=None, k=5)) <= set(data)) |
| 296 | with self.assertRaises(ValueError): |
| 297 | choices(data, [1,2], k=5) # len(weights) != len(population) |
| 298 | with self.assertRaises(TypeError): |
| 299 | choices(data, 10, k=5) # non-iterable weights |
| 300 | with self.assertRaises(TypeError): |
| 301 | choices(data, [None]*4, k=5) # non-numeric weights |
| 302 | for weights in [ |
| 303 | [15, 10, 25, 30], # integer weights |
| 304 | [15.1, 10.2, 25.2, 30.3], # float weights |
| 305 | [Fraction(1, 3), Fraction(2, 6), Fraction(3, 6), Fraction(4, 6)], # fractional weights |
| 306 | [True, False, True, False] # booleans (include / exclude) |
| 307 | ]: |
| 308 | self.assertTrue(set(choices(data, weights, k=5)) <= set(data)) |
| 309 | |
| 310 | with self.assertRaises(ValueError): |
| 311 | choices(data, cum_weights=[1,2], k=5) # len(weights) != len(population) |
| 312 | with self.assertRaises(TypeError): |
| 313 | choices(data, cum_weights=10, k=5) # non-iterable cum_weights |
| 314 | with self.assertRaises(TypeError): |
| 315 | choices(data, cum_weights=[None]*4, k=5) # non-numeric cum_weights |
| 316 | with self.assertRaises(TypeError): |
| 317 | choices(data, range(4), cum_weights=range(4), k=5) # both weights and cum_weights |
| 318 | for weights in [ |
nothing calls this directly
no test coverage detected