package simpledb.unittest;

import simpledb.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import junit.framework.JUnit4TestAdapter;

public class TupleDescTest {

    /**
     * Unit test for TupleDesc.combine()
     */
    @Test public void combine() {
	TupleDesc td1, td2, td3;
	
	td1 = Utility.getTupleDesc(1);
	td2 = Utility.getTupleDesc(2);
    
	// test td1.combine(td2)
	td3 = TupleDesc.combine(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.getType(i));
	
	// test td2.combine(td1)
	td3 = TupleDesc.combine(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.getType(i));

	// test td2.combine(td2)
	td3 = TupleDesc.combine(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.getType(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.getType(i));
	}
    }

    /**
     * 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());
	}
    }
    
    /**
     * 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());
	}
    }

    /**
     * JUnit suite target
     */
    public static junit.framework.Test suite() {
	return new JUnit4TestAdapter(TupleDescTest.class);
    }
}

