Tests for loops with possible index variable overflow

This commit is contained in:
Dmitry Petrov
2018-12-28 15:35:09 +03:00
parent 54cba32426
commit a2c618366b
5 changed files with 129 additions and 0 deletions
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
const val MinUL = ULong.MIN_VALUE
val M = MaxUI.toULong()
val N = Int.MAX_VALUE.toUInt()
fun testSimpleUIntLoop() {
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 {
testSimpleUIntLoop()
testEmptyUIntLoop()
@@ -88,6 +111,8 @@ fun box(): String {
testEmptyULongLoop2()
testMaxUIdownToMinUI()
testMaxULdownToMinUL()
testWrappingULongLoop()
testWrappingUIntLoop()
return "OK"
}
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
const val MinUL = ULong.MIN_VALUE
val M = MaxUI.toULong()
val N = Int.MAX_VALUE.toUInt()
val p1 = 6u downTo 1u
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 {
testSimpleUIntLoop()
testEmptyUIntLoop()
@@ -96,6 +121,8 @@ fun box(): String {
testEmptyULongLoop2()
testMaxUIdownToMinUI()
testMaxULdownToMinUL()
testWrappingULongLoop()
testWrappingUIntLoop()
return "OK"
}
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
const val MinUL = ULong.MIN_VALUE
val M = MaxUI.toULong()
val N = Int.MAX_VALUE.toUInt()
val range1 = 1u .. 6u
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 {
testSimpleUIntLoop()
testEmptyUIntLoop()
@@ -96,6 +121,8 @@ fun box(): String {
testEmptyULongLoop2()
testMaxUItoMinUI()
testMaxULtoMinUL()
testWrappingULongLoop()
testWrappingUIntLoop()
return "OK"
}
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
const val MinUL = ULong.MIN_VALUE
val M = MaxUI.toULong()
val N = Int.MAX_VALUE.toUInt()
fun testSimpleUIntLoop() {
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 {
testSimpleUIntLoop()
testEmptyUIntLoop()
@@ -88,6 +111,8 @@ fun box(): String {
testEmptyULongLoop2()
testMaxUItoMinUI()
testMaxULtoMinUL()
testWrappingULongLoop()
testWrappingUIntLoop()
return "OK"
}
@@ -8,6 +8,7 @@ const val MaxUL = ULong.MAX_VALUE
const val MinUL = ULong.MIN_VALUE
val M = MaxUI.toULong()
val N = Int.MAX_VALUE.toUInt()
fun testSimpleUIntLoop() {
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 {
testSimpleUIntLoop()
testEmptyUIntLoop()
@@ -88,6 +111,8 @@ fun box(): String {
testEmptyULongLoop2()
testMaxUItoMinUI()
testMaxULtoMinUL()
testWrappingUIntLoop()
testWrappingULongLoop()
return "OK"
}