/* * Copyright (c) 2017, 2020, 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.
*/
staticint arraySizeFor(Class<?> type) {
type = type.isPrimitive() ? type : Object.class; return 4 * MAX_WIDTH / typeToWidth.get(type);
}
@DataProvider publicstatic Object[][] arrayTypesProvider() { if (arrayTypes == null) {
arrayTypes = new Object[][]{ new Object[]{new MismatchTest.ArrayType.Bytes(false)},
};
} return arrayTypes;
}
staticboolean isEqual(MismatchTest.ArrayType<?> at, Object a, int aFromIndex, int aToIndex,
Object b, int bFromIndex, int bToIndex) { int aLength = aToIndex - aFromIndex; int bLength = bToIndex - bFromIndex; if (aLength != bLength) returnfalse;
for (int i = 0; i < aLength; i++) {
Object av = at.get(a, aFromIndex++);
Object bv = at.get(b, bFromIndex++); if (!Objects.equals(av, bv)) returnfalse;
}
returntrue;
}
// Equality and comparison tests
staticboolean isEqual(MismatchTest.ArrayType<?> at, Object a, int aFrom, Object b, int bFrom) {
Object av = at.get(a, aFrom);
Object bv = at.get(b, bFrom);
return Objects.equals(av, bv);
}
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();
}
}
@Test(dataProvider = "arrayTypesProvider") publicvoid testArray(MismatchTest.ArrayType<?> arrayType) {
BiFunction<MismatchTest.ArrayType<?>, Integer, Object> constructor = (at, s) -> {
Object a = at.construct(s); for (int x = 0; x < s; x++) {
at.set(a, x, x % 8);
} return a;
};
BiFunction<MismatchTest.ArrayType<?>, Object, Object> cloner = (at, a) ->
constructor.apply(at, Array.getLength(a));
testArrayType(arrayType, constructor, cloner);
}
void testArrayType(MismatchTest.ArrayType<?> at,
BiFunction<MismatchTest.ArrayType<?>, Integer, Object> constructor,
BiFunction<MismatchTest.ArrayType<?>, Object, Object> cloner) { int n = arraySizeFor(at.componentType);
for (int s : ranges(0, n)) {
Object a = constructor.apply(at, s);
Object b = cloner.apply(at, a);
for (int aFrom : ranges(0, s)) { for (int aTo : ranges(aFrom, s)) { int aLength = aTo - aFrom;
@Override void set(Object a, int i, Object v) { short pv; if (v instanceofShort) {
pv = (Short) v;
} elseif (v instanceof Integer) {
pv = ((Integer) v).shortValue();
} elsethrownew IllegalStateException();
@Override void set(Object a, int i, Object v) { int pv; if (v instanceof Integer) {
pv = ((Integer) v).shortValue();
} elsethrownew IllegalStateException();
@Override void set(Object a, int i, Object v) { long pv; if (v instanceofLong) {
pv = (Long) v;
} elseif (v instanceof Integer) {
pv = ((Integer) v).longValue();
} elsethrownew IllegalStateException();
((long[]) a)[i] = pv;
}
}
staticclass Floats extends MismatchTest.ArrayType<float[]> { public Floats() { super(float[].class);
}
@Override void set(Object a, int i, Object v) { float pv; if (v instanceofFloat) {
pv = (Float) v;
} elseif (v instanceof Integer) {
pv = ((Integer) v).floatValue();
} elsethrownew IllegalStateException();
((float[]) a)[i] = pv;
}
}
staticclass Doubles extends MismatchTest.ArrayType<double[]> { public Doubles() { super(double[].class);
}
@Override void set(Object a, int i, Object v) { double pv; if (v instanceofDouble) {
pv = (Double) v;
} elseif (v instanceof Integer) {
pv = ((Integer) v).doubleValue();
} elsethrownew IllegalStateException();
((double[]) a)[i] = pv;
}
}
}
}
¤ Dauer der Verarbeitung: 0.17 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.