forked from msbrogli/sdm-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
132 lines (102 loc) · 3.97 KB
/
tests.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import print_function
import unittest
from sdm import Bitstring, AddressSpace, Counter, SDM
import sdm as sdmlib
from sdm import utils
import random
class BitstringTests(unittest.TestCase):
def test_bitstring_init_random(self):
bs1 = Bitstring.init_random(1000)
cnt1 = bs1.to_binary().count('1')
self.assertLessEqual(abs(cnt1 - 500), 100)
def test_bitstring_init_zeros(self):
bs1 = Bitstring.init_zeros(1000)
cnt0 = bs1.to_binary().count('0')
self.assertEqual(1000, cnt0)
def test_bitstring_init_ones(self):
bs1 = Bitstring.init_ones(1000)
cnt1 = bs1.to_binary().count('1')
self.assertEqual(1000, cnt1)
def test_bitstring_copy(self):
bs1 = Bitstring.init_random(1000)
bs2 = bs1.copy()
self.assertEqual(0, bs1.distance_to(bs2))
def test_bitstring_flip_random_bits(self):
bs1 = Bitstring.init_random(1000)
for i in range(1001):
bs2 = bs1.copy()
bs2.flip_random_bits(i)
self.assertEqual(i, bs1.distance_to(bs2))
def test_bitstring_xor(self):
bs1 = Bitstring.init_random(1000)
bs2 = Bitstring.init_ones(1000)
bs3 = bs1.copy()
bs3.xor(bs2)
self.assertEqual(1000, bs1.distance_to(bs3))
bs2 = Bitstring.init_zeros(1000)
bs3 = bs1.copy()
bs3.xor(bs2)
self.assertEqual(0, bs1.distance_to(bs3))
class CounterTests(unittest.TestCase):
def test_init(self):
Counter.init_zero(256, 1000000)
def test_basic(self):
bits = 256
radius = 103
sample = 1000000
address_space = AddressSpace.init_random(bits, sample)
counter = Counter.init_zero(bits, sample)
scanner_type = sdmlib.SDM_SCANNER_OPENCL
sdm = SDM(address_space, counter, radius, scanner_type)
sdm.write_random_bitstrings(1000)
for k in range(1000):
idx = random.choice(range(sample))
bs = counter.to_bitstring(idx)
counter_values = counter.get_counter(idx)
for i, x in enumerate(counter_values):
if bs.get_bit(i):
assert(x >= 0)
else:
assert(x <= 0)
class AddressSpaceTests(unittest.TestCase):
def test_init_opencl(self):
as1 = AddressSpace.init_random(1000, 1000000)
as1.opencl_init()
class SDMTests(unittest.TestCase):
def test_radius_calculation(self):
self.assertEqual(451, utils.calculate_radius(1000))
self.assertEqual(103, utils.calculate_radius(256))
self.assertEqual(4845, utils.calculate_radius(10000))
def test_address_space(self):
as1 = AddressSpace.init_random(1000, 1000000)
bs = Bitstring.init_random(1000)
as1.scan_thread2(bs, 451)
def test_counter(self):
Counter.init_zero(1000, 1000000)
def _test_sdm(self, scanner_type):
as1 = AddressSpace.init_random(1000, 1000000)
counter = Counter.init_zero(1000, 1000000)
sdm = SDM(as1, counter, 451, scanner_type)
bs1 = Bitstring.init_random(1000)
sdm.write(bs1, bs1)
bs2 = sdm.read(bs1)
self.assertEqual(0, bs1.distance_to(bs2))
def test_sdm_linear(self):
self._test_sdm(sdmlib.SDM_SCANNER_LINEAR)
def test_sdm_thread(self):
self._test_sdm(sdmlib.SDM_SCANNER_THREAD)
def test_sdm_opencl(self):
self._test_sdm(sdmlib.SDM_SCANNER_OPENCL)
def test_sdm_opencl_kernels(self):
as1 = AddressSpace.init_random(1000, 1000000)
counter = Counter.init_zero(1000, 1000000)
sdm = SDM(as1, counter, 451, sdmlib.SDM_SCANNER_OPENCL)
as1.opencl_opts.verbose = 1
for kernel in sdmlib.OPENCL_KERNEL_NAMES:
as1.set_opencl_kernel(kernel)
bs1 = Bitstring.init_random(1000)
sdm.write(bs1, bs1)
bs2 = sdm.read(bs1)
self.assertEqual(0, bs1.distance_to(bs2))
if __name__ == '__main__':
unittest.main()