FileDocCategorySizeDatePackage
CyclicBufferTestCase.javaAPI DocApache log4j 1.2.154402Sat Aug 25 00:09:32 BST 2007org.apache.log4j.helpers

CyclicBufferTestCase

public class CyclicBufferTestCase extends TestCase
Unit test the {@link CyclicBuffer}.
author
Ceki Gülcü

Fields Summary
static Logger
cat
static int
MAX
static LoggingEvent[]
e
Constructors Summary
public CyclicBufferTestCase(String name)


  
    for (int i = 0; i < MAX; i++) {
      e[i] =  new LoggingEvent("", cat, Level.DEBUG, "e"+i, null);
    }
  
    super(name);
  
Methods Summary
voiddoTest1(int size)

    //System.out.println("Doing test with size = "+size);
    CyclicBuffer cb = new CyclicBuffer(size);

    assertEquals(cb.getMaxSize(), size);

    for(int i = -(size+10); i < (size+10); i++) {
      assertNull(cb.get(i));
    }
    
    for(int i = 0; i < MAX; i++) {
      cb.add(e[i]);
      int limit = i < size-1 ? i : size-1;

      //System.out.println("\nLimit is " + limit + ", i="+i);

      for(int j = limit; j >= 0; j--) {
	//System.out.println("i= "+i+", j="+j);
	assertEquals(cb.get(j), e[i-(limit-j)]);
      }
      assertNull(cb.get(-1));
      assertNull(cb.get(limit+1));
    }
  
voiddoTestResize(int initialSize, int numberOfAdds, int newSize)

    //System.out.println("initialSize = "+initialSize+", numberOfAdds="
    //	       +numberOfAdds+", newSize="+newSize);
    CyclicBuffer cb = new CyclicBuffer(initialSize);
    for(int i = 0; i < numberOfAdds; i++) {
      cb.add(e[i]);
    }    
    cb.resize(newSize);

    int offset = numberOfAdds - initialSize;
    if(offset< 0)
      offset = 0;

    int len = newSize < numberOfAdds ? newSize : numberOfAdds;
    len = len < initialSize ? len : initialSize;
    //System.out.println("Len = "+len+", offset="+offset);
    for(int j = 0; j < len; j++) {
      assertEquals(cb.get(j), e[offset+j]);
    }

  
public voidsetUp()


  
public static junit.framework.Testsuite()

    TestSuite suite = new TestSuite();
    suite.addTest(new CyclicBufferTestCase("test0"));
    suite.addTest(new CyclicBufferTestCase("test1"));
    suite.addTest(new CyclicBufferTestCase("testResize"));
    return suite;
  
public voidtest0()

    int size = 2;

    CyclicBuffer cb = new CyclicBuffer(size);    
    assertEquals(cb.getMaxSize(), size);    

    cb.add(e[0]);
    assertEquals(cb.length(), 1);    
    assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 0);
    assertNull(cb.get()); assertEquals(cb.length(), 0);


    cb = new CyclicBuffer(size);    
    cb.add(e[0]);
    cb.add(e[1]);
    assertEquals(cb.length(), 2);    
    assertEquals(cb.get(), e[0]); assertEquals(cb.length(), 1);
    assertEquals(cb.get(), e[1]); assertEquals(cb.length(), 0);
    assertNull(cb.get()); assertEquals(cb.length(), 0);

    
  
public voidtest1()
Test a buffer of size 1,2,4,8,..,128

    for(int bufSize = 1; bufSize <= 128; bufSize *=2) 
      doTest1(bufSize);
  
public voidtestResize()

    for(int isize = 1; isize <= 128; isize *=2) {      
      doTestResize(isize, isize/2+1, isize/2+1);
      doTestResize(isize, isize/2+1, isize+10);
      doTestResize(isize, isize+10, isize/2+1);
      doTestResize(isize, isize+10, isize+10);
    }