def test_fromstring_types(self): from _numpypy import (fromstring, int8, int16, int32, int64, uint8, uint16, uint32, float32, float64) a = fromstring('\xFF', dtype=int8) assert a[0] == -1 b = fromstring('\xFF', dtype=uint8) assert b[0] == 255 c = fromstring('\xFF\xFF', dtype=int16) assert c[0] == -1 d = fromstring('\xFF\xFF', dtype=uint16) assert d[0] == 65535 e = fromstring('\xFF\xFF\xFF\xFF', dtype=int32) assert e[0] == -1 f = fromstring('\xFF\xFF\xFF\xFF', dtype=uint32) assert repr(f[0]) == '4294967295' g = fromstring('\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF', dtype=int64) assert g[0] == -1 h = fromstring(self.float32val, dtype=float32) assert h[0] == float32(5.2) i = fromstring(self.float64val, dtype=float64) assert i[0] == float64(300.4) j = fromstring(self.ulongval, dtype='L') assert j[0] == 12
def test_fromstring(self): import sys from _numpypy import fromstring, array, uint8, float32, int32 a = fromstring(self.data) for i in range(4): assert a[i] == i + 1 b = fromstring('\x01\x02', dtype=uint8) assert a[0] == 1 assert a[1] == 2 c = fromstring(self.fdata, dtype=float32) assert c[0] == float32(2.3) d = fromstring("1 2", sep=' ', count=2, dtype=uint8) assert len(d) == 2 assert d[0] == 1 assert d[1] == 2 e = fromstring('3, 4,5', dtype=uint8, sep=',') assert len(e) == 3 assert e[0] == 3 assert e[1] == 4 assert e[2] == 5 f = fromstring('\x01\x02\x03\x04\x05', dtype=uint8, count=3) assert len(f) == 3 assert f[0] == 1 assert f[1] == 2 assert f[2] == 3 g = fromstring("1 2 3 ", dtype=uint8, sep=" ") assert len(g) == 3 assert g[0] == 1 assert g[1] == 2 assert g[2] == 3 h = fromstring("1, , 2, 3", dtype=uint8, sep=",") assert (h == [1, 0, 2, 3]).all() i = fromstring("1 2 3", dtype=uint8, sep=" ") assert (i == [1, 2, 3]).all() j = fromstring("1\t\t\t\t2\t3", dtype=uint8, sep="\t") assert (j == [1, 2, 3]).all() k = fromstring("1,x,2,3", dtype=uint8, sep=",") assert (k == [1, 0]).all() l = fromstring("1,x,2,3", dtype='float32', sep=",") assert (l == [1.0, -1.0]).all() m = fromstring("1,,2,3", sep=",") assert (m == [1.0, -1.0, 2.0, 3.0]).all() n = fromstring("3.4 2.0 3.8 2.2", dtype=int32, sep=" ") assert (n == [3]).all() o = fromstring("1.0 2f.0f 3.8 2.2", dtype=float32, sep=" ") assert len(o) == 2 assert o[0] == 1.0 assert o[1] == 2.0 p = fromstring("1.0,,2.0,3.0", sep=",") assert (p == [1.0, -1.0, 2.0, 3.0]).all() q = fromstring("1.0,,2.0,3.0", sep=" ") assert (q == [1.0]).all() r = fromstring("\x01\x00\x02", dtype='bool') assert (r == [True, False, True]).all() s = fromstring("1,2,3,,5", dtype=bool, sep=",") assert (s == [True, True, True, False, True]).all() t = fromstring("", bool) assert (t == []).all() u = fromstring("\x01\x00\x00\x00\x00\x00\x00\x00", dtype=int) if sys.maxint > 2 ** 31 - 1: assert (u == [1]).all() else: assert (u == [1, 0]).all()
def test_fromstring(self): import sys from _numpypy import fromstring, array, uint8, float32, int32 a = fromstring(self.data) for i in range(4): assert a[i] == i + 1 b = fromstring('\x01\x02', dtype=uint8) assert a[0] == 1 assert a[1] == 2 c = fromstring(self.fdata, dtype=float32) assert c[0] == float32(2.3) d = fromstring("1 2", sep=' ', count=2, dtype=uint8) assert len(d) == 2 assert d[0] == 1 assert d[1] == 2 e = fromstring('3, 4,5', dtype=uint8, sep=',') assert len(e) == 3 assert e[0] == 3 assert e[1] == 4 assert e[2] == 5 f = fromstring('\x01\x02\x03\x04\x05', dtype=uint8, count=3) assert len(f) == 3 assert f[0] == 1 assert f[1] == 2 assert f[2] == 3 g = fromstring("1 2 3 ", dtype=uint8, sep=" ") assert len(g) == 3 assert g[0] == 1 assert g[1] == 2 assert g[2] == 3 h = fromstring("1, , 2, 3", dtype=uint8, sep=",") assert (h == [1, 0, 2, 3]).all() i = fromstring("1 2 3", dtype=uint8, sep=" ") assert (i == [1, 2, 3]).all() j = fromstring("1\t\t\t\t2\t3", dtype=uint8, sep="\t") assert (j == [1, 2, 3]).all() k = fromstring("1,x,2,3", dtype=uint8, sep=",") assert (k == [1, 0]).all() l = fromstring("1,x,2,3", dtype='float32', sep=",") assert (l == [1.0, -1.0]).all() m = fromstring("1,,2,3", sep=",") assert (m == [1.0, -1.0, 2.0, 3.0]).all() n = fromstring("3.4 2.0 3.8 2.2", dtype=int32, sep=" ") assert (n == [3]).all() o = fromstring("1.0 2f.0f 3.8 2.2", dtype=float32, sep=" ") assert len(o) == 2 assert o[0] == 1.0 assert o[1] == 2.0 p = fromstring("1.0,,2.0,3.0", sep=",") assert (p == [1.0, -1.0, 2.0, 3.0]).all() q = fromstring("1.0,,2.0,3.0", sep=" ") assert (q == [1.0]).all() r = fromstring("\x01\x00\x02", dtype='bool') assert (r == [True, False, True]).all() s = fromstring("1,2,3,,5", dtype=bool, sep=",") assert (s == [True, True, True, False, True]).all() t = fromstring("", bool) assert (t == []).all() u = fromstring("\x01\x00\x00\x00\x00\x00\x00\x00", dtype=int) if sys.maxint > 2**31 - 1: assert (u == [1]).all() else: assert (u == [1, 0]).all()