Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

244 строки
7.4 KiB

  1. const assert = require('assert');
  2. const UnicodeTrieBuilder = require('../builder');
  3. const UnicodeTrie = require('../');
  4. describe('unicode trie', () => {
  5. it('set', () => {
  6. const trie = new UnicodeTrieBuilder(10, 666);
  7. trie.set(0x4567, 99);
  8. assert.equal(trie.get(0x4566), 10);
  9. assert.equal(trie.get(0x4567), 99);
  10. assert.equal(trie.get(-1), 666);
  11. assert.equal(trie.get(0x110000), 666);
  12. });
  13. it('set -> compacted trie', () => {
  14. const t = new UnicodeTrieBuilder(10, 666);
  15. t.set(0x4567, 99);
  16. const trie = t.freeze();
  17. assert.equal(trie.get(0x4566), 10);
  18. assert.equal(trie.get(0x4567), 99);
  19. assert.equal(trie.get(-1), 666);
  20. assert.equal(trie.get(0x110000), 666);
  21. });
  22. it('setRange', () => {
  23. const trie = new UnicodeTrieBuilder(10, 666);
  24. trie.setRange(13, 6666, 7788, false);
  25. trie.setRange(6000, 7000, 9900, true);
  26. assert.equal(trie.get(12), 10);
  27. assert.equal(trie.get(13), 7788);
  28. assert.equal(trie.get(5999), 7788);
  29. assert.equal(trie.get(6000), 9900);
  30. assert.equal(trie.get(7000), 9900);
  31. assert.equal(trie.get(7001), 10);
  32. assert.equal(trie.get(0x110000), 666);
  33. });
  34. it('setRange -> compacted trie', () => {
  35. const t = new UnicodeTrieBuilder(10, 666);
  36. t.setRange(13, 6666, 7788, false);
  37. t.setRange(6000, 7000, 9900, true);
  38. const trie = t.freeze();
  39. assert.equal(trie.get(12), 10);
  40. assert.equal(trie.get(13), 7788);
  41. assert.equal(trie.get(5999), 7788);
  42. assert.equal(trie.get(6000), 9900);
  43. assert.equal(trie.get(7000), 9900);
  44. assert.equal(trie.get(7001), 10);
  45. assert.equal(trie.get(0x110000), 666);
  46. });
  47. it('toBuffer written in little-endian', () => {
  48. const trie = new UnicodeTrieBuilder();
  49. trie.set(0x4567, 99);
  50. const buf = trie.toBuffer();
  51. const bufferExpected = new Buffer.from([0, 72, 0, 0, 0, 0, 0, 0, 128, 36, 0, 0, 123, 123, 206, 144, 235, 128, 2, 143, 67, 96, 225, 171, 23, 55, 54, 38, 231, 47, 44, 127, 233, 90, 109, 194, 92, 246, 126, 197, 131, 223, 31, 56, 102, 78, 154, 20, 108, 117, 88, 244, 93, 192, 190, 218, 229, 156, 12, 107, 86, 235, 125, 96, 102, 0, 129, 15, 239, 109, 219, 204, 58, 151, 92, 52, 126, 152, 198, 14, 0]);
  52. assert.equal(buf.toString('hex'), bufferExpected.toString('hex'));
  53. });
  54. it('should work with compressed serialization format', () => {
  55. const t = new UnicodeTrieBuilder(10, 666);
  56. t.setRange(13, 6666, 7788, false);
  57. t.setRange(6000, 7000, 9900, true);
  58. const buf = t.toBuffer();
  59. const trie = new UnicodeTrie(buf);
  60. assert.equal(trie.get(12), 10);
  61. assert.equal(trie.get(13), 7788);
  62. assert.equal(trie.get(5999), 7788);
  63. assert.equal(trie.get(6000), 9900);
  64. assert.equal(trie.get(7000), 9900);
  65. assert.equal(trie.get(7001), 10);
  66. assert.equal(trie.get(0x110000), 666);
  67. });
  68. const rangeTests = [
  69. {
  70. ranges: [
  71. [ 0, 0, 0, 0 ],
  72. [ 0, 0x40, 0, 0 ],
  73. [ 0x40, 0xe7, 0x1234, 0 ],
  74. [ 0xe7, 0x3400, 0, 0 ],
  75. [ 0x3400, 0x9fa6, 0x6162, 0 ],
  76. [ 0x9fa6, 0xda9e, 0x3132, 0 ],
  77. [ 0xdada, 0xeeee, 0x87ff, 0 ],
  78. [ 0xeeee, 0x11111, 1, 0 ],
  79. [ 0x11111, 0x44444, 0x6162, 0 ],
  80. [ 0x44444, 0x60003, 0, 0 ],
  81. [ 0xf0003, 0xf0004, 0xf, 0 ],
  82. [ 0xf0004, 0xf0006, 0x10, 0 ],
  83. [ 0xf0006, 0xf0007, 0x11, 0 ],
  84. [ 0xf0007, 0xf0040, 0x12, 0 ],
  85. [ 0xf0040, 0x110000, 0, 0 ]
  86. ],
  87. check: [
  88. [ 0, 0 ],
  89. [ 0x40, 0 ],
  90. [ 0xe7, 0x1234 ],
  91. [ 0x3400, 0 ],
  92. [ 0x9fa6, 0x6162 ],
  93. [ 0xda9e, 0x3132 ],
  94. [ 0xdada, 0 ],
  95. [ 0xeeee, 0x87ff ],
  96. [ 0x11111, 1 ],
  97. [ 0x44444, 0x6162 ],
  98. [ 0xf0003, 0 ],
  99. [ 0xf0004, 0xf ],
  100. [ 0xf0006, 0x10 ],
  101. [ 0xf0007, 0x11 ],
  102. [ 0xf0040, 0x12 ],
  103. [ 0x110000, 0 ]
  104. ]
  105. },
  106. {
  107. // set some interesting overlapping ranges
  108. ranges: [
  109. [ 0, 0, 0, 0 ],
  110. [ 0x21, 0x7f, 0x5555, 1 ],
  111. [ 0x2f800, 0x2fedc, 0x7a, 1 ],
  112. [ 0x72, 0xdd, 3, 1 ],
  113. [ 0xdd, 0xde, 4, 0 ],
  114. [ 0x201, 0x240, 6, 1 ], // 3 consecutive blocks with the same pattern but
  115. [ 0x241, 0x280, 6, 1 ], // discontiguous value ranges, testing utrie2_enum()
  116. [ 0x281, 0x2c0, 6, 1 ],
  117. [ 0x2f987, 0x2fa98, 5, 1 ],
  118. [ 0x2f777, 0x2f883, 0, 1 ],
  119. [ 0x2f900, 0x2ffaa, 1, 0 ],
  120. [ 0x2ffaa, 0x2ffab, 2, 1 ],
  121. [ 0x2ffbb, 0x2ffc0, 7, 1 ]
  122. ],
  123. check: [
  124. [ 0, 0 ],
  125. [ 0x21, 0 ],
  126. [ 0x72, 0x5555 ],
  127. [ 0xdd, 3 ],
  128. [ 0xde, 4 ],
  129. [ 0x201, 0 ],
  130. [ 0x240, 6 ],
  131. [ 0x241, 0 ],
  132. [ 0x280, 6 ],
  133. [ 0x281, 0 ],
  134. [ 0x2c0, 6 ],
  135. [ 0x2f883, 0 ],
  136. [ 0x2f987, 0x7a ],
  137. [ 0x2fa98, 5 ],
  138. [ 0x2fedc, 0x7a ],
  139. [ 0x2ffaa, 1 ],
  140. [ 0x2ffab, 2 ],
  141. [ 0x2ffbb, 0 ],
  142. [ 0x2ffc0, 7 ],
  143. [ 0x110000, 0 ]
  144. ]
  145. },
  146. {
  147. // use a non-zero initial value
  148. ranges: [
  149. [ 0, 0, 9, 0 ], // non-zero initial value.
  150. [ 0x31, 0xa4, 1, 0 ],
  151. [ 0x3400, 0x6789, 2, 0 ],
  152. [ 0x8000, 0x89ab, 9, 1 ],
  153. [ 0x9000, 0xa000, 4, 1 ],
  154. [ 0xabcd, 0xbcde, 3, 1 ],
  155. [ 0x55555, 0x110000, 6, 1 ], // highStart<U+ffff with non-initialValue
  156. [ 0xcccc, 0x55555, 6, 1 ]
  157. ],
  158. check: [
  159. [ 0, 9 ], // non-zero initialValue
  160. [ 0x31, 9 ],
  161. [ 0xa4, 1 ],
  162. [ 0x3400, 9 ],
  163. [ 0x6789, 2 ],
  164. [ 0x9000, 9 ],
  165. [ 0xa000, 4 ],
  166. [ 0xabcd, 9 ],
  167. [ 0xbcde, 3 ],
  168. [ 0xcccc, 9 ],
  169. [ 0x110000, 6 ]
  170. ]
  171. },
  172. {
  173. // empty or single-value tries, testing highStart==0
  174. ranges: [
  175. [ 0, 0, 3, 0 ] // Only the element with the initial value.
  176. ],
  177. check: [
  178. [ 0, 3 ],
  179. [ 0x110000, 3 ]
  180. ]
  181. },
  182. {
  183. ranges: [
  184. [ 0, 0, 3, 0 ], // Initial value = 3
  185. [ 0, 0x110000, 5, 1 ]
  186. ],
  187. check: [
  188. [ 0, 3 ],
  189. [ 0x110000, 5 ]
  190. ]
  191. }
  192. ];
  193. it('should pass range tests', () => {
  194. const result = [];
  195. for (let test of rangeTests) {
  196. let initialValue = 0;
  197. let errorValue = 0x0bad;
  198. let i = 0;
  199. if (test.ranges[i][1] < 0) {
  200. errorValue = test.ranges[i][2];
  201. i++;
  202. }
  203. initialValue = test.ranges[i++][2];
  204. var trie = new UnicodeTrieBuilder(initialValue, errorValue);
  205. for (let range of test.ranges.slice(i)) {
  206. trie.setRange(range[0], range[1] - 1, range[2], range[3] !== 0);
  207. }
  208. var frozen = trie.freeze();
  209. var start = 0;
  210. result.push(test.check.map((check) => {
  211. let end;
  212. const result1 = [];
  213. for (start = start, end = check[0]; start < end; start++) {
  214. assert.equal(trie.get(start), check[1]);
  215. result1.push(assert.equal(frozen.get(start), check[1]));
  216. }
  217. return result1;
  218. }));
  219. }
  220. });
  221. });