array_sparse_test.go 894 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package goja
  2. import "testing"
  3. func TestSparseArraySetLengthWithPropItems(t *testing.T) {
  4. const SCRIPT = `
  5. var a = [1,2,3,4];
  6. a[100000] = 5;
  7. var thrown = false;
  8. Object.defineProperty(a, "2", {value: 42, configurable: false, writable: false});
  9. try {
  10. Object.defineProperty(a, "length", {value: 0, writable: false});
  11. } catch (e) {
  12. thrown = e instanceof TypeError;
  13. }
  14. thrown && a.length === 3;
  15. `
  16. testScript1(SCRIPT, valueTrue, t)
  17. }
  18. func TestSparseArraySwitch(t *testing.T) {
  19. const SCRIPT = `
  20. var a = [];
  21. a[20470] = 5; // switch to sparse
  22. for (var i = a.length - 1; i >= 0; i--) {
  23. a[i] = i; // switch to normal at some point
  24. }
  25. if (a.length != 20471) {
  26. throw new Error("Invalid length: " + a.length);
  27. }
  28. for (var i = 0; i < a.length; i++) {
  29. if (a[i] !== i) {
  30. throw new Error("Invalid value at " + i + ": " + a[i]);
  31. }
  32. }
  33. `
  34. testScript1(SCRIPT, _undefined, t)
  35. }