1   /*
2    * 
3    * $Revision: 1.1 $
4    * $Date: 2006/02/24 16:50:42 $
5    *
6    * ====================================================================
7    * Struts back mechanism
8    * Copyright (C) 2006 - Manfred Wolff
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 21-01-2006 Manfred Wolff (wolff@mwolff.org)
23   */
24  package org.mwolff.struts.back;
25  
26  import junit.framework.TestCase;
27  
28  
29  /***
30   * . Responsibilities:
31   * <ul>
32   * <li></li>
33   * <li></li>
34   * <li></li>
35   * </ul>
36   * 
37   * @since 1.0
38   * @author <a href="mailto:wolff@mwolff.org">Manfred Wolff </a>
39   */
40  public class RingBufferTest extends TestCase {
41      
42      /***
43       * Tests the buffer without overflow
44       */
45      public void testWithoutOverflow() {
46          
47          ArrayEntry a0 = new ArrayEntry(new Integer(0));
48          ArrayEntry a1 = new ArrayEntry(new Integer(1));
49          ArrayEntry a2 = new ArrayEntry(new Integer(2));
50          ArrayEntry a3 = new ArrayEntry(new Integer(3));
51          ArrayEntry a4 = new ArrayEntry(new Integer(4));
52          
53          RingBuffer buffer = new RingBuffer(5);
54          buffer.push(a0);
55          buffer.push(a1);
56          buffer.push(a2);
57          buffer.push(a3);
58          buffer.push(a4);
59          
60          ArrayEntry result = buffer.back();
61          System.out.println("Must be 3.");
62          System.out.println("Result  " + ((Integer) result.getValue()).intValue());
63          assertTrue(((Integer) result.getValue()).intValue() == 3);
64  
65          result = buffer.back();
66          System.out.println("Must be 2.");
67          System.out.println("Result  " + ((Integer) result.getValue()).intValue());
68          assertTrue(((Integer) result.getValue()).intValue() == 2);
69          
70          result = buffer.back();
71          System.out.println("Must be 1.");
72          System.out.println("Result  " + ((Integer) result.getValue()).intValue());
73          assertTrue(((Integer) result.getValue()).intValue() == 1);
74  
75          result = buffer.back();
76          System.out.println("Must be 0.");
77          System.out.println("Result  " + ((Integer) result.getValue()).intValue());
78          assertTrue(((Integer) result.getValue()).intValue() == 0);
79  
80          result = buffer.back();
81          System.out.println("Must be null");
82          System.out.println("Result  " + result);
83          assertNull(result);
84          
85          assertFalse(buffer.isOverflow());
86      }
87  
88      /***
89       * Tests the buffer with overflow.
90       */
91      public void testWithOverflow() {
92          
93          ArrayEntry a0 = new ArrayEntry(new Integer(0));
94          ArrayEntry a1 = new ArrayEntry(new Integer(1));
95          ArrayEntry a2 = new ArrayEntry(new Integer(2));
96          ArrayEntry a3 = new ArrayEntry(new Integer(3));
97          ArrayEntry a4 = new ArrayEntry(new Integer(4));
98          ArrayEntry a5 = new ArrayEntry(new Integer(5));
99          
100         RingBuffer buffer = new RingBuffer(5);
101         buffer.push(a0);
102         buffer.push(a1);
103         buffer.push(a2);
104         buffer.push(a3);
105         buffer.push(a4);
106         buffer.push(a5);
107         
108         ArrayEntry result = buffer.back();
109         System.out.println("Must be 4.");
110         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
111         assertTrue(((Integer) result.getValue()).intValue() == 4);
112 
113         result = buffer.back();
114         System.out.println("Must be 3.");
115         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
116         assertTrue(((Integer) result.getValue()).intValue() == 3);
117         
118         result = buffer.back();
119         System.out.println("Must be 2.");
120         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
121         assertTrue(((Integer) result.getValue()).intValue() == 2);
122 
123         result = buffer.back();
124         System.out.println("Must be 1.");
125         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
126         assertTrue(((Integer) result.getValue()).intValue() == 1);
127 
128         result = buffer.back();
129         System.out.println("Must be null");
130         System.out.println("Result  " + result);
131         assertNull(result);
132 
133         assertTrue(buffer.isOverflow());
134     }
135 
136     /***
137      * Tests moving forward and backward.
138      */
139     public void testMoveForwardAndBack() {
140         
141         ArrayEntry a0 = new ArrayEntry(new Integer(0));
142         ArrayEntry a1 = new ArrayEntry(new Integer(1));
143         ArrayEntry a2 = new ArrayEntry(new Integer(2));
144         ArrayEntry a3 = new ArrayEntry(new Integer(3));
145         ArrayEntry a4 = new ArrayEntry(new Integer(4));
146         
147         RingBuffer buffer = new RingBuffer(10);
148         buffer.push(a0);
149         buffer.push(a1);
150         buffer.push(a2);
151         
152         buffer.print();
153         
154         ArrayEntry result = buffer.back();
155         buffer.print();
156         System.out.println("Must be 1.");
157         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
158         assertTrue(((Integer) result.getValue()).intValue() == 1);
159        
160         buffer.push(a3);
161         buffer.print();
162 
163         result = buffer.back();
164         buffer.print();
165         System.out.println("Must be 1.");
166         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
167         assertTrue(((Integer) result.getValue()).intValue() == 1);
168 
169         result = buffer.forward();
170         buffer.print();
171         System.out.println("Must be 3.");
172         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
173         assertTrue(((Integer) result.getValue()).intValue() == 3);
174 
175         
176         buffer.push(a4);
177         buffer.print();
178         
179         result = buffer.back();
180         buffer.print();
181         System.out.println("Must be 3.");
182         System.out.println("Result  " + ((Integer) result.getValue()).intValue());
183         assertTrue(((Integer) result.getValue()).intValue() == 3);
184     }
185 }