Exemplo n.º 1
0
  @Test
  public void checkMaxNodeIndexOnAddAndRemove() {
    assumeTrue(dataStructure instanceof INodeListDatastructure);
    assumeTrue(Node.class.isAssignableFrom(elementClass));
    INodeListDatastructure tempDS = (INodeListDatastructure) dataStructure;

    Node[] dummies = new Node[10];
    for (int i = 0; i < dummies.length; i++) {
      dummies[i] = (Node) mock(this.elementClass);
      when(dummies[i].getIndex()).thenReturn(i);
      assertTrue(tempDS.add(dummies[i]));
      assertEquals(i, tempDS.getMaxNodeIndex());
    }

    /*
     * Since we don't know if the data structure really used the
     * maxNodeIndex property and did not return the count property, we will
     * run some more sophisticated tests now
     */

    Node[] secondDummies = new Node[10];
    int[] prevIndex = new int[10];
    int lastIndex = tempDS.getMaxNodeIndex();

    for (int i = 0; i < secondDummies.length; i++) {
      secondDummies[i] = (Node) mock(this.elementClass);
      prevIndex[i] = lastIndex;
      lastIndex = lastIndex + Rand.rand.nextInt(i + 1) + 1;
      when(secondDummies[i].getIndex()).thenReturn(lastIndex);
      assertTrue(tempDS.add(secondDummies[i]));
      assertEquals(lastIndex, tempDS.getMaxNodeIndex());
    }

    for (int i = secondDummies.length - 1; i >= 0; i--) {
      assertTrue(tempDS.remove(secondDummies[i]));
      assertEquals(prevIndex[i], tempDS.getMaxNodeIndex());
    }

    assertEquals(dummies[dummies.length - 1].getIndex(), tempDS.getMaxNodeIndex());
  }
Exemplo n.º 2
0
 public boolean removeNode(Node n) {
   return nodes.remove(n);
 }