-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.py
190 lines (151 loc) · 5.48 KB
/
test_utils.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
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, division
"""
Created on Fri Jun 27 15:47:47 2014
@author: enrico.giampieri2
"""
import unittest
import sympy
from utils import Counter
from utils import variazione
from utils import shift
from utils import WRGnumpy
class test_Counter(unittest.TestCase):
def test_creation_1(self):
a = Counter()
self.assertTrue(a is not None)
self.assertEqual(a['a'], 0)
def test_creation_2(self):
a = Counter({'a':3})
self.assertEqual(a['a'], 3)
def test_creation_3(self):
a = Counter({'a':3})
b = Counter(a)
self.assertEqual(b['a'], 3)
self.assertIs(a.keymap, b.keymap)
def test_creation_4(self):
a = Counter(5)
self.assertEqual(a['a'], 5)
def test_calculate_total_1(self):
a = Counter({'a':3, 'b':5, 'c':2})
self.assertEqual(a.total(), 10)
def test_normalize_1(self):
a = Counter({'a':3, 'b':5, 'c':2})
b = a.normalize()
self.assertAlmostEqual(b.total(), 1.0)
self.assertAlmostEqual(b['a'], 0.3)
self.assertAlmostEqual(b['b'], 0.5)
self.assertAlmostEqual(b['c'], 0.2)
self.assertIs(a.keymap, b.keymap)
def test_add_other_counter(self):
a = Counter({'b':5, 'c':2})
b = Counter({'a':3, 'b':5})
c = a + b
self.assertAlmostEqual(c['a'], 3)
self.assertAlmostEqual(c['b'], 10)
self.assertAlmostEqual(c['c'], 2)
def test_add_dict(self):
a = Counter({'b':5, 'c':2})
b = {'a':3, 'b':5}
c = a + b
self.assertAlmostEqual(c['a'], 3)
self.assertAlmostEqual(c['b'], 10)
self.assertAlmostEqual(c['c'], 2)
def test_add_dict_reverse(self):
a = Counter({'b':5, 'c':2})
b = {'a':3, 'b':5}
c = b + a
self.assertAlmostEqual(c['a'], 3)
self.assertAlmostEqual(c['b'], 10)
self.assertAlmostEqual(c['c'], 2)
def test_add_number(self):
a = Counter({'b':5, 'c':2})
with self.assertRaises(NotImplementedError):
a + 1
with self.assertRaises(NotImplementedError):
1 + a
def test_multiply_number(self):
a = Counter({'b':5, 'c':2})
b = a * 2
self.assertAlmostEqual(b['b'], 10)
self.assertAlmostEqual(b['c'], 4)
self.assertIs(a.keymap, b.keymap)
def test_multiply_other_counter(self):
a = Counter({'b':5, 'c':2})
b = Counter({'a':3, 'b':5})
c = a * b
self.assertAlmostEqual(c['a'], 0)
self.assertAlmostEqual(c['b'], 25)
self.assertAlmostEqual(c['c'], 0)
def test_itermap_1(self):
a = Counter({'b':5, 'c':2}, keymap=lambda s: s*2)
b = dict(a.itermap())
self.assertEqual(b, {'bb': 5, 'cc': 2})
def test_itermap_2(self):
a = Counter({'b':5, 'c':2})
b = dict(a.itermap(lambda s: s*2))
self.assertEqual(b, {'bb': 5, 'cc': 2})
def test_itermap_3(self):
a = Counter({'b':5, 'c':2}, keymap=lambda s: s*3)
b = dict(a.itermap(lambda s: s*2))
self.assertEqual(b, {'bb': 5, 'cc': 2})
def test_map_1(self):
a = Counter({'b':5, 'c':2}, keymap=lambda s: s*2)
b = a.map()
self.assertEqual(b, {'bb': 5, 'cc': 2})
def test_map_2(self):
a = Counter({(1, 2): 0.5}, keymap=['a', 'b'])
b = a.map()
self.assertEqual(b, {(('a', 1), ('b', 2)): 0.5})
def test_positive(self):
a = Counter({'a':1})
self.assertTrue(a.positive())
a = Counter({'a':-1})
self.assertFalse(a.positive())
a = Counter({'a':1, 'b':-1})
self.assertFalse(a.positive())
class test_variazione(unittest.TestCase):
def test_base(self):
A = sympy.Symbol('A')
B = sympy.Symbol('B')
r = variazione(A)
self.assertEqual(r, {A:1})
r = variazione(A+B)
self.assertEqual(r, {A:1, B:1})
r = variazione(2*A)
self.assertEqual(r, {A:2})
r = variazione(A+A)
self.assertEqual(r, {A:2})
class test_shift(unittest.TestCase):
def test_base(self):
A = sympy.Symbol('A')
B = sympy.Symbol('B')
state_0 = Counter({A:2})
substrate = Counter({A:1})
products = Counter({B:2})
kinetic = A*(A-1)
new_state, kinetic_val = shift(state_0, substrate, products, kinetic)
self.assertEqual(new_state, {A:1, B:2})
self.assertEqual(kinetic_val, 2)
def test_base_impossible_1(self):
A = sympy.Symbol('A')
B = sympy.Symbol('B')
state_0 = Counter({A:1})
substrate = Counter({A:1})
products = Counter({B:2})
kinetic = A*(A-1)
new_state, kinetic_val = shift(state_0, substrate, products, kinetic)
self.assertEqual(new_state, None)
self.assertEqual(kinetic_val, None)
def test_base_impossible_2(self):
A = sympy.Symbol('A')
B = sympy.Symbol('B')
state_0 = Counter({A:2})
substrate = Counter({A:3})
products = Counter({B:2})
kinetic = A*(A-1)
new_state, kinetic_val = shift(state_0, substrate, products, kinetic)
self.assertEqual(new_state, None)
self.assertEqual(kinetic_val, None)
if __name__ == '__main__':
unittest.main()