• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WassimTenachi / PhySO / #14

10 Jun 2024 12:28AM UTC coverage: 77.799% (+25.7%) from 52.052%
#14

push

coveralls-python

WassimTenachi
Update requirements.txt

6525 of 8387 relevant lines covered (77.8%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.14
/physo/physym/tests/token_UnitTest.py
1
import unittest
1✔
2
import numpy as np
1✔
3

4
# Internal imports
5
from physo.physym import token as Tok
1✔
6

7
class TokenTest(unittest.TestCase):
1✔
8

9
    # ------------------------------------------------------------------------
10
    # ------------------------------ TEST TOKEN ------------------------------
11
    # ------------------------------------------------------------------------
12

13
    # --------------------- Token representing operation --------------------
14

15
    # Test token creation
16
    def test_token_operation_creation(self):
1✔
17
        # Test token creation
18
        try:
1✔
19
            add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
20
                            function=np.add,
21
                            var_id=None)
22
        except:
×
23
            self.fail("Token creation failed")
×
24
        # Same test with type specific class
25
        try:
1✔
26
            add = Tok.TokenOp(name='add', sympy_repr='add', arity=2, complexity=0, function=np.add,)
1✔
27
        except:
×
28
            self.fail("Token creation failed")
×
29

30
    # Test token creation exceptions
31
    def test_token_operation_creation_exceptions(self):
1✔
32

33
        # Test exception: function is supposed to be callable
34
        with self.assertRaises(AssertionError):
1✔
35
            add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
36
                            function=None,
37
                            var_id=None)
38
        # Same test with type specific class
39
        with self.assertRaises(AssertionError):
1✔
40
            add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0,
1✔
41
                            function=None,)
42

43
        # Test exception: var_id is supposed to be Nan
44
        with self.assertRaises(AssertionError):
1✔
45
            add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
46
                            function=np.add,
47
                            var_id=0)
48

49
        # Test exception: arity is supposed to be >= 0
50
        with self.assertRaises(AssertionError):
1✔
51
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=-1, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
52
                           function=None,
53
                           var_id=0)
54
        # Same test with type specific class
55
        with self.assertRaises(AssertionError):
1✔
56
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=-1, complexity=0,
1✔
57
                           function=None,)
58

59
    # ------------------ Token representing input variable ------------------
60

61
    # Test token creation
62
    def test_token_input_var_creation(self):
1✔
63
        # Test token creation
64
        try:
1✔
65
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
66
                           function=None,
67
                           var_id=0)
68
        except:
×
69
            self.fail("Token creation failed")
×
70

71
        # Same test with type specific class
72
        try:
1✔
73
            x0 = Tok.TokenInputVar(name='x0', sympy_repr='x0', complexity=0,var_id=0)
1✔
74
        except:
×
75
            self.fail("Token creation failed")
×
76

77
    # Test token creation exceptions
78
    def test_token_input_var_creation_exceptions(self):
1✔
79

80
        # Test exception: function is supposed to be None
81
        with self.assertRaises(AssertionError):
1✔
82
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
83
                           function=np.multiply,
84
                           var_id=0)
85

86
        # Test exception: var_id is supposed to be int
87
        with self.assertRaises(AssertionError):
1✔
88
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
89
                           function=None,
90
                           var_id='0')
91
        # Same test with type specific class
92
        with self.assertRaises(AssertionError):
1✔
93
            x0 = Tok.TokenInputVar(name='x0', sympy_repr='x0', complexity=0,
1✔
94
                           var_id='0')
95

96
        # Test exception: arity is supposed to be = 0
97
        with self.assertRaises(AssertionError):
1✔
98
            x0 = Tok.Token(name='x0', sympy_repr='x0', arity=1, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
99
                           function=None,
100
                           var_id=0)
101

102
    # Test token usage
103
    def test_token_input_var_usage(self):
1✔
104
        data_x0 = np.linspace(start=-10, stop=10, num=1000)
1✔
105
        data_x1 = np.linspace(start=-5, stop=15, num=1000)
1✔
106
        dataset = np.array([data_x0, data_x1])
1✔
107
        x0 = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
108
                       function=None,
109
                       var_id=0)
