def test_lo_hi_equal(self): with self.assertRaises(ValueError): random.randint(3, 3, size=3) with self.assertRaises(ValueError): # int(-0.2) is not less than int(0.3) random.randint(-0.2, 0.3)
def test_goodness_of_fit(self): mx = 5 trial = 100 vals = [random.randint(0, mx).get() for _ in range(trial)] counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0] expected = numpy.array([float(trial) / mx] * mx) self.assertTrue(hypothesis.chi_square_test(counts, expected))
def test_dtype(self, dtype): size = (1000,) low = numpy.iinfo(dtype).min high = numpy.iinfo(dtype).max + 1 x = random.randint(low, high, size, dtype) self.assertLessEqual(low, min(x)) self.assertLessEqual(max(x), high)
def test_goodness_of_fit_2(self): mx = 5 # vals = random.randint(mx, size=(5, 20)).get() # counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0] vals = random.randint(mx, size=(5, 20)).reshape(5 * 20) counts = numpy.histogram(numpy.asarray(vals), bins=numpy.arange(mx + 1))[0] expected = numpy.array([float(vals.size) / mx] * mx) self.assertTrue(hypothesis.chi_square_test(counts, expected))
def test_bound_float1(self): # generate floats s.t. int(low) < int(high) low, high = sorted(numpy.random.uniform(-5, 5, size=2)) low -= 1 high += 1 vals = [random.randint(low, high, (2, 3)) for _ in range(10)] for val in vals: self.assertEqual(val.shape, (2, 3)) self.assertEqual(min(_.min() for _ in vals), int(low)) self.assertEqual(max(_.max() for _ in vals), int(high) - 1)
def test_dtype2(self, dtype): dtype = numpy.dtype(dtype) # randint does not support 64 bit integers # if dtype in (numpy.int64, numpy.uint64): # return iinfo = numpy.iinfo(dtype) size = (10000,) x = random.randint(iinfo.min, iinfo.max + 1, size, dtype) self.assertEqual(x.dtype, dtype) self.assertLessEqual(iinfo.min, min(x)) self.assertLessEqual(max(x), iinfo.max) # Lower bound check with self.assertRaises(ValueError): random.randint(iinfo.min - 1, iinfo.min + 10, size, dtype) # Upper bound check with self.assertRaises(ValueError): random.randint(iinfo.max - 10, iinfo.max + 2, size, dtype)
def test_bound_float2(self): vals = [random.randint(-1.0, 1.0, (2, 3)) for _ in range(10)] for val in vals: self.assertEqual(val.shape, (2, 3)) self.assertEqual(min(_.min() for _ in vals), -1) self.assertEqual(max(_.max() for _ in vals), 0)
def test_bound_overflow(self): # 100 - (-100) exceeds the range of int8 val = random.randint(numpy.int8(-100), numpy.int8(100), size=20) self.assertEqual(val.shape, (20,)) self.assertGreaterEqual(val.min(), -100) self.assertLess(val.max(), 100)
def test_bound_2(self): vals = [random.randint(0, 2) for _ in range(20)] for val in vals: self.assertEqual(val.shape, ()) self.assertEqual(min(_.min() for _ in vals), 0) self.assertEqual(max(_.max() for _ in vals), 1)
def test_zero_sizes(self): a = random.randint(10, size=(0,)) numpy.testing.assert_array_equal(a, cupy.array(())) a = random.randint(10, size=0) numpy.testing.assert_array_equal(a, cupy.array(()))
def test_lo_hi_nonrandom(self): a = random.randint(-0.9, 1.1, size=3) numpy.testing.assert_array_equal(a, cupy.full((3,), 0)) a = random.randint(-1.1, -0.9, size=(2, 2)) numpy.testing.assert_array_equal(a, cupy.full((2, 2), -1))
def test_lo_hi_reversed(self): with self.assertRaises(ValueError): random.randint(100, 1)
def test_bound_1(self): vals = [random.randint(0, 10, (2, 3)).get() for _ in range(10)] for val in vals: self.assertEqual(val.shape, (2, 3)) self.assertEqual(min(_.min() for _ in vals), 0) self.assertEqual(max(_.max() for _ in vals), 9)