Define NormalizedNodeStreamVersion.POTASSIUM
[yangtools.git] / codec / yang-data-codec-binfmt / src / test / java / org / opendaylight / yangtools / yang / data / codec / binfmt / BitsSerializationTest.java
1 /*
2  * Copyright (c) 2019 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.yangtools.yang.data.codec.binfmt;
9
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;
24
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 });
31     }
32
33     @Parameter(1)
34     public int emptySize;
35     @Parameter(2)
36     public int oneSize;
37     @Parameter(3)
38     public int size29;
39     @Parameter(4)
40     public int size285;
41     @Parameter(5)
42     public int size65821;
43     @Parameter(6)
44     public int twiceSize65821;
45
46     @Test
47     public void testEmptyBytes() {
48         assertSame(ImmutableSet.of(), emptySize);
49     }
50
51     @Test
52     public void testOne() {
53         assertEquals(ImmutableSet.of("a"), oneSize);
54     }
55
56     @Test
57     public void test29() {
58         assertEquals(fillBits(29), size29);
59     }
60
61     @Test
62     public void test285() {
63         assertEquals(fillBits(285), size285);
64     }
65
66     @Test
67     public void test65821() {
68         assertEquals(fillBits(65821), size65821);
69     }
70
71     @Test
72     public void testTwice65536() {
73         final LeafNode<ImmutableSet<String>> leaf = ImmutableNodes.leafNode(TestModel.TEST_QNAME, fillBits(65821));
74
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);
81         }
82
83         final byte[] bytes = baos.toByteArray();
84         Assert.assertEquals(twiceSize65821, bytes.length);
85
86         try {
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);
92         }
93     }
94
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));
99         }
100         final ImmutableSet<String> ret = builder.build();
101         Assert.assertEquals(size, ret.size());
102         return ret;
103     }
104 }