110
        works_bool = np.array_equal(dataset[x0.var_id], data_x0)
1✔
111
        self.assertEqual(works_bool, True)
1✔
112

113
        # Same test with type specific class
114
        data_x0 = np.linspace(start=-10, stop=10, num=1000)
1✔
115
        data_x1 = np.linspace(start=-5, stop=15, num=1000)
1✔
116
        dataset = np.array([data_x0, data_x1])
1✔
117
        x0 = Tok.TokenInputVar(name='x0', sympy_repr='x0', complexity=0, var_id=0)
1✔
118
        works_bool = np.array_equal(dataset[x0.var_id], data_x0)
1✔
119
        self.assertEqual(works_bool, True)
1✔
120

121

122
    # --------------------- Token representing fixed constant ---------------------
123

124
    # Test token creation
125
    def test_token_fixed_constant_creation(self):
1✔
126
        # Test token creation
127
        try:
1✔
128
            pi = Tok.Token(name='pi', sympy_repr='pi', arity=0, complexity=0, var_type=Tok.VAR_TYPE_FIXED_CONST,
1✔
129
                           fixed_const=np.pi,
130
                           function=None,
131
                           var_id=None)
132
        except:
×
133
            self.fail("Token creation failed")
×
134
        # Same test with type specific class
135
        try:
1✔
136
            pi = Tok.TokenFixedConst(name='pi', sympy_repr='pi', complexity=0, fixed_const=np.pi,)
1✔
137
        except:
×
138
            self.fail("Token creation failed")
×
139

140
    # Test token creation exceptions
141
    def test_token_fixed_constant_creation_exceptions(self):
1✔
142
        # Test exception: fixed_const is supposed to be non nan
143
        with self.assertRaises(AssertionError):
1✔
144
            pi = Tok.Token(name='pi', sympy_repr='pi', arity=0, complexity=0, var_type=Tok.VAR_TYPE_FIXED_CONST,
1✔
145
                           fixed_const=np.NaN,
146
                           function=None,
147
                           var_id=None)
148
        # Same test with type specific class
149
        with self.assertRaises(AssertionError):
1✔
150
            pi = Tok.TokenFixedConst(name='pi', sympy_repr='pi', complexity=0,
1✔
151
                           fixed_const=np.NaN,)
152

153
        # Test exception: function is supposed to be None
154
        with self.assertRaises(AssertionError):
1✔
155
            pi = Tok.Token(name='pi', sympy_repr='pi', arity=0, complexity=0, var_type=Tok.VAR_TYPE_FIXED_CONST,
1✔
156
                           fixed_const=np.pi,
157
                           function=lambda const = np.pi: const,
158
                           var_id=None)
159
        # Test exception: var_id is supposed to be Nan
160
        with self.assertRaises(AssertionError):
1✔
161
            pi = Tok.Token(name='pi', sympy_repr='pi', arity=0, complexity=0, var_type=Tok.VAR_TYPE_FIXED_CONST,
1✔
162
                           fixed_const=np.pi,
163
                           function=None,
164
                           var_id=0)
165
        # Test exception: arity is supposed to be = 0
166
        with self.assertRaises(AssertionError):
1✔
167
            pi = Tok.Token(name='pi', sympy_repr='pi', arity=1, complexity=0, var_type=Tok.VAR_TYPE_FIXED_CONST,
1✔
168
                           fixed_const=np.pi,
169
                           function=None,
170
                           var_id=None)
171

172
    # --------------------- Token representing class free constant ---------------------
173

174
    # Test token creation
175
    def test_token_class_free_constant_creation(self):
1✔
176
        # Test token creation
177
        try:
1✔
178
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_CLASS_FREE_CONST,
1✔
179
                           function=None,
180
                           var_id=0,
181
                           init_val=1.)
182
        except:
×
183
            self.fail("Token creation failed")
×
184

185
        # Same test with type specific class
186
        try:
1✔
187
            c0 = Tok.TokenClassFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
188
                           var_id=0,
189
                           init_val=1.)
190
        except:
×
191
            self.fail("Token creation failed")
×
192

193
    # Test token creation exceptions
194
    def test_token_class_free_constant_creation_exceptions(self):
1✔
195

