package simpledb.unittest;

import simpledb.*;

import java.util.*;
import java.io.*;

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

public class HeapFileRandomTest {

    private byte[] data;
    private HeapFile hf;
    private HeapFile empty;
    private TupleDesc td;
    
    /**
     * Set up initial resources for each unit test.
     */
    @Before public void setUp() throws Exception {
        this.empty = new HeapFile(new File("heapfile"));
        Type typeAr[] = new Type[1];
        typeAr[0] = Type.INT_TYPE;
        td = new TupleDesc(typeAr);
        Database.getCatalog().addTable(empty, td);
        empty.init();
    }

    @Test public void testRandomAccess() throws Exception {
        Tuple t1 = new Tuple(td);
        t1.setField(0, new IntField(new Integer(1)));
        Tuple t2 = new Tuple(td);
        t2.setField(0, new IntField(new Integer(2)));
        Tuple t3 = new Tuple(td);
        t3.setField(0, new IntField(new Integer(3)));

        empty.setTuple(new TransactionId(), 1, t1);
        assertEquals(empty.getTuple(new TransactionId(), 1), t1);
        empty.setTuple(new TransactionId(), 3, t2);
        assertEquals(empty.getTuple(new TransactionId(), 3), t2);
        empty.setTuple(new TransactionId(), 16384, t3);
        assertEquals(empty.getTuple(new TransactionId(), 16384), t3);
    }
    

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

