1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.tcp;
17  
18  import junit.framework.TestCase;
19  
20  public class SlidingWindowTest extends TestCase {
21  	
22  	public void testSlide() throws Exception {
23  		SlidingWindow window = new SlidingWindow(50);
24  		window.moveBy(10);
25  		window.slide(10, 50);
26  		assertEquals(10, window.getPosition());
27  		assertEquals(50, window.getWindowSize());
28  		assertEquals(10, window.getStart());
29  		assertEquals(60, window.getEnd());
30  		assertEquals(50, window.remaining());
31  	}
32  	
33  	public void testMoveBy() throws Exception {
34  		SlidingWindow window = new SlidingWindow(50);
35  		assertEquals(0, window.getStart());
36  		assertEquals(0, window.getPosition());
37  		assertEquals(50, window.remaining());
38  		assertEquals(50, window.getEnd());
39  		window.moveBy(50);
40  		assertEquals(0, window.getStart());
41  		assertEquals(50, window.getPosition());
42  		assertEquals(0, window.remaining());
43  		assertEquals(50, window.getEnd());
44  	}
45  	
46  	public void testSlideAround() throws Exception {
47  		SlidingWindow window = new SlidingWindow(SlidingWindow.MAX - 10, 50);
48  		assertEquals(SlidingWindow.MAX - 10, window.getStart());
49  		assertEquals(SlidingWindow.MAX - 10, window.getPosition());
50  		assertEquals(50, window.remaining());
51  		assertEquals(39, window.getEnd());
52  		
53  		window.moveBy(10);
54  		assertEquals(SlidingWindow.MAX - 10, window.getStart());
55  		assertEquals(SlidingWindow.MAX, window.getPosition());
56  		assertEquals(40, window.remaining());
57  		assertEquals(39, window.getEnd());
58  		
59  		window.slide(SlidingWindow.MAX, 50);
60  		assertEquals(SlidingWindow.MAX, window.getStart());
61  		assertEquals(SlidingWindow.MAX, window.getPosition());
62  		assertEquals(50, window.remaining());
63  		assertEquals(49, window.getEnd());
64  	}
65  	
66  	public void testSlideOver() throws Exception {
67  		SlidingWindow window = new SlidingWindow(50);
68  		try {
69  			window.slide(10, 50);
70  			fail("sliding the window over the current position is an error");
71  		} catch (IllegalArgumentException e) {
72  			// expected
73  		}
74  	}
75  	
76  	public void testMoveRightWindowEdgeToTheLeft() throws Exception {
77  		SlidingWindow window = new SlidingWindow(50);
78  		try {
79  			window.slide(0, 49);
80  			fail("moving the right window edge to the left is not possible");
81  		} catch (IllegalArgumentException e) {
82  			// expected
83  		}
84  	}
85  	
86  	public void testMovePositionOverEnd() throws Exception {
87  		SlidingWindow window = new SlidingWindow(50);
88  		try {
89  			window.moveBy(51);
90  			fail("moving the position over the end is not possible");
91  		} catch (IllegalArgumentException e) {
92  			// expected
93  		}
94  	}
95  	
96  	public void testMovePositionOverEndWithWrapping() throws Exception {
97  		SlidingWindow window = new SlidingWindow(SlidingWindow.MAX - 10, 50);
98  		window.moveBy(30);
99  		assertEquals(19, window.getPosition());
100 		try {
101 			window.moveBy(21);
102 			fail("moving the position over the end is not possible");
103 		} catch (IllegalArgumentException e) {
104 			// expected
105 		}
106 	}
107 
108 }