-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_madoka.py
280 lines (219 loc) · 8.12 KB
/
test_madoka.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
from nose.tools import assert_equal, assert_true
import madoka
import os
import tempfile
import numbers
class MadokaTest(object):
def test___len__(self):
sketch = self.target_class(width=100)
assert_true(isinstance(len(sketch), numbers.Integral))
def test___contains__(self):
sketch = self.target_class(width=100)
sketch.add('mami', 1)
assert_true('mami' in sketch)
def test___setitem__(self):
sketch = self.target_class(width=100)
sketch['mami'] += 20
assert_equal(sketch.get('mami'), 20)
sketch['mami'] *= 2
assert_equal(sketch.get('mami'), 40)
def test___getitem__(self):
sketch = self.target_class(width=100)
sketch.set('mami', 1)
assert_equal(sketch['mami'], 1)
def test___add__(self):
sketch = self.target_class(width=100)
sketch.inc('mami')
other_sketch = self.target_class(width=100)
other_sketch.inc('mami')
new_sketch = sketch + other_sketch
assert_equal(new_sketch['mami'], 2)
new_sketch = sketch + {'mami': 1, 'madoka': 2}
assert_equal(new_sketch['mami'], 2)
def test___iadd__(self):
sketch = self.target_class(width=100)
sketch.inc('mami')
other_sketch = self.target_class(width=100)
other_sketch.inc('mami')
sketch += other_sketch
assert_equal(sketch['mami'], 2)
assert_equal(other_sketch['mami'], 1)
sketch = self.target_class(width=100)
sketch.inc('mami')
sketch += {'mami': 2}
assert_equal(sketch['mami'], 3)
def test_add(self):
sketch = self.target_class(width=100)
sketch.add('mami', 2)
assert_equal(sketch['mami'], 2)
sketch.add('mami', 8)
assert_equal(sketch['mami'], 10)
sketch['mami'] += 10
assert_equal(sketch['mami'], 20)
def test_set(self):
sketch = self.target_class(width=100)
sketch.set('mami', 14)
assert_equal(sketch['mami'], 14)
def test_inc(self):
sketch = self.target_class(width=100)
sketch.inc('mami')
assert_equal(sketch['mami'], 1)
sketch.inc('mami')
assert_equal(sketch['mami'], 2)
def test_clear(self):
sketch = self.target_class(width=100)
sketch['mami'] = 14
sketch.clear()
assert_equal(sketch['mami'], 0)
def test_create(self):
sketch = self.target_class(width=100)
sketch.create()
sketch['mami'] = 14
assert_equal(sketch['mami'], 14)
def test_fromdict(self):
sketch = self.target_class(width=100)
src_dict = {'mami': 14, 'madoka': 13}
sketch.fromdict(src_dict)
assert_equal(sketch['mami'], 14)
def test_save_and_load(self):
try:
sketch_file = tempfile.mktemp()
sketch = self.target_class(width=100)
sketch['mami'] = 14
sketch.save(sketch_file)
assert_true(os.path.exists(sketch_file))
sketch = self.target_class(width=100)
sketch.load(sketch_file)
assert_equal(sketch['mami'], 14)
sketch = self.target_class(width=100)
sketch.open(sketch_file)
assert_equal(sketch['mami'], 14)
finally:
del sketch
os.remove(sketch_file)
def test_copy(self):
sketch = self.target_class(width=100)
sketch['mami'] = 14
new_sketch = self.target_class(width=100)
new_sketch.copy(sketch)
assert_equal(new_sketch['mami'], 14)
def test_inner_product(self):
sketch = self.target_class(width=100)
sketch['mami'] = 2
sketch['homura'] = 1
sketch['kyouko'] = 2
new_sketch = self.target_class(width=100)
new_sketch['mami'] = 2
new_sketch['kyouko'] = 3
got = sketch.inner_product(new_sketch)
assert_equal(got, [10, 9, 13])
def test_median(self):
sketch = self.target_class(width=100)
sketch['mami'] = 1
sketch['madoka'] = 2
sketch['sayaka'] = 3
sketch['kyouko'] = 4
sketch['homura'] = 5
assert_equal(sketch.median(), 3)
def test_filter(self):
sketch = self.target_class(width=100)
sketch['mami'] = 2
filter_method = lambda x: x * 2
sketch.filter(filter_method)
assert_equal(sketch['mami'], 4)
sketch = self.target_class(width=100)
sketch['mami'] = 2
neg_sign = lambda x: 0 if x else 1
sketch.filter(neg_sign, apply_zerovalue=True)
assert_equal(sketch['mami'], 0)
assert_equal(sketch['homura'], 1)
def test_merge(self):
sketch = self.target_class(width=1000)
sketch['mami'] = 14
new_sketch = self.target_class(width=1000)
new_sketch['mami'] = 14
new_sketch.merge(sketch)
assert_equal(new_sketch['mami'], 28)
def test_shrink(self):
sketch = self.target_class(width=10000)
sketch['mami'] = 2
sketch.shrink(sketch, width=1000)
assert_equal(sketch['mami'], 2)
assert_equal(sketch.width, 1000)
def test_values(self):
sketch = self.target_class(width=100)
sketch['mami'] = 2
sketch['madoka'] = 3
got = [i for i in sketch.values()]
assert_equal(set(got), set([2, 3]))
def test_most_common(self):
sketch = self.target_class(width=100)
sketch['mami'] = 14
sketch['madoka'] = 13
sketch['sayaka'] = 13
got = list(sketch.most_common())
assert_equal(got, [('mami', 14), ('sayaka', 13), ('madoka', 13)])
def test_with_statement(self):
with self.target_class() as m:
m['test'] += 1
assert_true(True)
def test_width_mask(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.width_mask, numbers.Integral))
def test_value_size(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.value_size, numbers.Integral))
def test_table_size(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.table_size, numbers.Integral))
def test_flags(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.flags, numbers.Integral))
class Test_Sketch(MadokaTest):
def __init__(self):
self.target_class = madoka.Sketch
def test_merge(self):
sketch = madoka.Sketch(width=1000)
sketch['mami'] = 14
new_sketch = madoka.Sketch(width=1000)
new_sketch['mami'] = 14
new_sketch.merge(sketch)
assert_equal(new_sketch['mami'], 28)
filter_method = lambda x: x // 10
new_sketch.merge(new_sketch, filter_method, filter_method)
assert_equal(new_sketch['mami'], 4)
def test_shrink(self):
sketch = madoka.Sketch(width=10000)
sketch['mami'] = 2
sketch.shrink(sketch, width=1000, max_value=100)
assert_equal(sketch['mami'], 2)
assert_equal(sketch.width, 1000)
filter_method = lambda x: x * 2
sketch.shrink(sketch, width=100, max_value=100,
filter_method=filter_method)
assert_equal(sketch['mami'], 4)
def test_value_mask(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.value_mask, numbers.Integral))
def test_mode(self):
sketch = self.target_class(width=100)
assert_true(isinstance(sketch.mode, numbers.Integral))
class Test_CroquisUint8(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisUint8
class Test_CroquisUint16(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisUint16
class Test_CroquisUint32(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisUint32
class Test_CroquisUint64(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisUint64
class Test_CroquisFloat(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisFloat
class Test_CroquisDouble(MadokaTest):
def __init__(self):
self.target_class = madoka.CroquisDouble