basen_test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2007 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. /**
  15. * @fileoverview Tests for arbitrary base conversion library baseconversion.js.
  16. */
  17. goog.provide('goog.crypt.baseNTest');
  18. goog.setTestOnly('goog.crypt.baseNTest');
  19. goog.require('goog.crypt.baseN');
  20. goog.require('goog.testing.jsunit');
  21. function testDecToHex() {
  22. verifyConversion(
  23. goog.crypt.baseN.BASE_DECIMAL, '0',
  24. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '0');
  25. verifyConversion(
  26. goog.crypt.baseN.BASE_DECIMAL, '9',
  27. goog.crypt.baseN.BASE_UPPERCASE_HEXADECIMAL, '9');
  28. verifyConversion(
  29. goog.crypt.baseN.BASE_DECIMAL, '13',
  30. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, 'd');
  31. verifyConversion(
  32. goog.crypt.baseN.BASE_DECIMAL, '255',
  33. goog.crypt.baseN.BASE_UPPERCASE_HEXADECIMAL, 'FF');
  34. verifyConversion(
  35. goog.crypt.baseN.BASE_DECIMAL, '53425987345897',
  36. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '309734ff5de9');
  37. verifyConversion(
  38. goog.crypt.baseN.BASE_DECIMAL, '987080888',
  39. goog.crypt.baseN.BASE_UPPERCASE_HEXADECIMAL, '3AD5A8B8');
  40. verifyConversion(
  41. goog.crypt.baseN.BASE_DECIMAL, '009341587237',
  42. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '22ccd4f25');
  43. }
  44. function testBinToDec() {
  45. verifyConversion(
  46. goog.crypt.baseN.BASE_BINARY,
  47. '11101010101000100010010000010010010000111101000100110111000000100001' +
  48. '01100100111110110010000010110100111101000010010100001011111011111100' +
  49. '00000010000010000101010101000000000101100000000100011111011101111001' +
  50. '10000001000000000100101110001001001101101001101111010101111100010001' +
  51. '11011100000110111000000100111011100100010010011001111011001111001011' +
  52. '10001000101111001010011101101100110110011110010000011100101011110010' +
  53. '11010001001111110011000000001001011011111011010000110011010000010111' +
  54. '10111100000001100010111100000100000000110001011101011110100000011010' +
  55. '0110000100011111',
  56. goog.crypt.baseN.BASE_DECIMAL,
  57. '34589745906769047354795784390596748934723904739085568907689045723489' +
  58. '05745789789078907890789023447892365623589745678902348976234598723459' +
  59. '087523496723486089723459078349087');
  60. }
  61. function testDecToBin() {
  62. verifyConversion(
  63. goog.crypt.baseN.BASE_DECIMAL,
  64. '00342589674590347859734908573490568347534805468907960579056785605496' +
  65. '83475873465859072390486756098742380573908572390463805745656623475234' +
  66. '82345670247851902784123897349486238502378940637925807378946358964328' +
  67. '57906148572346857346409823758034763928401296023947234784623765456367' +
  68. '764623627623574',
  69. goog.crypt.baseN.BASE_BINARY,
  70. '10010011011100101010001111100111001100110000110111111110010110101000' +
  71. '01010110110010000111000001100110100101010000101001100001011000101111' +
  72. '01011101111100101101010010000111011110011110010101111001110010100100' +
  73. '10111110000101111011010000000111111011110010011110101011100101000001' +
  74. '00011000101010011001101000011101001010001101011110101001011011100101' +
  75. '11100000101000010010101001011001100100101110111101010000011010001010' +
  76. '01011100100111110001100111100100011001001001100011011100100111011111' +
  77. '01000100101001000100110001011010010000011010111101111111111111110100' +
  78. '01100101001111001111100110101000001100100000111111100101110010111011' +
  79. '10110110001100100011101010110110100001001000101011001001100011010110' +
  80. '10110100000110000110010111110100000100110110010010010101111001001111' +
  81. '11100100000010101111110100011010011101011010001101110011100110111111' +
  82. '11000100001111010000000101011011000010010000000100111111010110111100' +
  83. '00101111010011011010011010010001000101100001111001110010010110');
  84. }
  85. function test7To9() {
  86. verifyConversion(
  87. '0123456', // Base 7.
  88. '60625646056660665666066534602566346056634560665606666656465634265434' +
  89. '66563465664566346406366534664656650660665623456663456654360665',
  90. '012345678', // Base 9.
  91. '11451222686557606458341381287142358175337801548087003804852781764284' +
  92. '273762357630423116743334671762638240652740158536');
  93. }
  94. function testZeros() {
  95. verifyConversion(
  96. goog.crypt.baseN.BASE_DECIMAL, '0',
  97. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '0');
  98. verifyConversion(
  99. goog.crypt.baseN.BASE_DECIMAL, '000',
  100. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '0');
  101. verifyConversion(
  102. goog.crypt.baseN.BASE_DECIMAL, '0000007',
  103. goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL, '7');
  104. }
  105. function testArbitraryBases() {
  106. verifyConversion(
  107. 'X9(', // Base 3.
  108. '9(XX((9X(XX9(9X9(X9(',
  109. 'a:*o9', // Base 5.
  110. ':oa**:9o9**9oo');
  111. }
  112. function testEmptyBases() {
  113. var e = assertThrows(function() {
  114. goog.crypt.baseN.recodeString('1230', '', '0123');
  115. });
  116. assertEquals(
  117. 'Exception message', 'Number 1230 contains a character ' +
  118. 'not found in base , which is 0',
  119. e.message);
  120. e = assertThrows(function() {
  121. goog.crypt.baseN.recodeString('1230', '0123', '');
  122. });
  123. assertEquals('Exception message', 'Empty output base', e.message);
  124. }
  125. function testInvalidDigits() {
  126. var e = assertThrows(function() {
  127. goog.crypt.baseN.recodeString('123x456', '01234567', '01234567');
  128. });
  129. assertEquals(
  130. 'Exception message', 'Number 123x456 contains a character ' +
  131. 'not found in base 01234567, which is x',
  132. e.message);
  133. }
  134. function makeHugeBase() {
  135. // Number of digits in the base.
  136. // Tests break if this is set to 200'000. The reason for that is
  137. // String.fromCharCode(196609) == String.fromCharCode(1).
  138. var baseSize = 20000;
  139. var tab = [];
  140. for (var i = 0; i < baseSize; i++) {
  141. tab.push(String.fromCharCode(i));
  142. }
  143. return tab.join('');
  144. }
  145. function testHugeInputBase() {
  146. verifyConversion(
  147. makeHugeBase(), String.fromCharCode(12345), goog.crypt.baseN.BASE_DECIMAL,
  148. '12345');
  149. }
  150. function testHugeOutputBase() {
  151. verifyConversion(
  152. goog.crypt.baseN.BASE_DECIMAL, '12345', makeHugeBase(),
  153. String.fromCharCode(12345));
  154. }
  155. function verifyConversion(inputBase, inputNumber, outputBase, outputNumber) {
  156. assertEquals(
  157. outputNumber,
  158. goog.crypt.baseN.recodeString(inputNumber, inputBase, outputBase));
  159. }