Add Unit Tests for RLEBitSet
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / RLEBitSetTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright Â© 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning.utils;
25
26
27 import static org.hamcrest.Matchers.is;
28 import static org.junit.Assert.assertThat;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 @RunWith(PowerMockRunner.class)
35 public class RLEBitSetTest {
36
37
38   private static RLEBitSet RLEBSet;
39
40   @Before
41   public void setUp() throws Exception {
42     RLEBSet = new RLEBitSet();
43   }
44
45   @Test
46   public void Given_Method_Is_Length_And_BitSet_Is_Empty_Return_0()
47       throws Exception {
48     assertThat(RLEBSet.length(), is(0L));
49   }
50
51   @Test
52   public void Given_Method_Is_Length_And_BitSet_Is_Not_Empty_Return_Length()
53       throws Exception {
54     RLEBSet.set(2L, 5L);
55     assertThat(RLEBSet.length(), is(5L));
56   }
57
58   @Test
59   public void Given_Method_Is_Get_And_Value_Is_Inside_BitSet_Return_True()
60       throws Exception {
61     long val = 4L;
62     RLEBSet.set(2L, 7L);
63     assertThat(RLEBSet.get(val), is(true));
64   }
65
66   @Test
67   public void Given_Method_Is_Get_And_Value_Is_Not_Inside_BitSet_Return_False()
68       throws Exception {
69     long val = 9L;
70     RLEBSet.set(2L, 7L);
71     assertThat(RLEBSet.get(val), is(false));
72   }
73
74   @Test
75   public void Given_Method_Is_Set_And_Value_Is_Range_And_Is_Outside_BitSet_Then_Update_Range()
76       throws Exception {
77     String val = "4-8";
78     RLEBSet.set(2L, 7L);
79     RLEBSet.set(val);
80     assertThat(RLEBSet.toString(), is("2-8"));
81   }
82
83   @Test
84   public void Given_Method_Is_Set_And_Value_Is_Not_Range_And_Is_Inside_BitSet_Then_Keep_Same_Range()
85       throws Exception {
86     String val = "3";
87     RLEBSet.set(2L, 7L);
88     RLEBSet.set(val);
89     assertThat(RLEBSet.toString(), is("2-6"));
90   }
91
92   @Test
93   public void Given_Method_Is_Set_And_Value_Is_Not_Range_And_Is_Outside_BitSet_Then_Add_New_Range()
94       throws Exception {
95     String val = "9";
96     RLEBSet.set(2L, 7L);
97     RLEBSet.set(val);
98     assertThat(RLEBSet.toString(), is("2-6,9"));
99   }
100
101   @Test
102   public void Given_Method_Is_Clear_Return_Blank_Set()
103       throws Exception {
104     RLEBSet.set(2L, 7L);
105     RLEBSet.clear();
106     assertThat(RLEBSet.toString(), is(""));
107   }
108
109   @Test
110   public void Given_Method_Is_Clear_ValueRange_And_Range_Overlaps_BitSet_Return_Trimmed_BitSet()
111       throws Exception {
112     long from = 1L;
113     long to = 4L;
114     RLEBSet.set(2L, 7L);
115     RLEBSet.clear(from, to);
116     assertThat(RLEBSet.toString(), is("4-6"));
117   }
118
119   @Test
120   public void Given_Method_Is_Clear_ValueRange_And_Range_Is_Inside_BitSet_Return_Split_BitSets()
121       throws Exception {
122     long from = 4L;
123     long to = 7L;
124     RLEBSet.set(2L, 9L);
125     RLEBSet.clear(from, to);
126     assertThat(RLEBSet.toString(), is("2-3,7-8"));
127   }
128
129   @Test
130   public void Given_Method_Is_Clear_ValueRange_And_Range_Is_Outside_BitSet_Return_Unchanged_BitSets()
131       throws Exception {
132     long from = 8L;
133     long to = 11L;
134     RLEBSet.set(2L, 7L);
135     RLEBSet.clear(from, to);
136     assertThat(RLEBSet.toString(), is("2-6"));
137   }
138
139   @Test
140   public void Given_Method_Is_And_Return_Common_Value_Set()
141       throws Exception {
142     RLEBitSet bSet = new RLEBitSet();
143     bSet.set(6L,11L);
144     RLEBSet.set(2L, 9L);
145     RLEBSet.and(bSet);
146     assertThat(RLEBSet.toString(), is("6-8"));
147   }
148
149   @Test
150   public void Given_Method_Is_AndNot_Return_Non_Common_Value_Set()
151       throws Exception {
152     RLEBitSet bSet = new RLEBitSet();
153     bSet.set(6L,11L);
154     RLEBSet.set(2L, 9L);
155     RLEBSet.andNot(bSet);
156     assertThat(RLEBSet.toString(), is("2-5"));
157   }
158
159   @Test
160   public void Given_Method_Is_Cardinality_Return_Bits_Set_To_True()
161       throws Exception {
162     RLEBSet.set(2L, 9L);
163     assertThat(RLEBSet.cardinality(), is(7));
164   }
165
166   @Test
167   public void Given_Method_Is_Clone_Return_Identical_Clone()
168       throws Exception {
169     RLEBSet.set(2L, 9L);
170     assertThat(RLEBSet.clone().toString(), is(RLEBSet.toString()));
171   }
172 }