// // Test on array parameters with or without potential aliasing. // publicclass Main {
// // Cross-over on parameters with potential aliasing on parameters. // The arrays a and b may point to the same memory, which (without // further runtime tests) prevents hoisting the seemingly invariant // array reference. //
// // False cross-over on parameters. Parameters have same width (which used to // cause a false type aliasing in an older version of the compiler), but since // the types are different cannot be aliased. Thus, the invariant array // reference can be hoisted. //
publicstaticvoid main(String[] args) { int[] aI = newint[100]; float[] aF = newfloat[100]; long[] aJ = newlong[100]; double[] aD = newdouble[100];
// Type I.
CrossOverLoop1(aI, aI); for (int i = 0; i < aI.length; i++) {
expectEquals(i <= 20 ? 92 : 85, aI[i]);
} // Type F.
CrossOverLoop2(aF, aF); for (int i = 0; i < aF.length; i++) {
expectEquals(i <= 20 ? 92 : 85, aF[i]);
} // Type J.
CrossOverLoop3(aJ, aJ); for (int i = 0; i < aJ.length; i++) {
expectEquals(i <= 20 ? 92 : 85, aJ[i]);
} // Type D.
CrossOverLoop4(aD, aD); for (int i = 0; i < aD.length; i++) {
expectEquals(i <= 20 ? 92 : 85, aD[i]);
}
// Type I vs F.
FalseCrossOverLoop1(aI, aF); for (int i = 0; i < aI.length; i++) {
expectEquals(-106, aI[i]);
} // Type F vs I.
FalseCrossOverLoop2(aF, aI); for (int i = 0; i < aF.length; i++) {
expectEquals(-106, aF[i]);
} // Type J vs D.
FalseCrossOverLoop3(aJ, aD); for (int i = 0; i < aJ.length; i++) {
expectEquals(-106, aJ[i]);
} // Type D vs J.
FalseCrossOverLoop4(aD, aJ); for (int i = 0; i < aD.length; i++) {
expectEquals(-106, aD[i]);
}
// Real-world example where incorrect type assignment could introduce a bug. // The library sorting algorithm is heavy on array reads and writes, and // assigning the wrong J/D type to one of these would introduce errors. for (int i = 0; i < aD.length; i++) {
aD[i] = aD.length - i - 1;
}
Arrays.sort(aD); for (int i = 0; i < aD.length; i++) {
expectEquals((double) i, aD[i]);
}
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 und die Messung sind noch experimentell.