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 assertEquals("MutableUnsignedLongSet{span=[0..0], size=1}", set.toString());
43 assertTrue(set.contains(1));
44 assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
46 assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
49 assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
52 assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
55 assertEquals("MutableUnsignedLongSet{span=[0..4], size=1}", set.toString());
57 assertTrue(set.contains(2));
58 assertTrue(set.contains(3));
59 assertTrue(set.contains(4));
63 public void testSerialization() throws IOException {
64 final var tmp = MutableUnsignedLongSet.of();
70 final var set = tmp.immutableCopy();
72 final var bos = new ByteArrayOutputStream();
73 try (var out = new DataOutputStream(bos)) {
77 final var bytes = bos.toByteArray();
78 assertArrayEquals(new byte[] { 0, 0, 0, 2, 16, 2, 17, 3, 5 }, bytes);
80 final ImmutableUnsignedLongSet read;
81 try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
82 read = ImmutableUnsignedLongSet.readFrom(in);
83 assertEquals(0, in.available());
86 assertEquals(set, read);
90 public void testToRangeSet() {
91 final var set = MutableUnsignedLongSet.of();
96 assertEquals("[[0..2), [3..5)]", set.toRangeSet().toString());
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);
109 public void testMutableCopy() {
110 final var orig = MutableUnsignedLongSet.of();
112 assertEquals("MutableUnsignedLongSet{span=[18446744073709551615..18446744073709551615], size=1}",
115 final var copy = orig.mutableCopy();
116 assertEquals(orig, copy);
117 assertNotSame(orig, copy);
120 assertNotEquals(orig, copy);
121 assertEquals("MutableUnsignedLongSet{span=[18446744073709551614..18446744073709551615], size=1}",
126 public void testWriteRangesTo() throws IOException {
127 ImmutableUnsignedLongSet.of().writeRangesTo(mock(DataOutput.class), 0);
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());