Populate codec/ directory
[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.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;
24
25 @RunWith(Parameterized.class)
26 public class BitsSerializationTest extends AbstractSerializationTest {
27     @Parameters(name = "{0}")
28     public static Iterable<Object[]> data() {
29         return Arrays.asList(
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 });
34     }
35
36     @Parameter(1)
37     public int emptySize;
38     @Parameter(2)
39     public int oneSize;
40     @Parameter(3)
41     public int size29;
42     @Parameter(4)
43     public int size285;
44     @Parameter(5)
45     public int size65821;
46     @Parameter(6)
47     public int twiceSize65821;
48
49     @Test
50     public void testEmptyBytes() {
51         assertSame(ImmutableSet.of(), emptySize);
52     }
53
54     @Test
55     public void testOne() {
56         assertEquals(ImmutableSet.of("a"), oneSize);
57     }
58
59     @Test
60     public void test29() {
61         assertEquals(fillBits(29), size29);
62     }
63
64     @Test
65     public void test285() {
66         assertEquals(fillBits(285), size285);
67     }
68
69     @Test
70     public void test65821() {
71         assertEquals(fillBits(65821), size65821);
72     }
73
74     @Test
75     public void testTwice65536() {
76         final LeafNode<ImmutableSet<String>> leaf = ImmutableNodes.leafNode(TestModel.TEST_QNAME, fillBits(65821));
77
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);
84         }
85
86         final byte[] bytes = baos.toByteArray();
87         Assert.assertEquals(twiceSize65821, bytes.length);
88
89         try {
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);
95         }
96     }
97
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));
102         }
103         final ImmutableSet<String> ret = builder.build();
104         Assert.assertEquals(size, ret.size());
105         return ret;
106     }
107 }