inversionmap_test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.structs.InversionMapTest');
  15. goog.setTestOnly('goog.structs.InversionMapTest');
  16. goog.require('goog.structs.InversionMap');
  17. goog.require('goog.testing.jsunit');
  18. function testInversionWithDelta() {
  19. var alphabetNames = new goog.structs.InversionMap(
  20. [0, 97, 1, 1, 1, 20, 1, 1, 1],
  21. [
  22. null, 'LATIN SMALL LETTER A', 'LATIN SMALL LETTER B',
  23. 'LATIN SMALL LETTER C', null, 'LATIN SMALL LETTER X',
  24. 'LATIN SMALL LETTER Y', 'LATIN SMALL LETTER Z', null
  25. ],
  26. true);
  27. assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
  28. assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
  29. assertEquals(null, alphabetNames.at(140));
  30. assertEquals(null, alphabetNames.at(0));
  31. }
  32. function testInversionWithoutDelta() {
  33. var alphabetNames = new goog.structs.InversionMap(
  34. [0, 97, 98, 99, 100, 120, 121, 122, 123],
  35. [
  36. null, 'LATIN SMALL LETTER A', 'LATIN SMALL LETTER B',
  37. 'LATIN SMALL LETTER C', null, 'LATIN SMALL LETTER X',
  38. 'LATIN SMALL LETTER Y', 'LATIN SMALL LETTER Z', null
  39. ],
  40. false);
  41. assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
  42. assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
  43. assertEquals(null, alphabetNames.at(140));
  44. assertEquals(null, alphabetNames.at(0));
  45. }
  46. function testInversionWithoutDeltaNoOpt() {
  47. var alphabetNames =
  48. new goog.structs.InversionMap([0, 97, 98, 99, 100, 120, 121, 122, 123], [
  49. null, 'LATIN SMALL LETTER A', 'LATIN SMALL LETTER B',
  50. 'LATIN SMALL LETTER C', null, 'LATIN SMALL LETTER X',
  51. 'LATIN SMALL LETTER Y', 'LATIN SMALL LETTER Z', null
  52. ]);
  53. assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
  54. assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
  55. assertEquals(null, alphabetNames.at(140));
  56. assertEquals(null, alphabetNames.at(0));
  57. }
  58. function testInversionMapSplice1() {
  59. var alphabetNames = newAsciiMap();
  60. alphabetNames.spliceInversion([99, 105, 114], ['XXX', 'YYY', 'ZZZ']);
  61. assertEquals('LATIN SMALL LETTER B', alphabetNames.at(98));
  62. assertEquals('XXX', alphabetNames.at(100));
  63. assertEquals('ZZZ', alphabetNames.at(114));
  64. assertEquals('ZZZ', alphabetNames.at(119));
  65. assertEquals('LATIN SMALL LETTER X', alphabetNames.at(120));
  66. }
  67. function testInversionMapSplice2() {
  68. var alphabetNames = newAsciiMap();
  69. alphabetNames.spliceInversion([105, 114, 121], ['XXX', 'YYY', 'ZZZ']);
  70. assertEquals(null, alphabetNames.at(104));
  71. assertEquals('XXX', alphabetNames.at(105));
  72. assertEquals('YYY', alphabetNames.at(120));
  73. assertEquals('ZZZ', alphabetNames.at(121));
  74. assertEquals('LATIN SMALL LETTER Z', alphabetNames.at(122));
  75. }
  76. function testInversionMapSplice3() {
  77. var alphabetNames = newAsciiMap();
  78. alphabetNames.spliceInversion(
  79. [98, 99], ['CHANGED LETTER B', 'CHANGED LETTER C']);
  80. assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
  81. assertEquals('CHANGED LETTER B', alphabetNames.at(98));
  82. assertEquals('CHANGED LETTER C', alphabetNames.at(99));
  83. assertEquals('LATIN SMALL LETTER D', alphabetNames.at(100));
  84. assertEquals(null, alphabetNames.at(101));
  85. }
  86. function testInversionMapSplice4() {
  87. var alphabetNames = newAsciiMap();
  88. alphabetNames.spliceInversion(
  89. [98, 1], ['CHANGED LETTER B', 'CHANGED LETTER C'], true /* delta mode */);
  90. assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
  91. assertEquals('CHANGED LETTER B', alphabetNames.at(98));
  92. assertEquals('CHANGED LETTER C', alphabetNames.at(99));
  93. assertEquals('LATIN SMALL LETTER D', alphabetNames.at(100));
  94. assertEquals(null, alphabetNames.at(101));
  95. }
  96. function testInversionMapSplice5() {
  97. var map = new goog.structs.InversionMap([0, 97, 98, 99], [
  98. null, 'LATIN SMALL LETTER A', 'LATIN SMALL LETTER B', 'LATIN SMALL LETTER C'
  99. ]);
  100. map.spliceInversion([98], ['CHANGED LETTER B']);
  101. assertEquals('LATIN SMALL LETTER A', map.at(97));
  102. assertEquals('CHANGED LETTER B', map.at(98));
  103. assertEquals('LATIN SMALL LETTER C', map.at(99));
  104. assertArrayEquals([0, 97, 98, 99], map.rangeArray);
  105. }
  106. function testInversionMapSpliceLarge() {
  107. var map = new goog.structs.InversionMap(
  108. [0, 99, 100, 101], [null, true, false, null]);
  109. var rangeArray = [];
  110. var values = [];
  111. for (var i = 100, value = true; i < 1000000; i++, value = !value) {
  112. rangeArray.push(i);
  113. values.push(value);
  114. }
  115. map.spliceInversion(rangeArray, values);
  116. assertEquals(null, map.at(98));
  117. assertEquals(true, map.at(99));
  118. assertEquals(true, map.at(100));
  119. assertEquals(false, map.at(101));
  120. assertEquals(true, map.at(102));
  121. assertEquals(false, map.at(103));
  122. assertEquals(false, map.at(999997));
  123. assertEquals(true, map.at(999998));
  124. assertEquals(false, map.at(999999));
  125. assertEquals(false, map.at(1000000));
  126. }
  127. function newAsciiMap() {
  128. return new goog.structs.InversionMap(
  129. [0, 97, 98, 99, 100, 101, 120, 121, 122, 123], [
  130. null, 'LATIN SMALL LETTER A', 'LATIN SMALL LETTER B',
  131. 'LATIN SMALL LETTER C', 'LATIN SMALL LETTER D', null,
  132. 'LATIN SMALL LETTER X', 'LATIN SMALL LETTER Y', 'LATIN SMALL LETTER Z',
  133. null
  134. ]);
  135. }