data.lua 15 KB

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