Beispiel #1
0
    def test_random_exact_bits(self):

        for _ in range(1000):
            a = IntegerNative.random(exact_bits=8)
            self.assertFalse(a < 128)
            self.assertFalse(a >= 256)

        for bits_value in range(1024, 1024 + 8):
            a = IntegerNative.random(exact_bits=bits_value)
            self.assertFalse(a < 2**(bits_value - 1))
            self.assertFalse(a >= 2**bits_value)
Beispiel #2
0
    def test_random_max_bits(self):

        flag = False
        for _ in range(1000):
            a = IntegerNative.random(max_bits=8)
            flag = flag or a < 128
            self.assertFalse(a>=256)
        self.assertTrue(flag)

        for bits_value in range(1024, 1024 + 8):
            a = IntegerNative.random(max_bits=bits_value)
            self.assertFalse(a >= 2**bits_value)
Beispiel #3
0
    def test_random_bits_custom_rng(self):
        class CustomRNG(object):
            def __init__(self):
                self.counter = 0

            def __call__(self, size):
                self.counter += size
                return bchr(0) * size

        custom_rng = CustomRNG()
        a = IntegerNative.random(exact_bits=32, randfunc=custom_rng)
        self.assertEqual(custom_rng.counter, 4)