2 * Copyright (c) 2019 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.yangtools.yang.data.codec.binfmt;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSet.Builder;
12 import com.google.common.io.ByteStreams;
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15 import java.util.Arrays;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.junit.runners.Parameterized;
20 import org.junit.runners.Parameterized.Parameter;
21 import org.junit.runners.Parameterized.Parameters;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 @RunWith(Parameterized.class)
26 public class BitsSerializationTest extends AbstractSerializationTest {
27 @Parameters(name = "{0}")
28 public static Iterable<Object[]> data() {
30 new Object[] { NormalizedNodeStreamVersion.LITHIUM, 100, 104, 229, 1538, 456_764, 785_890 },
31 new Object[] { NormalizedNodeStreamVersion.NEON_SR2, 102, 106, 231, 1540, 456_766, 785_882 },
32 new Object[] { NormalizedNodeStreamVersion.SODIUM_SR1, 96, 100, 226, 1536, 456_764, 654_045 },
33 new Object[] { NormalizedNodeStreamVersion.MAGNESIUM, 96, 100, 226, 1536, 456_764, 654_045 });
47 public int twiceSize65821;
50 public void testEmptyBytes() {
51 assertSame(ImmutableSet.of(), emptySize);
55 public void testOne() {
56 assertEquals(ImmutableSet.of("a"), oneSize);
60 public void test29() {
61 assertEquals(fillBits(29), size29);
65 public void test285() {
66 assertEquals(fillBits(285), size285);
70 public void test65821() {
71 assertEquals(fillBits(65821), size65821);
75 public void testTwice65536() {
76 final LeafNode<ImmutableSet<String>> leaf = ImmutableNodes.leafNode(TestModel.TEST_QNAME, fillBits(65821));
78 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
80 nnout.writeNormalizedNode(leaf);
81 nnout.writeNormalizedNode(leaf);
82 } catch (IOException e) {
83 throw new AssertionError("Failed to serialize", e);
86 final byte[] bytes = baos.toByteArray();
87 Assert.assertEquals(twiceSize65821, bytes.length);
90 final NormalizedNodeDataInput input = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
91 Assert.assertEquals(leaf, input.readNormalizedNode());
92 Assert.assertEquals(leaf, input.readNormalizedNode());
93 } catch (IOException e) {
94 throw new AssertionError("Failed to deserialize", e);
98 private static ImmutableSet<String> fillBits(final int size) {
99 final Builder<String> builder = ImmutableSet.builderWithExpectedSize(size);
100 for (int i = 0; i < size; ++i) {
101 builder.add(Integer.toHexString(i));
103 final ImmutableSet<String> ret = builder.build();
104 Assert.assertEquals(size, ret.size());