unit.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import gameduino
  2. import unittest
  3. import Image
  4. import StringIO
  5. import gameduino.prep as gdprep
  6. import gameduino.sim as gdsim
  7. import gameduino.compress as compress
  8. import gameduino
  9. class TestGameduino(unittest.TestCase):
  10. def setUp(self):
  11. self.bg = Image.open("platformer.png")
  12. pass
  13. def test_encode(self):
  14. im = self.bg.convert("RGB")
  15. w,h = im.size
  16. (pic,chr,pal) = gdprep.encode(im)
  17. self.assertEqual(len(chr.tostring()) / 16, len(pal.tostring()) / 8)
  18. self.assertEqual(len(pic), (w / 8) * (h / 8))
  19. def test_imageRAM(self):
  20. hh = StringIO.StringIO()
  21. singles = [Image.open("rock0r-pal16.png"),
  22. gdprep.palettize(Image.open("rock0r.png"), 16),
  23. gdprep.palettize(Image.open("rock0r-pal16.png"), 16)]
  24. for r in singles:
  25. print r.mode
  26. ir = gdprep.ImageRAM(hh)
  27. ir.addsprites("rock0", (16, 16), r, gdprep.PALETTE16A, center = (8,8))
  28. self.assert_(len(hh.getvalue()) > 0)
  29. self.assertEqual(len(gdprep.getpal(r)), 16)
  30. ir = gdprep.ImageRAM(hh)
  31. (walk,) = gdprep.palettize([Image.open("walk.png")], 16)
  32. ir.addsprites("walk", (32, 32), walk, gdprep.PALETTE16A, center = (8,32))
  33. print len(ir.used())
  34. print hh.getvalue()
  35. def test_compress(self):
  36. cc = compress.Codec(b_off = 9, b_len = 3)
  37. for plain in [ "00000111100000", "This is this" * 4]:
  38. compressed = cc.compress(plain)
  39. print "compressed to", len(compressed), "tokens"
  40. print compressed
  41. self.assertEqual(plain, cc.decompress(compressed))
  42. print len(cc.sched2bs(compressed))
  43. def test_sim(self):
  44. gd = gdsim.Gameduino()
  45. self.assertEqual(gd.rd(gameduino.IDENT), 0x6d)
  46. i = gd.im()
  47. self.assertEqual(i.size, (400,300))
  48. def test_prep_sim(self):
  49. im = self.bg.convert("RGB")
  50. (pic,chr,pal) = gdprep.encode(im)
  51. gd = gdsim.Gameduino()
  52. gd.wrstr(gameduino.RAM_PIC, pic)
  53. gd.wrstr(gameduino.RAM_CHR, chr)
  54. gd.wrstr(gameduino.RAM_PAL, pal)
  55. gd.im().save("preview.png")
  56. def test_471fcf9e(self):
  57. im = Image.open("471fcf9e.png")
  58. imp = gdprep.palettize(im, 16)
  59. print gdprep.getpal(imp)
  60. if __name__ == '__main__':
  61. unittest.main()