-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add tests for JSON en/de-code functions
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |