forked from diffpy/diffpy.srreal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_atomradiitable.py
203 lines (163 loc) · 6.62 KB
/
test_atomradiitable.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
#!/usr/bin/env python
"""Unit tests for the AtomRadiiTable class."""
import pickle
import unittest
import pytest
from diffpy.srreal.atomradiitable import AtomRadiiTable, ConstantRadiiTable, CovalentRadiiTable
# ----------------------------------------------------------------------------
class TestAtomRadiiTable(unittest.TestCase):
def setUp(self):
self.rtb = AtomRadiiTable()
self.ctb = ConstantRadiiTable()
return
def tearDown(self):
return
def test_pickling(self):
"""Check pickling and unpickling of AtomRadiiTable."""
ctb1 = pickle.loads(pickle.dumps(self.ctb))
self.assertTrue(type(ctb1) is ConstantRadiiTable)
self.assertEqual({}, ctb1.getAllCustom())
self.ctb.setCustom("Na", 1.3)
self.ctb.foobar = "foo"
self.ctb.setDefault(3.7)
ctb2 = pickle.loads(pickle.dumps(self.ctb))
self.assertEqual({"Na": 1.3}, ctb2.getAllCustom())
self.assertFalse(hasattr(ctb2, "foobar"))
self.assertEqual(3.7, ctb2.getDefault())
return
def test__standardLookup(self):
"""Check AtomRadiiTable._standardLookup()"""
self.assertRaises(RuntimeError, self.rtb._standardLookup, "anything")
self.assertEqual(0.0, self.ctb._standardLookup("anything"))
self.ctb.setDefault(7.3)
self.assertEqual(7.3, self.ctb._standardLookup("anything"))
return
def test_fromString(self):
"""Check AtomRadiiTable.fromString()"""
self.rtb.fromString("H:0.33, B:0.42")
self.assertEqual({"H": 0.33, "B": 0.42}, self.rtb.getAllCustom())
self.assertRaises(ValueError, self.rtb.fromString, "C:2.3, U:asdf")
self.assertEqual({"H": 0.33, "B": 0.42}, self.rtb.getAllCustom())
self.rtb.fromString("C:2.3,,,")
self.assertEqual(3, len(self.rtb.getAllCustom()))
self.assertEqual(2.3, self.rtb.lookup("C"))
self.rtb.fromString("H:3.3")
self.assertEqual(3, len(self.rtb.getAllCustom()))
self.assertEqual(3.3, self.rtb.lookup("H"))
return
def test_getAllCustom(self):
"""Check AtomRadiiTable.getAllCustom()"""
self.assertEqual({}, self.rtb.getAllCustom())
return
def test_lookup(self):
"""Check AtomRadiiTable.lookup()"""
self.assertRaises(RuntimeError, self.rtb.lookup, "C")
self.assertEqual(0.0, self.ctb.lookup("C"))
self.rtb.setCustom("C", 1.23)
self.assertEqual(1.23, self.rtb.lookup("C"))
return
def test_resetCustom(self):
"""Check AtomRadiiTable.resetCustom()"""
self.rtb.setCustom("C", 1.23)
self.assertTrue(self.rtb.getAllCustom())
self.rtb.resetAll()
self.assertFalse(self.rtb.getAllCustom())
return
def test_setCustom(self):
"""Check AtomRadiiTable.setCustom()"""
self.rtb.setCustom("C", 1.23)
self.assertEqual(1.23, self.rtb.lookup("C"))
self.rtb.setCustom("C", 3.3)
self.assertEqual(3.3, self.rtb.lookup("C"))
return
def test_toString(self):
"""Check AtomRadiiTable.toString()"""
rtb = self.rtb
self.assertEqual("", rtb.toString())
self.assertEqual("", rtb.toString("; "))
rtb.fromString("C : 1.5, B:2.0")
self.assertEqual("B:2,C:1.5", rtb.toString())
self.assertEqual("B:2; C:1.5", rtb.toString("; "))
return
# End of class TestAtomRadiiTable
# ----------------------------------------------------------------------------
class TestCovalentRadiiTable(unittest.TestCase):
@pytest.fixture(autouse=True)
def _check_periodictable(self, has_periodictable, _msg_noperiodictable):
if not has_periodictable:
pytest.skip(_msg_noperiodictable)
def setUp(self):
self.rtb = CovalentRadiiTable()
return
def tearDown(self):
return
def test_pickling(self):
"""Check pickling and unpickling of CovalentRadiiTable."""
rtb1 = pickle.loads(pickle.dumps(self.rtb))
self.assertTrue(type(rtb1) is CovalentRadiiTable)
self.assertEqual({}, rtb1.getAllCustom())
self.rtb.setCustom("Na", 1.3)
self.rtb.foobar = "foo"
rtb2 = pickle.loads(pickle.dumps(self.rtb))
self.assertEqual({"Na": 1.3}, rtb2.getAllCustom())
self.assertEqual("foo", rtb2.foobar)
return
def test__standardLookup(self):
"""Check CovalentRadiiTable._standardLookup()"""
self.assertEqual(1.22, self.rtb._standardLookup("Ga"))
return
def test_create(self):
"""Check CovalentRadiiTable.create()"""
self.rtb.setCustom("Na", 1.3)
rtb2 = self.rtb.create()
self.assertTrue(isinstance(rtb2, CovalentRadiiTable))
self.assertEqual(1, len(self.rtb.getAllCustom()))
self.assertEqual(0, len(rtb2.getAllCustom()))
return
def test_clone(self):
"""Check CovalentRadiiTable.clone()"""
self.rtb.setCustom("Na", 1.3)
rtb2 = self.rtb.clone()
self.assertTrue(isinstance(rtb2, CovalentRadiiTable))
self.assertEqual(1, len(rtb2.getAllCustom()))
self.assertEqual(1.3, rtb2.lookup("Na"))
return
def test_fromString(self):
"""Check CovalentRadiiTable.fromString()"""
self.rtb.fromString("Ga:2.22")
self.assertEqual(2.22, self.rtb.lookup("Ga"))
return
def test_getAllCustom(self):
"""Check CovalentRadiiTable.getAllCustom()"""
self.assertEqual({}, self.rtb.getAllCustom())
return
def test_lookup(self):
"""Check CovalentRadiiTable.lookup()"""
self.assertEqual(1.22, self.rtb.lookup("Ga"))
self.rtb.fromString("Ga:2.22")
self.assertEqual(2.22, self.rtb.lookup("Ga"))
return
def test_resetCustom(self):
"""Check CovalentRadiiTable.resetCustom()"""
self.rtb.fromString("B:2.33, Ga:2.22")
self.rtb.resetCustom("B")
self.rtb.resetCustom("nada")
self.assertEqual(1, len(self.rtb.getAllCustom()))
self.assertEqual(0.84, self.rtb.lookup("B"))
self.assertEqual(2.22, self.rtb.lookup("Ga"))
return
def test_setCustom(self):
"""Check CovalentRadiiTable.setCustom()"""
self.assertEqual(0.84, self.rtb.lookup("B"))
self.rtb.setCustom("B", 0.9)
self.assertEqual(0.9, self.rtb.lookup("B"))
return
def test_toString(self):
"""Check CovalentRadiiTable.toString()"""
self.assertEqual("", self.rtb.toString(";---"))
return
# End of class TestCovalentRadiiTable
# ----------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()
# End of file