2014-12-04 17:52:22 +08:00
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
from fractions import Fraction
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2015-01-17 19:38:20 +08:00
|
|
|
from artiq.protocols import pyon
|
2014-12-04 17:52:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
_pyon_test_object = {
|
|
|
|
(1, 2): [(3, 4.2), (2, )],
|
2014-12-09 10:48:47 +08:00
|
|
|
Fraction(3, 4): np.linspace(5, 10, 1),
|
2016-02-11 16:37:27 +08:00
|
|
|
"set": {"testing", "sets"},
|
2015-07-25 12:28:41 +08:00
|
|
|
"a": np.int8(9), "b": np.int16(-98), "c": np.int32(42), "d": np.int64(-5),
|
|
|
|
"e": np.uint8(8), "f": np.uint16(5), "g": np.uint32(4), "h": np.uint64(9),
|
|
|
|
"x": np.float16(9.0), "y": np.float32(9.0), "z": np.float64(9.0),
|
2014-12-04 17:52:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PYON(unittest.TestCase):
|
|
|
|
def test_encdec(self):
|
|
|
|
for enc in pyon.encode, lambda x: pyon.encode(x, True):
|
|
|
|
self.assertEqual(pyon.decode(enc(_pyon_test_object)),
|
|
|
|
_pyon_test_object)
|
|
|
|
|
|
|
|
|
|
|
|
_json_test_object = {
|
|
|
|
"a": "b",
|
|
|
|
"x": [1, 2, {}],
|
2016-01-28 03:43:42 +08:00
|
|
|
"foo\nbaz\\qux\"\r2": ["bar", 1.2, {"x": "y"}],
|
2014-12-04 17:52:22 +08:00
|
|
|
"bar": [True, False, None]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class JSONPYON(unittest.TestCase):
|
|
|
|
def test_encdec(self):
|
|
|
|
for enc in pyon.encode, lambda x: pyon.encode(x, True), json.dumps:
|
|
|
|
for dec in pyon.decode, json.loads:
|
|
|
|
self.assertEqual(dec(enc(_json_test_object)),
|
|
|
|
_json_test_object)
|