Package elemental.util

Examples of elemental.util.ArrayOfNumber


    }
    return array;
  }

  private static ArrayOfNumber arrayFrom(double... items) {
    final ArrayOfNumber array = Collections.arrayOfNumber();
    for (int i = 0, n = items.length; i < n; ++i) {
      array.push(items[i]);
    }
    return array;
  }
View Full Code Here


  /**
   * Tests {@link ArrayOfNumber}.
   */
  public void testArraysOfNumbers() {
    // This is our test subject.
    final ArrayOfNumber array = Collections.arrayOfNumber();

    // These are items to put in him.
    final double[] items = new double[] {0.0, 1.0, 2.0};

    // Let's put the items in him.
    for (int i = 0, n = items.length; i < n; ++i) {
      array.push(items[i]);
    }

    // Are the items in the right places?
    assertEquals(items.length, array.length());
    for (int i = 0, n = items.length; i < n; ++i) {
      assertEquals(items[i], array.get(i));
    }

    // These are some more items to put in him.
    final double[] newItems = new double[] {3.0, 4.0, 5.0};

    // Put all these items in where the others were.
    for (int i = 0, n = items.length; i < n; ++i) {
      array.set(i, newItems[i]);
      assertEquals(newItems[i], array.get(i));
    }

    // Shift our test subject to squeeze out the first item.
    assertEquals(newItems[0], array.shift());
    assertEquals(newItems.length - 1, array.length());

    // Then Unshift.
    array.unshift(newItems[0]);
    assertEquals(newItems[0], array.get(0));
    assertEquals(newItems.length, array.length());

    // Now join them together in harmony.
    assertEquals("3$4$5", array.join("$"));

    final double[] itemsA = new double[] {0.01, 0.02};
    final double[] itemsB = new double[] {0.03, 0.04};
    final ArrayOfNumber a = arrayFrom(itemsA);
    final ArrayOfNumber b = arrayFrom(itemsB);
    assertSamelitude(new double[] {itemsA[0], itemsA[1], itemsB[0], itemsB[1]}, a.concat(b));
    assertSamelitude(itemsA, a);
    assertSamelitude(itemsB, b);
  }
View Full Code Here

  /**
   * Tests {@link ArrayOfNumber#insert(int, double)}.
   */
  public void testInsertingIntoArraysOfNumbers() {
    final ArrayOfNumber array = Collections.arrayOfNumber();

    array.insert(0, 0.1);
    assertSamelitude(new double[] {0.1}, array);

    array.insert(0, 0.2);
    assertSamelitude(new double[] {0.2, 0.1}, array);

    array.insert(100, 0.3);
    assertSamelitude(new double[] {0.2, 0.1, 0.3}, array);

    array.insert(-1, 0.4);
    assertSamelitude(new double[] {0.2, 0.1, 0.4, 0.3}, array);
  }
View Full Code Here

   * Tests {@link ArrayOfNumber#sort()} and
   * {@link ArrayOfNumber#sort(CanCompareNumber)}.
   */
  public void testSortingOfArraysOfNumbers() {
    final double[] items = new double[] {0.0, 0.1, 0.2, 0.3};
    final ArrayOfNumber array = Collections.arrayOfNumber();
    array.push(items[2]);
    array.push(items[1]);
    array.push(items[3]);
    array.push(items[0]);

    array.sort();
    assertEquals(items.length, array.length());
    for (int i = 0, n = array.length(); i < n; ++i) {
      assertEquals(items[i], array.get(i), 0.01);
    }

    array.sort(new CanCompareNumber() {
      @Override
      public int compare(double a, double b) {
        return (a > b) ? -1 : (a < b) ? 1 : 0;
      }
    });
    assertEquals(items.length, array.length());
    for (int i = 0, n = array.length(); i < n; ++i) {
      assertEquals(items[n - 1 - i], array.get(i), 0.01);
    }
  }
View Full Code Here

   * Tests {@link ArrayOfNumber#splice(int, int)}.
   */
  public void testSplicingOfArraysOfNumbers() {
    final double[] items = new double[] {0.0, 0.01, 0.001, 0.0001, 0.00001};

    final ArrayOfNumber a = arrayFrom(items);
    assertSamelitude(items, a.splice(0, items.length));
    assertSamelitude(new double[] {}, a);

    final ArrayOfNumber b = arrayFrom(items);
    assertSamelitude(new double[] {}, b.splice(0, 0));
    assertSamelitude(items, b);

    final ArrayOfNumber c = arrayFrom(items);
    assertSamelitude(new double[] {items[0], items[1]}, c.splice(0, 2));
    assertSamelitude(new double[] {items[2], items[3], items[4]}, c);
  }
View Full Code Here

    mapKeys.sort();
    assertSamelitude(keys, mapKeys);

    values = Arrays.copyOf(values, values.length);
    Arrays.sort(values);
    ArrayOfNumber mapValues = map.values();
    mapValues.sort();
    assertSamelitude(values, mapValues);
  }
View Full Code Here

TOP

Related Classes of elemental.util.ArrayOfNumber

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.