1 22 23 package tests.sosnoski.util.queue; 24 25 import com.sosnoski.util.queue.ObjectQueue; 26 27 import junit.framework.*; 28 29 public class ObjectQueueTest extends TestCase 30 { 31 private static final int TEST_ITEMS = 10; 32 33 private ObjectQueue m_queue; 34 35 public ObjectQueueTest(String name) { 36 super(name); 37 } 38 39 public void setUp() { 40 m_queue = new ObjectQueue(); 41 } 42 43 public void tearDown() { 44 m_queue = null; 45 } 46 47 private Object gen(int index) { 48 return new Integer (index); 49 } 50 51 private boolean ifMatch(Object a, int b) { 52 return gen(b).equals(a); 53 } 54 55 private void fillSequential(int count) { 56 for (int i = 0; i < count; i++) { 57 m_queue.add(gen(i)); 58 } 59 } 60 61 public void testAdd() { 62 fillSequential(TEST_ITEMS); 63 assert(m_queue.size() == TEST_ITEMS); 64 fillSequential(TEST_ITEMS); 65 assert(m_queue.size() == TEST_ITEMS*2); 66 } 67 68 public void testClear() { 69 fillSequential(TEST_ITEMS); 70 m_queue.clear(); 71 assert(m_queue.size() == 0); 72 assert(m_queue.isEmpty()); 73 } 74 75 public void testRemove() { 76 fillSequential(TEST_ITEMS); 77 assert(m_queue.size() == TEST_ITEMS); 78 for (int i = 0; i < TEST_ITEMS; i++) { 79 assert(ifMatch(m_queue.remove(), i)); 80 } 81 assert(m_queue.size() == 0); 82 fillSequential(TEST_ITEMS); 83 for (int i = 0; i < TEST_ITEMS; i++) { 84 assert(ifMatch(m_queue.remove(), i)); 85 } 86 } 87 88 public void testDiscard() { 89 fillSequential(TEST_ITEMS); 90 m_queue.discard(TEST_ITEMS/2); 91 assert(m_queue.size() == TEST_ITEMS/2); 92 for (int i = TEST_ITEMS/2; i < TEST_ITEMS; i++) { 93 assert(ifMatch(m_queue.remove(), i)); 94 } 95 } 96 97 public void testToArray() { 98 fillSequential(TEST_ITEMS); 99 Object [] array = m_queue.toArray(); 100 assert(array.length == TEST_ITEMS); 101 for (int i = 0; i < TEST_ITEMS; i++) { 102 assert(ifMatch(array[i], i)); 103 } 104 } 105 106 public void testToTyped() { 107 fillSequential(TEST_ITEMS); 108 Integer [] array = (Integer [])m_queue.toArray(Integer .class); 109 assert(array.length == TEST_ITEMS); 110 for (int i = 0; i < TEST_ITEMS; i++) { 111 assert(ifMatch(array[i], i)); 112 } 113 } 114 115 public void testClone() { 116 fillSequential(TEST_ITEMS); 117 ObjectQueue clone = (ObjectQueue)m_queue.clone(); 118 assert(clone.size() == TEST_ITEMS); 119 for (int i = 0; i < TEST_ITEMS; i++) { 120 assert(ifMatch(m_queue.remove(), i)); 121 } 122 } 123 124 public static Test suite() { 125 return new TestSuite(ObjectQueueTest.class); 126 } 127 128 public static void main(String [] args) { 129 String [] names = { ObjectQueueTest.class.getName() }; 130 junit.textui.TestRunner.main(names); 131 } 132 } 133 | Popular Tags |