-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt_test.py
62 lines (50 loc) · 2.04 KB
/
mt_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import unittest
import mt
import random
class Tests(unittest.TestCase):
def test_randomness(self):
""" Gives repeatable random numbers for same seed """
rand = mt.MT19937(888)
rand1a = rand.extract_number()
rand2a = rand.extract_number()
rand = mt.MT19937(888)
rand1b = rand.extract_number()
rand2b = rand.extract_number()
rand = mt.MT19937(889)
rand1c = rand.extract_number()
rand2c = rand.extract_number()
self.assertEqual(rand1a, rand1b)
self.assertEqual(rand2a, rand2b)
self.assertNotEqual(rand1a, rand1c)
self.assertNotEqual(rand2a, rand2c)
def test_untemper(self):
""" Correctly applies the reverse of the tempering of the MT """
untempered = random.randint(0, 10000000)
tempered = untempered
# Right shift by 11 bits
tempered = tempered ^ tempered >> 11
# Shift tempered left by 7 and take the bitwise and of 2636928640
tempered = tempered ^ tempered << 7 & 2636928640
# Shift tempered left by 15 and take the bitwise and of tempered and 4022730752
tempered = tempered ^ tempered << 15 & 4022730752
# Right shift by 18 bits
tempered = tempered ^ tempered >> 18
test = mt.untemper(tempered)
self.assertEqual(untempered, test)
def test_reverse_right(self):
for i in range(30):
input = random.randint(0, 0xFFFFFFFF)
shift = random.randint(1, 32)
shifted = input ^ input >> shift
test = mt.reverse_right(shifted, shift)
self.assertEqual(input, test)
def test_reverse_left(self):
for i in range(30):
input = random.randint(0, 0xFFFFFFFF)
shift = random.randint(1, 32)
magic_number = random.randint(0, 0xFFFFFFFF)
shifted = input ^ input << shift & magic_number
test = mt.reverse_left(shifted, shift, magic_number)
self.assertEqual(input, test)
if __name__ == '__main__':
unittest.main()