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.assertTrue;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import org.junit.Test;
22 public class UnsignedLongSetTest {
24 public void testOperations() {
25 final var set = MutableUnsignedLongSet.of();
26 assertEquals("MutableUnsignedLongSet{size=0}", set.toString());
27 assertFalse(set.contains(0));
30 assertTrue(set.contains(0));
31 assertEquals("MutableUnsignedLongSet{span=[0..0], size=1}", set.toString());
34 assertTrue(set.contains(1));
35 assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
37 assertEquals("MutableUnsignedLongSet{span=[0..1], size=1}", set.toString());
40 assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
43 assertEquals("MutableUnsignedLongSet{span=[0..4], size=2}", set.toString());
46 assertEquals("MutableUnsignedLongSet{span=[0..4], size=1}", set.toString());
48 assertTrue(set.contains(2));
49 assertTrue(set.contains(3));
50 assertTrue(set.contains(4));
54 public void testSerialization() throws IOException {
55 final var tmp = MutableUnsignedLongSet.of();
61 final var set = tmp.immutableCopy();
63 final var bos = new ByteArrayOutputStream();
64 try (var out = new DataOutputStream(bos)) {
68 final var bytes = bos.toByteArray();
69 assertArrayEquals(new byte[] { 0, 0, 0, 2, 16, 2, 17, 3, 5 }, bytes);
71 final ImmutableUnsignedLongSet read;
72 try (var in = new DataInputStream(new ByteArrayInputStream(bytes))) {
73 read = ImmutableUnsignedLongSet.readFrom(in);
74 assertEquals(0, in.available());
77 assertEquals(set, read);
81 public void testToRangeSet() {
82 final var set = MutableUnsignedLongSet.of();
87 assertEquals("[[0..2), [3..5)]", set.toRangeSet().toString());