blob: 6824a727a11bf0afbb91e276b5031cfa0ed28f5e [file] [log] [blame]
Carmelo Cascone6b32c992016-04-13 11:53:09 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.util;
18
19import com.google.common.testing.EqualsTester;
20import org.junit.Test;
21
22import java.nio.ByteBuffer;
23import java.nio.ByteOrder;
24import java.util.Random;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.equalTo;
28import static org.hamcrest.Matchers.is;
29
30public class ImmutableByteSequenceTest {
31
32 @Test
33 public void testCopy() throws Exception {
34
35 byte byteValue = (byte) 1;
36 short shortValue = (short) byteValue;
37 int intValue = (int) byteValue;
38 long longValue = (long) byteValue;
39 byte[] arrayValue = new byte[64];
40 arrayValue[63] = byteValue;
41 ByteBuffer bufferValue = ByteBuffer.allocate(64).put(arrayValue);
42
43 ImmutableByteSequence bsByte = ImmutableByteSequence.copyFrom(byteValue);
44 ImmutableByteSequence bsShort = ImmutableByteSequence.copyFrom(shortValue);
45 ImmutableByteSequence bsInt = ImmutableByteSequence.copyFrom(intValue);
46 ImmutableByteSequence bsLong = ImmutableByteSequence.copyFrom(longValue);
47 ImmutableByteSequence bsArray = ImmutableByteSequence.copyFrom(arrayValue);
48 ImmutableByteSequence bsBuffer = ImmutableByteSequence.copyFrom(bufferValue);
49
50 assertThat("byte sequence of a byte value must have size 1",
51 bsByte.size(), is(equalTo(1)));
52 assertThat("byte sequence of a short value must have size 2",
53 bsShort.size(), is(equalTo(2)));
54 assertThat("byte sequence of an int value must have size 4",
55 bsInt.size(), is(equalTo(4)));
56 assertThat("byte sequence of a long value must have size 8",
57 bsLong.size(), is(equalTo(8)));
58 assertThat("byte sequence of a byte array value must have same size of the array",
59 bsArray.size(), is(equalTo(arrayValue.length)));
60 assertThat("byte sequence of a byte buffer value must have same size of the buffer",
61 bsBuffer.size(), is(equalTo(bufferValue.capacity())));
62
63 String errStr = "incorrect byte sequence value";
64
65 assertThat(errStr, bsByte.asArray()[0], is(equalTo(byteValue)));
66 assertThat(errStr, bsShort.asArray()[1], is(equalTo(byteValue)));
67 assertThat(errStr, bsInt.asArray()[3], is(equalTo(byteValue)));
68 assertThat(errStr, bsLong.asArray()[7], is(equalTo(byteValue)));
69 assertThat(errStr, bsArray.asArray()[63], is(equalTo(byteValue)));
70 assertThat(errStr, bsBuffer.asArray()[63], is(equalTo(byteValue)));
71 }
72
73 @Test
74 public void testEndianness() throws Exception {
75
76 long longValue = new Random().nextLong();
77
78 // creates a new sequence from a big-endian buffer
79 ByteBuffer bbBigEndian = ByteBuffer
80 .allocate(8)
81 .order(ByteOrder.BIG_ENDIAN)
82 .putLong(longValue);
83 ImmutableByteSequence bsBufferCopyBigEndian =
84 ImmutableByteSequence.copyFrom(bbBigEndian);
85
86 // creates a new sequence from a little-endian buffer
87 ByteBuffer bbLittleEndian = ByteBuffer
88 .allocate(8)
89 .order(ByteOrder.LITTLE_ENDIAN)
90 .putLong(longValue);
91 ImmutableByteSequence bsBufferCopyLittleEndian =
92 ImmutableByteSequence.copyFrom(bbLittleEndian);
93
94 // creates a new sequence from primitive type
95 ImmutableByteSequence bsLongCopy =
96 ImmutableByteSequence.copyFrom(longValue);
97
98
99 new EqualsTester()
100 // big-endian byte array cannot be equal to little-endian array
101 .addEqualityGroup(bbBigEndian.array())
102 .addEqualityGroup(bbLittleEndian.array())
103 // all byte sequences must be equal
104 .addEqualityGroup(bsBufferCopyBigEndian,
105 bsBufferCopyLittleEndian,
106 bsLongCopy)
107 // byte buffer views of all sequences must be equal
108 .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer(),
109 bsBufferCopyLittleEndian.asReadOnlyBuffer(),
110 bsLongCopy.asReadOnlyBuffer())
111 // byte buffer orders of all sequences must be ByteOrder.BIG_ENDIAN
112 .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer().order(),
113 bsBufferCopyLittleEndian.asReadOnlyBuffer().order(),
114 bsLongCopy.asReadOnlyBuffer().order(),
115 ByteOrder.BIG_ENDIAN)
116 .testEquals();
117 }
118}