def test_rw_seek_tell(self): data = byteify("A Teststring of length 25", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) pos = rwops.rw_seek(rw, 0, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == len(data)) pos = rwops.rw_seek(rw, 0, rwops.RW_SEEK_SET) self.assertTrue(pos == buf.tell() == 0) pos = rwops.rw_seek(rw, 15, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 15) pos = rwops.rw_seek(rw, -3, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 12) pos = rwops.rw_seek(rw, 7, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 19) pos = rwops.rw_seek(rw, -11, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == 14) pos = rwops.rw_seek(rw, 8, rwops.RW_SEEK_SET) self.assertTrue(pos == buf.tell() == 8) pos = rwops.rw_seek(rw, -2, rwops.RW_SEEK_SET) self.assertEqual(pos, -1) self.assertTrue(buf.tell() == 8) pos = rwops.rw_seek(rw, 12, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == len(data) + 12)
def test_write_be_16(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) value = ((ord("q") << 8) | (ord("%"))) rwops.write_be_16(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "q%Teststring") rwops.rw_seek(rw, 6, rwops.RW_SEEK_SET) rwops.write_be_16(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "q%Testq%ring")
def test_rw_from_object(self): buf = BytesIO() rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) for s in("Test", "Test", "Test", "Banana"): buf.write(byteify(s, "utf-8")) length = rwops.rw_seek(rw, 0, rwops.RW_SEEK_END) rwops.rw_seek(rw, 0, rwops.RW_SEEK_SET) self.assertEqual(len(buf.getvalue()), length) rwops.rw_close(rw) self.assertTrue(buf.closed) self.assertRaises(ValueError, buf.write, "Test") self.assertRaises(ValueError, buf.getvalue)
def test_read_be_64(self): data = byteify("A Teststring 64b", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) ch = rwops.read_be_64(rw) self.assertEqual(chr(ch & 0x00000000000000FF), "t") self.assertEqual(chr((ch & 0x000000000000FF00) >> 8), "s") self.assertEqual(chr((ch & 0x0000000000FF0000) >> 16), "t") self.assertEqual(chr((ch & 0x00000000FF000000) >> 24), "s") self.assertEqual(chr((ch & 0x000000FF00000000) >> 32), "e") self.assertEqual(chr((ch & 0x0000FF0000000000) >> 40), "T") self.assertEqual(chr((ch & 0x00FF000000000000) >> 48), " ") self.assertEqual(chr((ch & 0xFF00000000000000) >> 56), "A") pos = rwops.rw_seek(rw, 8, rwops.RW_SEEK_SET) self.assertEqual(pos, 8) ch = rwops.read_be_64(rw) self.assertEqual(chr(ch & 0x00000000000000FF), "b") self.assertEqual(chr((ch & 0x000000000000FF00) >> 8), "4") self.assertEqual(chr((ch & 0x0000000000FF0000) >> 16), "6") self.assertEqual(chr((ch & 0x00000000FF000000) >> 24), " ") self.assertEqual(chr((ch & 0x000000FF00000000) >> 32), "g") self.assertEqual(chr((ch & 0x0000FF0000000000) >> 40), "n") self.assertEqual(chr((ch & 0x00FF000000000000) >> 48), "i") self.assertEqual(chr((ch & 0xFF00000000000000) >> 56), "r")
def test_write_be_32(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) value = ((ord("a") << 24) | (ord("c") << 16) | (ord("f") << 8) | (ord("z")) ) rwops.write_be_32(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "acfzststring") rwops.rw_seek(rw, 6, rwops.RW_SEEK_SET) rwops.write_be_32(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "acfzstacfzng")
def test_write_be_64(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) value = ((ord("q") << 56) | (ord("w") << 48) | (ord("b") << 40) | (ord("k") << 32) | (ord("a") << 24) | (ord("c") << 16) | (ord("f") << 8) | (ord("z")) ) rwops.write_be_64(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "qwbkacfzring") rwops.rw_seek(rw, 4, rwops.RW_SEEK_SET) rwops.write_be_64(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "qwbkqwbkacfz")
def test_read_be_16(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) ch = rwops.read_be_16(rw) self.assertEqual(chr(ch & 0x00FF), " ") self.assertEqual(chr(ch >> 8), "A") pos = rwops.rw_seek(rw, 8, rwops.RW_SEEK_SET) self.assertEqual(pos, 8) ch = rwops.read_be_16(rw) self.assertEqual(chr(ch & 0x00FF), "i") self.assertEqual(chr(ch >> 8), "r")