1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
package org.python.core;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Common apparatus for tests involving <code>byte[]</code> and <code>java.nio.ByteBuffer</code>
* material, in particular the tests of {@link PyBuffer} implementations. A test case that extends
* this class will be equipped with additional assertion methods and a class to represent
* <code>byte[]</code> test material in several forms.
*/
public class ByteBufferTestSupport {
/**
* Class to hold test material representing the same sequence of values 0..255 in several
* different ways.
*/
protected static class ByteMaterial {
/** Length in bytes (length of every array in this material). */
final int length;
/** The byte values. */
byte[] bytes;
/** The byte values individually as ints. */
int[] ints;
/** The byte values treated as characters (unicode < 256). */
String string;
/** Construct from int array. */
public ByteMaterial(int[] a) {
ints = a.clone();
length = replicate();
}
/** Construct from String. */
public ByteMaterial(String s) {
ints = new int[s.length()];
for (int i = 0; i < ints.length; i++) {
ints[i] = 0xff & s.charAt(i);
}
length = replicate();
}
/** Construct from byte array. */
public ByteMaterial(byte[] b) {
ints = new int[b.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = 0xff & b[i];
}
length = replicate();
}
/** Construct from pattern on values (used modulo 256). */
public ByteMaterial(int start, int count, int inc) {
ints = new int[count];
int x = start;
for (int i = 0; i < count; i++) {
ints[i] = x;
x = (x + inc) & 0xff;
}
length = replicate();
}
/**
* Once the integer value array {@link #ints} has been initialised, fill the others from it.
*
* @return length of (all) arrays in units
*/
private int replicate() {
int n = ints.length;
bytes = new byte[n];
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
int x = ints[i];
bytes[i] = (byte)x;
sb.appendCodePoint(x);
}
string = sb.toString();
return n;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(100);
sb.append("byte[").append(length).append("]={ ");
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(", ");
}
if (i >= 5) {
sb.append(" ...");
break;
} else {
sb.append(ints[i]);
}
}
sb.append(" }");
return sb.toString();
}
/**
* @return a copy of the bytes array (that the client is allowed to modify)
*/
byte[] getBytes() {
return bytes.clone();
}
/**
* @return a buffer on a copy of the bytes array (that the client is allowed to modify)
*/
ByteBuffer getBuffer() {
return ByteBuffer.wrap(getBytes());
}
/**
* Create material equivalent to a slice of this material. This may act as a reference
* result for testing slice operations.
*
* @param start first byte-index to include
* @param length number of items
* @param stride between byte-indices
* @return ByteMaterial in which the arrays are a slice of this one
*/
ByteMaterial slice(int start, int length, int stride) {
return new ByteMaterial(sliceBytes(bytes, 1, start, length, stride));
}
/**
* Create material equivalent to a slice of this material. This may act as a reference
* result for testing slice operations.
*
* @param start first byte-index to include
* @param itemsize number of consecutive bytes treated as one item
* @param length number of items
* @param stride between byte-indices
* @return ByteMaterial in which the arrays are a slice of this one
*/
ByteMaterial slice(int itemsize, int start, int length, int stride) {
return new ByteMaterial(sliceBytes(bytes, itemsize, start, length, stride));
}
}
/**
* Create a byte array that is a strided copy of the one passed in. The specifications are
* assumed correct for the size of that array.
*
* @param b source array
* @param start first index to include
* @param length number of indices
* @param stride between indices
* @return slice of b
*/
protected static byte[] sliceBytes(byte[] b, int start, int length, int stride) {
return sliceBytes(b, 1, start, length, stride);
}
/**
* Create a multi-byte item array that is a strided copy of the one passed in. The
* specifications are assumed correct for the size of that array.
*
* @param b source array
* @param itemsize number of consecutive bytes treated as one item
* @param start first byte-index to include
* @param length number of indices to visit (items to copy)
* @param stride between byte-indices
* @return slice of b
*/
protected static byte[] sliceBytes(byte[] b, int itemsize, int start, int length, int stride) {
byte[] a = new byte[length];
for (int i = 0, j = start; i < length; i++, j += stride) {
for (int k = 0; k < itemsize; k++) {
a[i + k] = b[j + k];
}
}
return a;
}
/**
* Custom assert method comparing the bytes in an NIO {@link ByteBuffer} to those in a byte
* array, when that <code>ByteBuffer</code> is obtained from a contiguous <code>PyBuffer</code>.
* Let <code>bb[i]</code> denote <code>bb.get(bb.position()+i)</code>, by analogy with a C
* pointer. It is required that <code>bb[k] == expected[k]</code>, for every index
* <code>k</code> in <code>expected</code>. If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param bb result to test
*/
static void assertBytesEqual(String message, byte[] expected, ByteBuffer bb) {
// Use the position-advancing buffer get()
byte[] actual = new byte[expected.length];
bb.get(actual);
assertBytesEqual(message, expected, actual);
}
/**
* Custom assert method comparing the bytes in an NIO {@link ByteBuffer} to those in a byte
* array, when that <code>ByteBuffer</code> is obtained from a striding <code>PyBuffer</code>.
* Let <code>bb[i]</code> denote <code>bb.get(bb.position()+i)</code>, by analogy with a C
* pointer. It is required that <code>bb[k*stride] == expected[k]</code>, for every index
* <code>k</code> in <code>expected</code>. If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param bb result to test
* @param stride in the buffer <code>bb</code>
*/
static void assertBytesEqual(String message, byte[] expected, ByteBuffer bb, int stride) {
assertBytesEqual(message, expected, 0, expected.length, bb, stride);
}
/**
* Custom assert method comparing the bytes in an NIO {@link ByteBuffer} to those in a byte
* array, when that <code>ByteBuffer</code> is obtained from a striding <code>PyBuffer</code>.
* Let <code>bb[i]</code> denote <code>bb.get(bb.position()+i)</code>, by analogy with a C
* pointer. It is required that <code>bb[k*stride] == expected[expectedStart+k]</code>, for
* <code>k=0</code> to <code>n-1</code>. If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param expectedStart where to start the comparison in <code>expected</code>
* @param n number of bytes to test
* @param bb result to test
* @param stride in the buffer <code>bb</code>
*/
static void assertBytesEqual(String message, byte[] expected, int expectedStart, int n,
ByteBuffer bb, int stride) {
// Note that this approach leaves the buffer position unmodified
int p = bb.position();
byte[] actual = new byte[n];
for (int k = 0; k < n; k++, p += stride) {
actual[k] = bb.get(p);
}
assertBytesEqual(message, expected, expectedStart, n, actual, 0);
}
/**
* Custom assert method comparing byte arrays: values in <code>actual[]</code> must match all
* those in <code>expected[]</code>, and they must be the same length.
*
* @param message to issue on failure
* @param expected expected byte array
* @param actual result to test
*/
static void assertBytesEqual(String message, byte[] expected, byte[] actual) {
assertEquals(message + " (array size)", expected.length, actual.length);
assertBytesEqual(message, expected, 0, expected.length, actual, 0, 1);
}
/**
* Custom assert method comparing byte arrays. It is required that
* <code>actual[k] == expected[k]</code>, for <code>k=0</code> to <code>expected.length-1</code>
* . If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param actual result to test
* @param actualStart where to start the comparison in <code>actual</code>
*/
static void assertBytesEqual(String message, byte[] expected, byte[] actual, int actualStart) {
assertBytesEqual(message, expected, 0, expected.length, actual, actualStart, 1);
}
/**
* Custom assert method comparing byte arrays. It is required that
* <code>actual[actualStart+k] == expected[expectedStart+k]</code>, for <code>k=0</code> to
* <code>n-1</code>. If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param expectedStart where to start the comparison in <code>expected</code>
* @param n number of bytes to test
* @param actual result to test
* @param actualStart where to start the comparison in <code>actual</code>
*/
protected static void assertBytesEqual(String message, byte[] expected, int expectedStart,
int n, byte[] actual, int actualStart) {
assertBytesEqual(message, expected, expectedStart, n, actual, actualStart, 1);
}
/**
* Custom assert method comparing byte arrays. It is required that
* <code>actual[actualStart+k*stride] == expected[expectedStart+k]</code>, for <code>k=0</code>
* to <code>n-1</code>. If not, a <code>fail()</code> is declared.
*
* @param message to issue on failure
* @param expected expected byte array
* @param expectedStart where to start the comparison in <code>expected</code>
* @param n number of bytes to test
* @param actual result to test
* @param actualStart where to start the comparison in <code>actual</code>
* @param stride spacing of bytes in <code>actual</code> array
*/
static void assertBytesEqual(String message, byte[] expected, int expectedStart, int n,
byte[] actual, int actualStart, int stride) {
if (actualStart < 0) {
fail(message + " (start<0 in result)");
} else if (expectedStart < 0) {
fail(message + " (start<0 in expected result): bug in test?");
} else if (actualStart + (n - 1) * stride + 1 > actual.length) {
fail(message + " (result too short)");
} else if (expectedStart + n > expected.length) {
fail(message + " (expected result too short): bug in test?");
} else {
// Should be safe to compare the values
int i = actualStart, j, jLimit = expectedStart + n;
for (j = expectedStart; j < jLimit; j++) {
if (actual[i] != expected[j]) {
break;
}
i += stride;
}
// If we stopped early, diagnose the problem
if (j < jLimit) {
byte[] a = Arrays.copyOfRange(actual, actualStart, actualStart + n);
byte[] e = Arrays.copyOfRange(expected, expectedStart, expectedStart + n);
System.out.println(" expected:" + Arrays.toString(e));
System.out.println(" actual:" + Arrays.toString(a));
System.out.println(" _actual_:" + Arrays.toString(actual));
fail(message + " (byte at " + j + ")");
}
}
}
/**
* Customised assert method comparing a int arrays: values in the actual value starting at
* actual[offset] must match all those in expected[], and there must be enough of them.
*
* @param message to issue on failure
* @param expected expected array
* @param actual result to test
* @param offset where to start the comparison in actual
*/
static void assertIntsEqual(String message, int[] expected, int[] actual, int offset) {
int n = expected.length;
if (offset < 0) {
fail(message + " (offset<0)");
} else if (offset + n > actual.length) {
fail(message + " (too short)");
} else {
// Should be safe to compare the values
int i = offset, j;
for (j = 0; j < n; j++) {
if (actual[i++] != expected[j]) {
break;
}
}
if (j < n) {
System.out.println(" expected:" + Arrays.toString(expected));
System.out.println(" actual:" + Arrays.toString(actual));
fail(message + " (int at " + j + ")");
}
}
}
/**
* Customised assert method comparing a int arrays: int in the actual value must match all those
* in expected[], and there must be the same number of them.
*
* @param message to issue on failure
* @param expected expected array
* @param actual result to test
*/
protected static void assertIntsEqual(String message, int[] expected, int[] actual) {
int n = expected.length;
assertEquals(message, n, actual.length);
// Should be safe to compare the values
int j;
for (j = 0; j < n; j++) {
if (actual[j] != expected[j]) {
break;
}
}
if (j < n) {
System.out.println(" expected:" + Arrays.toString(expected));
System.out.println(" actual:" + Arrays.toString(actual));
fail(message + " (int at " + j + ")");
}
}
/**
* Method comparing byte arrays after a read (or view creation) operation involving a slice.
* <p>
* The invariant asserted must be explained carefully because of its generality. Suppose there
* to be three arrays of bytes <i>a</i>, <i>b</i> and <i>c</i>. Let <i>a</i> represent the state
* of some byte storage of length <i>L</i> before an operation. Let <i>b</i> represent the state
* of the same storage after an operation. Let <i>c</i> be related as follows.
* <p>
* <i>c</i> is the result, representing <i>n</i> blocks of <i>u</i> bytes copied from the
* storage, the <i>k</i>th block starting at position <i>s+kp</i> in the storage and at
* <i>t+ku</i> in <i>c</i>. <i>c</i> is of length <i>M≥nu</i>, and we assume
* <i>0≤s+kp<L</i>. After a read operation, it is required that:
* <ol>
* <li><i>c[t+iu+j] = b[s+ip+j]</i> for <i>0≤i<n</i> and <i>0≤j<u</i>, and</li>
* <li><i>a[k] = b[k]</i> for <i>0≤k<L</i>.
* </ol>
* <p>
*
* @param a storage state before the operation (typically reference data)
* @param b storage state after the operation (typically from object under test)
* @param c bytes read
* @param t index in <code>c</code> of the start byte of item 0
* @param n number of items
* @param u number of consecutive bytes per item
* @param s index in <code>b</code> of the start byte of item 0
* @param p the distance in <code>b</code> between the start bytes of successive items
*/
static void checkReadCorrect(byte[] a, byte[] b, byte[] c, int t, int n, int u, int s, int p) {
// Check the b is the same as a
assertEquals("Storage size differs from reference", a.length, b.length);
for (int k = 0; k < b.length; k++) {
if (a[k] != b[k]) {
fail("Stored data changed during read");
}
}
// Check slice read out
checkEqualInSlice(b, c, t, n, u, s, p);
}
/**
* Method comparing byte arrays where a change operation has taken place involving a slice.
* <p>
* The invariant asserted must be explained carefully because of its generality. Suppose there
* to be three arrays of bytes <i>a</i>, <i>b</i> and <i>c</i>. Let <i>a</i> represent the state
* of some byte storage of length <i>L</i> before an operation. Let <i>b</i> represent the state
* of the same storage after an operation. Let <i>c</i> be related as follows.
* <p>
* <i>c</i> is the source, contaning at index <i>t</i>, <i>n</i> blocks of <i>u</i> bytes copied
* to the storage. As before, the <i>k</i>th block starts at position <i>s+kp</i> in the storage
* and at <i>t+ku</i> in <i>c</i>. <i>c</i> is of length <i>M≥t+nu</i>, and we assume
* <i>0≤s+kp<L</i>. After a write operation, it is required that:
* <ol>
* <li><i>c[t+iu+j] = b[s+ip+j]</i> for <i>0≤i<n</i> and <i>0≤j<u</i>, and</li>
* <li><i>a[k] = b[k]</i> for <i>0≤k<L</i> and <i>k≠s+ip+j</i> for any choice of <i</i>
* and <i>j</i> where <i>0≤i<n</i> and <i>0≤j<u</i>.
* </ol>
* Note that the first of these is the same as for a read and the second requires equality
* "everywhere else".
*
* @param a storage state before the operation (typically reference data)
* @param b storage state after the operation (typically from object under test)
* @param c bytes written
* @param t index in <code>c</code> of the start byte of item 0
* @param n number of items
* @param u number of consecutive bytes per item
* @param s index in <code>b</code> of the start byte of item 0
* @param p the distance in <code>b</code> between the start bytes of successive items
*/
static void checkWriteCorrect(byte[] a, byte[] b, byte[] c, int t, int n, int u, int s, int p) {
assertEquals("Stored size has changed", a.length, b.length);
checkEqualInSlice(b, c, t, n, u, s, p);
checkUnchangedElsewhere(a, b, n, u, s, p);
}
/**
* Method comparing bytes in a slice pattern of one byte array to bytes taken consecutively in
* another array. This is needed in testing when bytes have been copied into or out of an array
* slice.
* <p>
* Let <i>b</i> represent the state of the byte storage of length <i>L</i> after the copy
* operation (the sliced array). Let <i>c</i> be a source or destination array, a section of
* which at index <i>t</i> represents <i>n</i> blocks of <i>u</i> bytes copied to or from the
* storage. <i>c</i> is of length at least <i>t+nu</i>. The <i>k</i>th block starts at position
* <i>s+kp</i> in the storage <i>b</i> and at <i>t+ku</i> in <i>c</i>, and we assume
* <i>0≤s+kp<L</i>. After a write operation, it is required that: <i>c[t+iu+j] =
* b[s+ip+j]</i> for <i>0≤i<n</i> and <i>0≤j<u</i>.
*
*
* @param b storage state after the operation (typically from object under test)
* @param c bytes written
* @param t index in <code>c</code> of the start byte of item 0
* @param n number of items
* @param u number of consecutive bytes per item
* @param s index in <code>b</code> of the start byte of item 0
* @param p the distance in <code>b</code> between the start bytes of successive items
*/
static void checkEqualInSlice(byte[] b, byte[] c, int t, int n, int u, int s, int p) {
// Check correct use of the test
checkSliceArgs(b, c, t, n, u, s, p);
// Check the data in copied units (and p-u bytes following)
for (int i = 0; i < n; i++) {
int ci = t + i * u, bi = s + i * p;
for (int j = 0; j < u; j++, bi++, ci++) {
// Compare corresponding bytes of this unit in c and b
if (c[ci] != b[bi]) {
fail(String.format("contiguous data at %d not equal to buffer at %d", ci, bi));
}
}
}
}
/**
* Method comparing the before and after state of the parts of a byte array that should be
* untouched where a change operation has taken place involving a slice.
* <p>
* Let <i>a</i> represent the state of some byte storage of length <i>L</i> before an operation.
* Let <i>b</i> represent the state of the same storage after an operation. After a write
* operation, it is required that: <i>a[k] = b[k]</i> for <i>0≤k<L</i> and
* <i>k≠s+ip+j</i> for any choice of <i</i> and <i>j</i> where <i>0≤i<n</i> and
* <i>0≤j<u</i>.
* <p>
* Note that requires equality "everywhere else" than in the slice defined by <i>n</i> units of
* size <i>u</i> starting at <i>s</i>.
*
* @param a storage state before the operation (typically reference data)
* @param b storage state after the operation (typically from object under test)
* @param n number of items
* @param u number of consecutive bytes per item
* @param s index in <code>b</code> of the start byte of item 0
* @param p the distance in <code>b</code> between the start bytes of successive items
*/
static void checkUnchangedElsewhere(byte[] a, byte[] b, int n, int u, int s, int p) {
// Check correct use of the test
assertEquals("Stored size has changed", a.length, b.length);
assertFalse("Unit size exceeds spacing", u > p && u + p > 0);
String bufferChangedAt = "buffer changed at %d (outside slice)";
int absp, low, high;
if (n < 1) {
// No elements: check whole array.
absp = low = high = 0;
} else if (p >= 0) {
// Stride is forwards in the range (easy case)
absp = p;
// Lowest byte index in the data is byte 0 of first unit in slice
low = s;
// One after highest byte index is just beyond last byte of last unit in slice
high = s + (n - 1) * p + u;
} else {
// p<0: stride is backwards in the range (delicate case)
absp = -p;
// Lowest byte index in the data is byte 0 of last unit in slice
low = s + (n - 1) * p;
// One after highest byte index is just beyond last byte of first unit in slice
high = s + u;
}
// Visit each copied unit (from low to high byte index) except the highest.
for (int i = 0; i < n - 1; i++) {
int bi = low + i * absp + u;
// Check there was no change to the absp-u bytes following unit in b
for (int j = u; j < absp; j++, bi++) {
if (b[bi] != a[bi]) {
fail(String.format(bufferChangedAt, bi));
}
}
}
// Check that b[0:low] is unchanged
for (int k = 0; k < low; k++) {
if (b[k] != a[k]) {
fail(String.format(bufferChangedAt, k));
}
}
// Check that [high:] is unchanged
for (int k = Math.max(high, 0); k < b.length; k++) {
if (b[k] != a[k]) {
fail(String.format(bufferChangedAt, k));
}
}
}
/** Common code for <code>checkReadCorrect</code> and <code>checkWriteCorrect</code>. */
private static void checkSliceArgs(byte[] b, byte[] c, int t, int n, int u, int s, int p) {
// Check correct use of the test
assertFalse("Slice data less than n units", c.length < t + n * u);
assertFalse("Slice data exceeds destination", n * u > b.length);
assertFalse("Unit size exceeds spacing", u > p && u + p > 0);
}
}
|