meshopt_decoder.test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. var assert = require('assert').strict;
  2. var decoder = require('./meshopt_decoder.js');
  3. process.on('unhandledRejection', error => {
  4. console.log('unhandledRejection', error);
  5. process.exit(1);
  6. });
  7. var tests = {
  8. decodeVertexBuffer: function() {
  9. var encoded = new Uint8Array([
  10. 0xa0, 0x01, 0x3f, 0x00, 0x00, 0x00, 0x58, 0x57, 0x58, 0x01, 0x26, 0x00, 0x00, 0x00, 0x01,
  11. 0x0c, 0x00, 0x00, 0x00, 0x58, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  12. 0x3f, 0x00, 0x00, 0x00, 0x17, 0x18, 0x17, 0x01, 0x26, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x00,
  13. 0x00, 0x00, 0x17, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16. ]);
  17. var expected = new Uint8Array([
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 44, 1, 0, 0, 0, 0, 0, 0, 244, 1, 0, 0,
  20. 0, 0, 44, 1, 0, 0, 0, 0, 0, 0, 244, 1,
  21. 44, 1, 44, 1, 0, 0, 0, 0, 244, 1, 244, 1
  22. ]);
  23. var result = new Uint8Array(expected.length);
  24. decoder.decodeVertexBuffer(result, 4, 12, encoded);
  25. assert.deepStrictEqual(result, expected);
  26. },
  27. decodeVertexBuffer_More: function() {
  28. var encoded = new Uint8Array([
  29. 0xa0, 0x00, 0x01, 0x2a, 0xaa, 0xaa, 0xaa, 0x02, 0x04, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  30. 0x44, 0x03, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  31. 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00,
  34. ]);
  35. var expected = new Uint8Array([
  36. 0, 0, 0, 0, 0, 1, 2, 8, 0, 2, 4, 16, 0, 3, 6, 24,
  37. 0, 4, 8, 32, 0, 5, 10, 40, 0, 6, 12, 48, 0, 7, 14, 56,
  38. 0, 8, 16, 64, 0, 9, 18, 72, 0, 10, 20, 80, 0, 11, 22, 88,
  39. 0, 12, 24, 96, 0, 13, 26, 104, 0, 14, 28, 112, 0, 15, 30, 120
  40. ]);
  41. var result = new Uint8Array(expected.length);
  42. decoder.decodeVertexBuffer(result, 16, 4, encoded);
  43. assert.deepStrictEqual(result, expected);
  44. },
  45. decodeVertexBuffer_Mode2: function() {
  46. var encoded = new Uint8Array([
  47. 0xa0, 0x02, 0x08, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x02, 0x0a, 0xaa, 0xaa, 0xaa,
  48. 0xaa, 0xaa, 0xaa, 0xaa, 0x02, 0x0c, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x02, 0x0e,
  49. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. ]);
  53. var expected = new Uint8Array([
  54. 0, 0, 0, 0, 4, 5, 6, 7, 8, 10, 12, 14, 12, 15, 18, 21,
  55. 16, 20, 24, 28, 20, 25, 30, 35, 24, 30, 36, 42, 28, 35, 42, 49,
  56. 32, 40, 48, 56, 36, 45, 54, 63, 40, 50, 60, 70, 44, 55, 66, 77,
  57. 48, 60, 72, 84, 52, 65, 78, 91, 56, 70, 84, 98, 60, 75, 90, 105
  58. ]);
  59. var result = new Uint8Array(expected.length);
  60. decoder.decodeVertexBuffer(result, 16, 4, encoded);
  61. assert.deepStrictEqual(result, expected);
  62. },
  63. decodeIndexBuffer16: function() {
  64. var encoded = new Uint8Array([
  65. 0xe0, 0xf0, 0x10, 0xfe, 0xff, 0xf0, 0x0c, 0xff, 0x02, 0x02, 0x02, 0x00, 0x76, 0x87, 0x56, 0x67,
  66. 0x78, 0xa9, 0x86, 0x65, 0x89, 0x68, 0x98, 0x01, 0x69, 0x00, 0x00,
  67. ]);
  68. var expected = new Uint16Array([
  69. 0, 1, 2, 2, 1, 3, 4, 6, 5, 7, 8, 9
  70. ]);
  71. var result = new Uint16Array(expected.length);
  72. decoder.decodeIndexBuffer(new Uint8Array(result.buffer), 12, 2, encoded);
  73. assert.deepEqual(result, expected);
  74. },
  75. decodeIndexBuffer32: function() {
  76. var encoded = new Uint8Array([
  77. 0xe0, 0xf0, 0x10, 0xfe, 0xff, 0xf0, 0x0c, 0xff, 0x02, 0x02, 0x02, 0x00, 0x76, 0x87, 0x56, 0x67,
  78. 0x78, 0xa9, 0x86, 0x65, 0x89, 0x68, 0x98, 0x01, 0x69, 0x00, 0x00,
  79. ]);
  80. var expected = new Uint32Array([
  81. 0, 1, 2, 2, 1, 3, 4, 6, 5, 7, 8, 9
  82. ]);
  83. var result = new Uint32Array(expected.length);
  84. decoder.decodeIndexBuffer(new Uint8Array(result.buffer), 12, 4, encoded);
  85. assert.deepStrictEqual(result, expected);
  86. },
  87. decodeIndexBufferV1: function() {
  88. var encoded = new Uint8Array([
  89. 0xe1, 0xf0, 0x10, 0xfe, 0x1f, 0x3d, 0x00, 0x0a, 0x00, 0x76, 0x87, 0x56, 0x67, 0x78, 0xa9, 0x86,
  90. 0x65, 0x89, 0x68, 0x98, 0x01, 0x69, 0x00, 0x00,
  91. ]);
  92. var expected = new Uint32Array([
  93. 0, 1, 2, 2, 1, 3, 0, 1, 2, 2, 1, 5, 2, 1, 4
  94. ]);
  95. var result = new Uint32Array(expected.length);
  96. decoder.decodeIndexBuffer(new Uint8Array(result.buffer), 15, 4, encoded);
  97. assert.deepStrictEqual(result, expected);
  98. },
  99. decodeIndexBufferV1_More: function() {
  100. var encoded = new Uint8Array([
  101. 0xe1, 0xf0, 0x10, 0xfe, 0xff, 0xf0, 0x0c, 0xff, 0x02, 0x02, 0x02, 0x00, 0x76, 0x87, 0x56, 0x67,
  102. 0x78, 0xa9, 0x86, 0x65, 0x89, 0x68, 0x98, 0x01, 0x69, 0x00, 0x00,
  103. ]);
  104. var expected = new Uint32Array([
  105. 0, 1, 2, 2, 1, 3, 4, 6, 5, 7, 8, 9
  106. ]);
  107. var result = new Uint32Array(expected.length);
  108. decoder.decodeIndexBuffer(new Uint8Array(result.buffer), 12, 4, encoded);
  109. assert.deepStrictEqual(result, expected);
  110. },
  111. decodeIndexBufferV1_3Edges: function() {
  112. var encoded = new Uint8Array([
  113. 0xe1, 0xf0, 0x20, 0x30, 0x40, 0x00, 0x76, 0x87, 0x56, 0x67, 0x78, 0xa9, 0x86, 0x65, 0x89,
  114. 0x68, 0x98, 0x01, 0x69, 0x00, 0x00,
  115. ]);
  116. var expected = new Uint32Array([
  117. 0, 1, 2, 1, 0, 3, 2, 1, 4, 0, 2, 5
  118. ]);
  119. var result = new Uint32Array(expected.length);
  120. decoder.decodeIndexBuffer(new Uint8Array(result.buffer), 12, 4, encoded);
  121. assert.deepStrictEqual(result, expected);
  122. },
  123. decodeIndexSequence: function() {
  124. var encoded = new Uint8Array([
  125. 0xd1, 0x00, 0x04, 0xcd, 0x01, 0x04, 0x07, 0x98, 0x1f, 0x00, 0x00, 0x00, 0x00,
  126. ]);
  127. var expected = new Uint32Array([
  128. 0, 1, 51, 2, 49, 1000
  129. ]);
  130. var result = new Uint32Array(expected.length);
  131. decoder.decodeIndexSequence(new Uint8Array(result.buffer), 6, 4, encoded);
  132. assert.deepStrictEqual(result, expected);
  133. },
  134. decodeFilterOct8: function() {
  135. var encoded = new Uint8Array([
  136. 0xa0, 0x01, 0x07, 0x00, 0x00, 0x00, 0x1e, 0x01, 0x3f, 0x00, 0x00, 0x00, 0x8b, 0x8c, 0xfd, 0x00,
  137. 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x01, 0x7f, 0x00,
  140. ]);
  141. var expected = new Uint8Array([
  142. 0, 1, 127, 0,
  143. 0, 159, 82, 1,
  144. 255, 1, 127, 0,
  145. 1, 130, 241, 1,
  146. ]);
  147. var result = new Uint8Array(expected.length);
  148. decoder.decodeVertexBuffer(new Uint8Array(result.buffer), 4, 4, encoded, /* filter= */ 1);
  149. assert.deepStrictEqual(result, expected);
  150. },
  151. decodeFilterOct12: function() {
  152. var encoded = new Uint8Array([
  153. 0xa0, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x3d, 0x5a, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x0e, 0x0d, 0x01,
  154. 0x3f, 0x00, 0x00, 0x00, 0x9a, 0x99, 0x26, 0x01, 0x3f, 0x00, 0x00, 0x00, 0x0e, 0x0d, 0x0a, 0x00,
  155. 0x00, 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. 0x00, 0x01, 0x00, 0xff, 0x07, 0x00, 0x00,
  158. ]);
  159. var expected = new Uint16Array([
  160. 0, 16, 32767, 0,
  161. 0, 32621, 3088, 1,
  162. 32764, 16, 471, 0,
  163. 307, 28541, 16093, 1,
  164. ]);
  165. var result = new Uint16Array(expected.length);
  166. decoder.decodeVertexBuffer(new Uint8Array(result.buffer), 4, 8, encoded, /* filter= */ 1);
  167. assert.deepStrictEqual(result, expected);
  168. },
  169. decodeFilterQuat12: function() {
  170. var encoded = new Uint8Array([
  171. 0xa0, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x3d, 0x5a, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x0e, 0x0d, 0x01,
  172. 0x3f, 0x00, 0x00, 0x00, 0x9a, 0x99, 0x26, 0x01, 0x3f, 0x00, 0x00, 0x00, 0x0e, 0x0d, 0x0a, 0x00,
  173. 0x00, 0x01, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x01, 0x00, 0x00, 0x00, 0xfc, 0x07,
  176. ]);
  177. var expected = new Uint16Array([
  178. 32767, 0, 11, 0,
  179. 0, 25013, 0, 21166,
  180. 11, 0, 23504, 22830,
  181. 158, 14715, 0, 29277,
  182. ]);
  183. var result = new Uint16Array(expected.length);
  184. decoder.decodeVertexBuffer(new Uint8Array(result.buffer), 4, 8, encoded, /* filter= */ 2);
  185. assert.deepStrictEqual(result, expected);
  186. },
  187. decodeFilterExp: function() {
  188. var encoded = new Uint8Array([
  189. 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0xff, 0xf7, 0xff, 0xff, 0x02, 0xff, 0xff, 0x7f,
  192. 0xfe,
  193. ]);
  194. var expected = new Uint32Array([
  195. 0,
  196. 0x3fc00000,
  197. 0xc2100000,
  198. 0x49fffffe,
  199. ]);
  200. var result = new Uint32Array(expected.length);
  201. decoder.decodeVertexBuffer(new Uint8Array(result.buffer), 1, 16, encoded, /* filter= */ 3);
  202. assert.deepStrictEqual(result, expected);
  203. },
  204. };
  205. decoder.ready.then(() => {
  206. var count = 0;
  207. for (var key in tests) {
  208. tests[key]();
  209. count++;
  210. }
  211. console.log(count, 'tests passed');
  212. });