Tests for loops with possible index variable overflow
This commit is contained in:
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
|
|||||||
const val MinUL = ULong.MIN_VALUE
|
const val MinUL = ULong.MIN_VALUE
|
||||||
|
|
||||||
val M = MaxUI.toULong()
|
val M = MaxUI.toULong()
|
||||||
|
val N = Int.MAX_VALUE.toUInt()
|
||||||
|
|
||||||
fun testSimpleUIntLoop() {
|
fun testSimpleUIntLoop() {
|
||||||
var s = 0
|
var s = 0
|
||||||
@@ -79,6 +80,28 @@ fun testMaxULdownToMinUL() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testWrappingULongLoop() {
|
||||||
|
val MA = M - 1UL
|
||||||
|
val MB = M + 1UL
|
||||||
|
val xs = ArrayList<ULong>()
|
||||||
|
for (i in MB downTo MA) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(MB, M, MA)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testWrappingUIntLoop() {
|
||||||
|
val NA = N - 1u
|
||||||
|
val NB = N + 1u
|
||||||
|
val xs = ArrayList<UInt>()
|
||||||
|
for (i in NB downTo NA) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(NB, N, NA)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
testSimpleUIntLoop()
|
testSimpleUIntLoop()
|
||||||
testEmptyUIntLoop()
|
testEmptyUIntLoop()
|
||||||
@@ -88,6 +111,8 @@ fun box(): String {
|
|||||||
testEmptyULongLoop2()
|
testEmptyULongLoop2()
|
||||||
testMaxUIdownToMinUI()
|
testMaxUIdownToMinUI()
|
||||||
testMaxULdownToMinUL()
|
testMaxULdownToMinUL()
|
||||||
|
testWrappingULongLoop()
|
||||||
|
testWrappingUIntLoop()
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
|
|||||||
const val MinUL = ULong.MIN_VALUE
|
const val MinUL = ULong.MIN_VALUE
|
||||||
|
|
||||||
val M = MaxUI.toULong()
|
val M = MaxUI.toULong()
|
||||||
|
val N = Int.MAX_VALUE.toUInt()
|
||||||
|
|
||||||
val p1 = 6u downTo 1u
|
val p1 = 6u downTo 1u
|
||||||
fun testSimpleUIntLoop() {
|
fun testSimpleUIntLoop() {
|
||||||
@@ -87,6 +88,30 @@ fun testMaxULdownToMinUL() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val MA = M - 1UL
|
||||||
|
val MB = M + 1UL
|
||||||
|
val p9 = MB downTo MA
|
||||||
|
fun testWrappingULongLoop() {
|
||||||
|
val xs = ArrayList<ULong>()
|
||||||
|
for (i in p9) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(MB, M, MA)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
|
val NA = N - 1u
|
||||||
|
val NB = N + 1u
|
||||||
|
val p10 = NB downTo NA
|
||||||
|
fun testWrappingUIntLoop() {
|
||||||
|
val xs = ArrayList<UInt>()
|
||||||
|
for (i in p10) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(NB, N, NA)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
testSimpleUIntLoop()
|
testSimpleUIntLoop()
|
||||||
testEmptyUIntLoop()
|
testEmptyUIntLoop()
|
||||||
@@ -96,6 +121,8 @@ fun box(): String {
|
|||||||
testEmptyULongLoop2()
|
testEmptyULongLoop2()
|
||||||
testMaxUIdownToMinUI()
|
testMaxUIdownToMinUI()
|
||||||
testMaxULdownToMinUL()
|
testMaxULdownToMinUL()
|
||||||
|
testWrappingULongLoop()
|
||||||
|
testWrappingUIntLoop()
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
|
|||||||
const val MinUL = ULong.MIN_VALUE
|
const val MinUL = ULong.MIN_VALUE
|
||||||
|
|
||||||
val M = MaxUI.toULong()
|
val M = MaxUI.toULong()
|
||||||
|
val N = Int.MAX_VALUE.toUInt()
|
||||||
|
|
||||||
val range1 = 1u .. 6u
|
val range1 = 1u .. 6u
|
||||||
fun testSimpleUIntLoop() {
|
fun testSimpleUIntLoop() {
|
||||||
@@ -87,6 +88,30 @@ fun testMaxULtoMinUL() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val MA = M - 1UL
|
||||||
|
val MB = M + 1UL
|
||||||
|
val range9 = MA..MB
|
||||||
|
fun testWrappingULongLoop() {
|
||||||
|
val xs = ArrayList<ULong>()
|
||||||
|
for (i in range9) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(MA, M, MB)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
|
val NA = N - 1u
|
||||||
|
val NB = N + 1u
|
||||||
|
val range10 = NA..NB
|
||||||
|
fun testWrappingUIntLoop() {
|
||||||
|
val xs = ArrayList<UInt>()
|
||||||
|
for (i in range10) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(NA, N, NB)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
testSimpleUIntLoop()
|
testSimpleUIntLoop()
|
||||||
testEmptyUIntLoop()
|
testEmptyUIntLoop()
|
||||||
@@ -96,6 +121,8 @@ fun box(): String {
|
|||||||
testEmptyULongLoop2()
|
testEmptyULongLoop2()
|
||||||
testMaxUItoMinUI()
|
testMaxUItoMinUI()
|
||||||
testMaxULtoMinUL()
|
testMaxULtoMinUL()
|
||||||
|
testWrappingULongLoop()
|
||||||
|
testWrappingUIntLoop()
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
|
|||||||
const val MinUL = ULong.MIN_VALUE
|
const val MinUL = ULong.MIN_VALUE
|
||||||
|
|
||||||
val M = MaxUI.toULong()
|
val M = MaxUI.toULong()
|
||||||
|
val N = Int.MAX_VALUE.toUInt()
|
||||||
|
|
||||||
fun testSimpleUIntLoop() {
|
fun testSimpleUIntLoop() {
|
||||||
var s = 0
|
var s = 0
|
||||||
@@ -79,6 +80,28 @@ fun testMaxULtoMinUL() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testWrappingULongLoop() {
|
||||||
|
val MA = M - 1UL
|
||||||
|
val MB = M + 1UL
|
||||||
|
val xs = ArrayList<ULong>()
|
||||||
|
for (i in MA .. MB) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(MA, M, MB)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testWrappingUIntLoop() {
|
||||||
|
val NA = N - 1u
|
||||||
|
val NB = N + 1u
|
||||||
|
val xs = ArrayList<UInt>()
|
||||||
|
for (i in NA .. NB) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(NA, N, NB)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
testSimpleUIntLoop()
|
testSimpleUIntLoop()
|
||||||
testEmptyUIntLoop()
|
testEmptyUIntLoop()
|
||||||
@@ -88,6 +111,8 @@ fun box(): String {
|
|||||||
testEmptyULongLoop2()
|
testEmptyULongLoop2()
|
||||||
testMaxUItoMinUI()
|
testMaxUItoMinUI()
|
||||||
testMaxULtoMinUL()
|
testMaxULtoMinUL()
|
||||||
|
testWrappingULongLoop()
|
||||||
|
testWrappingUIntLoop()
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
|
|||||||
const val MinUL = ULong.MIN_VALUE
|
const val MinUL = ULong.MIN_VALUE
|
||||||
|
|
||||||
val M = MaxUI.toULong()
|
val M = MaxUI.toULong()
|
||||||
|
val N = Int.MAX_VALUE.toUInt()
|
||||||
|
|
||||||
fun testSimpleUIntLoop() {
|
fun testSimpleUIntLoop() {
|
||||||
var s = 0
|
var s = 0
|
||||||
@@ -79,6 +80,28 @@ fun testMaxULtoMinUL() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testWrappingUIntLoop() {
|
||||||
|
val NA = N - 1u
|
||||||
|
val NB = N + 1u
|
||||||
|
val xs = ArrayList<UInt>()
|
||||||
|
for (i in NA until NB) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(NA, N)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testWrappingULongLoop() {
|
||||||
|
val MA = M - 1UL
|
||||||
|
val MB = M + 1UL
|
||||||
|
val xs = ArrayList<ULong>()
|
||||||
|
for (i in MA until MB) {
|
||||||
|
xs.add(i)
|
||||||
|
if (xs.size > 3) break
|
||||||
|
}
|
||||||
|
if (xs != listOf(MA, M)) throw AssertionError("$xs")
|
||||||
|
}
|
||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
testSimpleUIntLoop()
|
testSimpleUIntLoop()
|
||||||
testEmptyUIntLoop()
|
testEmptyUIntLoop()
|
||||||
@@ -88,6 +111,8 @@ fun box(): String {
|
|||||||
testEmptyULongLoop2()
|
testEmptyULongLoop2()
|
||||||
testMaxUItoMinUI()
|
testMaxUItoMinUI()
|
||||||
testMaxULtoMinUL()
|
testMaxULtoMinUL()
|
||||||
|
testWrappingUIntLoop()
|
||||||
|
testWrappingULongLoop()
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user