def test_scalars(self): """Check that scalar types can be encoded and decoded.""" for i in range(len(self.scalars)): s = self.scalars[i] self.assertTrue(yencodable(s), 'yencodable fails item ' + str(i)) msg = Message(None, s) v = msg.decode() self.assertEqual(s, v, 'codec failed on item ' + str(i))
def test_scalars(self): """Check that scalar types can be encoded and decoded.""" for i in range(len(self.scalars)): s = self.scalars[i] self.assertTrue(yencodable(s), 'yencodable fails item '+str(i)) msg = Message(None, s) v = msg.decode() self.assertEqual(s, v, 'codec failed on item '+str(i))
def test_arrays(self): """Check that array types can be encoded and decoded.""" for i in range(len(self.arrays)): s = self.arrays[i] self.assertTrue(yencodable(s), 'yencodable fails item ' + str(i)) msg = Message(None, s) v = msg.decode() self.assertTrue(np.array_equal(np.array(s), v), 'codec failed on item ' + str(i))
def test_arrays(self): """Check that array types can be encoded and decoded.""" for i in range(len(self.arrays)): s = self.arrays[i] self.assertTrue(yencodable(s), 'yencodable fails item '+str(i)) msg = Message(None, s) v = msg.decode() self.assertTrue(np.array_equal(np.array(s), v), 'codec failed on item '+str(i))
def test_groups(self): """Check that group types can be encoded and decoded.""" for i in range(len(self.groups)): s = self.groups[i] self.assertTrue(yencodable(s), 'yencodable fails item ' + str(i)) msg = Message(None, s) v = msg.decode() if isinstance(s, tuple): s = list(s) self.assertEqual(s, v, 'codec failed on item ' + str(i))
def test_strings(self): """Check that string types can be encoded and decoded.""" for i in range(len(self.strings)): s = self.strings[i] self.assertTrue(yencodable(s), 'yencodable fails item ' + str(i)) msg = Message(None, s) v = msg.decode() if isinstance(s, np.ndarray): s = s.tolist() self.assertEqual(s, v, 'codec failed on item ' + str(i))
def test_groups(self): """Check that group types can be encoded and decoded.""" for i in range(len(self.groups)): s = self.groups[i] self.assertTrue(yencodable(s), 'yencodable fails item '+str(i)) msg = Message(None, s) v = msg.decode() if isinstance(s, tuple): s = list(s) self.assertEqual(s, v, 'codec failed on item '+str(i))
def test_strings(self): """Check that string types can be encoded and decoded.""" for i in range(len(self.strings)): s = self.strings[i] self.assertTrue(yencodable(s), 'yencodable fails item '+str(i)) msg = Message(None, s) v = msg.decode() if isinstance(s, np.ndarray): s = s.tolist() self.assertEqual(s, v, 'codec failed on item '+str(i))
def test_bad(self): """Check that unencodable types cannot be encoded.""" for i in range(len(self.bad)): s = self.bad[i] self.assertFalse(yencodable(s), 'yencodable fails item ' + str(i)) ypickling(False) with self.assertRaises(PYorickError) as cm: msg = Message(None, s) self.assertIsInstance(cm.exception, PYorickError, 'codec failed on item ' + str(i)) ypickling(encode=True, decode=True) print('doing {}'.format(i)) msg = Message(None, s) v = msg.decode() self.assertEqual(s, v, 'codec failed on item ' + str(i))
def test_bad(self): """Check that unencodable types cannot be encoded.""" for i in range(len(self.bad)): s = self.bad[i] self.assertFalse(yencodable(s), 'yencodable fails item '+str(i)) ypickling(False) with self.assertRaises(PYorickError) as cm: msg = Message(None, s) self.assertIsInstance(cm.exception, PYorickError, 'codec failed on item '+str(i)) ypickling(encode=True, decode=True) print('doing {}'.format(i)) msg = Message(None, s) v = msg.decode() self.assertEqual(s, v, 'codec failed on item '+str(i))
def test_reader(self): """Check codec readers.""" for obj, m in self.gen_messages(): mlen = len(m.packets) msg = Message() i = 0 for packet in msg.reader(): em = str(i)+': '+repr(obj) self.assertLess(i, mlen, 'reader stopped late on ' + em) self.assertEqual(packet.dtype.itemsize, m.packets[i].dtype.itemsize, 'reader wrong size on ' + em) # np.copyto(packet, m.packets[i], casting='safe') # following two lines work back to numpy 1.5: self.assertTrue(np.can_cast(m.packets[i].dtype, packet.dtype, casting='safe'), 'reader wrong type on '+ em) packet[...] = m.packets[i] i += 1 self.assertEqual(i, mlen, 'reader stopped early on ' + str(i)+': '+repr(obj))
def test_reader(self): """Check codec readers.""" for obj, m in self.gen_messages(): mlen = len(m.packets) msg = Message() i = 0 for packet in msg.reader(): em = str(i) + ': ' + repr(obj) self.assertLess(i, mlen, 'reader stopped late on ' + em) self.assertEqual(packet.dtype.itemsize, m.packets[i].dtype.itemsize, 'reader wrong size on ' + em) # np.copyto(packet, m.packets[i], casting='safe') # following two lines work back to numpy 1.5: self.assertTrue( np.can_cast(m.packets[i].dtype, packet.dtype, casting='safe'), 'reader wrong type on ' + em) packet[...] = m.packets[i] i += 1 self.assertEqual( i, mlen, 'reader stopped early on ' + str(i) + ': ' + repr(obj))
def gen_messages(self): # for test_reader for obj in self.scalars + self.arrays + self.strings + self.groups: yield obj, Message(None, obj) yield 'ID_EOL', Message(ID_EOL, 4) yield 'ID_EVAL', Message(ID_EVAL, 'hi mom') yield 'ID_EXEC', Message(ID_EXEC, 'hi mom') yield 'ID_GETVAR', Message(ID_GETVAR, 'vvv') yield 'ID_GETSHAPE', Message(ID_GETSHAPE, 'vvv') yield 'ID_SETVAR', Message(ID_SETVAR, 'vvv', 31.7) yield 'ID_FUNCALL', Message(ID_FUNCALL, 'vvv', 31.7, None, wow=-21) yield 'ID_SUBCALL', Message(ID_SUBCALL, 'vvv', 31.7, None, wow=-21) yield 'ID_GETSLICE', Message(ID_GETSLICE, 'vvv', 42, Ellipsis) yield 'ID_SETSLICE', Message(ID_SETSLICE, 'vvv', 42, Ellipsis, 'q')
def test_active(self): """Check codec for active messages.""" msg = Message(ID_EOL, 4) v = msg.decode() self.assertTrue(v[0] == ID_EOL and v[1][0] == 4, 'ID_EOL broken') msg = Message(ID_EVAL, 'hi mom') v = msg.decode() self.assertTrue(v[0] == ID_EVAL and v[1][0] == 'hi mom', 'ID_EVAL broken') msg = Message(ID_EXEC, 'hi mom') v = msg.decode() self.assertTrue(v[0] == ID_EXEC and v[1][0] == 'hi mom', 'ID_EXEC broken') msg = Message(ID_GETVAR, 'vvv') v = msg.decode() self.assertTrue(v[0] == ID_GETVAR and v[1][0] == 'vvv', 'ID_GETVAR broken') msg = Message(ID_GETSHAPE, 'vvv') v = msg.decode() self.assertTrue(v[0] == ID_GETSHAPE and v[1][0] == 'vvv', 'ID_GETSHAPE broken') msg = Message(ID_SETVAR, 'vvv', 31.7) v = msg.decode() self.assertTrue( v[0] == ID_SETVAR and v[1][0] == 'vvv' and v[1][1] == 31.7, 'ID_SETVAR broken') for ident in [ID_FUNCALL, ID_SUBCALL]: if ident == ID_FUNCALL: err = 'ID_FUNCALL broken' else: err = 'ID_SUBCALL broken' msg = Message(ident, 'vvv') v = msg.decode() self.assertTrue( v[0] == ident and v[1][0] == 'vvv' and len(v[1]) == 1, err + ' 1') msg = Message(ident, 'vvv', 31.7) v = msg.decode() self.assertTrue( v[0] == ident and v[1][0] == 'vvv' and v[1][1] == 31.7, err + ' 2') msg = Message(ident, 'vvv', wow=-21) v = msg.decode() self.assertTrue( v[0] == ident and v[1][0] == 'vvv' and len(v[1]) == 1 and v[2]['wow'] == -21, err + ' 3') msg = Message(ident, 'vvv', 31.7, wow=-21) v = msg.decode() self.assertTrue( v[0] == ident and v[1][0] == 'vvv' and v[1][1] == 31.7 and v[2]['wow'] == -21, err + ' 4') msg = Message(ident, 'vvv', 31.7, None, wow=-21, zow='abc') v = msg.decode() self.assertTrue( v[0] == ident and v[1][0] == 'vvv' and v[1][1] == 31.7 and v[1][2] == None and v[2]['wow'] == -21 and v[2]['zow'] == 'abc', err + ' 5') msg = Message(ID_GETSLICE, 'vvv') v = msg.decode() self.assertTrue( v[0] == ID_GETSLICE and v[1][0] == 'vvv' and len(v[1]) == 1, 'ID_GETSLICE broken') msg = Message(ID_GETSLICE, 'vvv', 42, Ellipsis) v = msg.decode() self.assertTrue( v[0] == ID_GETSLICE and v[1][0] == 'vvv' and v[1][1] == 42 and v[1][2] == Ellipsis, 'ID_GETSLICE broken') msg = Message(ID_SETSLICE, 'vvv', 'q') v = msg.decode() self.assertTrue( v[0] == ID_SETSLICE and v[1][0] == 'vvv' and v[1][1] == 'q', 'ID_SETSLICE broken 1') msg = Message(ID_SETSLICE, 'vvv', 42, Ellipsis, 'q') v = msg.decode() self.assertTrue( v[0] == ID_SETSLICE and v[1][0] == 'vvv' and v[1][1] == 42 and v[1][2] == Ellipsis and v[1][3] == 'q', 'ID_SETSLICE broken 2')
def test_active(self): """Check codec for active messages.""" msg = Message(ID_EOL, 4) v = msg.decode() self.assertTrue(v[0]==ID_EOL and v[1][0]==4, 'ID_EOL broken') msg = Message(ID_EVAL, 'hi mom') v = msg.decode() self.assertTrue(v[0]==ID_EVAL and v[1][0]=='hi mom', 'ID_EVAL broken') msg = Message(ID_EXEC, 'hi mom') v = msg.decode() self.assertTrue(v[0]==ID_EXEC and v[1][0]=='hi mom', 'ID_EXEC broken') msg = Message(ID_GETVAR, 'vvv') v = msg.decode() self.assertTrue(v[0]==ID_GETVAR and v[1][0]=='vvv', 'ID_GETVAR broken') msg = Message(ID_GETSHAPE, 'vvv') v = msg.decode() self.assertTrue(v[0]==ID_GETSHAPE and v[1][0]=='vvv', 'ID_GETSHAPE broken') msg = Message(ID_SETVAR, 'vvv', 31.7) v = msg.decode() self.assertTrue(v[0]==ID_SETVAR and v[1][0]=='vvv' and v[1][1]==31.7, 'ID_SETVAR broken') for ident in [ID_FUNCALL, ID_SUBCALL]: if ident == ID_FUNCALL: err = 'ID_FUNCALL broken' else: err = 'ID_SUBCALL broken' msg = Message(ident, 'vvv') v = msg.decode() self.assertTrue(v[0]==ident and v[1][0]=='vvv' and len(v[1])==1, err+' 1') msg = Message(ident, 'vvv', 31.7) v = msg.decode() self.assertTrue(v[0]==ident and v[1][0]=='vvv' and v[1][1]==31.7, err+' 2') msg = Message(ident, 'vvv', wow=-21) v = msg.decode() self.assertTrue(v[0]==ident and v[1][0]=='vvv' and len(v[1])==1 and v[2]['wow']==-21, err+' 3') msg = Message(ident, 'vvv', 31.7, wow=-21) v = msg.decode() self.assertTrue(v[0]==ident and v[1][0]=='vvv' and v[1][1]==31.7 and v[2]['wow']==-21, err+' 4') msg = Message(ident, 'vvv', 31.7, None, wow=-21, zow='abc') v = msg.decode() self.assertTrue(v[0]==ident and v[1][0]=='vvv' and v[1][1]==31.7 and v[1][2]==None and v[2]['wow']==-21 and v[2]['zow']=='abc', err+' 5') msg = Message(ID_GETSLICE, 'vvv') v = msg.decode() self.assertTrue(v[0]==ID_GETSLICE and v[1][0]=='vvv' and len(v[1])==1, 'ID_GETSLICE broken') msg = Message(ID_GETSLICE, 'vvv', 42, Ellipsis) v = msg.decode() self.assertTrue(v[0]==ID_GETSLICE and v[1][0]=='vvv' and v[1][1]==42 and v[1][2]==Ellipsis, 'ID_GETSLICE broken') msg = Message(ID_SETSLICE, 'vvv', 'q') v = msg.decode() self.assertTrue(v[0]==ID_SETSLICE and v[1][0]=='vvv' and v[1][1]=='q', 'ID_SETSLICE broken 1') msg = Message(ID_SETSLICE, 'vvv', 42, Ellipsis, 'q') v = msg.decode() self.assertTrue(v[0]==ID_SETSLICE and v[1][0]=='vvv' and v[1][1]==42 and v[1][2]==Ellipsis and v[1][3]=='q', 'ID_SETSLICE broken 2')