/** 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 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));
    }
  }
  /** 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));
    }
  }
  /** 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());
    }
  }
  /** 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());
    }
  }
  /** 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));
      }
    }
  }
  /** 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
      }
    }
  }
  @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.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.");
    }
  }
  /** 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));
  }
  @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);
  }
 /** 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();
 }
 /** Unit test for Tuple.getTupleDesc() */
 @Test
 public void getTupleDesc() {
   TupleDesc td = Utility.getTupleDesc(5);
   Tuple tup = new Tuple(td);
   assertEquals(td, tup.getTupleDesc());
 }