package simpledb.test;

import simpledb.*;
import simpledb.unittest.Utility;
import java.util.*;

/**
 * Attempts to verify that BufferPool eviction is working by creating a
 * large amount of data. We intend for this test to be started with a VM
 * heap size limitation -Xmx16m. On a Sun 1.5 JVM, inserting 1024 * 500
 * tuples seems to require around 90M of resident memory.  If the
 * BufferPool is not flushing pages to disk, the VM should crash with an
 * OutOfMemoryError. If it is flushing pages and dropping references, then
 * the garbage collector should kick in whenever we run low on heap space.
 */
public class EvictionTest extends Test {

  private static final String emptyPath = 
    "src/simpledb/unittest/testdata/empty.dat";

  public boolean runTest(String args[]) {
    try {
      HeapFile empty = Utility.createEmptyHeapFile(emptyPath, 2);
      TransactionId tid = new TransactionId();

      System.out.println("EvictionTest populating");
      for (int i = 0; i < 1024 * 500; ++i) {
        empty.addTuple(tid, Utility.getHeapTuple(i, 2));
      }
      System.out.println("EvictionTest done");
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
      System.out.println("EvictionTest returning true");
    return true;
  }
}
