array_indexOf.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var x = [1, 2, 2, 4, 5, +0, -0, NaN, 0, true, true , false]
  6. for(i=-3; i < 15;i++)
  7. {
  8. WScript.Echo(x.indexOf(i));
  9. for(j=-3; j< 15;j++)
  10. {
  11. WScript.Echo(x.indexOf(x[i],j));
  12. WScript.Echo(x.indexOf(i,j));
  13. }
  14. }
  15. WScript.Echo(x.indexOf(-0, -0));
  16. var b = function(){};
  17. b.prototype = Array.prototype;
  18. var y = new b();
  19. var z = new Object();
  20. var a = new Object();
  21. y[0] = "abc";
  22. y[1] = "def";
  23. y[2] = "efg";
  24. y[3] = true;
  25. y[4] = true;
  26. y[5] = false;
  27. y[6] = a;
  28. y[7] = a;
  29. y[8] = null;
  30. y.length = 10;
  31. WScript.Echo(y.indexOf("abc"));
  32. WScript.Echo(y.indexOf("abc", 3));
  33. WScript.Echo(y.indexOf("abc", 2));
  34. WScript.Echo(y.indexOf("abc", -2));
  35. WScript.Echo(y.indexOf("efg"));
  36. WScript.Echo(y.indexOf("efg", 6));
  37. WScript.Echo(y.indexOf("efg", 1));
  38. WScript.Echo(y.indexOf("efg", -3));
  39. WScript.Echo(y.indexOf("xyg"));
  40. WScript.Echo(y.indexOf("esg", 2));
  41. WScript.Echo(y.indexOf("eag", 2));
  42. WScript.Echo(y.indexOf("", -2));
  43. WScript.Echo(y.indexOf(true));
  44. WScript.Echo(y.indexOf(false));
  45. WScript.Echo(y.indexOf(new Boolean(true)));
  46. WScript.Echo(y.indexOf(a , 6));
  47. WScript.Echo(y.indexOf(a , 1));
  48. WScript.Echo(y.indexOf(a ));
  49. WScript.Echo(y.indexOf(b));
  50. WScript.Echo(y.indexOf(null));
  51. WScript.Echo(y.indexOf());
  52. //implicit calls
  53. var a ;
  54. var arr = [10];
  55. Object.defineProperty(Array.prototype, "4", {configurable : true, get: function(){a = true; return 30;}});
  56. a = false;
  57. arr.length = 6;
  58. var f = arr.indexOf(30);
  59. WScript.Echo(a);
  60. //Float array with gaps
  61. var floatArray = new Array(5.5, 5.6);
  62. floatArray[6] = 5.6;
  63. WScript.Echo(floatArray.indexOf(5.7));
  64. // Cases where we do/don't have to resume after failing to find the value in the head segment.
  65. // Run with -forcearraybtree to really stress these.
  66. var gap = [0, 1];
  67. WScript.Echo(gap.indexOf(4));
  68. Array.prototype[2] = 'foo';
  69. WScript.Echo(gap.indexOf('foo'));
  70. gap[5] = 4;
  71. WScript.Echo(gap.indexOf('foo'));
  72. WScript.Echo(gap.indexOf(4));
  73. gap = [0, 1.1];
  74. WScript.Echo(gap.indexOf(4));
  75. Array.prototype[2] = 'bar';
  76. WScript.Echo(gap.indexOf('bar'));
  77. gap[5] = 4;
  78. WScript.Echo(gap.indexOf(4));
  79. WScript.Echo(gap.indexOf('bar'));
  80. gap = [0, 'test'];
  81. WScript.Echo(gap.indexOf(4));
  82. Array.prototype[2] = 4;
  83. WScript.Echo(gap.indexOf(4));
  84. gap[5] = 4;
  85. WScript.Echo(gap.indexOf(4));
  86. delete Array.prototype[2]
  87. WScript.Echo(gap.indexOf(4));
  88. var undefinedValues = [];
  89. undefinedValues[9] = "abc";
  90. undefinedValues[10] = undefined;
  91. WScript.Echo(undefinedValues.indexOf(undefined));
  92. undefinedValues.length = 8;
  93. WScript.Echo(undefinedValues.indexOf(undefined));
  94. WScript.Echo(Array.prototype.indexOf.prototype === undefined);
  95. WScript.Echo("prototype" in Array.prototype.indexOf)