data.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. -- love.data
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- ByteData (love.data.newByteData)
  8. love.test.data.ByteData = function(test)
  9. -- create new obj
  10. local data = love.data.newByteData('helloworld')
  11. test:assertObject(data)
  12. -- check properties match expected
  13. test:assertEquals('helloworld', data:getString(), 'check data string')
  14. test:assertEquals(10, data:getSize(), 'check data size')
  15. -- check cloning the bytedata
  16. local cloneddata = data:clone()
  17. test:assertObject(cloneddata)
  18. test:assertEquals('helloworld', cloneddata:getString(), 'check cloned data')
  19. test:assertEquals(10, cloneddata:getSize(), 'check cloned size')
  20. -- check pointer access if allowed
  21. if data:getFFIPointer() ~= nil and ffi ~= nil then
  22. local pointer = data:getFFIPointer()
  23. local ptr = ffi.cast('uint8_t*', pointer)
  24. local byte5 = ptr[4]
  25. test:assertEquals('o', byte5)
  26. end
  27. end
  28. -- CompressedData (love.data.compress)
  29. love.test.data.CompressedData = function(test)
  30. -- create new compressed data
  31. local cdata = love.data.compress('data', 'zlib', 'helloworld', -1)
  32. test:assertObject(cdata)
  33. test:assertEquals('zlib', cdata:getFormat(), 'check format used')
  34. -- check properties match expected
  35. test:assertEquals(18, cdata:getSize())
  36. test:assertEquals('helloworld', love.data.decompress('data', cdata):getString())
  37. -- check cloning the data
  38. local clonedcdata = cdata:clone()
  39. test:assertObject(clonedcdata)
  40. test:assertEquals('zlib', clonedcdata:getFormat())
  41. test:assertEquals(18, clonedcdata:getSize())
  42. test:assertEquals('helloworld', love.data.decompress('data', clonedcdata):getString())
  43. end
  44. --------------------------------------------------------------------------------
  45. --------------------------------------------------------------------------------
  46. ------------------------------------METHODS-------------------------------------
  47. --------------------------------------------------------------------------------
  48. --------------------------------------------------------------------------------
  49. -- love.data.compress
  50. love.test.data.compress = function(test)
  51. -- here just testing each combo 'works' - in decompress's test method
  52. -- we actually check the compress + decompress give the right value back
  53. local compressions = {
  54. { love.data.compress('string', 'lz4', 'helloworld', -1), 'string'},
  55. { love.data.compress('string', 'lz4', 'helloworld', 0), 'string'},
  56. { love.data.compress('string', 'lz4', 'helloworld', 9), 'string'},
  57. { love.data.compress('string', 'zlib', 'helloworld', -1), 'string'},
  58. { love.data.compress('string', 'zlib', 'helloworld', 0), 'string'},
  59. { love.data.compress('string', 'zlib', 'helloworld', 9), 'string'},
  60. { love.data.compress('string', 'gzip', 'helloworld', -1), 'string'},
  61. { love.data.compress('string', 'gzip', 'helloworld', 0), 'string'},
  62. { love.data.compress('string', 'gzip', 'helloworld', 9), 'string'},
  63. { love.data.compress('data', 'lz4', 'helloworld', -1), 'userdata'},
  64. { love.data.compress('data', 'lz4', 'helloworld', 0), 'userdata'},
  65. { love.data.compress('data', 'lz4', 'helloworld', 9), 'userdata'},
  66. { love.data.compress('data', 'zlib', 'helloworld', -1), 'userdata'},
  67. { love.data.compress('data', 'zlib', 'helloworld', 0), 'userdata'},
  68. { love.data.compress('data', 'zlib', 'helloworld', 9), 'userdata'},
  69. { love.data.compress('data', 'gzip', 'helloworld', -1), 'userdata'},
  70. { love.data.compress('data', 'gzip', 'helloworld', 0), 'userdata'},
  71. { love.data.compress('data', 'gzip', 'helloworld', 9), 'userdata'}
  72. }
  73. for c=1,#compressions do
  74. test:assertNotNil(compressions[c][1])
  75. -- sense check return type and make sure bytedata returns are an object
  76. test:assertEquals(compressions[c][2], type(compressions[c][1]), 'check is userdata')
  77. if compressions[c][2] == 'userdata' then
  78. test:assertNotEquals(nil, compressions[c][1]:type(), 'check has :type()')
  79. end
  80. end
  81. end
  82. -- love.data.decode
  83. love.test.data.decode = function(test)
  84. -- setup encoded strings
  85. local str1 = love.data.encode('string', 'base64', 'helloworld', 0)
  86. local str2 = love.data.encode('string', 'hex', 'helloworld')
  87. local str3 = love.data.encode('data', 'base64', 'helloworld', 0)
  88. local str4 = love.data.encode('data', 'hex', 'helloworld')
  89. -- check value matches expected when decoded back
  90. test:assertEquals('helloworld', love.data.decode('string', 'base64', str1), 'check string base64 decode')
  91. test:assertEquals('helloworld', love.data.decode('string', 'hex', str2), 'check string hex decode')
  92. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decode('data', 'base64', str3):getString(), 'check data base64 decode')
  93. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decode('data', 'hex', str4):getString(), 'check data hex decode')
  94. end
  95. -- love.data.decompress
  96. love.test.data.decompress = function(test)
  97. -- setup compressed data for each combination
  98. local str1 = love.data.compress('string', 'lz4', 'helloworld', -1)
  99. local str2 = love.data.compress('string', 'lz4', 'helloworld', 0)
  100. local str3 = love.data.compress('string', 'lz4', 'helloworld', 9)
  101. local str4 = love.data.compress('string', 'zlib', 'helloworld', -1)
  102. local str5 = love.data.compress('string', 'zlib', 'helloworld', 0)
  103. local str6 = love.data.compress('string', 'zlib', 'helloworld', 9)
  104. local str7 = love.data.compress('string', 'gzip', 'helloworld', -1)
  105. local str8 = love.data.compress('string', 'gzip', 'helloworld', 0)
  106. local str9 = love.data.compress('string', 'gzip', 'helloworld', 9)
  107. local str10 = love.data.compress('data', 'lz4', 'helloworld', -1)
  108. local str11 = love.data.compress('data', 'lz4', 'helloworld', 0)
  109. local str12 = love.data.compress('data', 'lz4', 'helloworld', 9)
  110. local str13 = love.data.compress('data', 'zlib', 'helloworld', -1)
  111. local str14 = love.data.compress('data', 'zlib', 'helloworld', 0)
  112. local str15 = love.data.compress('data', 'zlib', 'helloworld', 9)
  113. local str16 = love.data.compress('data', 'gzip', 'helloworld', -1)
  114. local str17 = love.data.compress('data', 'gzip', 'helloworld', 0)
  115. local str18 = love.data.compress('data', 'gzip', 'helloworld', 9)
  116. -- check decompressed value matches whats expected
  117. test:assertEquals('helloworld', love.data.decompress('string', 'lz4', str1), 'check string lz4 decompress')
  118. test:assertEquals('helloworld', love.data.decompress('string', 'lz4', str2), 'check string lz4 decompress')
  119. test:assertEquals('helloworld', love.data.decompress('string', 'lz4', str3), 'check string lz4 decompress')
  120. test:assertEquals('helloworld', love.data.decompress('string', 'zlib', str4), 'check string zlib decompress')
  121. test:assertEquals('helloworld', love.data.decompress('string', 'zlib', str5), 'check string zlib decompress')
  122. test:assertEquals('helloworld', love.data.decompress('string', 'zlib', str6), 'check string zlib decompress')
  123. test:assertEquals('helloworld', love.data.decompress('string', 'gzip', str7), 'check string glib decompress')
  124. test:assertEquals('helloworld', love.data.decompress('string', 'gzip', str8), 'check string glib decompress')
  125. test:assertEquals('helloworld', love.data.decompress('string', 'gzip', str9), 'check string glib decompress')
  126. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'lz4', str10):getString(), 'check data lz4 decompress')
  127. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'lz4', str11):getString(), 'check data lz4 decompress')
  128. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'lz4', str12):getString(), 'check data lz4 decompress')
  129. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'zlib', str13):getString(), 'check data zlib decompress')
  130. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'zlib', str14):getString(), 'check data zlib decompress')
  131. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'zlib', str15):getString(), 'check data zlib decompress')
  132. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'gzip', str16):getString(), 'check data glib decompress')
  133. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'gzip', str17):getString(), 'check data glib decompress')
  134. test:assertEquals(love.data.newByteData('helloworld'):getString(), love.data.decompress('data', 'gzip', str18):getString(), 'check data glib decompress')
  135. end
  136. -- love.data.encode
  137. love.test.data.encode = function(test)
  138. -- here just testing each combo 'works' - in decode's test method
  139. -- we actually check the encode + decode give the right value back
  140. local encodes = {
  141. { love.data.encode('string', 'base64', 'helloworld', 0), 'string'},
  142. { love.data.encode('string', 'base64', 'helloworld', 2), 'string'},
  143. { love.data.encode('string', 'hex', 'helloworld'), 'string'},
  144. { love.data.encode('data', 'base64', 'helloworld', 0), 'userdata'},
  145. { love.data.encode('data', 'base64', 'helloworld', 2), 'userdata'},
  146. { love.data.encode('data', 'hex', 'helloworld'), 'userdata'}
  147. }
  148. for e=1,#encodes do
  149. test:assertNotNil(encodes[e][1])
  150. -- sense check return type and make sure bytedata returns are an object
  151. test:assertEquals(encodes[e][2], type(encodes[e][1]), 'check is usedata')
  152. if encodes[e][2] == 'userdata' then
  153. test:assertNotEquals(nil, encodes[e][1]:type(), 'check has :type()')
  154. end
  155. end
  156. end
  157. -- love.data.getPackedSize
  158. love.test.data.getPackedSize = function(test)
  159. local pack1 = love.data.getPackedSize('>xI3b')
  160. local pack2 = love.data.getPackedSize('>I2B')
  161. local pack3 = love.data.getPackedSize('>I4I4I4I4x')
  162. test:assertEquals(5, pack1, 'check pack size 1')
  163. test:assertEquals(3, pack2, 'check pack size 2')
  164. test:assertEquals(17, pack3, 'check pack size 3')
  165. end
  166. -- love.data.hash
  167. love.test.data.hash = function(test)
  168. -- setup all the different hashing types
  169. local str1 = love.data.hash('md5', 'helloworld')
  170. local str2 = love.data.hash('sha1', 'helloworld')
  171. local str3 = love.data.hash('sha224', 'helloworld')
  172. local str4 = love.data.hash('sha256', 'helloworld')
  173. local str5 = love.data.hash('sha384', 'helloworld')
  174. local str6 = love.data.hash('sha512', 'helloworld')
  175. -- check encoded hash value matches what's expected for that algo
  176. test:assertEquals('fc5e038d38a57032085441e7fe7010b0', love.data.encode("string", "hex", str1), 'check md5 encode')
  177. test:assertEquals('6adfb183a4a2c94a2f92dab5ade762a47889a5a1', love.data.encode("string", "hex", str2), 'check sha1 encode')
  178. test:assertEquals('b033d770602994efa135c5248af300d81567ad5b59cec4bccbf15bcc', love.data.encode("string", "hex", str3), 'check sha224 encode')
  179. test:assertEquals('936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af', love.data.encode("string", "hex", str4), 'check sha256 encode')
  180. test:assertEquals('97982a5b1414b9078103a1c008c4e3526c27b41cdbcf80790560a40f2a9bf2ed4427ab1428789915ed4b3dc07c454bd9', love.data.encode("string", "hex", str5), 'check sha384 encode')
  181. test:assertEquals('1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60', love.data.encode("string", "hex", str6), 'check sha512 encode')
  182. end
  183. -- love.data.newByteData
  184. -- @NOTE this is just basic nil checking, objs have their own test method
  185. love.test.data.newByteData = function(test)
  186. test:assertObject(love.data.newByteData('helloworld'))
  187. end
  188. -- love.data.newDataView
  189. -- @NOTE this is just basic nil checking, objs have their own test method
  190. love.test.data.newDataView = function(test)
  191. test:assertObject(love.data.newDataView(love.data.newByteData('helloworld'), 0, 10))
  192. end
  193. -- love.data.pack
  194. love.test.data.pack = function(test)
  195. local packed1 = love.data.pack('string', '>I4I4I4I4', 9999, 1000, 1010, 2030)
  196. local packed2 = love.data.pack('data', '>I4I4I4I4', 9999, 1000, 1010, 2030)
  197. local a, b, c, d = love.data.unpack('>I4I4I4I4', packed1)
  198. local e, f, g, h = love.data.unpack('>I4I4I4I4', packed2)
  199. test:assertEquals(9999+9999, a+e, 'check packed 1')
  200. test:assertEquals(1000+1000, b+f, 'check packed 2')
  201. test:assertEquals(1010+1010, c+g, 'check packed 3')
  202. test:assertEquals(2030+2030, d+h, 'check packed 4')
  203. end
  204. -- love.data.unpack
  205. love.test.data.unpack = function(test)
  206. local packed1 = love.data.pack('string', '>s5s4I3', 'hello', 'love', 100)
  207. local packed2 = love.data.pack('data', '>s5I2', 'world', 20)
  208. local a, b, c = love.data.unpack('>s5s4I3', packed1)
  209. local d, e = love.data.unpack('>s5I2', packed2)
  210. test:assertEquals(a .. ' ' .. d, 'hello world', 'check unpack 1')
  211. test:assertEquals(b, 'love', 'check unpack 2')
  212. test:assertEquals(c - e, 80, 'check unpack 3')
  213. end