196
        # Test exception: function is supposed to be None
197
        with self.assertRaises(AssertionError):
1✔
198
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_CLASS_FREE_CONST,
1✔
199
                           function=np.multiply,
200
                           var_id=0,
201
                           init_val=1.)
202

203
        # Test exception: var_id is supposed to be int
204
        with self.assertRaises(AssertionError):
1✔
205
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_CLASS_FREE_CONST,
1✔
206
                           function=None,
207
                           var_id='0',
208
                           init_val=1.)
209
        # Same test with type specific class
210
        with self.assertRaises(AssertionError):
1✔
211
            c0 = Tok.TokenClassFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
212
                           var_id='0',
213
                           init_val=1.)
214

215
        # Test exception: arity is supposed to be = 0
216
        with self.assertRaises(AssertionError):
1✔
217
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=1, complexity=0, var_type=Tok.VAR_TYPE_CLASS_FREE_CONST,
1✔
218
                           function=None,
219
                           var_id=0,
220
                           init_val=1.)
221

222
    # --------------------- Token representing spe free constant ---------------------
223

224
    # Test token creation
225
    def test_token_spe_free_constant_creation(self):
1✔
226
        # Test token creation
227
        try:
1✔
228
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
229
                           function=None,
230
                           var_id=0,
231
                           init_val=1.)
232
        except:
×
233
            self.fail("Token creation failed")
×
234

235
        # Same test with type specific class
236
        try:
1✔
237
            c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
238
                           var_id=0,
239
                           init_val=1.)
240
        except:
×
241
            self.fail("Token creation failed")
×
242

243
    # Test token creation exceptions
244
    def test_token_spe_free_constant_creation_exceptions(self):
1✔
245
        # Test exception: function is supposed to be None
246
        with self.assertRaises(AssertionError):
1✔
247
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
248
                           function=np.multiply,
249
                           var_id=0,
250
                           init_val=1.)
251

252
        # Test exception: var_id is supposed to be int
253
        with self.assertRaises(AssertionError):
1✔
254
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
255
                           function=None,
256
                           var_id='0',
257
                           init_val=1.)
258
        # Same test with type specific class
259
        with self.assertRaises(AssertionError):
1✔
260
            c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
261
                           var_id='0',
262
                           init_val=1.)
263

264
        # Test exception: arity is supposed to be = 0
265
        with self.assertRaises(AssertionError):
1✔
266
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=1, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
267
                           function=None,
268
                           var_id=0,
269
                           init_val=1.)
270

271
    def test_token_spe_free_constant_creation_exceptions_init_val_related(self):
1✔
272
        # Test exception: init_val is supposed to be a non-nan
273
        with self.assertRaises(AssertionError):
1✔
274
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
275
                           function=None,
276
                           var_id=0,
277
                           init_val=np.nan)
278
        # Same test with type specific class
279
        with self.assertRaises(AssertionError):
1✔
280
            c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
281
                           var_id=0,
282
                           init_val=np.nan)
283

284
        # Test exception: init_val is supposed to be a non-nan (multi realization)
285
        with self.assertRaises(AssertionError):
1✔
286
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
287
                           function=None,
288
                           var_id=0,
289
                           init_val=[np.nan,2.,3.])
290
        # Same test with type specific class
291
        with self.assertRaises(AssertionError):
1✔
292
            c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
293
                           var_id=0,
294
                           init_val=[np.nan,2.,3.])
295

296
        # Test exception: init_val is supposed to be a float or a 1D array of floats
297
        with self.assertRaises(AssertionError):
1✔
298
            c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
299
                           function=None,
300
                           var_id=0,
301
                           init_val=np.ones((2,2)))
302
        # Same test with type specific class
303
        with self.assertRaises(AssertionError):
1✔
304
            c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
305
                           var_id=0,
306
                           init_val=np.ones((2,2)))
307

308
    def test_token_spe_free_constant_init_val_related (self):
1✔
309
            n_realizations = 10
1✔
310
            aa = 2.1
1✔
311
            aa_expected = np.array([aa])
1✔
312
            bb = np.random.rand(n_realizations,)
1✔
313
            bb_expected = bb
1✔
314

315

316
            # Test creation (single float init_val)
