示例#1
0
  /** Unit test for TupleDesc.combine() */
  @Test
  public void combine() {
    TupleDesc td1, td2, td3;

    td1 = Utility.getTupleDesc(1, "td1");
    td2 = Utility.getTupleDesc(2, "td2");

    // test td1.combine(td2)
    td3 = TupleDesc.merge(td1, td2);
    assertEquals(3, td3.numFields());
    assertEquals(3 * Type.INT_TYPE.getLen(), td3.getSize());
    for (int i = 0; i < 3; ++i) assertEquals(Type.INT_TYPE, td3.getFieldType(i));
    assertEquals(combinedStringArrays(td1, td2, td3), true);

    // test td2.combine(td1)
    td3 = TupleDesc.merge(td2, td1);
    assertEquals(3, td3.numFields());
    assertEquals(3 * Type.INT_TYPE.getLen(), td3.getSize());
    for (int i = 0; i < 3; ++i) assertEquals(Type.INT_TYPE, td3.getFieldType(i));
    assertEquals(combinedStringArrays(td2, td1, td3), true);

    // test td2.combine(td2)
    td3 = TupleDesc.merge(td2, td2);
    assertEquals(4, td3.numFields());
    assertEquals(4 * Type.INT_TYPE.getLen(), td3.getSize());
    for (int i = 0; i < 4; ++i) assertEquals(Type.INT_TYPE, td3.getFieldType(i));
    assertEquals(combinedStringArrays(td2, td2, td3), true);
  }
 @Test
 public void shouldGiveCorrectTerrainFactors() {
   // plains have multiplier 1
   assertEquals(1, Utility.getTerrainFactor(game, new Position(0, 1)));
   // hills have multiplier 2
   assertEquals(2, Utility.getTerrainFactor(game, new Position(1, 0)));
   // forest have multiplier 2
   assertEquals(2, Utility.getTerrainFactor(game, new Position(0, 0)));
   // cities have multiplier 3
   assertEquals(3, Utility.getTerrainFactor(game, new Position(1, 1)));
 }
 @Test
 public void shouldGiveSum0ForBlueAtP2_4() {
   assertEquals(
       "Blue unit at (2,4) should get +0 support",
       +0,
       Utility.getFriendlySupport(game, new Position(2, 4), Player.BLUE));
 }
 @Test
 public void shouldGiveSum2ForRedAtP2_4() {
   assertEquals(
       "Red unit at (2,4) should get +2 support",
       +2,
       Utility.getFriendlySupport(game, new Position(2, 4), Player.RED));
 }
 @Test
 public void shouldGiveSum3ForRedAtP2_2() {
   assertEquals(
       "Red unit at (2,2) should get +3 support",
       +3,
       Utility.getFriendlySupport(game, new Position(2, 2), Player.RED));
 }
示例#6
0
  @Test
  public void new_getFieldTypeTest() {
    int length = 100;
    String name = "td";
    TupleDesc td = Utility.getTupleDesc(length, name);

    // Lower than index bound.
    try {
      td.getFieldType(length + 1);
      fail("expected exception");
    } catch (NoSuchElementException e) {
    }

    // Higher than index bound.
    try {
      td.getFieldType(-1);
      fail("expected exception");
    } catch (NoSuchElementException e) {
    }

    // Check each name.
    for (int i = 0; i < length; i++) {
      assertEquals(Type.INT_TYPE, td.getFieldType(i));
    }
  }
 @Test
 public void shouldGiveSum1ForBlueAtP5_5() {
   assertEquals(
       "Blue unit at (5,5) should get +1 support",
       +1,
       Utility.getFriendlySupport(game, new Position(5, 5), Player.BLUE));
 }
示例#8
0
  /** Unit test for TupleDesc.numFields() */
  @Test
  public void numFields() {
    int[] lengths = new int[] {1, 2, 1000};

    for (int len : lengths) {
      TupleDesc td = Utility.getTupleDesc(len);
      assertEquals(len, td.numFields());
    }
  }
示例#9
0
  /** Unit test for TupleDesc.getType() */
  @Test
  public void getType() {
    int[] lengths = new int[] {1, 2, 1000};

    for (int len : lengths) {
      TupleDesc td = Utility.getTupleDesc(len);
      for (int i = 0; i < len; ++i) assertEquals(Type.INT_TYPE, td.getFieldType(i));
    }
  }
示例#10
0
  /** Unit test for TupleDesc.getSize() */
  @Test
  public void getSize() {
    int[] lengths = new int[] {1, 2, 1000};

    for (int len : lengths) {
      TupleDesc td = Utility.getTupleDesc(len);
      assertEquals(len * Type.INT_TYPE.getLen(), td.getSize());
    }
  }
示例#11
0
  /** Test one prarmeter constructor, it should set the filed name to null. */
  @Test
  public void new_oneParamsConTest() {
    int[] lengths = new int[] {1, 2, 1000};

    for (int len : lengths) {
      TupleDesc td = Utility.getTupleDesc(len);
      for (int i = 0; i < len; i++) {
        assertEquals("", td.getFieldName(i));
      }
    }
  }
  @Test
  public void shouldGive3PositionsForP15_15() {
    center = new Position(15, 15);
    iter = Utility.get8NeighborhoodIterator(center);
    neighborhood = convertIteration2List(iter);

    assertTrue("Must contain (14,15)", neighborhood.contains(new Position(14, 15)));
    assertTrue("Must contain (14,14)", neighborhood.contains(new Position(14, 14)));
    assertTrue("Must contain (15,14)", neighborhood.contains(new Position(15, 14)));

    assertEquals("Should be 3 positions in the iterator", 3, neighborhood.size());
  }
