2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.utils;
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;
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;
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class UnsignedLongSetTest {
33 public void testOperations() {
34 final var set = MutableUnsignedLongSet.of();
35 assertEquals("MutableUnsignedLongSet{size=0}", set.toString());
36 assertFalse(set.contains(0));
39 assertTrue(set.contains(0));
40 assertRanges("[[0..0]]", set);
43 assertTrue(set.contains(1));
44 assertRanges("[[0..1]]", set);
46 assertRanges("[[0..1]]", set);
49 assertRanges("[[0..1], [4..4]]", set);
52 assertRanges("[[0..1], [3..4]]", set);
55 assertRanges("[[0..4]]", set);
57 assertTrue(set.contains(2));
58 assertTrue(set.contains(3));
59 assertTrue(set.contains(4));
62 assertRanges("[[0..4], [8..8]]", set);
64 assertRanges("[[0..4], [6..6], [8..8]]", set);
66 assertRanges("[[0..4], [6..8]]", set);
68 assertRanges("[[0..8]]", set);
71 assertRanges("[[0..8], [11..11]]", set);
73 assertRanges("[[0..9], [11..11]]", set);
77 public void testSerialization() throws IOException {
79 final var set = MutableUnsignedLongSet.of(0, 1, 4, 3).immutableCopy();
81 final var bos = new ByteArrayOutputStream();
82 try (var out = new DataOutputStream(bos)) {
86 final var bytes = bos.toByteArray();
87 assertArrayEquals(new byte[] { 0, 0, 0, 2, 16, 2, 17, 3, 5 }, bytes);
89 final ImmutableUnsignedLongSet read;
90 try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
91 read = ImmutableUnsignedLongSet.readFrom(in);
92 assertEquals(0, in.available());
95 assertEquals(set, read);
99 public void testToRangeSet() {
100 final var set = MutableUnsignedLongSet.of(0, 1, 4, 3);
101 assertEquals("[[0..2), [3..5)]", set.toRangeSet().toString());
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);
114 public void testMutableCopy() {
115 final var orig = MutableUnsignedLongSet.of();
117 assertEquals("MutableUnsignedLongSet{span=[18446744073709551615..18446744073709551615], size=1}",
120 final var copy = orig.mutableCopy();
121 assertEquals(orig, copy);
122 assertNotSame(orig, copy);
125 assertNotEquals(orig, copy);
126 assertEquals("MutableUnsignedLongSet{span=[18446744073709551614..18446744073709551615], size=1}",
131 public void testWriteRangesTo() throws IOException {
132 ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 0);
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());
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);
153 set.addAll(MutableUnsignedLongSet.of(2, 3, 4, 5));
154 assertRanges("[[1..6], [9..10], [13..14]]", set);
156 set.addAll(MutableUnsignedLongSet.of(6, 7));
157 assertRanges("[[1..7], [9..10], [13..14]]", set);
159 set.addAll(MutableUnsignedLongSet.of(8));
160 assertRanges("[[1..10], [13..14]]", set);
162 set = MutableUnsignedLongSet.of();
163 set.addAll(MutableUnsignedLongSet.of(1, 2));
164 assertRanges("[[1..2]]", set);
167 set.addAll(MutableUnsignedLongSet.of(4, 5));
168 assertRanges("[[1..2], [4..6], [9..10], [13..14]]", set);
170 set.addAll(MutableUnsignedLongSet.of(12, 13, 14, 15));
171 assertRanges("[[1..2], [4..6], [9..10], [12..15]]", set);
173 set.addAll(MutableUnsignedLongSet.of(8, 9, 10, 11));
174 assertRanges("[[1..2], [4..6], [8..15]]", set);
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);
180 set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3));
181 assertRanges("[[0..3], [5..6], [9..10], [13..14]]", set);
184 set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3, 4, 5, 6, 7, 8));
185 assertRanges("[[0..10], [13..14]]", set);
188 set.addAll(MutableUnsignedLongSet.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
189 assertRanges("[[0..10], [13..14]]", set);
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);
198 private static void assertRanges(final String expected, final UnsignedLongSet set) {
199 assertEquals(expected, set.ranges().toString());