317
            try:
1✔
318
                c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
319
                               function = None,
320
                               var_id   = 0,
321
                               init_val = aa)
322
            except:
×
323
                self.fail("Token creation failed")
×
324
            bool_works = np.array_equal(c0.init_val, aa_expected)
1✔
325
            self.assertEqual(bool_works, True)
1✔
326
            # Same test with type specific class
327
            try:
1✔
328
                c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
329
                                           var_id=0,
330
                                           init_val=aa,)
331
            except:
×
332
                self.fail("Token creation failed")
×
333
            bool_works = np.array_equal(c0.init_val, aa_expected)
1✔
334
            self.assertEqual(bool_works, True)
1✔
335

336

337
            # Test creation (multi realization init_val)
338
            try:
1✔
339
                c0 = Tok.Token(name='c0', sympy_repr='c0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_SPE_FREE_CONST,
1✔
340
                               function = None,
341
                               var_id   = 0,
342
                               init_val = bb,
343
                             )
344
            except:
×
345
                self.fail("Token creation failed")
×
346
            bool_works = np.array_equal(c0.init_val, bb_expected)
1✔
347
            self.assertEqual(bool_works, True)
1✔
348
            # Same test with type specific class
349
            try:
1✔
350
                c0 = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,
1✔
351
                                           var_id=0,
352
                                           init_val=bb,)
353
            except:
×
354
                self.fail("Token creation failed")
×
355
            bool_works = np.array_equal(c0.init_val, bb_expected)
1✔
356
            self.assertEqual(bool_works, True)
1✔
357

358

359
            return None
1✔
360

361

362
    # ----------------------------- Token call ------------------------------
363

364
    # Test token call method (all types of tokens)
365
    def test_token_call(self):
1✔
366
        # ----- Data for tests -----
367
        data_x0 = np.linspace(start=-10, stop=10, num=1000)
1✔
368
        data_x1 = np.linspace(start=-5, stop=15, num=1000)
1✔
369
        dataset = np.array([data_x0, data_x1])
1✔
370
        add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
371
                        function=np.add,
372
                        var_id=None)
373
        cos = Tok.Token(name='cos', sympy_repr='cos', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
374
                        function=np.cos,
375
                        var_id=None)
376
        x0 = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0, var_type=Tok.VAR_TYPE_INPUT_VAR,
1✔
377
                       function=None,
378
                       var_id=0)
379
        pi = Tok.Token(name='pi', sympy_repr='pi', arity=0, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
380
                       function=lambda const=np.pi: const,
381
                       var_id=None)
382
        # ----- Test legal calls -----
383
        works_bool = np.array_equal(add(data_x0, data_x1), np.add(data_x0, data_x1))
1✔
384
        self.assertEqual(works_bool, True)
1✔
385
        works_bool = np.array_equal(cos(data_x0), np.cos(data_x0))
1✔
386
        self.assertEqual(works_bool, True)
1✔
387
        works_bool = np.array_equal(pi(), np.pi)
1✔
388
        self.assertEqual(works_bool, True)
1✔
389
        works_bool = np.array_equal(add(cos(pi()), dataset[x0.var_id]), np.add(np.cos(np.pi), data_x0))
1✔
390
        self.assertEqual(works_bool, True)
1✔
391
        # ----- Test exception: n args is supposed to be = arity -----
392
        with self.assertRaises(AssertionError): res = add(data_x0)
1✔
393
        with self.assertRaises(AssertionError): res = cos(data_x0, data_x1)
1✔
394
        with self.assertRaises(AssertionError): res = cos()
1✔
395
        # ----- Test exception: token representing data is not supposed to be called -----
396
        with self.assertRaises(AssertionError): res = x0(data_x0, data_x1)
1✔
397
        with self.assertRaises(AssertionError): res = x0()
1✔
398

399
        # Same test with type specific class
400
        # ----- Data for tests -----
401
        data_x0 = np.linspace(start=-10, stop=10, num=1000)
1✔
402
        data_x1 = np.linspace(start=-5, stop=15, num=1000)
1✔
403
        dataset = np.array([data_x0, data_x1])
1✔
404
        add = Tok.TokenOp(name='add', sympy_repr='add', arity=2, complexity=0,
1✔
405
                        function=np.add,)
