Saturday, 19 March 2011

CircularBuffer Unit Tests

Yesterday, I dumped the code for a CircularBuffer into this blog, with the idea being that a circular buffer is a collection class that’s missing from the JSE Collections framework.

Today, I’m adding the JUnit tests for that piece of code, just to prove it works! A few days ago I blogged about a Google Tech Talk given by Misko Hevery, part of which discussed redefining unit tests as Specifications. In writing the CircularBuffer unit tests, I decided to try out Misko’s proposal, making the name of each test refer to a scenario and ‘tell a story’. Did I get the wrong end of the stick and mess this up? I’m guess someone will let me know...

Anyhow, here’s the code...

package marin.utils.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;

/**
*
@author Roger
*
*/
public class CircularBufferTest {

 
private CircularBuffer<String> instance;

 
/**
   * Create something to set
   */
 
@Before
 
public void setUp() {

   
instance = new CircularBuffer<String>(3);

 
}

 
/**   */
 
@Test
 
public void whereInputDoesNotExceedCapacity() {

   
final String[] data = { "This", "is" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);

    String result = instance.dequeue
();
    assertEquals
(data[0], result);

    result = instance.dequeue
();
    assertEquals
(data[1], result);

    assertTrue
(instance.isEmpty());
 
}

 
/** */
 
@Test
 
public void whereInputEqualsCapacity() {

   
final String[] data = { "This", "is", "a" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);

    String result = instance.dequeue
();
    assertEquals
(data[0], result);

    result = instance.dequeue
();
    assertEquals
(data[1], result);

    result = instance.dequeue
();
    assertEquals
(data[2], result);

    assertTrue
(instance.isEmpty());

 
}

 
/**  */
 
@Test
 
public void whereInputExeedsCapacityByOne() {

   
final String[] data = { "This", "is", "a", "Test" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);
    instance.enqueue
(data[3]);

    String result = instance.dequeue
();
    assertEquals
(data[1], result);

    result = instance.dequeue
();
    assertEquals
(data[2], result);

    result = instance.dequeue
();
    assertEquals
(data[3], result);

    assertTrue
(instance.isEmpty());

 
}

 
/** */
 
@Test
 
public void whereInputExeedsCapacityByTwo() {

   
final String[] data = { "This", "is", "a", "Test", "Today" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);
    instance.enqueue
(data[3]);
    instance.enqueue
(data[4]);

    String result = instance.dequeue
();
    assertEquals
(data[2], result);

    result = instance.dequeue
();
    assertEquals
(data[3], result);

    result = instance.dequeue
();
    assertEquals
(data[4], result);

    assertTrue
(instance.isEmpty());

 
}

 
/**  */
 
@Test
 
public void whereInputExeedsCapacityByLots() {

   
final String[] data = { "This", "is", "a", "Test", "Today", "Expected:", "Monday",
       
"tuesday", "Wednesday" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);
    instance.enqueue
(data[3]);
    instance.enqueue
(data[4]);
    instance.enqueue
(data[5]);
    instance.enqueue
(data[6]);
    instance.enqueue
(data[7]);
    instance.enqueue
(data[8]);

    String result = instance.dequeue
();
    assertEquals
(data[6], result);

    result = instance.dequeue
();
    assertEquals
(data[7], result);

    result = instance.dequeue
();
    assertEquals
(data[8], result);

    assertTrue
(instance.isEmpty());
 
}

 
/**   */
 
@Test
 
public void hasNextReturnsFalseWhenEmpty() {

   
Iterator<String> it = instance.iterator();

    assertFalse
(it.hasNext());
 
}

 
/**   */
 
@Test
 
public void hasNextReturnsTrueWhenContainsData() {

   
instance.enqueue("fred");
    Iterator<String> it = instance.iterator
();

    assertTrue
(it.hasNext());
 
}

 
/**   */
 
@Test
 
public void nextReturnsCorrectData() {

   
instance.enqueue("fred");
    Iterator<String> it = instance.iterator
();

    assertEquals
("fred", it.next());
 
}

 
/**  */
 
@Test
 
public void iteratorWorksOverManyValues() {

   
final String[] data = { "This", "is", "a", "Test", "Today", "Expected:", "Monday",
       
"tuesday", "Wednesday" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);
    instance.enqueue
(data[3]);
    instance.enqueue
(data[4]);
    instance.enqueue
(data[5]);
    instance.enqueue
(data[6]);
    instance.enqueue
(data[7]);
    instance.enqueue
(data[8]);

    Iterator<String> it = instance.iterator
();

    String result = it.next
();
    assertEquals
(data[6], result);

    result = it.next
();
    assertEquals
(data[7], result);

    result = it.next
();
    assertEquals
(data[8], result);

    assertEquals
(3, instance.size());
 
}

 
/**  */
 
@Test
 
public void iteratorWorksOverOneValue() {

   
final String[] data = { "This" };

    instance.enqueue
(data[0]);

    Iterator<String> it = instance.iterator
();

    assertTrue
(it.hasNext());
    String result = it.next
();
    assertEquals
(data[0], result);

    assertFalse
(it.hasNext());
    assertEquals
(1, instance.size());
 
}

 
/**  */
 
@Test
 
public void iteratorWorksOverThreeValues() {

   
final String[] data = { "This", "is", "a" };

    instance.enqueue
(data[0]);
    instance.enqueue
(data[1]);
    instance.enqueue
(data[2]);

    Iterator<String> it = instance.iterator
();

    assertTrue
(it.hasNext());
    String result = it.next
();
    assertEquals
(data[0], result);

    assertTrue
(it.hasNext());
    result = it.next
();
    assertEquals
(data[1], result);

    assertTrue
(it.hasNext());
    result = it.next
();
    assertEquals
(data[2], result);

    assertFalse
(it.hasNext());
    assertEquals
(3, instance.size());
 
}
}

No comments: