blob: 884606fc4b2d43a75681954e5b8f2da03a8622ab [file] [log] [blame]
Jonathan Hart233a18a2015-03-02 17:24:58 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.util;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21
22import static junit.framework.TestCase.fail;
23import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertTrue;
25
26/**
27 * Unit tests for the sliding window counter.
28 */
29public class SlidingWindowCounterTest {
30
31 private SlidingWindowCounter counter;
32
33 @Before
34 public void setUp() {
35 counter = new SlidingWindowCounter(2);
36 }
37
38 @After
39 public void tearDown() {
40 counter.destroy();
41 }
42
43 @Test
44 public void testIncrementCount() {
45 assertEquals(0, counter.get(1));
46 assertEquals(0, counter.get(2));
47 counter.incrementCount();
48 assertEquals(1, counter.get(1));
49 assertEquals(1, counter.get(2));
50 counter.incrementCount(2);
51 assertEquals(3, counter.get(2));
52 }
53
54 @Test
55 public void testSlide() {
56 counter.incrementCount();
57 counter.advanceHead();
58 assertEquals(0, counter.get(1));
59 assertEquals(1, counter.get(2));
60 counter.incrementCount(2);
61 assertEquals(2, counter.get(1));
62 assertEquals(3, counter.get(2));
63 }
64
65 @Test
66 public void testWrap() {
67 counter.incrementCount();
68 counter.advanceHead();
69 counter.incrementCount(2);
70 counter.advanceHead();
71 assertEquals(0, counter.get(1));
72 assertEquals(2, counter.get(2));
73 counter.advanceHead();
74 assertEquals(0, counter.get(1));
75 assertEquals(0, counter.get(2));
76
77 }
78
79 @Test
80 public void testCornerCases() {
81 try {
82 counter.get(3);
83 fail("Exception should have been thrown");
84 } catch (IllegalArgumentException e) {
85 assertTrue(true);
86 }
87
88 try {
89 new SlidingWindowCounter(0);
90 fail("Exception should have been thrown");
91 } catch (IllegalArgumentException e) {
92 assertTrue(true);
93 }
94
95 try {
96 new SlidingWindowCounter(-1);
97 fail("Exception should have been thrown");
98 } catch (IllegalArgumentException e) {
99 assertTrue(true);
100 }
101 }
102}