From 2743ee146da4c7446d67da312c1cd947b8b65144 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Tue, 16 Jul 2024 14:33:42 -0500 Subject: [PATCH] WIP: Add tests for JSON en/de-code functions --- tests/test_types.py | 108 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/test_types.py diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..ed2999c --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,108 @@ +import json +import unittest + +import appose + +class TypesTest(unittest.TestCase): + JSON = ( + '{"posByte":123,"negByte":-98,' + '"posDouble":9.876543210123456,"negDouble":-1.234567890987654e+302,' + '"posFloat":9.876543,"negFloat":-1.2345678,' + '"posInt":1234567890,"negInt":-987654321,' + '"posLong":12345678987654321,"negLong":-98765432123456789,' + '"posShort":32109,"negShort":-23456,' + '"trueBoolean":true,"falseBoolean":false,' + '"aChar":"\\u0000",' + '"aString":"-=[]\\\\;\',./_+{}|:\\"<>?' + 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz' + '~!@#$%^&*()",' + '"numbers":[1,1,2,3,5,8],' + '"words":["quick","brown","fox"],' + '"ndArray":{' + '"appose_type":"ndarray",' + '"shm":{' + '"appose_type":"shm",' + '"name":"SHM_NAME",' + '"size":4000' + '},' + '"dtype":"float32",' + '"shape":[2,20,25]' + '}}' + ) + + STRING = ( + '-=[]\\;\',./_+{}|:"<>?' + 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz' + '~!@#$%^&*()' + ) + + NUMBERS = [1, 1, 2, 3, 5, 8] + + WORDS = ["quick", "brown", "fox"] + + def test_encode(self): + data = { + 'posByte': 123, + 'negByte': -98, + 'posDouble': 9.876543210123456, + 'negDouble': -1.234567890987654e+302, + 'posFloat': 9.876543, + 'negFloat': -1.2345678, + 'posInt': 1234567890, + 'negInt': -987654321, + 'posLong': 12345678987654321, + 'negLong': -98765432123456789, + 'posShort': 32109, + 'negShort': -23456, + 'trueBoolean': True, + 'falseBoolean': False, + 'nullChar': '\0', + 'aString': self.STRING, + 'numbers': self.NUMBERS, + 'words': self.WORDS, + } + + dtype = "float" + shape = [2, 20, 25] + with appose.types.NDArray(dtype, shape) as ndArray: + data['ndArray'] = ndArray + json_str = appose.types.encode(data) + self.assertIsNotNone(json_str) + self.assertEqual(self.JSON, self.generalize_shm_name(json_str)) + + def test_decode(self): + data = appose.types.decode(self.JSON) + self.assertIsNotNone(data) + self.assertEqual(19, len(data)) + self.assertEqual(123, data['posByte']) # NB: decodes back to int + self.assertEqual(-98, data['negByte']) # NB: decodes back to int + self.assertEqual(9.876543210123456, data['posDouble']) + self.assertEqual(-1.234567890987654e+302, data['negDouble']) + self.assertEqual(9.876543, self.bd(data['posFloat'])) + self.assertEqual(-1.2345678, self.bd(data['negFloat'])) + self.assertEqual(1234567890, data['posInt']) + self.assertEqual(-987654321, data['negInt']) + self.assertEqual(12345678987654321, data['posLong']) + self.assertEqual(-98765432123456789, data['negLong']) + self.assertEqual(32109, data['posShort']) # NB: decodes back to int + self.assertEqual(-23456, data['negShort']) # NB: decodes back to int + self.assertTrue(data['trueBoolean']) + self.assertFalse(data['falseBoolean']) + self.assertEqual('\0', data['nullChar']) + self.assertEqual(self.STRING, data['aString']) + self.assertEqual(self.NUMBERS, data['numbers']) + self.assertEqual(self.WORDS, data['words']) + + ndArray = data['ndArray'] + with ndArray: + self.assertEqual("float32", ndArray.dtype) + self.assertEqual([2, 20, 25], ndArray.shape) + + def generalize_shm_name(self, json_str): + if json_str is None: + return None + return re.sub( + r'("shm":\{"appose_type":"shm","name":").*?"', + r'\1SHM_NAME"', + json_str + )