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.Collections;
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() {
29 return Collections.singletonList(
30 new Object[] { NormalizedNodeStreamVersion.POTASSIUM, 96, 100, 226, 1_536, 456_764, 654_045 });
44 public int twiceSize65821;
47 public void testEmptyBytes() {
48 assertSame(ImmutableSet.of(), emptySize);
52 public void testOne() {
53 assertEquals(ImmutableSet.of("a"), oneSize);
57 public void test29() {
58 assertEquals(fillBits(29), size29);
62 public void test285() {
63 assertEquals(fillBits(285), size285);
67 public void test65821() {
68 assertEquals(fillBits(65821), size65821);
72 public void testTwice65536() {
73 final LeafNode<ImmutableSet<String>> leaf = ImmutableNodes.leafNode(TestModel.TEST_QNAME, fillBits(65821));
75 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
76 try (NormalizedNodeDataOutput nnout = version.newDataOutput(ByteStreams.newDataOutput(baos))) {
77 nnout.writeNormalizedNode(leaf);
78 nnout.writeNormalizedNode(leaf);
79 } catch (IOException e) {
80 throw new AssertionError("Failed to serialize", e);
83 final byte[] bytes = baos.toByteArray();
84 Assert.assertEquals(twiceSize65821, bytes.length);
87 final NormalizedNodeDataInput input = NormalizedNodeDataInput.newDataInput(ByteStreams.newDataInput(bytes));
88 Assert.assertEquals(leaf, input.readNormalizedNode());
89 Assert.assertEquals(leaf, input.readNormalizedNode());
90 } catch (IOException e) {
91 throw new AssertionError("Failed to deserialize", e);
95 private static ImmutableSet<String> fillBits(final int size) {
96 final Builder<String> builder = ImmutableSet.builderWithExpectedSize(size);
97 for (int i = 0; i < size; ++i) {
98 builder.add(Integer.toHexString(i));
100 final ImmutableSet<String> ret = builder.build();
101 Assert.assertEquals(size, ret.size());