406
        cos = Tok.TokenOp(name='cos', sympy_repr='cos', arity=1, complexity=0,
1✔
407
                        function=np.cos,)
408
        x0 = Tok.TokenInputVar(name='x0', sympy_repr='x0', complexity=0,
1✔
409
                       var_id=0)
410
        pi = Tok.TokenOp(name='pi', sympy_repr='pi', arity=0, complexity=0,
1✔
411
                       function=lambda const=np.pi: const,)
412
        # ----- Test legal calls -----
413
        works_bool = np.array_equal(add(data_x0, data_x1), np.add(data_x0, data_x1))
1✔
414
        self.assertEqual(works_bool, True)
1✔
415
        works_bool = np.array_equal(cos(data_x0), np.cos(data_x0))
1✔
416
        self.assertEqual(works_bool, True)
1✔
417
        works_bool = np.array_equal(pi(), np.pi)
1✔
418
        self.assertEqual(works_bool, True)
1✔
419
        works_bool = np.array_equal(add(cos(pi()), dataset[x0.var_id]), np.add(np.cos(np.pi), data_x0))
1✔
420
        self.assertEqual(works_bool, True)
1✔
421
        # ----- Test exception: n args is supposed to be = arity -----
422
        with self.assertRaises(AssertionError): res = add(data_x0)
1✔
423
        with self.assertRaises(AssertionError): res = cos(data_x0, data_x1)
1✔
424
        with self.assertRaises(AssertionError): res = cos()
1✔
425
        # ----- Test exception: token representing data is not supposed to be called -----
426
        with self.assertRaises(AssertionError): res = x0(data_x0, data_x1)
1✔
427
        with self.assertRaises(AssertionError): res = x0()
1✔
428

429
    # Test token with too long name
430
    def test_token_name_too_long(self):
1✔
431
        too_large_name = "blabla" * Tok.MAX_NAME_SIZE
1✔
432
        with self.assertRaises(AssertionError):
1✔
433
            add = Tok.Token(name=too_large_name, sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
434
                            function=np.add,
435
                            var_id=None)
436
        with self.assertRaises(AssertionError):
1✔
437
            add = Tok.Token(name='add', sympy_repr=too_large_name, arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
438
                            function=np.add,
439
                            var_id=None)
440

441
    # ----------------------------- Token units ------------------------------
442
    # Test token creation
443
    def test_token_creation_units(self):
1✔
444
        try:
1✔
445
            physical_quantity = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0,
1✔
446
                                          var_type=Tok.VAR_TYPE_INPUT_VAR,
447
                                          function=None,
448
                                          var_id=0,
449
                                          is_constraining_phy_units=True,
450
                                          phy_units=np.ones(Tok.UNITS_VECTOR_SIZE))
451

452
            cos = Tok.Token(name='cos', sympy_repr='cos', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
453
                            function=np.cos,
454
                            var_id=None,
455
                            is_constraining_phy_units = True,
456
                            phy_units = np.zeros(Tok.UNITS_VECTOR_SIZE), )
457
            sqrt = Tok.Token(name='sqrt', sympy_repr='sqrt', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
458
                             function=np.sqrt,
459
                             var_id=None,
460
                             is_power=True,
461
                             power=0.5)
462
        except:
×
463
            self.fail("Token creation failed")
×
464

465
    # Test token creation exceptions : phy_units
466
    def test_token_creation_units_exceptions_phy_units(self):
1✔
467
        # Test exception: token with is_constraining_phy_units = True must have units
468
        with self.assertRaises(AssertionError):
1✔
469
            physical_quantity = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0,
1✔
470
                                          var_type=Tok.VAR_TYPE_INPUT_VAR,
471
                                          function=None,
472
                                          var_id=0,
473
                                          is_constraining_phy_units=True,
474
                                          phy_units=None)
475
        # Test exception: token with is_constraining_phy_units = True must have units vector of size Lib.UNITS_VECTOR_SIZE
476
        with self.assertRaises(AssertionError):
1✔
477
            physical_quantity = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0,
1✔
478
                                          var_type=Tok.VAR_TYPE_INPUT_VAR,
