def test_SDL_ReadBE64(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.SDL_ReadBE64(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.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) self.assertEqual(pos, 8) ch = rwops.SDL_ReadBE64(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_SDL_RWSeekTell(self): data = byteify("A Teststring of length 25", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) pos = rwops.SDL_RWseek(rw, 0, rwops.RW_SEEK_END) assert pos == buf.tell() == len(data) pos = rwops.SDL_RWseek(rw, 0, rwops.RW_SEEK_SET) assert pos == buf.tell() == 0 pos = rwops.SDL_RWseek(rw, 15, rwops.RW_SEEK_CUR) assert pos == buf.tell() == 15 pos = rwops.SDL_RWseek(rw, -3, rwops.RW_SEEK_CUR) assert pos == buf.tell() == 12 pos = rwops.SDL_RWseek(rw, 7, rwops.RW_SEEK_CUR) assert pos == buf.tell() == 19 pos = rwops.SDL_RWseek(rw, -11, rwops.RW_SEEK_END) assert pos == buf.tell() == 14 pos = rwops.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) assert pos == buf.tell() == 8 pos = rwops.SDL_RWseek(rw, -2, rwops.RW_SEEK_SET) assert pos == -1 assert buf.tell() == 8 pos = rwops.SDL_RWseek(rw, 12, rwops.RW_SEEK_END) assert pos == buf.tell() == len(data) + 12
def test_SDL_RWSeekTell(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.SDL_RWseek(rw, 0, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == len(data)) pos = rwops.SDL_RWseek(rw, 0, rwops.RW_SEEK_SET) self.assertTrue(pos == buf.tell() == 0) pos = rwops.SDL_RWseek(rw, 15, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 15) pos = rwops.SDL_RWseek(rw, -3, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 12) pos = rwops.SDL_RWseek(rw, 7, rwops.RW_SEEK_CUR) self.assertTrue(pos == buf.tell() == 19) pos = rwops.SDL_RWseek(rw, -11, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == 14) pos = rwops.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) self.assertTrue(pos == buf.tell() == 8) pos = rwops.SDL_RWseek(rw, -2, rwops.RW_SEEK_SET) self.assertEqual(pos, -1) self.assertTrue(buf.tell() == 8) pos = rwops.SDL_RWseek(rw, 12, rwops.RW_SEEK_END) self.assertTrue(pos == buf.tell() == len(data) + 12)
def test_IMG_LoadXV_RW(self): testdir = os.path.dirname(os.path.abspath(__file__)) fp = open(os.path.join(testdir, "resources", "surfacetest.xv"), "rb") sf = sdlimage.IMG_LoadXV_RW(rwops.rw_from_object(fp)) fp.close() self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_IMG_LoadTexture_RW(self): sf = surface.SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0) rd = render.SDL_CreateSoftwareRenderer(sf) fname = "surfacetest.%s" for fmt in formats: if fmt == "tga": # SDL_image does not support loading TGA via # IMG_LoadTexture_RW() continue if fmt in ("svg", "webp"): # FIXME: svg and webp crashes with SDL2_image 2.0.2 continue filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) print(filename) with open(filename, "rb") as fp: tex = sdlimage.IMG_LoadTexture_RW(rd, rwops.rw_from_object(fp), 0) self.assertIsNotNone(tex) self.assertIsInstance(tex.contents, render.SDL_Texture) render.SDL_DestroyTexture(tex) render.SDL_DestroyRenderer(rd) surface.SDL_FreeSurface(sf)
def test_SDL_ReadBE64(self): data = byteify("A Teststring 64b", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) ch = rwops.SDL_ReadBE64(rw) assert chr(ch & 0x00000000000000FF) == "t" assert chr((ch & 0x000000000000FF00) >> 8) == "s" assert chr((ch & 0x0000000000FF0000) >> 16) == "t" assert chr((ch & 0x00000000FF000000) >> 24) == "s" assert chr((ch & 0x000000FF00000000) >> 32) == "e" assert chr((ch & 0x0000FF0000000000) >> 40) == "T" assert chr((ch & 0x00FF000000000000) >> 48) == " " assert chr((ch & 0xFF00000000000000) >> 56) == "A" pos = rwops.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) assert pos == 8 ch = rwops.SDL_ReadBE64(rw) assert chr(ch & 0x00000000000000FF) == "b" assert chr((ch & 0x000000000000FF00) >> 8) == "4" assert chr((ch & 0x0000000000FF0000) >> 16) == "6" assert chr((ch & 0x00000000FF000000) >> 24) == " " assert chr((ch & 0x000000FF00000000) >> 32) == "g" assert chr((ch & 0x0000FF0000000000) >> 40) == "n" assert chr((ch & 0x00FF000000000000) >> 48) == "i" assert chr((ch & 0xFF00000000000000) >> 56) == "r"
def test_SDL_LoadBMP_RW(self): fp = open(self.testfile, "rb") imgrw = rwops.rw_from_object(fp) imgsurface = surface.SDL_LoadBMP_RW(imgrw, 0) self.assertIsInstance(imgsurface.contents, surface.SDL_Surface) surface.SDL_FreeSurface(imgsurface) imgrw.close(imgrw)
def test_TTF_OpenFontIndexRW(self): fp = open(fontfile, "rb") fontrw = rwops.rw_from_object(fp) for x in range(-10, 15): fp.seek(0) font = sdlttf.TTF_OpenFontIndexRW(fontrw, 0, x, 0) self.assertIsInstance(font.contents, sdlttf.TTF_Font) sdlttf.TTF_CloseFont(font) fp.close()
def test_TTF_OpenFontRW(self): fp = open(fontfile, "rb") fontrw = rwops.rw_from_object(fp) for x in range(6, 26): fp.seek(0) font = sdlttf.TTF_OpenFontRW(fontrw, 0, x) assert isinstance(font.contents, sdlttf.TTF_Font) sdlttf.TTF_CloseFont(font) fp.close()
def test_SDL_RWclose(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) self.assertEqual(buf.getvalue(), data) rwops.SDL_RWclose(rw) self.assertRaises(ValueError, buf.getvalue)
def test_SDL_RWclose(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) assert buf.getvalue() == data rwops.SDL_RWclose(rw) with pytest.raises(ValueError): buf.getvalue()
def test_IMG_isPNM(self): fname = "surfacetest.%s" for fmt in formats: filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) with open(filename, "rb") as fp: imgrw = rwops.rw_from_object(fp) if fmt in ("pnm", "pbm", "ppm", "pgm"): self.assertTrue(sdlimage.IMG_isPNM(imgrw)) else: self.assertFalse(sdlimage.IMG_isPNM(imgrw))
def test_IMG_isXV(self): fname = "surfacetest.%s" for fmt in formats: testdir = os.path.dirname(os.path.abspath(__file__)) filename = os.path.join(testdir, "resources", fname % fmt) with open(filename, "rb") as fp: imgrw = rwops.rw_from_object(fp) if fmt == "xv": self.assertTrue(sdlimage.IMG_isXV(imgrw)) else: self.assertFalse(sdlimage.IMG_isXV(imgrw))
def test_IMG_isWEBP(self): fname = "surfacetest.%s" for fmt in formats: testdir = os.path.dirname(os.path.abspath(__file__)) filename = os.path.join(testdir, "resources", fname % fmt) with open(filename, "rb") as fp: imgrw = rwops.rw_from_object(fp) if fmt == "webp": assert sdlimage.IMG_isWEBP(imgrw) else: assert not sdlimage.IMG_isWEBP(imgrw)
def test_IMG_LoadTyped_RW(self): fname = "surfacetest.%s" for fmt in formats: filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) fp = open(filename, "rb") sf = sdlimage.IMG_LoadTyped_RW(rwops.rw_from_object(fp), False, fmt.upper().encode("utf-8")) fp.close() self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_IMG_Load_RW(self): fname = "surfacetest.%s" for fmt in formats: if fmt in ("tga", "webp"): # FIXME: tga and webp crashes with SDL2_image 2.0.2 continue filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) with open(filename, "rb") as fp: sf = sdlimage.IMG_Load_RW(rwops.rw_from_object(fp), False) self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_IMG_isXPM(self): fname = "surfacetest.%s" for fmt in formats: filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) fp = open(filename, "rb") imgrw = rwops.rw_from_object(fp) if fmt == "xpm": self.assertTrue(sdlimage.IMG_isXPM(imgrw)) else: self.assertFalse(sdlimage.IMG_isXPM(imgrw)) fp.close()
def test_IMG_Load_RW(self): skip = ['tga']#, 'webp'] fname = "surfacetest.%s" for fmt in formats: if fmt in skip: continue testdir = os.path.dirname(os.path.abspath(__file__)) filename = os.path.join(testdir, "resources", fname % fmt) with open(filename, "rb") as fp: sf = sdlimage.IMG_Load_RW(rwops.rw_from_object(fp), False) self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_IMG_Load_RW(self): fname = "surfacetest.%s" for fmt in formats: if fmt == "tga": # SDL_image does not support loading TGA via IMG_Load_RW() continue filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "resources", fname % fmt) fp = open(filename, "rb") sf = sdlimage.IMG_Load_RW(rwops.rw_from_object(fp), False) fp.close() self.assertIsInstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_SDL_WriteLE16(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) value = ((ord("q") << 8) | (ord("%"))) rwops.SDL_WriteLE16(rw, value) assert stringify(buf.getvalue(), "utf-8") == "%qTeststring" rwops.SDL_RWseek(rw, 6, rwops.RW_SEEK_SET) rwops.SDL_WriteLE16(rw, value) assert stringify(buf.getvalue(), "utf-8") == "%qTest%qring"
def test_SDL_WriteBE16(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.SDL_WriteBE16(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "q%Teststring") rwops.SDL_RWseek(rw, 6, rwops.RW_SEEK_SET) rwops.SDL_WriteBE16(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "q%Testq%ring")
def test_IMG_LoadTyped_RW(self): skip = [] fname = "surfacetest.%s" for fmt in formats: if fmt in skip: continue testdir = os.path.dirname(os.path.abspath(__file__)) filename = os.path.join(testdir, "resources", fname % fmt) with open(filename, "rb") as fp: sf = sdlimage.IMG_LoadTyped_RW(rwops.rw_from_object(fp), False, fmt.upper().encode("utf-8")) assert isinstance(sf.contents, surface.SDL_Surface) surface.SDL_FreeSurface(sf)
def test_SDL_WriteBE32(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.SDL_WriteBE32(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "acfzststring") rwops.SDL_RWseek(rw, 6, rwops.RW_SEEK_SET) rwops.SDL_WriteBE32(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "acfzstacfzng")
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.SDL_RWseek(rw, 0, rwops.RW_SEEK_END) rwops.SDL_RWseek(rw, 0, rwops.RW_SEEK_SET) self.assertEqual(len(buf.getvalue()), length) rwops.SDL_RWclose(rw) self.assertTrue(buf.closed) self.assertRaises(ValueError, buf.write, "Test") self.assertRaises(ValueError, buf.getvalue)
def test_SDL_RWread(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) readbuf = ctypes.create_string_buffer(2) read = rwops.SDL_RWread(rw, readbuf, 1, 2) self.assertEqual(read, 2) self.assertEqual(readbuf.raw, b"A ") readbuf = ctypes.create_string_buffer(10) read = rwops.SDL_RWread(rw, readbuf, 1, 10) self.assertEqual(read, 10) self.assertEqual(readbuf.raw, b"Teststring")
def test_SDL_ReadBE16(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) self.assertIsInstance(rw, rwops.SDL_RWops) ch = rwops.SDL_ReadBE16(rw) self.assertEqual(chr(ch & 0x00FF), " ") self.assertEqual(chr(ch >> 8), "A") pos = rwops.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) self.assertEqual(pos, 8) ch = rwops.SDL_ReadBE16(rw) self.assertEqual(chr(ch & 0x00FF), "i") self.assertEqual(chr(ch >> 8), "r")
def test_SDL_RWread(self): data = byteify("A Teststring of length 25", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) readbuf = ctypes.create_string_buffer(2) read = rwops.SDL_RWread(rw, readbuf, 1, 2) assert read == 2 assert readbuf.raw == b"A " readbuf = ctypes.create_string_buffer(10) read = rwops.SDL_RWread(rw, readbuf, 1, 10) assert read == 10 assert readbuf.raw == b"Teststring"
def test_SDL_ReadBE16(self): data = byteify("A Teststring", "utf-8") buf = BytesIO(data) rw = rwops.rw_from_object(buf) assert isinstance(rw, rwops.SDL_RWops) ch = rwops.SDL_ReadBE16(rw) assert chr(ch & 0x00FF) == " " assert chr(ch >> 8) == "A" pos = rwops.SDL_RWseek(rw, 8, rwops.RW_SEEK_SET) assert pos == 8 ch = rwops.SDL_ReadBE16(rw) assert chr(ch & 0x00FF) == "i" assert chr(ch >> 8) == "r"
def test_SDL_WriteBE64(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.SDL_WriteBE64(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "qwbkacfzring") rwops.SDL_RWseek(rw, 4, rwops.RW_SEEK_SET) rwops.SDL_WriteBE64(rw, value) self.assertEqual(stringify(buf.getvalue(), "utf-8"), "qwbkqwbkacfz")
def test_SDL_RWwrite(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) writebuf = ctypes.create_string_buffer(b"XQ") written = rwops.SDL_RWwrite(rw, writebuf, 1, 2) self.assertEqual(written, 2) self.assertEqual(buf.getvalue(), b"XQTeststring of length 25") writebuf = ctypes.create_string_buffer(b"banana") rwops.SDL_RWseek(rw, 14, rwops.RW_SEEK_CUR) written = rwops.SDL_RWwrite(rw, writebuf, 1, 6) self.assertEqual(written, 6) self.assertEqual(buf.getvalue(), b"XQTeststring of banana 25")