sparsearray.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 a = new Array(0x15000); //makes this sparse
  6. var i=0;
  7. for(var i=50;i<60;i++)
  8. {
  9. a[i] = i+10;
  10. }
  11. for(var i=0;i<10;i++)
  12. {
  13. a[i] = i+20;
  14. }
  15. for(var i=100;i<110;i++)
  16. {
  17. a[i] = i*10;
  18. }
  19. var b = new Array(0x15000); //makes this sparse
  20. for(var i=50;i<60;i++)
  21. {
  22. a[i] = i+10;
  23. }
  24. for(var i=0;i<10;i++)
  25. {
  26. a[i] = i+20;
  27. }
  28. for(var i=100;i<110;i++)
  29. {
  30. a[i] = i+40;
  31. }
  32. var c = a.concat(b);
  33. WScript.Echo(c[50]);
  34. WScript.Echo(c[0]);
  35. WScript.Echo(a.shift());
  36. WScript.Echo(a[7]);
  37. WScript.Echo(a[8]);
  38. WScript.Echo(a.shift());
  39. WScript.Echo(a.length);
  40. var d = a.slice(10);
  41. WScript.Echo(d[41]);
  42. WScript.Echo(d[90]);
  43. a.splice(45,3,"a","b","c");
  44. WScript.Echo(a[45]);
  45. WScript.Echo(a[46]);
  46. WScript.Echo(a[50]);
  47. WScript.Echo(a[100]);
  48. WScript.Echo(a.length);
  49. var x = [];
  50. x[0xFFFFFFFF] = 0;
  51. WScript.Echo(x[0xFFFFFFFF], x.length);
  52. x[0xFFFFFFFE] = 1;
  53. WScript.Echo(x[0xFFFFFFFE], x.length === 0xFFFFFFFF);
  54. x[0xFFFFFFFD] = 2;
  55. WScript.Echo(x[0xFFFFFFFD], x.length === 0xFFFFFFFF);
  56. function errors() {
  57. try {
  58. var e1 = new Array(-0.0);
  59. WScript.Echo("[-0.0].length = " + e1.length);
  60. }
  61. catch(e) {
  62. WScript.Echo("[-0.0]: error");
  63. }
  64. try {
  65. var e1 = new Array(-0.01);
  66. WScript.Echo("[-0.01].length = " + e1.length);
  67. }
  68. catch(e) {
  69. WScript.Echo("[-0.01]: error");
  70. }
  71. try {
  72. var e1 = new Array(0.01);
  73. WScript.Echo("[0.01].length = " + e1.length);
  74. }
  75. catch(e) {
  76. WScript.Echo("[0.01]: error");
  77. }
  78. try {
  79. var e1 = new Array(4294967295.0);
  80. WScript.Echo("[4294967295.0].length = " + e1.length);
  81. }
  82. catch(e) {
  83. WScript.Echo("[4294967295.0]: error");
  84. }
  85. try {
  86. var e1 = new Array(4294967294.9);
  87. WScript.Echo("[4294967294.9].length = " + e1.length);
  88. }
  89. catch(e) {
  90. WScript.Echo("[4294967294.9]: error");
  91. }
  92. try {
  93. var e1 = new Array(4294967296.0);
  94. WScript.Echo("[4294967296.0].length = " + e1.length);
  95. }
  96. catch(e) {
  97. WScript.Echo("[4294967296.0]: error");
  98. }
  99. }
  100. // Very special case of a sparse array with single element at index > 0x7fffffff.
  101. var sparse = [];
  102. sparse[0x80000000] = "hello";
  103. if (sparse[1] != undefined)
  104. WScript.Echo("Bad element in sparse array");
  105. sparse[0] = "here i am";
  106. WScript.Echo(sparse[0]);
  107. errors();
  108. // Regress Win8 611708
  109. (function () {
  110. WScript.Echo("Win8 611708: bound size to max array length");
  111. var a = [];
  112. a[0xfffffffd] = 1; // A segment at boundary
  113. a[0] = 0; // Change "last" accessed segment
  114. a[0xfffffffe] = 2; // Trigger the original assertion
  115. // Some more verification
  116. a[0xffffffff] = 3;
  117. for (var i = 0; i < 10; i++) {
  118. a[0x100000000 + i] = i;
  119. }
  120. WScript.Echo("length:", a.length);
  121. for (var p in a) {
  122. WScript.Echo(p + ":", a[p]);
  123. }
  124. })();