lastindexof.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var sevenBitStr = "abcdef";
  7. var eightBitStr = "ÄÜÖABC";
  8. var unicodeStr = "\u0100\u0111\u0112\u0113ab";
  9. var tests = [
  10. {
  11. name: "Empty strings",
  12. body: function () {
  13. var sevenBitStr = "aabbccddaabbccdd";
  14. var eightBitStr = "ÄÜÖABCÄÜÖ";
  15. var unicodeStr = "\u0100\u0111\u0112\u0113abc\u0100\u0111\u0112\u0113";
  16. assert.areEqual(0, "".lastIndexOf(""), "Matching \"\" with \"\" at default offset returns 0");
  17. assert.areEqual(0, "".lastIndexOf("", 0), "Matching \"\" with \"\" at offset 0 returns 0");
  18. assert.areEqual(0, "".lastIndexOf("", 1), "Matching \"\" with \"\" at offset 1 returns 0");
  19. assert.areEqual(0, "".lastIndexOf("", 2), "Matching \"\" with \"\" at offset 2 returns 0");
  20. assert.areEqual(0, "".lastIndexOf("", -1), "Matching \"\" with \"\" at offset -1 returns 0");
  21. assert.areEqual(0, "".lastIndexOf("", -2), "Matching \"\" with \"\" at offset -2 returns 0");
  22. for (var str of [new String(sevenBitStr), new String(eightBitStr), new String(unicodeStr)]) {
  23. assert.areEqual(str.length, str.lastIndexOf(""), "Matching \"" + str + "\" with \"\" at default offset returns the length of the string");
  24. for (var i = 0; i < str.length; ++i) {
  25. assert.areEqual(i, str.lastIndexOf("", i), "Matching \"" + str + "\" with \"\" at offset " + i + " returns the length of the string");
  26. }
  27. }
  28. }
  29. },
  30. {
  31. name: "Negative matches",
  32. body: function () {
  33. for (var str of [new String(sevenBitStr), new String(eightBitStr), new String(unicodeStr)]) {
  34. var doubleStr = str + str;
  35. assert.areEqual(-1, str.lastIndexOf(doubleStr), "Matching \"" + str + "\" with \"" + doubleStr + "\" at default offset returns -1");
  36. for (var match of ["z", "zz", "zå", "åå", "å"]) {
  37. assert.areEqual(-1, str.lastIndexOf(match), "Matching \"" + str + "\" with \"" + match + "\" at default offset returns -1");
  38. for (var i = 0; i < str.length; ++i) {
  39. assert.areEqual(-1, str.lastIndexOf("z", i), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " returns -1");
  40. }
  41. }
  42. }
  43. }
  44. },
  45. {
  46. name: "Single char matching",
  47. body: function () {
  48. for (var str of [new String(sevenBitStr), new String(eightBitStr), new String(unicodeStr)]) {
  49. for (var i = 0; i < str.length; ++i) {
  50. var c = str[i];
  51. if (i > 0) {
  52. assert.areEqual(-1, str.lastIndexOf(c, -2), "Matching \"" + str + "\" with \"" + c + "\" at offset -2 returns -1");
  53. assert.areEqual(-1, str.lastIndexOf(c, -1), "Matching \"" + str + "\" with \"" + c + "\" at offset -1 returns -1");
  54. }
  55. assert.areEqual(i, str.lastIndexOf(c), "Matching \"" + str + "\" with \"" + c + "\" at default offset returns " + i);
  56. assert.areEqual(i, str.lastIndexOf(c, str.length - 1), "Matching \"" + str + "\" with \"" + c + "\" at default offset returns " + i);
  57. assert.areEqual(i, str.lastIndexOf(c, str.length), "Matching \"" + str + "\" with \"" + c + "\" at default offset returns " + i);
  58. assert.areEqual(i, str.lastIndexOf(c, str.length + 1), "Matching \"" + str + "\" with \"" + c + "\" at default offset returns " + i);
  59. // Match character at each possible position in the string. If we give an offset before the position of the character, we expect -1.
  60. for (var j = 0; j < str.length; ++j) {
  61. var expected = (pos) => pos < i ? -1 : i;
  62. assert.areEqual(expected(j), str.lastIndexOf(c, j), "Matching \"" + str + "\" with \"" + c + "\" at offset " + j + " returns " + expected(i));
  63. }
  64. }
  65. }
  66. }
  67. },
  68. {
  69. name: "Substring matching",
  70. body: function () {
  71. for (var str of [new String(sevenBitStr), new String(eightBitStr), new String(unicodeStr)]) {
  72. for (var i = 0; i < str.length; ++i) {
  73. var match = str.substring(i);
  74. assert.areEqual(i, str.lastIndexOf(match), "Matching \"" + str + "\" with \"" + match + "\" at default offset returns " + i);
  75. assert.areEqual(i, str.lastIndexOf(match, i), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " returns " + i);
  76. assert.areEqual(i, str.lastIndexOf(match, i + 1), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " + 1 returns " + i);
  77. assert.areEqual(i, str.lastIndexOf(match, i + 2), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " + 2 returns " + i);
  78. assert.areEqual(i, str.lastIndexOf(match, str.length - 1), "Matching \"" + str + "\" with \"" + match + "\" at offset " + (str.length - 1) + " returns " + i);
  79. assert.areEqual(i, str.lastIndexOf(match, str.length), "Matching \"" + str + "\" with \"" + match + "\" at offset " + str.length + " returns " + i);
  80. assert.areEqual(i, str.lastIndexOf(match, str.length + 1), "Matching \"" + str + "\" with \"" + match + "\" at offset " + (str.length + 1) + " returns " + i);
  81. if (i > 0) {
  82. assert.areEqual(-1, str.lastIndexOf(match, i - 1), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " - 1 returns -1");
  83. }
  84. if (i > 1) {
  85. assert.areEqual(-1, str.lastIndexOf(match, i - 2), "Matching \"" + str + "\" with \"" + match + "\" at offset " + i + " - 2 returns -1");
  86. }
  87. }
  88. }
  89. }
  90. }
  91. ];
  92. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });