Use ImmutableSortedSet for small ImmutableUnsignedLongSets
[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         assertEquals("MutableUnsignedLongSet{span=[0..0], size=1}", set.toString());
41
42         set.add(1);
43         assertTrue(set.contains(1));
44         assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
45         set.add(1);
46         assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
47
48         set.add(4);
49         assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
50
51         set.add(3);
52         assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
53
54         set.add(2);
55         assertEquals("MutableUnsignedLongSet{span=[0..4], size=1}", set.toString());
56
57         assertTrue(set.contains(2));
58         assertTrue(set.contains(3));
59         assertTrue(set.contains(4));
60     }
61
62     @Test
63     public void testSerialization() throws IOException {
64         final var tmp = MutableUnsignedLongSet.of();
65         tmp.add(0);
66         tmp.add(1);
67         tmp.add(4);
68         tmp.add(3);
69
70         final var set = tmp.immutableCopy();
71
72         final var bos = new ByteArrayOutputStream();
73         try (var out = new DataOutputStream(bos)) {
74             set.writeTo(out);
75         }
76
77         final var bytes = bos.toByteArray();
78         assertArrayEquals(new byte[] { 0, 0, 0, 2, 16, 2, 17, 3, 5 }, bytes);
79
80         final ImmutableUnsignedLongSet read;
81         try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
82             read = ImmutableUnsignedLongSet.readFrom(in);
83             assertEquals(0, in.available());
84         }
85
86         assertEquals(set, read);
87     }
88
89     @Test
90     public void testToRangeSet() {
91         final var set = MutableUnsignedLongSet.of();
92         set.add(0);
93         set.add(1);
94         set.add(4);
95         set.add(3);
96         assertEquals("[[0..2), [3..5)]", set.toRangeSet().toString());
97     }
98
99     @Test
100     public void testEmptyCopy() {
101         final var orig = MutableUnsignedLongSet.of();
102         assertSame(ImmutableUnsignedLongSet.of(), orig.immutableCopy());
103         final var copy = orig.mutableCopy();
104         assertEquals(orig, copy);
105         assertNotSame(orig, copy);
106     }
107
108     @Test
109     public void testMutableCopy() {
110         final var orig = MutableUnsignedLongSet.of();
111         orig.add(-1);
112         assertEquals("MutableUnsignedLongSet{span=[18446744073709551615..18446744073709551615], size=1}",
113             orig.toString());
114
115         final var copy = orig.mutableCopy();
116         assertEquals(orig, copy);
117         assertNotSame(orig, copy);
118
119         orig.add(-2);
120         assertNotEquals(orig, copy);
121         assertEquals("MutableUnsignedLongSet{span=[18446744073709551614..18446744073709551615], size=1}",
122             orig.toString());
123     }
124
125     @Test
126     public void testWriteRangesTo() throws IOException {
127         ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 0);
128     }
129
130     @Test
131     public void testWriteRangesToViolation() {
132         final var ex = assertThrows(IOException.class,
133             () -> ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 1));
134         assertEquals("Mismatched size: expected 0, got 1", ex.getMessage());
135     }
136 }