479
                                          function=None,
480
                                          var_id=0,
481
                                          is_constraining_phy_units=True,
482
                                          phy_units=np.ones(Tok.UNITS_VECTOR_SIZE + 1))
483
        # Test exception: token with is_constraining_phy_units = True must not contain any NAN
484
        wrong_units = np.ones(Tok.UNITS_VECTOR_SIZE)
1✔
485
        wrong_units[0] = np.NAN
1✔
486
        with self.assertRaises(AssertionError):
1✔
487
            physical_quantity = Tok.Token(name='x0', sympy_repr='x0', arity=0, complexity=0,
1✔
488
                                          var_type=Tok.VAR_TYPE_INPUT_VAR,
489
                                          function=None,
490
                                          var_id=0,
491
                                          is_constraining_phy_units=True,
492
                                          phy_units=wrong_units)
493
        # Test exception: token with is_constraining_phy_units = False must not have units
494
        with self.assertRaises(AssertionError):
1✔
495
            add = Tok.Token(name='add', sympy_repr='add', arity=2, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
496
                            function=np.add,
497
                            var_id=None,
498
                            is_constraining_phy_units=False,
499
                            phy_units=np.ones(Tok.UNITS_VECTOR_SIZE))
500

501
    # Test token creation exceptions : behavior_id
502
    def test_token_creation_units_exceptions_behavior_id(self):
1✔
503
        # Test exception: behavior_id must be an int
504
        with self.assertRaises(AssertionError):
1✔
505
            cos = Tok.Token(name='cos', sympy_repr='cos', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
506
                            function=np.cos,
507
                            var_id=None,
508
                            is_constraining_phy_units=True,
509
                            phy_units=np.zeros(Tok.UNITS_VECTOR_SIZE),
510
                            behavior_id="not an int")
511

512
    # Test token creation exceptions : power
513
    def test_token_creation_units_exceptions_power(self):
1✔
514
        # Test exception: power must be a float
515
        with self.assertRaises(AssertionError):
1✔
516
            sqrt = Tok.Token(name='sqrt', sympy_repr='sqrt', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
517
                             function=np.sqrt,
518
                             var_id=None,
519
                             is_power=True,
520
                             power="not a float")
521

522
        with self.assertRaises(AssertionError):
1✔
523
            sqrt = Tok.Token(name='sqrt', sympy_repr='sqrt', arity=1, complexity=0, var_type=Tok.VAR_TYPE_OP,
1✔
524
                             function=np.sqrt,
525
                             var_id=None,
526
                             is_power=False,
527
                             power=0.5)
528

529
    # Test that type specific token classes are using correct type under the hood
530
    def test_token_types_of_subclasses(self):
1✔
531
        # Creating a token of each type and testing its type
532

533
        my_tok = Tok.TokenInputVar(name='x0', sympy_repr='x0', complexity=0,var_id=0)
1✔
534
        self.assertEqual(my_tok.var_type, Tok.VAR_TYPE_INPUT_VAR)
1✔
535

536
        my_tok = Tok.TokenOp(name='add', sympy_repr='add', arity=2, complexity=0, function=np.add,)
1✔
537
        self.assertEqual(my_tok.var_type, Tok.VAR_TYPE_OP)
1✔
538

539
        my_tok = Tok.TokenFixedConst(name='pi', sympy_repr='pi', complexity=0, fixed_const=np.pi,)
1✔
540
        self.assertEqual(my_tok.var_type, Tok.VAR_TYPE_FIXED_CONST)
1✔
541

542
        my_tok = Tok.TokenClassFreeConst(name='c0', sympy_repr='c0', complexity=0,var_id=0,init_val=1.)
1✔
543
        self.assertEqual(my_tok.var_type, Tok.VAR_TYPE_CLASS_FREE_CONST)
1✔
544

545
        my_tok = Tok.TokenSpeFreeConst(name='c0', sympy_repr='c0', complexity=0,var_id=0,init_val=1.)
1✔
546
        self.assertEqual(my_tok.var_type, Tok.VAR_TYPE_SPE_FREE_CONST)
1✔
547

548

549

550
if __name__ == '__main__':
1✔
551
    unittest.main(verbosity = 2)
×
552

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc