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; |
Yuta HIGUCHI | 6800ced | 2017-11-20 11:15:37 -0800 | [diff] [blame] | 20 | |
| 21 | import org.apache.commons.lang3.RandomUtils; |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 22 | import org.junit.Assert; |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 23 | import org.junit.Rule; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 24 | import org.junit.Test; |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 25 | import org.junit.rules.ExpectedException; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | import java.nio.ByteOrder; |
| 29 | import java.util.Random; |
| 30 | |
Carmelo Cascone | d047bd2 | 2017-08-29 21:36:07 +0200 | [diff] [blame] | 31 | import static java.lang.Integer.max; |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 32 | import static java.lang.String.format; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.equalTo; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | |
| 37 | public class ImmutableByteSequenceTest { |
Carmelo Cascone | d047bd2 | 2017-08-29 21:36:07 +0200 | [diff] [blame] | 38 | public static final int MIN_RAND_FIT_VALUE = 0xf; |
| 39 | public static final int MAX_RAND_FIT_VALUE = 0x7fffffff; |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 40 | @Rule |
| 41 | public ExpectedException thrown = ExpectedException.none(); |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 42 | |
| 43 | @Test |
| 44 | public void testCopy() throws Exception { |
| 45 | |
| 46 | byte byteValue = (byte) 1; |
Yuta HIGUCHI | 6800ced | 2017-11-20 11:15:37 -0800 | [diff] [blame] | 47 | short shortValue = byteValue; |
| 48 | int intValue = byteValue; |
| 49 | long longValue = byteValue; |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 50 | byte[] arrayValue = new byte[64]; |
| 51 | arrayValue[63] = byteValue; |
| 52 | ByteBuffer bufferValue = ByteBuffer.allocate(64).put(arrayValue); |
| 53 | |
| 54 | ImmutableByteSequence bsByte = ImmutableByteSequence.copyFrom(byteValue); |
| 55 | ImmutableByteSequence bsShort = ImmutableByteSequence.copyFrom(shortValue); |
| 56 | ImmutableByteSequence bsInt = ImmutableByteSequence.copyFrom(intValue); |
| 57 | ImmutableByteSequence bsLong = ImmutableByteSequence.copyFrom(longValue); |
| 58 | ImmutableByteSequence bsArray = ImmutableByteSequence.copyFrom(arrayValue); |
| 59 | ImmutableByteSequence bsBuffer = ImmutableByteSequence.copyFrom(bufferValue); |
| 60 | |
| 61 | assertThat("byte sequence of a byte value must have size 1", |
| 62 | bsByte.size(), is(equalTo(1))); |
| 63 | assertThat("byte sequence of a short value must have size 2", |
| 64 | bsShort.size(), is(equalTo(2))); |
| 65 | assertThat("byte sequence of an int value must have size 4", |
| 66 | bsInt.size(), is(equalTo(4))); |
| 67 | assertThat("byte sequence of a long value must have size 8", |
| 68 | bsLong.size(), is(equalTo(8))); |
| 69 | assertThat("byte sequence of a byte array value must have same size of the array", |
| 70 | bsArray.size(), is(equalTo(arrayValue.length))); |
| 71 | assertThat("byte sequence of a byte buffer value must have same size of the buffer", |
| 72 | bsBuffer.size(), is(equalTo(bufferValue.capacity()))); |
| 73 | |
| 74 | String errStr = "incorrect byte sequence value"; |
| 75 | |
| 76 | assertThat(errStr, bsByte.asArray()[0], is(equalTo(byteValue))); |
| 77 | assertThat(errStr, bsShort.asArray()[1], is(equalTo(byteValue))); |
| 78 | assertThat(errStr, bsInt.asArray()[3], is(equalTo(byteValue))); |
| 79 | assertThat(errStr, bsLong.asArray()[7], is(equalTo(byteValue))); |
| 80 | assertThat(errStr, bsArray.asArray()[63], is(equalTo(byteValue))); |
| 81 | assertThat(errStr, bsBuffer.asArray()[63], is(equalTo(byteValue))); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testEndianness() throws Exception { |
| 86 | |
Yuta HIGUCHI | 6800ced | 2017-11-20 11:15:37 -0800 | [diff] [blame] | 87 | long longValue = RandomUtils.nextLong(); |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 88 | |
| 89 | // creates a new sequence from a big-endian buffer |
| 90 | ByteBuffer bbBigEndian = ByteBuffer |
| 91 | .allocate(8) |
| 92 | .order(ByteOrder.BIG_ENDIAN) |
| 93 | .putLong(longValue); |
| 94 | ImmutableByteSequence bsBufferCopyBigEndian = |
| 95 | ImmutableByteSequence.copyFrom(bbBigEndian); |
| 96 | |
| 97 | // creates a new sequence from a little-endian buffer |
| 98 | ByteBuffer bbLittleEndian = ByteBuffer |
| 99 | .allocate(8) |
| 100 | .order(ByteOrder.LITTLE_ENDIAN) |
| 101 | .putLong(longValue); |
| 102 | ImmutableByteSequence bsBufferCopyLittleEndian = |
| 103 | ImmutableByteSequence.copyFrom(bbLittleEndian); |
| 104 | |
| 105 | // creates a new sequence from primitive type |
| 106 | ImmutableByteSequence bsLongCopy = |
| 107 | ImmutableByteSequence.copyFrom(longValue); |
| 108 | |
| 109 | |
| 110 | new EqualsTester() |
| 111 | // big-endian byte array cannot be equal to little-endian array |
| 112 | .addEqualityGroup(bbBigEndian.array()) |
| 113 | .addEqualityGroup(bbLittleEndian.array()) |
| 114 | // all byte sequences must be equal |
| 115 | .addEqualityGroup(bsBufferCopyBigEndian, |
| 116 | bsBufferCopyLittleEndian, |
| 117 | bsLongCopy) |
| 118 | // byte buffer views of all sequences must be equal |
| 119 | .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer(), |
| 120 | bsBufferCopyLittleEndian.asReadOnlyBuffer(), |
| 121 | bsLongCopy.asReadOnlyBuffer()) |
| 122 | // byte buffer orders of all sequences must be ByteOrder.BIG_ENDIAN |
| 123 | .addEqualityGroup(bsBufferCopyBigEndian.asReadOnlyBuffer().order(), |
| 124 | bsBufferCopyLittleEndian.asReadOnlyBuffer().order(), |
| 125 | bsLongCopy.asReadOnlyBuffer().order(), |
| 126 | ByteOrder.BIG_ENDIAN) |
| 127 | .testEquals(); |
| 128 | } |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 129 | |
| 130 | @Test |
| 131 | public void testBitSetMethods() throws Exception { |
| 132 | // All zeros tests |
| 133 | assertThat("3 bytes, all 0's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 134 | ImmutableByteSequence.ofZeros(3), |
| 135 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 136 | new byte[]{0, 0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 137 | assertThat("3 bytes, all 0's via prefix", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 138 | ImmutableByteSequence.prefixZeros(3, 3 * Byte.SIZE), |
| 139 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 140 | new byte[]{0, 0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 141 | |
| 142 | // All ones tests |
| 143 | assertThat("3 bytes, all 1's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 144 | ImmutableByteSequence.ofZeros(3), |
| 145 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 146 | new byte[]{0, 0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 147 | assertThat("3 bytes, all 1's via prefix", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 148 | ImmutableByteSequence.prefixOnes(3, 3 * Byte.SIZE), |
| 149 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 150 | new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 151 | |
| 152 | // Zero prefix tests |
| 153 | assertThat("2 bytes, prefixed with 5 0's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 154 | ImmutableByteSequence.prefix(2, 5, (byte) 0), |
| 155 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 156 | new byte[]{(byte) 0x7, (byte) 0xff})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 157 | assertThat("4 bytes, prefixed with 16 0's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 158 | ImmutableByteSequence.prefix(4, 16, (byte) 0), |
| 159 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 160 | new byte[]{0, 0, (byte) 0xff, (byte) 0xff})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 161 | assertThat("4 bytes, prefixed with 20 0's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 162 | ImmutableByteSequence.prefix(4, 20, (byte) 0), |
| 163 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 164 | new byte[]{0, 0, (byte) 0x0f, (byte) 0xff})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 165 | assertThat("8 bytes, prefixed with 36 0's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 166 | ImmutableByteSequence.prefixZeros(8, 38), |
| 167 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 168 | new byte[]{0, 0, 0, 0, (byte) 0x03, (byte) 0xff, (byte) 0xff, (byte) 0xff})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 169 | |
| 170 | // Ones prefix tests |
| 171 | assertThat("2 bytes, prefixed with 5 1's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 172 | ImmutableByteSequence.prefix(2, 5, (byte) 0xff), |
| 173 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 174 | new byte[]{(byte) 0xf8, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 175 | assertThat("4 bytes, prefixed with 16 1's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 176 | ImmutableByteSequence.prefix(4, 16, (byte) 0xff), |
| 177 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 178 | new byte[]{(byte) 0xff, (byte) 0xff, 0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 179 | assertThat("4 bytes, prefixed with 20 1's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 180 | ImmutableByteSequence.prefix(4, 20, (byte) 0xff), |
| 181 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 182 | new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xf0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 183 | assertThat("8 bytes, prefixed with 10 1's", |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 184 | ImmutableByteSequence.prefixOnes(8, 10), |
| 185 | is(equalTo(ImmutableByteSequence.copyFrom( |
| 186 | new byte[]{(byte) 0xff, (byte) 0xc0, 0, 0, 0, 0, 0, 0})))); |
Brian O'Connor | af1d12e | 2017-08-17 12:21:48 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void testBadPrefixVal() { |
| 191 | thrown.expect(IllegalArgumentException.class); |
| 192 | thrown.reportMissingExceptionWithMessage( |
| 193 | "Expect IllegalArgumentException due to val = 0x7"); |
| 194 | ImmutableByteSequence.prefix(5, 10, (byte) 0x7); |
| 195 | } |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 196 | |
| 197 | @Test |
| 198 | public void testMsbIndex() { |
| 199 | assertThat("Value 0 should have MSB index -1", |
| 200 | ImmutableByteSequence.copyFrom(0).msbIndex(), is(-1)); |
| 201 | for (int i = 0; i < 63; i++) { |
| 202 | long value = (long) Math.pow(2, i); |
| 203 | assertThat(format("Value %d should have MSB index %d", value, i), |
| 204 | ImmutableByteSequence.copyFrom(value).msbIndex(), is(i)); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | private void checkIllegalFit(ImmutableByteSequence bytes, int bitWidth) { |
| 209 | try { |
| 210 | ImmutableByteSequence.fit(bytes, bitWidth); |
| 211 | Assert.fail(format("Except ByteSequenceTrimException due to value = %s and bitWidth %d", |
| 212 | bytes.toString(), bitWidth)); |
| 213 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 214 | // We expect this. |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private void checkLegalFit(ImmutableByteSequence bytes, int bitWidth) |
| 219 | throws ImmutableByteSequence.ByteSequenceTrimException { |
| 220 | ImmutableByteSequence fitBytes = ImmutableByteSequence.fit(bytes, bitWidth); |
| 221 | ImmutableByteSequence sameBytes = ImmutableByteSequence.fit(fitBytes, bytes.size() * 8); |
| 222 | assertThat(format("Fitted value %s (re-extended to %s) not equal to original value %s", |
| 223 | fitBytes, sameBytes, bytes), |
| 224 | sameBytes, |
| 225 | is(equalTo(bytes))); |
| 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void testFit() throws ImmutableByteSequence.ByteSequenceTrimException { |
| 230 | // Test fit by forcing a given MSB index. |
| 231 | for (int msbIndex = 0; msbIndex < 32; msbIndex++) { |
| 232 | long value = (long) Math.pow(2, msbIndex); |
| 233 | ImmutableByteSequence bytes = ImmutableByteSequence.copyFrom(value); |
| 234 | checkLegalFit(bytes, msbIndex + 1); |
| 235 | if (msbIndex != 0) { |
| 236 | checkIllegalFit(bytes, msbIndex); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | @Test |
| 242 | public void testRandomFit() throws ImmutableByteSequence.ByteSequenceTrimException { |
| 243 | // Test fit against the computed MSB index. |
| 244 | Random random = new Random(); |
| 245 | for (int i = 0; i < 1000; i++) { |
Carmelo Cascone | d047bd2 | 2017-08-29 21:36:07 +0200 | [diff] [blame] | 246 | int randValue = random.nextInt((MAX_RAND_FIT_VALUE - MIN_RAND_FIT_VALUE) + 1) + MIN_RAND_FIT_VALUE; |
| 247 | ImmutableByteSequence bytes = ImmutableByteSequence.copyFrom((long) randValue); |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 248 | int msbIndex = bytes.msbIndex(); |
Carmelo Cascone | d047bd2 | 2017-08-29 21:36:07 +0200 | [diff] [blame] | 249 | // Truncate. |
| 250 | checkIllegalFit(bytes, max(msbIndex - random.nextInt(16), 1)); |
| 251 | // Expand. |
| 252 | checkLegalFit(bytes, msbIndex + 2 + random.nextInt(128)); |
| 253 | // Fit to same bit-width of original value. |
Carmelo Cascone | b10194c | 2017-08-23 19:16:51 +0200 | [diff] [blame] | 254 | checkLegalFit(bytes, msbIndex + 1); |
| 255 | } |
| 256 | } |
Carmelo Cascone | 6b32c99 | 2016-04-13 11:53:09 -0700 | [diff] [blame] | 257 | } |