Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onlab.util; |
| 18 | |
| 19 | import com.google.common.testing.EqualsTester; |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame^] | 20 | import org.junit.Rule; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame^] | 22 | import org.junit.rules.ExpectedException; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.nio.ByteOrder; |
| 26 | import java.util.Random; |
| 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.equalTo; |
| 30 | import static org.hamcrest.Matchers.is; |
| 31 | |
| 32 | public class ImmutableByteSequenceTest { |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame^] | 33 | @Rule |
| 34 | public ExpectedException thrown = ExpectedException.none(); |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 35 | |
| 36 | @Test |
| 37 | public void testCopy() throws Exception { |
| 38 | |
| 39 | byte byteValue = (byte) 1; |
| 40 | short shortValue = (short) byteValue; |
| 41 | int intValue = (int) byteValue; |
| 42 | long longValue = (long) byteValue; |
| 43 | byte[] arrayValue = new byte[64]; |
| 44 | arrayValue[63] = byteValue; |
| 45 | ByteBuffer bufferValue = ByteBuffer.allocate(64).put(arrayValue); |
| 46 | |
| 47 | ImmutableByteSequence bsByte = ImmutableByteSequence.copyFrom(byteValue); |
| 48 | ImmutableByteSequence bsShort = ImmutableByteSequence.copyFrom(shortValue); |
| 49 | ImmutableByteSequence bsInt = ImmutableByteSequence.copyFrom(intValue); |
| 50 | ImmutableByteSequence bsLong = ImmutableByteSequence.copyFrom(longValue); |
| 51 | ImmutableByteSequence bsArray = ImmutableByteSequence.copyFrom(arrayValue); |
| 52 | ImmutableByteSequence bsBuffer = ImmutableByteSequence.copyFrom(bufferValue); |
| 53 | |
| 54 | assertThat("byte sequence of a byte value must have size 1", |
| 55 | bsByte.size(), is(equalTo(1))); |
| 56 | assertThat("byte sequence of a short value must have size 2", |
| 57 | bsShort.size(), is(equalTo(2))); |
| 58 | assertThat("byte sequence of an int value must have size 4", |
| 59 | bsInt.size(), is(equalTo(4))); |
| 60 | assertThat("byte sequence of a long value must have size 8", |
| 61 | bsLong.size(), is(equalTo(8))); |
| 62 | assertThat("byte sequence of a byte array value must have same size of the array", |
| 63 | bsArray.size(), is(equalTo(arrayValue.length))); |
| 64 | assertThat("byte sequence of a byte buffer value must have same size of the buffer", |
| 65 | bsBuffer.size(), is(equalTo(bufferValue.capacity()))); |
| 66 | |
| 67 | String errStr = "incorrect byte sequence value"; |
| 68 | |
| 69 | assertThat(errStr, bsByte.asArray()[0], is(equalTo(byteValue))); |
| 70 | assertThat(errStr, bsShort.asArray()[1], is(equalTo(byteValue))); |
| 71 | assertThat(errStr, bsInt.asArray()[3], is(equalTo(byteValue))); |
| 72 | assertThat(errStr, bsLong.asArray()[7], is(equalTo(byteValue))); |
| 73 | assertThat(errStr, bsArray.asArray()[63], is(equalTo(byteValue))); |
| 74 | assertThat(errStr, bsBuffer.asArray()[63], is(equalTo(byteValue))); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testEndianness() throws Exception { |
| 79 | |
| 80 | long longValue = new Random().nextLong(); |
| 81 | |
| 82 | // creates a new sequence from a big-endian buffer |
| 83 | ByteBuffer bbBigEndian = ByteBuffer |
| 84 | .allocate(8) |
| 85 | .order(ByteOrder.BIG_ENDIAN) |
| 86 | .putLong(longValue); |
| 87 | ImmutableByteSequence bsBufferCopyBigEndian = |
| 88 | ImmutableByteSequence.copyFrom(bbBigEndian); |
| 89 | |
| 90 | // creates a new sequence from a little-endian buffer |
| 91 | ByteBuffer bbLittleEndian = ByteBuffer |
| 92 | .allocate(8) |
| 93 | .order(ByteOrder.LITTLE_ENDIAN) |
| 94 | .putLong(longValue); |
| 95 | ImmutableByteSequence bsBufferCopyLittleEndian = |
| 96 | ImmutableByteSequence.copyFrom(bbLittleEndian); |
| 97 | |
| 98 | // creates a new sequence from primitive type |
| 99 | ImmutableByteSequence bsLongCopy = |
| 100 | ImmutableByteSequence.copyFrom(longValue); |
| 101 | |
| 102 | |
| 103 | new EqualsTester() |
| 104 | // big-endian byte array cannot be equal to little-endian array |
| 105 | .addEqualityGroup(bbBigEndian.array()) |
| 106 | .addEqualityGroup(bbLittleEndian.array()) |
| 107 | // all byte sequences must be equal |
| 108 | .addEqualityGroup(bsBufferCopyBigEndian, |
| 109 | bsBufferCopyLittleEndian, |
| 110 | bsLongCopy) |
| 111 | // byte buffer views of all sequences must be equal |
| 112 | .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer(), |
| 113 | bsBufferCopyLittleEndian.asReadOnlyBuffer(), |
| 114 | bsLongCopy.asReadOnlyBuffer()) |
| 115 | // byte buffer orders of all sequences must be ByteOrder.BIG_ENDIAN |
| 116 | .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer().order(), |
| 117 | bsBufferCopyLittleEndian.asReadOnlyBuffer().order(), |
| 118 | bsLongCopy.asReadOnlyBuffer().order(), |
| 119 | ByteOrder.BIG_ENDIAN) |
| 120 | .testEquals(); |
| 121 | } |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame^] | 122 | |
| 123 | @Test |
| 124 | public void testBitSetMethods() throws Exception { |
| 125 | // All zeros tests |
| 126 | assertThat("3 bytes, all 0's", |
| 127 | ImmutableByteSequence.ofZeros(3), |
| 128 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 129 | new byte[]{0, 0, 0})))); |
| 130 | assertThat("3 bytes, all 0's via prefix", |
| 131 | ImmutableByteSequence.prefixZeros(3, 3 * Byte.SIZE), |
| 132 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 133 | new byte[]{0, 0, 0})))); |
| 134 | |
| 135 | // All ones tests |
| 136 | assertThat("3 bytes, all 1's", |
| 137 | ImmutableByteSequence.ofZeros(3), |
| 138 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 139 | new byte[]{0, 0, 0})))); |
| 140 | assertThat("3 bytes, all 1's via prefix", |
| 141 | ImmutableByteSequence.prefixOnes(3, 3 * Byte.SIZE), |
| 142 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 143 | new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff})))); |
| 144 | |
| 145 | // Zero prefix tests |
| 146 | assertThat("2 bytes, prefixed with 5 0's", |
| 147 | ImmutableByteSequence.prefix(2, 5, (byte) 0), |
| 148 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 149 | new byte[]{(byte) 0x7, (byte) 0xff})))); |
| 150 | assertThat("4 bytes, prefixed with 16 0's", |
| 151 | ImmutableByteSequence.prefix(4, 16, (byte) 0), |
| 152 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 153 | new byte[]{0, 0, (byte) 0xff, (byte) 0xff})))); |
| 154 | assertThat("4 bytes, prefixed with 20 0's", |
| 155 | ImmutableByteSequence.prefix(4, 20, (byte) 0), |
| 156 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 157 | new byte[]{0, 0, (byte) 0x0f, (byte) 0xff})))); |
| 158 | assertThat("8 bytes, prefixed with 36 0's", |
| 159 | ImmutableByteSequence.prefixZeros(8, 38), |
| 160 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 161 | new byte[]{0, 0, 0, 0, (byte) 0x03, (byte) 0xff, (byte) 0xff, (byte) 0xff})))); |
| 162 | |
| 163 | // Ones prefix tests |
| 164 | assertThat("2 bytes, prefixed with 5 1's", |
| 165 | ImmutableByteSequence.prefix(2, 5, (byte) 0xff), |
| 166 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 167 | new byte[]{(byte) 0xf8, 0})))); |
| 168 | assertThat("4 bytes, prefixed with 16 1's", |
| 169 | ImmutableByteSequence.prefix(4, 16, (byte) 0xff), |
| 170 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 171 | new byte[]{(byte) 0xff, (byte) 0xff, 0, 0})))); |
| 172 | assertThat("4 bytes, prefixed with 20 1's", |
| 173 | ImmutableByteSequence.prefix(4, 20, (byte) 0xff), |
| 174 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 175 | new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xf0, 0})))); |
| 176 | assertThat("8 bytes, prefixed with 10 1's", |
| 177 | ImmutableByteSequence.prefixOnes(8, 10), |
| 178 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 179 | new byte[]{(byte) 0xff, (byte) 0xc0, 0, 0, 0, 0, 0, 0})))); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testBadPrefixVal() { |
| 184 | thrown.expect(IllegalArgumentException.class); |
| 185 | thrown.reportMissingExceptionWithMessage( |
| 186 | "Expect IllegalArgumentException due to val = 0x7"); |
| 187 | ImmutableByteSequence.prefix(5, 10, (byte) 0x7); |
| 188 | } |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 189 | } |