/* * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
T construct(int length) { return construct(length, ByteOrder.BIG_ENDIAN);
}
abstract T construct(int length, ByteOrder bo);
@SuppressWarnings("unchecked")
T slice(T a, int from, int to, boolean dupOtherwiseSlice) {
a = (T) a.position(from).limit(to); return (T) (dupOtherwiseSlice ? a.duplicate() : a.slice());
}
void set(T a, int i, Object v) { try {
setter.invoke(a, i, convert(v));
} catch (RuntimeException | Error e) { throw e;
} catch (Throwable t) { thrownew Error(t);
}
}
abstract Object convert(Object o);
boolean equals(T a, T b) { try { return (boolean) eq.invoke(a, b);
} catch (RuntimeException | Error e) { throw e;
} catch (Throwable t) { thrownew Error(t);
}
}
int compare(T a, T b) { try { return (int) cmp.invoke(a, b);
} catch (RuntimeException | Error e) { throw e;
} catch (Throwable t) { thrownew Error(t);
}
}
boolean pairWiseEquals(T a, T b) { if (a.remaining() != b.remaining()) returnfalse; int p = a.position(); for (int i = a.limit() - 1, j = b.limit() - 1; i >= p; i--, j--) if (!get(a, i).equals(get(b, j))) returnfalse; returntrue;
}
int mismatch(T a, T b) { try { return (int) mismtch.invoke(a, b);
} catch (RuntimeException | Error e) { throw e;
} catch (Throwable t) { thrownew Error(t);
}
}
floatbufferTypes = new Object[][]{ // canonical and non-canonical NaNs // If conversion is a signalling NaN it may be subject to conversion to a // quiet NaN on some processors, even if a copy is performed // The tests assume that if conversion occurs it does not convert to the // canonical NaN new Object[]{new BufferType.Floats(BufferKind.HEAP), 0x7fc00000L, 0x7f800001L, bTof}, new Object[]{new BufferType.Floats(BufferKind.HEAP_VIEW), 0x7fc00000L, 0x7f800001L, bTof}, new Object[]{new BufferType.Floats(BufferKind.DIRECT), 0x7fc00000L, 0x7f800001L, bTof}, new Object[]{new BufferType.Doubles(BufferKind.HEAP), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD}, new Object[]{new BufferType.Doubles(BufferKind.HEAP_VIEW), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD}, new Object[]{new BufferType.Doubles(BufferKind.DIRECT), 0x7ff8000000000000L, 0x7ff0000000000001L, bToD},
// +0.0 and -0.0 new Object[]{new BufferType.Floats(BufferKind.HEAP), 0x0L, 0x80000000L, bTof}, new Object[]{new BufferType.Floats(BufferKind.HEAP_VIEW), 0x0L, 0x80000000L, bTof}, new Object[]{new BufferType.Floats(BufferKind.DIRECT), 0x0L, 0x80000000L, bTof}, new Object[]{new BufferType.Doubles(BufferKind.HEAP), 0x0L, 0x8000000000000000L, bToD}, new Object[]{new BufferType.Doubles(BufferKind.HEAP_VIEW), 0x0L, 0x8000000000000000L, bToD}, new Object[]{new BufferType.Doubles(BufferKind.DIRECT), 0x0L, 0x8000000000000000L, bToD},
};
} return floatbufferTypes;
}
// Tests all primitive buffers
@Test(dataProvider = "bufferTypesProvider")
<E> void testBuffers(BufferType<Buffer, E> bufferType) { // Test with buffers of the same byte order (BE)
BiFunction<BufferType<Buffer, E>, Integer, Buffer> constructor = (at, s) -> {
Buffer a = at.construct(s); for (int x = 0; x < s; x++) {
at.set(a, x, x % 8);
} return a;
};
// Test with buffers of different byte order if (bufferType.elementType != byte.class &&
(bufferType.k == BufferKind.HEAP_VIEW ||
bufferType.k == BufferKind.DIRECT)) {
BiFunction<BufferType<Buffer, E>, Integer, Buffer> leConstructor = (at, s) -> {
Buffer a = at.construct(s, ByteOrder.LITTLE_ENDIAN); for (int x = 0; x < s; x++) {
at.set(a, x, x % 8);
} return a;
};
testBufferType(bufferType, constructor, leConstructor);
}
}
// Tests float and double buffers with edge-case values (NaN, -0.0, +0.0)
@Test(dataProvider = "floatBufferTypesProvider") publicvoid testFloatBuffers(
BufferType<Buffer, Float> bufferType, long rawBitsA, long rawBitsB,
LongFunction<Object> bitsToFloat) {
Object av = bitsToFloat.apply(rawBitsA);
Object bv = bitsToFloat.apply(rawBitsB);
BiFunction<BufferType<Buffer, Float>, Integer, Buffer> allAs = (at, s) -> {
Buffer b = at.construct(s); for (int x = 0; x < s; x++) {
at.set(b, x, av);
} return b;
};
BiFunction<BufferType<Buffer, Float>, Integer, Buffer> allBs = (at, s) -> {
Buffer b = at.construct(s); for (int x = 0; x < s; x++) {
at.set(b, x, bv);
} return b;
};
BiFunction<BufferType<Buffer, Float>, Integer, Buffer> halfBs = (at, s) -> {
Buffer b = at.construct(s); for (int x = 0; x < s / 2; x++) {
at.set(b, x, bv);
} for (int x = s / 2; x < s; x++) {
at.set(b, x, 1);
} return b;
};
<B extends Buffer, E, BT extends BufferType<B, E>> void testBufferType(BT bt,
BiFunction<BT, Integer, B> aConstructor,
BiFunction<BT, Integer, B> bConstructor) { int n = arraySizeFor(bt.elementType);
for (boolean dupOtherwiseSlice : newboolean[]{ false, true }) { for (int s : ranges(0, n)) {
B a = aConstructor.apply(bt, s);
B b = bConstructor.apply(bt, s);
for (int aFrom : ranges(0, s)) { for (int aTo : ranges(aFrom, s)) { int aLength = aTo - aFrom;
B as = aLength != s
? bt.slice(a, aFrom, aTo, dupOtherwiseSlice)
: a;
for (int bFrom : ranges(0, s)) { for (int bTo : ranges(bFrom, s)) { int bLength = bTo - bFrom;
B bs = bLength != s
? bt.slice(b, bFrom, bTo, dupOtherwiseSlice)
: b;
// If buffers are equal, there shall be no mismatch Assert.assertEquals(bt.mismatch(as, bs), -1); Assert.assertEquals(bt.mismatch(bs, as), -1);
} else { int aCb = bt.compare(as, bs); int bCa = bt.compare(bs, as); int v = Integer.signum(aCb) * Integer.signum(bCa); Assert.assertTrue(v == -1);
int aMs = bt.mismatch(as, bs); int bMs = bt.mismatch(bs, as); Assert.assertNotEquals(aMs, -1); Assert.assertEquals(aMs, bMs);
}
}
}
if (aLength > 0 && !a.isReadOnly()) { for (int i = aFrom; i < aTo; i++) {
B c = aConstructor.apply(bt, a.capacity());
B cs = aLength != s
? bt.slice(c, aFrom, aTo, dupOtherwiseSlice)
: c;
// Create common prefix with a length of i - aFrom
bt.set(c, i, -1);
Assert.assertFalse(bt.equals(c, a));
int cCa = bt.compare(cs, as); int aCc = bt.compare(as, cs); int v = Integer.signum(cCa) * Integer.signum(aCc); Assert.assertTrue(v == -1);
int cMa = bt.mismatch(cs, as); int aMc = bt.mismatch(as, cs); Assert.assertEquals(cMa, aMc); Assert.assertEquals(cMa, i - aFrom);
}
}
}
}
}
}
}
staticint[] ranges(int from, int to) { int width = to - from; switch (width) { case 0: returnnewint[]{}; case 1: returnnewint[]{from, to}; case 2: returnnewint[]{from, from + 1, to}; case 3: returnnewint[]{from, from + 1, from + 2, to}; default: return IntStream.of(from, from + 1, from + 2, to / 2 - 1, to / 2, to / 2 + 1, to - 2, to - 1, to)
.filter(i -> i >= from && i <= to)
.distinct().toArray();
}
}
}
¤ Dauer der Verarbeitung: 0.27 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.