Add MutableUnsignedLongSet.addAll()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / UnsignedLongSetTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.junit.Assert.assertNotSame;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertThrows;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.Mockito.mock;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.DataInputStream;
23 import java.io.DataOutput;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.junit.MockitoJUnitRunner;
29
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class UnsignedLongSetTest {
32     @Test
33     public void testOperations() {
34         final var set = MutableUnsignedLongSet.of();
35         assertEquals("MutableUnsignedLongSet{size=0}", set.toString());
36         assertFalse(set.contains(0));
37
38         set.add(0);
39         assertTrue(set.contains(0));
40         assertRanges("[[0..0]]", set);
41
42         set.add(1);
43         assertTrue(set.contains(1));
44         assertRanges("[[0..1]]", set);
45         set.add(1);
46         assertRanges("[[0..1]]", set);
47
48         set.add(4);
49         assertRanges("[[0..1], [4..4]]", set);
50
51         set.add(3);
52         assertRanges("[[0..1], [3..4]]", set);
53
54         set.add(2);
55         assertRanges("[[0..4]]", set);
56
57         assertTrue(set.contains(2));
58         assertTrue(set.contains(3));
59         assertTrue(set.contains(4));
60
61         set.add(8);
62         assertRanges("[[0..4], [8..8]]", set);
63         set.add(6);
64         assertRanges("[[0..4], [6..6], [8..8]]", set);
65         set.add(7);
66         assertRanges("[[0..4], [6..8]]", set);
67         set.add(5);
68         assertRanges("[[0..8]]", set);
69
70         set.add(11);
71         assertRanges("[[0..8], [11..11]]", set);
72         set.add(9);
73         assertRanges("[[0..9], [11..11]]", set);
74     }
75
76     @Test
77     public void testSerialization() throws IOException {
78
79         final var set = MutableUnsignedLongSet.of(0, 1, 4, 3).immutableCopy();
80
81         final var bos = new ByteArrayOutputStream();
82         try (var out = new DataOutputStream(bos)) {
83             set.writeTo(out);
84         }
85
86         final var bytes = bos.toByteArray();
87         assertArrayEquals(new byte[] { 0, 0, 0, 2, 16, 2, 17, 3, 5 }, bytes);
88
89         final ImmutableUnsignedLongSet read;
90         try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
91             read = ImmutableUnsignedLongSet.readFrom(in);
92             assertEquals(0, in.available());
93         }
94
95         assertEquals(set, read);
96     }
97
98     @Test
99     public void testToRangeSet() {
100         final var set = MutableUnsignedLongSet.of(0, 1, 4, 3);
101         assertEquals("[[0..2), [3..5)]", set.toRangeSet().toString());
102     }
103
104     @Test
105     public void testEmptyCopy() {
106         final var orig = MutableUnsignedLongSet.of();
107         assertSame(ImmutableUnsignedLongSet.of(), orig.immutableCopy());
108         final var copy = orig.mutableCopy();
109         assertEquals(orig, copy);
110         assertNotSame(orig, copy);
111     }
112
113     @Test
114     public void testMutableCopy() {
115         final var orig = MutableUnsignedLongSet.of();
116         orig.add(-1);
117         assertEquals("MutableUnsignedLongSet{span=[18446744073709551615..18446744073709551615], size=1}",
118             orig.toString());
119
120         final var copy = orig.mutableCopy();
121         assertEquals(orig, copy);
122         assertNotSame(orig, copy);
123
124         orig.add(-2);
125         assertNotEquals(orig, copy);
126         assertEquals("MutableUnsignedLongSet{span=[18446744073709551614..18446744073709551615], size=1}",
127             orig.toString());
128     }
129
130     @Test
131     public void testWriteRangesTo() throws IOException {
132         ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 0);
133     }
134
135     @Test
136     public void testWriteRangesToViolation() {
137         final var ex = assertThrows(IOException.class,
138             () -> ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 1));
139         assertEquals("Mismatched size: expected 0, got 1", ex.getMessage());
140     }
141
142     @Test
143     public void testAddRange() {
144         var set = sparseSet();
145         set.addAll(MutableUnsignedLongSet.of(1, 2));
146         assertRanges("[[1..2], [5..6], [9..10], [13..14]]", set);
147         set.addAll(MutableUnsignedLongSet.of(3, 4));
148         assertRanges("[[1..6], [9..10], [13..14]]", set);
149         set.addAll(MutableUnsignedLongSet.of(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
150         assertRanges("[[1..15]]", set);
151
152         set = sparseSet();
153         set.addAll(MutableUnsignedLongSet.of(2, 3, 4, 5));
154         assertRanges("[[1..6], [9..10], [13..14]]", set);
155
156         set.addAll(MutableUnsignedLongSet.of(6, 7));
157         assertRanges("[[1..7], [9..10], [13..14]]", set);
158
159         set.addAll(MutableUnsignedLongSet.of(8));
160         assertRanges("[[1..10], [13..14]]", set);
161
162         set = MutableUnsignedLongSet.of();
163         set.addAll(MutableUnsignedLongSet.of(1, 2));
164         assertRanges("[[1..2]]", set);
165
166         set = sparseSet();
167         set.addAll(MutableUnsignedLongSet.of(4, 5));
168         assertRanges("[[1..2], [4..6], [9..10], [13..14]]", set);
169
170         set.addAll(MutableUnsignedLongSet.of(12, 13, 14, 15));
171         assertRanges("[[1..2], [4..6], [9..10], [12..15]]", set);
172
173         set.addAll(MutableUnsignedLongSet.of(8, 9, 10, 11));
174         assertRanges("[[1..2], [4..6], [8..15]]", set);
175
176         set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
177         assertRanges("[[0..16]]", set);
178
179         set = sparseSet();
180         set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3));
181         assertRanges("[[0..3], [5..6], [9..10], [13..14]]", set);
182
183         set = sparseSet();
184         set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3, 4, 5, 6, 7, 8));
185         assertRanges("[[0..10], [13..14]]", set);
186
187         set = sparseSet();
188         set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
189         assertRanges("[[0..10], [13..14]]", set);
190     }
191
192     private static MutableUnsignedLongSet sparseSet() {
193         final var ret = MutableUnsignedLongSet.of(1, 2, 5, 6, 9, 10, 13, 14);
194         assertRanges("[[1..2], [5..6], [9..10], [13..14]]", ret);
195         return ret;
196     }
197
198     private static void assertRanges(final String expected, final UnsignedLongSet set) {
199         assertEquals(expected, set.ranges().toString());
200     }
201 }