improved rawtexture tests

This commit is contained in:
Andrew Cassidy 2021-04-06 23:41:26 -07:00
parent f06fc809b2
commit fea4d6c2b1
2 changed files with 31 additions and 29 deletions

@ -1 +1 @@
Subproject commit 1023b4934473aef3c2ff24687bef4f41db1f702b Subproject commit 31c8cebdc5c668ce71fcf7d6ecd8b6f47c6b471e

View File

@ -1,60 +1,62 @@
import unittest import unittest
import nose import nose
import quicktex import os.path
import tests.images as images from tests.images import image_path
import tempfile from quicktex import RawTexture
from PIL import Image from PIL import Image
class TestRawTexture(unittest.TestCase): class TestRawTexture(unittest.TestCase):
boilerplate = Image.open(images.image_path + '/Boilerplate.png') boilerplate = Image.open(os.path.join(image_path, 'Boilerplate.png'))
bp_bytes = boilerplate.tobytes('raw', 'RGBX') boilerplate_bytes = boilerplate.tobytes('raw', 'RGBX')
width = boilerplate.width width, height = boilerplate.size
height = boilerplate.height size = width * height * 4
def setUp(self): def setUp(self):
self.texture = quicktex.RawTexture(self.width, self.height) self.tex = RawTexture(self.width, self.height)
def test_size(self): def test_size(self):
"""Test byte size and image dimensions""" """Test byte size and image dimensions"""
self.assertEqual(self.texture.size, self.width * self.height * 4, "incorrect texture byte size") self.assertEqual(self.tex.size, self.size, "incorrect texture byte size")
self.assertEqual(self.texture.width, self.width, "incorrect texture width") self.assertEqual(self.tex.width, self.width, "incorrect texture width")
self.assertEqual(self.texture.height, self.height, "incorrect texture height") self.assertEqual(self.tex.height, self.height, "incorrect texture height")
self.assertEqual(self.texture.dimensions, (self.width, self.height), "incorrect texture dimension tuple") self.assertEqual(self.tex.dimensions, (self.width, self.height), "incorrect texture dimensions")
def test_pixels(self): def test_pixels(self):
"""Test getting and setting pixel values""" """Test getting and setting pixel values"""
color1 = (69, 13, 12, 0) # totally random color color1 = (69, 13, 12, 0) # totally random color
color2 = (19, 142, 93, 44) color2 = (19, 142, 93, 44)
self.texture[0, 0] = color1 self.tex[0, 0] = color1
self.texture[-1, -1] = color2 self.tex[-1, -1] = color2
data = self.texture.tobytes() data = self.tex.tobytes()
self.assertEqual(self.texture[0, 0], color1) self.assertEqual(self.tex[0, 0], color1)
self.assertEqual(self.texture[-1, -1], color2) self.assertEqual(self.tex[-1, -1], color2)
self.assertEqual(tuple(data[0:4]), color1) self.assertEqual(tuple(data[0:4]), color1)
self.assertEqual(tuple(data[-4:]), color2) self.assertEqual(tuple(data[-4:]), color2)
with self.assertRaises(IndexError): with self.assertRaises(IndexError):
thing = self.texture[self.width, self.height] thing = self.tex[self.width, self.height]
with self.assertRaises(IndexError): with self.assertRaises(IndexError):
thing = self.texture[-1 - self.width, -1 - self.height] thing = self.tex[-1 - self.width, -1 - self.height]
def test_buffer(self): def test_buffer(self):
"""Test the Buffer protocol implementation for RawTexture""" """Test the Buffer protocol implementation for RawTexture"""
with tempfile.TemporaryFile('r+b') as fp: mv = memoryview(self.tex)
fp.write(self.bp_bytes)
fp.seek(0)
bytes_read = fp.readinto(self.texture)
self.assertEqual(bytes_read, self.texture.size, 'buffer over/underrun') self.assertFalse(mv.readonly, 'buffer is readonly')
self.assertEqual(self.bp_bytes, self.texture.tobytes(), 'Incorrect bytes after writing to buffer') self.assertTrue(mv.c_contiguous, 'buffer is not contiguous')
self.assertEqual(self.bp_bytes, bytes(self.texture), "Incorrect bytes after reading from buffer") self.assertEqual(mv.nbytes, self.size, 'buffer is the wrong size')
self.assertEqual(mv.format, 'B', 'buffer has the wrong format')
mv[:] = self.boilerplate_bytes
self.assertEqual(mv.tobytes(), self.boilerplate_bytes, 'incorrect buffer data')
def test_frombytes(self): def test_frombytes(self):
"""Test the frombytes factory function""" """Test the frombytes factory function"""
bytetex = quicktex.RawTexture.frombytes(self.bp_bytes, *self.boilerplate.size) bytetex = RawTexture.frombytes(self.boilerplate_bytes, *self.boilerplate.size)
self.assertEqual(self.bp_bytes, bytetex.tobytes(), 'Incorrect bytes after writing to buffer') self.assertEqual(self.boilerplate_bytes, bytetex.tobytes(), 'Incorrect bytes after writing to buffer')
if __name__ == '__main__': if __name__ == '__main__':