コード例 #1
0
  /** Test of getValue method, of class DefaultIndexer. */
  @Test
  public void testGetValue() {
    DefaultIndexer<String> instance = new DefaultIndexer<String>();

    instance.add("a");
    assertSame("a", instance.getValue(0));

    instance.add("b");
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));

    instance.add("another");
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));
    assertSame("another", instance.getValue(2));

    instance.add(new String("b"));
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));
    assertSame("another", instance.getValue(2));

    instance.add(null);
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));
    assertSame("another", instance.getValue(2));
    assertSame(null, instance.getValue(3));

    instance.add(null);
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));
    assertSame("another", instance.getValue(2));
    assertSame(null, instance.getValue(3));

    instance.add("something else");
    assertSame("a", instance.getValue(0));
    assertSame("b", instance.getValue(1));
    assertSame("another", instance.getValue(2));
    assertSame(null, instance.getValue(3));
    assertSame("something else", instance.getValue(4));

    Integer[] badValues = {-1, -2, 5, instance.size(), instance.size() + 1, null};
    for (Integer badIndex : badValues) {
      boolean exceptionThrown = false;
      try {
        instance.getValue(badIndex);
      } catch (Exception e) {
        exceptionThrown = true;
      } finally {
        assertTrue(exceptionThrown);
      }
    }
  }
コード例 #2
0
  /** Test of size method, of class DefaultIndexer. */
  @Test
  public void testSize() {
    DefaultIndexer<String> instance = new DefaultIndexer<String>();
    assertEquals(0, instance.size());

    instance.add("a");
    assertEquals(1, instance.size());

    instance.add("b");
    assertEquals(2, instance.size());

    instance.add("another");
    assertEquals(3, instance.size());

    instance.add(new String("b"));
    assertEquals(3, instance.size());

    instance.add(null);
    assertEquals(4, instance.size());

    instance.add(null);
    assertEquals(4, instance.size());

    instance.add("something else");
    assertEquals(5, instance.size());
  }
コード例 #3
0
  @Test
  public void testConstructors() {
    DefaultIndexer<String> instance = new DefaultIndexer<String>();
    assertTrue(instance.isEmpty());
    assertTrue(instance.valueSet().isEmpty());
    assertTrue(instance.valueList().isEmpty());
    assertTrue(instance.asMap().isEmpty());

    instance = new DefaultIndexer<String>(123);
    assertTrue(instance.isEmpty());
    assertTrue(instance.valueSet().isEmpty());
    assertTrue(instance.valueList().isEmpty());
    assertTrue(instance.asMap().isEmpty());

    List<String> values = Arrays.asList("a", "c", new String("a"), null, "b");
    instance = new DefaultIndexer<String>(values);
    assertEquals(4, instance.size());
    assertEquals(4, instance.valueSet().size());
    assertEquals(4, instance.valueList().size());
    assertEquals(4, instance.asMap().size());
    assertTrue(instance.valueSet().containsAll(values));
    assertTrue(instance.valueList().containsAll(values));
    assertTrue(instance.asMap().keySet().containsAll(values));
    assertEquals(0, (int) instance.getIndex("a"));
    assertEquals(3, (int) instance.getIndex("b"));
    assertEquals(1, (int) instance.getIndex("c"));
    assertEquals(2, (int) instance.getIndex(null));
  }
コード例 #4
0
 /** Test of the clear method */
 @Test
 public void testClear() {
   DefaultIndexer<String> instance = new DefaultIndexer<>(5);
   instance.add("bob");
   instance.add("frank");
   assertEquals(2, instance.size());
   assertTrue(instance.hasValue("bob"));
   assertTrue(instance.hasValue("frank"));
   instance.clear();
   assertEquals(0, instance.size());
   assertFalse(instance.hasValue("bob"));
   assertFalse(instance.hasValue("frank"));
   instance.add("billy");
   instance.add("bob");
   assertEquals(2, instance.size());
   assertTrue(instance.hasValue("bob"));
   assertTrue(instance.hasValue("billy"));
   assertFalse(instance.hasValue("frank"));
 }
コード例 #5
0
  /** Test of clone method, of class DefaultIndexer. */
  @Test
  public void testClone() {
    DefaultIndexer<String> instance = new DefaultIndexer<String>();
    DefaultIndexer<String> clone = instance.clone();
    assertNotSame(instance, clone);
    assertNotSame(clone, instance.clone());

    instance.add("a");
    assertEquals(0, (int) instance.getIndex("a"));
    assertNull(clone.getIndex("a"));
    assertTrue(clone.isEmpty());
    assertTrue(clone.valueSet().isEmpty());
    assertTrue(clone.valueList().isEmpty());

    instance.add("b");
    clone = instance.clone();
    assertEquals(2, clone.size());
    assertEquals(instance.valueList(), clone.valueList());
    assertEquals(instance.asMap(), clone.asMap());
    assertEquals(0, (int) clone.getIndex("a"));
    assertEquals(1, (int) clone.getIndex("b"));
  }
コード例 #6
0
  /** Test of addAll method, of class DefaultIndexer. */
  @Test
  public void testAddAll() {
    DefaultIndexer<String> instance = new DefaultIndexer<String>();
    assertTrue(instance.isEmpty());

    instance.add("something");

    List<String> values = Arrays.asList("a", "c", new String("a"), null, "b");
    instance.addAll(values);

    assertEquals(5, instance.size());
    assertEquals(0, (int) instance.getIndex("something"));
    assertEquals(1, (int) instance.getIndex("a"));
    assertEquals(4, (int) instance.getIndex("b"));
    assertEquals(2, (int) instance.getIndex("c"));
    assertEquals(3, (int) instance.getIndex(null));

    instance.addAll(values);
    assertEquals(0, (int) instance.getIndex("something"));
    assertEquals(1, (int) instance.getIndex("a"));
    assertEquals(4, (int) instance.getIndex("b"));
    assertEquals(2, (int) instance.getIndex("c"));
    assertEquals(3, (int) instance.getIndex(null));
  }