示例#13
0
  /** Unit test for TupleDesc.nameToId() */
  @Test
  public void nameToId() {
    int[] lengths = new int[] {1, 2, 1000};
    String prefix = "test";

    for (int len : lengths) {
      // Make sure you retrieve well-named fields
      TupleDesc td = Utility.getTupleDesc(len, prefix);
      for (int i = 0; i < len; ++i) {
        assertEquals(i, td.fieldNameToIndex(prefix + i));
      }

      // Make sure you throw exception for non-existent fields
      try {
        td.fieldNameToIndex("foo");
        Assert.fail("foo is not a valid field name");
      } catch (NoSuchElementException e) {
        // expected to get here
      }

      // Make sure you throw exception for null searches
      try {
        td.fieldNameToIndex(null);
        Assert.fail("null is not a valid field name");
      } catch (NoSuchElementException e) {
        // expected to get here
      }

      // Make sure you throw exception when all field names are null
      td = Utility.getTupleDesc(len);
      try {
        td.fieldNameToIndex(prefix);
        Assert.fail("no fields are named, so you can't find it");
      } catch (NoSuchElementException e) {
        // expected to get here
      }
    }
  }
示例#14
0
  @Test
  public void new_iteratorTest() {
    int[] lengths = new int[] {1, 2, 1000};

    for (int len : lengths) {
      TupleDesc td = Utility.getTupleDesc(len);
      Iterator<TupleDesc.TDItem> i = td.iterator();
      while (i.hasNext()) {
        TupleDesc.TDItem item = i.next();
        assertEquals(Type.INT_TYPE, item.fieldType);
        assertEquals("", item.fieldName);
      }
    }
  }
  @Test
  public void new_fieldsTest() {
    int length = 10;
    String name = "td";
    Tuple t = new Tuple(Utility.getTupleDesc(length, name));

    Iterator<Field> fs = t.fields();

    int i = 0;
    while (fs.hasNext()) {
      i++;
      fs.next();
    }

    assertEquals(length, i);
  }
  /** Unit test for Tuple.getField() and Tuple.setField() */
  @Test
  public void modifyFields() {
    TupleDesc td = Utility.getTupleDesc(2);

    Tuple tup = new Tuple(td);
    tup.setField(0, new IntField(-1));
    tup.setField(1, new IntField(0));

    assertEquals(new IntField(-1), tup.getField(0));
    assertEquals(new IntField(0), tup.getField(1));

    tup.setField(0, new IntField(1));
    tup.setField(1, new IntField(37));

    assertEquals(new IntField(1), tup.getField(0));
    assertEquals(new IntField(37), tup.getField(1));
  }
  /** Unit test for Tuple.getRecordId() and Tuple.setRecordId() */
  @Test
  public void modifyRecordId() {
    Tuple tup1 = new Tuple(Utility.getTupleDesc(1));
    HeapPageId pid1 = new HeapPageId(0, 0);
    RecordId rid1 = new RecordId(pid1, 0);
    tup1.setRecordId(rid1);

    try {
      assertEquals(rid1, tup1.getRecordId());
    } catch (java.lang.UnsupportedOperationException e) {
      // rethrow the exception with an explanation
      throw new UnsupportedOperationException(
          "modifyRecordId() test failed due to "
              + "RecordId.equals() not being implemented.  This is not required for Lab 1, "
              + "but should pass when you do implement the RecordId class.");
    }
  }
  @Test
  public void shouldGive8PositionsForP8_8() {
    center = new Position(8, 8);
    iter = Utility.get8NeighborhoodIterator(center);
    neighborhood = convertIteration2List(iter);

    assertTrue("Must contain (7,7)", neighborhood.contains(new Position(7, 7)));
    assertTrue("Must contain (9,9)", neighborhood.contains(new Position(9, 9)));
    assertTrue("Must contain (7,9)", neighborhood.contains(new Position(7, 9)));
    assertTrue("Must contain (8,7)", neighborhood.contains(new Position(8, 7)));

    assertFalse("Must not contain center position", neighborhood.contains(center));

    assertFalse("Must not contain (5,5) position", neighborhood.contains(new Position(5, 5)));

    assertEquals("Should be 8 positions in the iterator", 8, neighborhood.size());
  }
  @Test
  public void new_toStringTest() {
    int length = 10;
    String name = "td";
    Tuple t = new Tuple(Utility.getTupleDesc(length, name));
    for (int i = 0; i < length; i++) {
      t.setField(i, new TestField());
    }

    String tString = t.toString();

    // Last character should be \n.
    assertEquals("\n", tString.substring(tString.length() - 1));

    // Only last character is \n.
    assertFalse(tString.substring(0, tString.length() - 1).contains("\n"));

    // Split string on any white character.
    String[] tStringAr = tString.substring(0, tString.length() - 1).split("\\s+");
    assertEquals(length, tStringAr.length);
  }
 /** Unit test for Tuple.getTupleDesc() */
 @Test
 public void getTupleDesc() {
   TupleDesc td = Utility.getTupleDesc(5);
   Tuple tup = new Tuple(td);
   assertEquals(td, tup.getTupleDesc());
 }
示例#21
0
 /** Set up initial resources for each unit test. */
 @Before
 public void setUp() throws Exception {
   hf = SystemTestUtil.createRandomHeapFile(2, 20, null, null);
   td = Utility.getTupleDesc(2);
   tid = new TransactionId();
 }