ManagedArraySpec.js 5.24 KB
Newer Older
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
import { ManagedArray } from "../../Source/Cesium.js";

describe("Core/ManagedArray", function () {
  function expectTrailingReferenceToBeRemoved(managedArray) {
    var array = managedArray._array;
    var length = managedArray._length;
    var reservedLength = array.length;
    for (var i = length; i < reservedLength; ++i) {
      expect(array[i]).toBeUndefined();
    }
  }

  it("constructor has expected default values", function () {
    var array = new ManagedArray();
    expect(array.length).toEqual(0);
  });

  it("constructor initializes length", function () {
    var array = new ManagedArray(10);
    expect(array.length).toEqual(10);
    expect(array.values.length).toEqual(10);
  });

  it("can get and set values", function () {
    var length = 10;
    var array = new ManagedArray(length);
    var i;
    for (i = 0; i < length; ++i) {
      array.set(i, i * i);
    }
    for (i = 0; i < length; ++i) {
      expect(array.get(i)).toEqual(i * i);
      expect(array.values[i]).toEqual(i * i);
    }
  });

  it("get throws if index does not exist", function () {
    var array = new ManagedArray();
    array.reserve(5);
    expect(array.values.length).toEqual(5);
    expect(function () {
      array.get(5);
    }).toThrowDeveloperError();
  });

  it("set throws if index invalid", function () {
    var array = new ManagedArray();
    array.resize(10);
    expect(function () {
      array.set(undefined, 5);
    }).toThrowDeveloperError();
  });

  it("length setter throws if length is less than 0", function () {
    var array = new ManagedArray();
    expect(function () {
      array.length = -1;
    }).toThrowDeveloperError();
  });

  it("set resizes array", function () {
    var array = new ManagedArray();
    array.set(0, "a");
    expect(array.length).toEqual(1);
    array.set(5, "b");
    expect(array.length).toEqual(6);
    array.set(2, "c");
    expect(array.length).toEqual(6);
  });

  it("peeks at the last element of the array", function () {
    var array = new ManagedArray();
    expect(array.peek()).toBeUndefined();
    array.push(0);
    expect(array.peek()).toBe(0);
    array.push(1);
    array.push(2);
    expect(array.peek()).toBe(2);
  });

  it("can push values", function () {
    var array = new ManagedArray();
    var length = 10;
    for (var i = 0; i < length; ++i) {
      var val = Math.random();
      array.push(val);
      expect(array.length).toEqual(i + 1);
      expect(array.values.length).toEqual(i + 1);
      expect(array.get(i)).toEqual(val);
      expect(array.values[i]).toEqual(val);
    }
  });

  it("can pop values", function () {
    var length = 10;
    var array = new ManagedArray(length);
    var i;
    for (i = 0; i < length; ++i) {
      array.set(i, Math.random());
    }
    for (i = length - 1; i >= 0; --i) {
      var val = array.get(i);
      expect(array.pop()).toEqual(val);
      expect(array.length).toEqual(i);
      expect(array.values.length).toEqual(length);
    }
  });

  it("pop removes trailing references", function () {
    var length = 10;
    var array = new ManagedArray(length);
    array.set(0, Math.random());
    array.set(1, Math.random());
    array.set(2, Math.random());
    array.pop();
    array.pop();
    expectTrailingReferenceToBeRemoved(array);
  });

  it("pop returns undefined if array is empty", function () {
    var array = new ManagedArray();
    array.push(1);
    expect(array.pop()).toBe(1);
    expect(array.pop()).toBeUndefined();
  });

  it("reserve throws if length is less than 0", function () {
    var array = new ManagedArray();
    expect(function () {
      array.reserve(-1);
    }).toThrowDeveloperError();
  });

  it("reserve", function () {
    var array = new ManagedArray(2);
    array.reserve(10);
    expect(array.values.length).toEqual(10);
    expect(array.length).toEqual(2);
    array.reserve(20);
    expect(array.values.length).toEqual(20);
    expect(array.length).toEqual(2);
    array.reserve(5);
    expect(array.values.length).toEqual(20);
    expect(array.length).toEqual(2);
  });

  it("resize throws if length is less than 0", function () {
    var array = new ManagedArray();
    expect(function () {
      array.resize(-1);
    }).toThrowDeveloperError();
  });

  it("resize", function () {
    var array = new ManagedArray(2);
    array.resize(10);
    expect(array.values.length).toEqual(10);
    expect(array.length).toEqual(10);
    array.resize(20);
    expect(array.values.length).toEqual(20);
    expect(array.length).toEqual(20);
    array.resize(5);
    expect(array.values.length).toEqual(20);
    expect(array.length).toEqual(5);
  });

  it("resize removes trailing references", function () {
    var length = 10;
    var array = new ManagedArray(length);
    array.set(0, Math.random());
    array.set(1, Math.random());
    array.set(2, Math.random());
    array.resize(1);
    expectTrailingReferenceToBeRemoved(array);
  });

  it("trim", function () {
    var array = new ManagedArray(2);
    array.reserve(10);
    expect(array.length).toEqual(2);
    expect(array.values.length).toEqual(10);
    array.trim();
    expect(array.values.length).toEqual(2);
    array.trim(5);
    expect(array.length).toEqual(2);
    expect(array.values.length).toEqual(5);
    array.trim(3);
    expect(array.length).toEqual(2);
    expect(array.values.length).toEqual(3);
  });
});