| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import org.testng.annotations.Test; |
| 17 |
|
| 18 |
import java.io.ByteArrayInputStream; |
| 19 |
import java.io.ByteArrayOutputStream; |
| 20 |
import java.io.IOException; |
| 21 |
import java.nio.file.Files; |
| 22 |
import java.nio.file.Path; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Random; |
| 28 |
|
| 29 |
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE; |
| 30 |
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE; |
| 31 |
import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT; |
| 32 |
import static io.airlift.slice.SizeOf.SIZE_OF_INT; |
| 33 |
import static io.airlift.slice.SizeOf.SIZE_OF_LONG; |
| 34 |
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT; |
| 35 |
import static io.airlift.slice.Slices.EMPTY_SLICE; |
| 36 |
import static io.airlift.slice.Slices.utf8Slice; |
| 37 |
import static java.lang.Double.doubleToLongBits; |
| 38 |
import static java.lang.Double.longBitsToDouble; |
| 39 |
import static java.lang.Float.floatToIntBits; |
| 40 |
import static java.lang.Float.intBitsToFloat; |
| 41 |
import static java.nio.charset.StandardCharsets.UTF_8; |
| 42 |
import static org.testng.Assert.assertEquals; |
| 43 |
import static org.testng.Assert.assertFalse; |
| 44 |
import static org.testng.Assert.assertNotEquals; |
| 45 |
import static org.testng.Assert.assertSame; |
| 46 |
import static org.testng.Assert.assertTrue; |
| 47 |
import static org.testng.Assert.fail; |
| 48 |
|
| 49 |
public class TestSlice |
| 50 |
{ |
| 51 |
@Test |
| 52 |
public void testFillAndClear() |
| 53 |
{ |
| 54 |
for (byte size = 0; size < 100; size++) { |
| 55 |
Slice slice = allocate(size); |
| 56 |
for (int i = 0; i < slice.length(); i++) { |
| 57 |
assertEquals(slice.getByte(i), (byte) 0); |
| 58 |
} |
| 59 |
slice.fill((byte) 0xA5); |
| 60 |
for (int i = 0; i < slice.length(); i++) { |
| 61 |
assertEquals(slice.getByte(i), (byte) 0xA5); |
| 62 |
} |
| 63 |
slice.clear(); |
| 64 |
for (int i = 0; i < slice.length(); i++) { |
| 65 |
assertEquals(slice.getByte(i), (byte) 0); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void testEqualsHashCodeCompare() |
| 72 |
{ |
| 73 |
for (int size = 0; size < 100; size++) { |
| 74 |
// self equals |
| 75 |
Slice slice = allocate(size); |
| 76 |
assertSlicesEquals(slice, slice); |
| 77 |
|
| 78 |
// equals other all zero |
| 79 |
Slice other = allocate(size); |
| 80 |
assertSlicesEquals(slice, other); |
| 81 |
|
| 82 |
// equals self fill pattern |
| 83 |
slice = allocate(size); // create a new slice since slices cache the hash code value |
| 84 |
slice.fill((byte) 0xA5); |
| 85 |
assertSlicesEquals(slice, slice); |
| 86 |
|
| 87 |
// equals other fill pattern |
| 88 |
other = allocate(size); // create a new slice since slices cache the hash code value |
| 89 |
other.fill((byte) 0xA5); |
| 90 |
assertSlicesEquals(slice, other); |
| 91 |
|
| 92 |
// different types |
| 93 |
assertNotEquals(slice, new Object()); |
| 94 |
//noinspection MisorderedAssertEqualsArgumentsTestNG |
| 95 |
assertNotEquals(new Object(), slice); |
| 96 |
|
| 97 |
// different sizes |
| 98 |
Slice oneBigger = allocate(size + 1); |
| 99 |
oneBigger.fill((byte) 0xA5); |
| 100 |
assertNotEquals(slice, oneBigger); |
| 101 |
assertNotEquals(oneBigger, slice); |
| 102 |
assertTrue(slice.compareTo(oneBigger) < 0); |
| 103 |
assertTrue(oneBigger.compareTo(slice) > 0); |
| 104 |
assertFalse(slice.equals(0, size, oneBigger, 0, size + 1)); |
| 105 |
assertFalse(oneBigger.equals(0, size + 1, slice, 0, size)); |
| 106 |
assertTrue(slice.compareTo(0, size, oneBigger, 0, size + 1) < 0); |
| 107 |
assertTrue(oneBigger.compareTo(0, size + 1, slice, 0, size) > 0); |
| 108 |
|
| 109 |
// different in one byte |
| 110 |
for (int i = 1; i < slice.length(); i++) { |
| 111 |
slice.setByte(i - 1, 0xA5); |
| 112 |
assertTrue(slice.equals(i - 1, size - i, other, i - 1, size - i)); |
| 113 |
slice.setByte(i, 0xFF); |
| 114 |
assertNotEquals(slice, other); |
| 115 |
assertFalse(slice.equals(i, size - i, other, i, size - i)); |
| 116 |
assertTrue(slice.compareTo(0, size, oneBigger, 0, size + 1) > 0); |
| 117 |
} |
| 118 |
|
| 119 |
// compare with empty slice |
| 120 |
if (slice.length() > 0) { |
| 121 |
assertNotEquals(slice, EMPTY_SLICE); |
| 122 |
//noinspection MisorderedAssertEqualsArgumentsTestNG |
| 123 |
assertNotEquals(EMPTY_SLICE, slice); |
| 124 |
|
| 125 |
assertFalse(slice.equals(0, size, EMPTY_SLICE, 0, 0)); |
| 126 |
assertFalse(EMPTY_SLICE.equals(0, 0, slice, 0, size)); |
| 127 |
|
| 128 |
assertTrue(slice.compareTo(0, size, EMPTY_SLICE, 0, 0) > 0); |
| 129 |
assertTrue(EMPTY_SLICE.compareTo(0, 0, slice, 0, size) < 0); |
| 130 |
|
| 131 |
try { |
| 132 |
//noinspection ResultOfMethodCallIgnored |
| 133 |
slice.equals(0, size, EMPTY_SLICE, 0, size); |
| 134 |
fail("expected IndexOutOfBoundsException"); |
| 135 |
} |
| 136 |
catch (IndexOutOfBoundsException expected) { |
| 137 |
} |
| 138 |
try { |
| 139 |
//noinspection ResultOfMethodCallIgnored |
| 140 |
EMPTY_SLICE.equals(0, size, slice, 0, size); |
| 141 |
fail("expected IndexOutOfBoundsException"); |
| 142 |
} |
| 143 |
catch (IndexOutOfBoundsException expected) { |
| 144 |
} |
| 145 |
|
| 146 |
try { |
| 147 |
slice.compareTo(0, size, EMPTY_SLICE, 0, size); |
| 148 |
fail("expected IndexOutOfBoundsException"); |
| 149 |
} |
| 150 |
catch (IndexOutOfBoundsException expected) { |
| 151 |
} |
| 152 |
try { |
| 153 |
EMPTY_SLICE.compareTo(0, size, slice, 0, size); |
| 154 |
fail("expected IndexOutOfBoundsException"); |
| 155 |
} |
| 156 |
catch (IndexOutOfBoundsException expected) { |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private static void assertSlicesEquals(Slice slice, Slice other) |
| 163 |
{ |
| 164 |
int size = slice.length(); |
| 165 |
|
| 166 |
assertEquals(slice, other); |
| 167 |
assertTrue(slice.equals(0, size, other, 0, size)); |
| 168 |
assertEquals(slice.hashCode(), other.hashCode()); |
| 169 |
assertEquals(slice.hashCode(), other.hashCode(0, size)); |
| 170 |
assertEquals(slice.compareTo(other), 0); |
| 171 |
assertEquals(slice.compareTo(0, size, other, 0, size), 0); |
| 172 |
for (int i = 0; i < slice.length(); i++) { |
| 173 |
assertTrue(slice.equals(i, size - i, other, i, size - i)); |
| 174 |
assertEquals(slice.hashCode(i, size - i), other.hashCode(i, size - i)); |
| 175 |
assertEquals(slice.compareTo(i, size - i, other, i, size - i), 0); |
| 176 |
} |
| 177 |
for (int i = 0; i < slice.length(); i++) { |
| 178 |
assertTrue(slice.equals(0, size - i, other, 0, size - i)); |
| 179 |
assertEquals(slice.hashCode(0, size - i), other.hashCode(0, size - i)); |
| 180 |
assertEquals(slice.compareTo(0, size - i, other, 0, size - i), 0); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void testToString() |
| 186 |
{ |
| 187 |
assertEquals(Slices.copiedBuffer("apple", UTF_8).toString(UTF_8), "apple"); |
| 188 |
|
| 189 |
for (int size = 0; size < 100; size++) { |
| 190 |
for (int index = 0; index < size; index++) { |
| 191 |
assertToStrings(allocate(size), index); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void testUtf8Conversion() |
| 198 |
{ |
| 199 |
String s = "apple \u2603 snowman"; |
| 200 |
Slice slice = Slices.copiedBuffer(s, UTF_8); |
| 201 |
|
| 202 |
assertEquals(utf8Slice(s), slice); |
| 203 |
assertEquals(slice.toStringUtf8(), s); |
| 204 |
assertEquals(utf8Slice(s).toStringUtf8(), s); |
| 205 |
} |
| 206 |
|
| 207 |
@SuppressWarnings("CharUsedInArithmeticContext") |
| 208 |
private static void assertToStrings(Slice slice, int index) |
| 209 |
{ |
| 210 |
// fill slice with FF |
| 211 |
slice.fill((byte) 0xFF); |
| 212 |
|
| 213 |
// set and get the value |
| 214 |
char[] chars = new char[(slice.length() - index) / 2]; |
| 215 |
for (int i = 0; i < chars.length; i++) { |
| 216 |
chars[i] = (char) ('a' + (i % 26)); |
| 217 |
} |
| 218 |
String string = new String(chars); |
| 219 |
Slice value = Slices.copiedBuffer(string, UTF_8); |
| 220 |
slice.setBytes(index, value); |
| 221 |
assertEquals(slice.toString(index, value.length(), UTF_8), string); |
| 222 |
|
| 223 |
for (int length = 0; length < value.length(); length++) { |
| 224 |
slice.fill((byte) 0xFF); |
| 225 |
slice.setBytes(index, value, 0, length); |
| 226 |
assertEquals(slice.toString(index, length, UTF_8), string.substring(0, length)); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void testByte() |
| 232 |
{ |
| 233 |
for (byte size = 0; size < 100; size++) { |
| 234 |
for (byte index = 0; index < (size - SIZE_OF_BYTE); index++) { |
| 235 |
assertByte(allocate(size), index); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private static void assertByte(Slice slice, byte index) |
| 241 |
{ |
| 242 |
// fill slice with FF |
| 243 |
slice.fill((byte) 0xFF); |
| 244 |
|
| 245 |
// set and get unsigned value |
| 246 |
slice.setByte(index, 0xA5); |
| 247 |
assertEquals(slice.getUnsignedByte(index), 0x0000_00A5); |
| 248 |
|
| 249 |
// set and get the value |
| 250 |
slice.setByte(index, 0xA5); |
| 251 |
assertEquals(slice.getByte(index), (byte) 0xA5); |
| 252 |
|
| 253 |
try { |
| 254 |
slice.getByte(-1); |
| 255 |
fail("expected IndexOutOfBoundsException"); |
| 256 |
} |
| 257 |
catch (IndexOutOfBoundsException e) { |
| 258 |
} |
| 259 |
|
| 260 |
try { |
| 261 |
slice.getByte((slice.length() - SIZE_OF_BYTE) + 1); |
| 262 |
fail("expected IndexOutOfBoundsException"); |
| 263 |
} |
| 264 |
catch (IndexOutOfBoundsException e) { |
| 265 |
} |
| 266 |
|
| 267 |
try { |
| 268 |
slice.getByte(slice.length()); |
| 269 |
fail("expected IndexOutOfBoundsException"); |
| 270 |
} |
| 271 |
catch (IndexOutOfBoundsException e) { |
| 272 |
} |
| 273 |
|
| 274 |
try { |
| 275 |
slice.getByte(slice.length() + 1); |
| 276 |
fail("expected IndexOutOfBoundsException"); |
| 277 |
} |
| 278 |
catch (IndexOutOfBoundsException e) { |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
@Test |
| 283 |
public void testShort() |
| 284 |
{ |
| 285 |
for (short size = 0; size < 100; size++) { |
| 286 |
for (short index = 0; index < (size - SIZE_OF_SHORT); index++) { |
| 287 |
assertShort(allocate(size), index); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
private static void assertShort(Slice slice, short index) |
| 293 |
{ |
| 294 |
// fill slice with FF |
| 295 |
slice.fill((byte) 0xFF); |
| 296 |
|
| 297 |
// set and get the value |
| 298 |
slice.setShort(index, 0xAA55); |
| 299 |
assertEquals(slice.getShort(index), (short) 0xAA55); |
| 300 |
|
| 301 |
try { |
| 302 |
slice.getShort(-1); |
| 303 |
fail("expected IndexOutOfBoundsException"); |
| 304 |
} |
| 305 |
catch (IndexOutOfBoundsException e) { |
| 306 |
} |
| 307 |
|
| 308 |
try { |
| 309 |
slice.getShort((slice.length() - SIZE_OF_SHORT) + 1); |
| 310 |
fail("expected IndexOutOfBoundsException"); |
| 311 |
} |
| 312 |
catch (IndexOutOfBoundsException e) { |
| 313 |
} |
| 314 |
|
| 315 |
try { |
| 316 |
slice.getShort(slice.length()); |
| 317 |
fail("expected IndexOutOfBoundsException"); |
| 318 |
} |
| 319 |
catch (IndexOutOfBoundsException e) { |
| 320 |
} |
| 321 |
|
| 322 |
try { |
| 323 |
slice.getShort(slice.length() + 1); |
| 324 |
fail("expected IndexOutOfBoundsException"); |
| 325 |
} |
| 326 |
catch (IndexOutOfBoundsException e) { |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
@Test |
| 331 |
public void testInt() |
| 332 |
{ |
| 333 |
for (int size = 0; size < 100; size++) { |
| 334 |
for (int index = 0; index < (size - SIZE_OF_INT); index++) { |
| 335 |
assertInt(allocate(size), index); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
private static void assertInt(Slice slice, int index) |
| 341 |
{ |
| 342 |
// fill slice with FF |
| 343 |
slice.fill((byte) 0xFF); |
| 344 |
|
| 345 |
// set and get the value |
| 346 |
slice.setInt(index, 0xAAAA_5555); |
| 347 |
assertEquals(slice.getInt(index), 0xAAAA_5555); |
| 348 |
|
| 349 |
try { |
| 350 |
slice.getInt(-1); |
| 351 |
fail("expected IndexOutOfBoundsException"); |
| 352 |
} |
| 353 |
catch (IndexOutOfBoundsException e) { |
| 354 |
} |
| 355 |
|
| 356 |
try { |
| 357 |
slice.getInt((slice.length() - SIZE_OF_INT) + 1); |
| 358 |
fail("expected IndexOutOfBoundsException"); |
| 359 |
} |
| 360 |
catch (IndexOutOfBoundsException e) { |
| 361 |
} |
| 362 |
|
| 363 |
try { |
| 364 |
slice.getInt(slice.length()); |
| 365 |
fail("expected IndexOutOfBoundsException"); |
| 366 |
} |
| 367 |
catch (IndexOutOfBoundsException e) { |
| 368 |
} |
| 369 |
|
| 370 |
try { |
| 371 |
slice.getInt(slice.length() + 1); |
| 372 |
fail("expected IndexOutOfBoundsException"); |
| 373 |
} |
| 374 |
catch (IndexOutOfBoundsException e) { |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void testLong() |
| 380 |
{ |
| 381 |
for (int size = 0; size < 100; size++) { |
| 382 |
for (int index = 0; index < (size - SIZE_OF_LONG); index++) { |
| 383 |
assertLong(allocate(size), index); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
private static void assertLong(Slice slice, int index) |
| 389 |
{ |
| 390 |
// fill slice with FF |
| 391 |
slice.fill((byte) 0xFF); |
| 392 |
|
| 393 |
// set and get the value |
| 394 |
slice.setLong(index, 0xAAAA_AAAA_5555_5555L); |
| 395 |
assertEquals(slice.getLong(index), 0xAAAA_AAAA_5555_5555L); |
| 396 |
|
| 397 |
try { |
| 398 |
slice.getLong(-1); |
| 399 |
fail("expected IndexOutOfBoundsException"); |
| 400 |
} |
| 401 |
catch (IndexOutOfBoundsException e) { |
| 402 |
} |
| 403 |
|
| 404 |
try { |
| 405 |
slice.getLong((slice.length() - SIZE_OF_LONG) + 1); |
| 406 |
fail("expected IndexOutOfBoundsException"); |
| 407 |
} |
| 408 |
catch (IndexOutOfBoundsException e) { |
| 409 |
} |
| 410 |
|
| 411 |
try { |
| 412 |
slice.getLong(slice.length()); |
| 413 |
fail("expected IndexOutOfBoundsException"); |
| 414 |
} |
| 415 |
catch (IndexOutOfBoundsException e) { |
| 416 |
} |
| 417 |
|
| 418 |
try { |
| 419 |
slice.getLong(slice.length() + 1); |
| 420 |
fail("expected IndexOutOfBoundsException"); |
| 421 |
} |
| 422 |
catch (IndexOutOfBoundsException e) { |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
@Test |
| 427 |
public void testFloat() |
| 428 |
{ |
| 429 |
for (int size = 0; size < 100; size++) { |
| 430 |
for (int index = 0; index < (size - SIZE_OF_FLOAT); index++) { |
| 431 |
assertFloat(allocate(size), index); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
private static void assertFloat(Slice slice, int index) |
| 437 |
{ |
| 438 |
// fill slice with FF |
| 439 |
slice.fill((byte) 0xFF); |
| 440 |
|
| 441 |
// set and get the value |
| 442 |
slice.setFloat(index, intBitsToFloat(0xAAAA_5555)); |
| 443 |
assertEquals(floatToIntBits(slice.getFloat(index)), 0xAAAA_5555); |
| 444 |
|
| 445 |
try { |
| 446 |
slice.getFloat(-1); |
| 447 |
fail("expected IndexOutOfBoundsException"); |
| 448 |
} |
| 449 |
catch (IndexOutOfBoundsException e) { |
| 450 |
} |
| 451 |
|
| 452 |
try { |
| 453 |
slice.getFloat((slice.length() - SIZE_OF_FLOAT) + 1); |
| 454 |
fail("expected IndexOutOfBoundsException"); |
| 455 |
} |
| 456 |
catch (IndexOutOfBoundsException e) { |
| 457 |
} |
| 458 |
|
| 459 |
try { |
| 460 |
slice.getFloat(slice.length()); |
| 461 |
fail("expected IndexOutOfBoundsException"); |
| 462 |
} |
| 463 |
catch (IndexOutOfBoundsException e) { |
| 464 |
} |
| 465 |
|
| 466 |
try { |
| 467 |
slice.getFloat(slice.length() + 1); |
| 468 |
fail("expected IndexOutOfBoundsException"); |
| 469 |
} |
| 470 |
catch (IndexOutOfBoundsException e) { |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
@Test |
| 475 |
public void testDouble() |
| 476 |
{ |
| 477 |
for (int size = 0; size < 100; size++) { |
| 478 |
for (int index = 0; index < (size - SIZE_OF_DOUBLE); index++) { |
| 479 |
assertDouble(allocate(size), index); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
private static void assertDouble(Slice slice, int index) |
| 485 |
{ |
| 486 |
// fill slice with FF |
| 487 |
slice.fill((byte) 0xFF); |
| 488 |
|
| 489 |
// set and get the value |
| 490 |
slice.setDouble(index, longBitsToDouble(0xAAAA_AAAA_5555_5555L)); |
| 491 |
assertEquals(doubleToLongBits(slice.getDouble(index)), 0xAAAA_AAAA_5555_5555L); |
| 492 |
|
| 493 |
try { |
| 494 |
slice.getDouble(-1); |
| 495 |
fail("expected IndexOutOfBoundsException"); |
| 496 |
} |
| 497 |
catch (IndexOutOfBoundsException e) { |
| 498 |
} |
| 499 |
|
| 500 |
try { |
| 501 |
slice.getDouble((slice.length() - SIZE_OF_DOUBLE) + 1); |
| 502 |
fail("expected IndexOutOfBoundsException"); |
| 503 |
} |
| 504 |
catch (IndexOutOfBoundsException e) { |
| 505 |
} |
| 506 |
|
| 507 |
try { |
| 508 |
slice.getDouble(slice.length()); |
| 509 |
fail("expected IndexOutOfBoundsException"); |
| 510 |
} |
| 511 |
catch (IndexOutOfBoundsException e) { |
| 512 |
} |
| 513 |
|
| 514 |
try { |
| 515 |
slice.getDouble(slice.length() + 1); |
| 516 |
fail("expected IndexOutOfBoundsException"); |
| 517 |
} |
| 518 |
catch (IndexOutOfBoundsException e) { |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
@Test |
| 523 |
public void testBytesArray() |
| 524 |
{ |
| 525 |
for (int size = 0; size < 100; size++) { |
| 526 |
for (int index = 0; index < size; index++) { |
| 527 |
assertBytesArray(allocate(size), index); |
| 528 |
} |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
private static void assertBytesArray(Slice slice, int index) |
| 533 |
{ |
| 534 |
// fill slice with FF |
| 535 |
slice.fill((byte) 0xFF); |
| 536 |
|
| 537 |
byte[] value = new byte[slice.length()]; |
| 538 |
Arrays.fill(value, (byte) 0xFF); |
| 539 |
assertEquals(slice.getBytes(), value); |
| 540 |
|
| 541 |
// set and get the value |
| 542 |
value = new byte[(slice.length() - index) / 2]; |
| 543 |
for (int i = 0; i < value.length; i++) { |
| 544 |
value[i] = (byte) i; |
| 545 |
} |
| 546 |
slice.setBytes(index, value); |
| 547 |
assertEquals(slice.getBytes(index, value.length), value); |
| 548 |
|
| 549 |
for (int length = 0; length < value.length; length++) { |
| 550 |
slice.fill((byte) 0xFF); |
| 551 |
slice.setBytes(index, value, 0, length); |
| 552 |
assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length)); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
@Test |
| 557 |
public void testBytesSlice() |
| 558 |
{ |
| 559 |
for (int size = 0; size < 100; size++) { |
| 560 |
for (int index = 0; index < size; index++) { |
| 561 |
assertBytesSlice(allocate(size), index); |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
private void assertBytesSlice(Slice slice, int index) |
| 567 |
{ |
| 568 |
// fill slice with FF |
| 569 |
slice.fill((byte) 0xFF); |
| 570 |
|
| 571 |
// compare to self slice |
| 572 |
assertEquals(slice.slice(0, slice.length()), slice); |
| 573 |
Slice value = allocate(slice.length()); |
| 574 |
slice.getBytes(0, value, 0, slice.length()); |
| 575 |
assertEquals(value, slice); |
| 576 |
|
| 577 |
// set and get the value |
| 578 |
value = allocate((slice.length() - index) / 2); |
| 579 |
for (int i = 0; i < value.length(); i++) { |
| 580 |
value.setByte(i, i); |
| 581 |
} |
| 582 |
|
| 583 |
// check by slicing out the region |
| 584 |
slice.setBytes(index, value); |
| 585 |
assertEquals(value, slice.slice(index, value.length())); |
| 586 |
|
| 587 |
// check by getting out the region |
| 588 |
Slice tempValue = allocate(value.length()); |
| 589 |
slice.getBytes(index, tempValue, 0, tempValue.length()); |
| 590 |
assertEquals(tempValue, slice.slice(index, tempValue.length())); |
| 591 |
assertTrue(tempValue.equals(0, tempValue.length(), slice, index, tempValue.length())); |
| 592 |
|
| 593 |
for (int length = 0; length < value.length(); length++) { |
| 594 |
slice.fill((byte) 0xFF); |
| 595 |
slice.setBytes(index, value, 0, length); |
| 596 |
|
| 597 |
// check by slicing out the region |
| 598 |
assertEquals(value.slice(0, length), slice.slice(index, length)); |
| 599 |
assertTrue(value.equals(0, length, slice, index, length)); |
| 600 |
|
| 601 |
// check by getting out the region |
| 602 |
tempValue = allocate(length); |
| 603 |
slice.getBytes(index, tempValue); |
| 604 |
assertEquals(tempValue, slice.slice(index, length)); |
| 605 |
assertTrue(tempValue.equals(0, length, slice, index, length)); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
@Test |
| 610 |
public void testBytesStreams() |
| 611 |
throws Exception |
| 612 |
{ |
| 613 |
for (int size = 0; size < 100; size++) { |
| 614 |
for (int index = 0; index < size; index++) { |
| 615 |
assertBytesStreams(allocate(size), index); |
| 616 |
} |
| 617 |
} |
| 618 |
assertBytesStreams(allocate(16 * 1024), 3); |
| 619 |
} |
| 620 |
|
| 621 |
private static void assertBytesStreams(Slice slice, int index) |
| 622 |
throws Exception |
| 623 |
{ |
| 624 |
// fill slice with FF |
| 625 |
slice.fill((byte) 0xFF); |
| 626 |
|
| 627 |
byte[] value = new byte[slice.length()]; |
| 628 |
Arrays.fill(value, (byte) 0xFF); |
| 629 |
assertEquals(slice.getBytes(), value); |
| 630 |
|
| 631 |
// set and get the value |
| 632 |
value = new byte[(slice.length() - index) / 2]; |
| 633 |
for (int i = 0; i < value.length; i++) { |
| 634 |
value[i] = (byte) i; |
| 635 |
} |
| 636 |
slice.setBytes(index, new ByteArrayInputStream(value), value.length); |
| 637 |
assertEquals(slice.getBytes(index, value.length), value); |
| 638 |
|
| 639 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 640 |
slice.getBytes(index, out, value.length); |
| 641 |
assertEquals(slice.getBytes(index, value.length), out.toByteArray()); |
| 642 |
|
| 643 |
for (int length = 0; length < value.length; length++) { |
| 644 |
slice.fill((byte) 0xFF); |
| 645 |
slice.setBytes(index, new ByteArrayInputStream(value), length); |
| 646 |
assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length)); |
| 647 |
|
| 648 |
out = new ByteArrayOutputStream(); |
| 649 |
slice.getBytes(index, out, length); |
| 650 |
assertEquals(slice.getBytes(index, length), out.toByteArray()); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
@Test |
| 655 |
public void testMemoryMappedReads() |
| 656 |
throws IOException |
| 657 |
{ |
| 658 |
Path path = Files.createTempFile("longs", null); |
| 659 |
List<Long> values = createRandomLongs(20000); |
| 660 |
|
| 661 |
Slice output = allocate(values.size() * (int) SIZE_OF_LONG); |
| 662 |
for (int i = 0; i < values.size(); i++) { |
| 663 |
output.setLong(i * SIZE_OF_LONG, values.get(i)); |
| 664 |
} |
| 665 |
|
| 666 |
Files.write(path, output.getBytes()); |
| 667 |
|
| 668 |
Slice slice = Slices.mapFileReadOnly(path.toFile()); |
| 669 |
for (int i = 0; i < values.size(); i++) { |
| 670 |
long actual = slice.getLong(i * SIZE_OF_LONG); |
| 671 |
long expected = values.get(i); |
| 672 |
assertEquals(actual, expected); |
| 673 |
} |
| 674 |
|
| 675 |
assertEquals(slice.getBytes(), output.getBytes()); |
| 676 |
} |
| 677 |
|
| 678 |
@Test |
| 679 |
public void testRetainedSize() |
| 680 |
throws Exception |
| 681 |
{ |
| 682 |
Slice slice = Slices.allocate(10); |
| 683 |
assertEquals(slice.getRetainedSize(), 10); |
| 684 |
assertEquals(slice.length(), 10); |
| 685 |
Slice subSlice = slice.slice(0, 1); |
| 686 |
assertEquals(subSlice.getRetainedSize(), 10); |
| 687 |
assertEquals(subSlice.length(), 1); |
| 688 |
} |
| 689 |
|
| 690 |
@Test |
| 691 |
public void testCopyOf() |
| 692 |
throws Exception |
| 693 |
{ |
| 694 |
// slightly stronger guarantees for empty slice |
| 695 |
assertSame(Slices.copyOf(EMPTY_SLICE), EMPTY_SLICE); |
| 696 |
assertSame(Slices.copyOf(utf8Slice("hello world"), 1, 0), EMPTY_SLICE); |
| 697 |
|
| 698 |
Slice slice = utf8Slice("hello world"); |
| 699 |
assertEquals(Slices.copyOf(slice), slice); |
| 700 |
assertEquals(Slices.copyOf(slice, 1, 3), slice.slice(1, 3)); |
| 701 |
|
| 702 |
// verify it's an actual copy |
| 703 |
Slice original = utf8Slice("hello world"); |
| 704 |
Slice copy = Slices.copyOf(original); |
| 705 |
|
| 706 |
original.fill((byte) 0); |
| 707 |
assertEquals(copy, utf8Slice("hello world")); |
| 708 |
|
| 709 |
// read before beginning |
| 710 |
try { |
| 711 |
Slices.copyOf(slice, -1, slice.length()); |
| 712 |
fail(); |
| 713 |
} |
| 714 |
catch (IndexOutOfBoundsException ignored) { |
| 715 |
} |
| 716 |
|
| 717 |
// read after end |
| 718 |
try { |
| 719 |
Slices.copyOf(slice, slice.length() + 1, 1); |
| 720 |
fail(); |
| 721 |
} |
| 722 |
catch (IndexOutOfBoundsException ignored) { |
| 723 |
} |
| 724 |
|
| 725 |
// start before but extend past end |
| 726 |
try { |
| 727 |
Slices.copyOf(slice, 1, slice.length()); |
| 728 |
fail(); |
| 729 |
} |
| 730 |
catch (IndexOutOfBoundsException ignored) { |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
@Test |
| 735 |
public void testIndexOf() |
| 736 |
throws Exception |
| 737 |
{ |
| 738 |
assertIndexOf(utf8Slice("no-match-bigger"), utf8Slice("test")); |
| 739 |
assertIndexOf(utf8Slice("no"), utf8Slice("test")); |
| 740 |
|
| 741 |
assertIndexOf(utf8Slice("test"), utf8Slice("test")); |
| 742 |
assertIndexOf(utf8Slice("test-start"), utf8Slice("test")); |
| 743 |
assertIndexOf(utf8Slice("end-test"), utf8Slice("test")); |
| 744 |
assertIndexOf(utf8Slice("a-test-middle"), utf8Slice("test")); |
| 745 |
assertIndexOf(utf8Slice("this-test-is-a-test"), utf8Slice("test")); |
| 746 |
|
| 747 |
assertIndexOf(utf8Slice("test"), EMPTY_SLICE, 0, 0); |
| 748 |
assertIndexOf(EMPTY_SLICE, utf8Slice("test"), 0, -1); |
| 749 |
|
| 750 |
assertIndexOf(utf8Slice("test"), utf8Slice("no"), 4, -1); |
| 751 |
assertIndexOf(utf8Slice("test"), utf8Slice("no"), 5, -1); |
| 752 |
assertIndexOf(utf8Slice("test"), utf8Slice("no"), -1, -1); |
| 753 |
} |
| 754 |
|
| 755 |
public static void assertIndexOf(Slice data, Slice pattern, int offset, int expected) |
| 756 |
{ |
| 757 |
assertEquals(data.indexOf(pattern, offset), expected); |
| 758 |
assertEquals(data.indexOfBruteForce(pattern, offset), expected); |
| 759 |
} |
| 760 |
|
| 761 |
public static void assertIndexOf(Slice data, Slice pattern) |
| 762 |
{ |
| 763 |
int index; |
| 764 |
|
| 765 |
List<Integer> bruteForce = new ArrayList<>(); |
| 766 |
index = 0; |
| 767 |
while (index >= 0 && index < data.length()) { |
| 768 |
index = data.indexOfBruteForce(pattern, index); |
| 769 |
if (index >= 0) { |
| 770 |
bruteForce.add(index); |
| 771 |
index++; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
List<Integer> indexOf = new ArrayList<>(); |
| 776 |
index = 0; |
| 777 |
while (index >= 0 && index < data.length()) { |
| 778 |
index = data.indexOf(pattern, index); |
| 779 |
if (index >= 0) { |
| 780 |
indexOf.add(index); |
| 781 |
index++; |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
assertEquals(bruteForce, indexOf); |
| 786 |
} |
| 787 |
|
| 788 |
private static List<Long> createRandomLongs(int count) |
| 789 |
{ |
| 790 |
Random random = new Random(); |
| 791 |
List<Long> list = new ArrayList<>(count); |
| 792 |
for (int i = 0; i < count; i++) { |
| 793 |
list.add(random.nextLong()); |
| 794 |
} |
| 795 |
return Collections.unmodifiableList(list); |
| 796 |
} |
| 797 |
|
| 798 |
protected Slice allocate(int size) |
| 799 |
{ |
| 800 |
return Slices.allocate(size); |
| 801 |
} |
| 802 |
} |