From 83fef68876184067e907cd583430059558202b22 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 3 Nov 2015 18:42:41 +0300 Subject: [PATCH] Add more cases to array in-place reversing test. Add array of objects case to all reversing tests. --- .../stdlib/test/collections/ArraysTest.kt | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 031f4e2e9ba..7415d7312a0 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -547,16 +547,29 @@ class ArraysTest { } @test fun reverseInPlace() { - assertArrayNotSameButEquals(intArrayOf(3, 2, 1), intArrayOf(1, 2, 3).apply { reverse() }) - assertArrayNotSameButEquals(byteArrayOf(3, 2, 1), byteArrayOf(1, 2, 3).apply { reverse() }) - assertArrayNotSameButEquals(shortArrayOf(3, 2, 1), shortArrayOf(1, 2, 3).apply { reverse() }) - assertArrayNotSameButEquals(longArrayOf(3, 2, 1), longArrayOf(1, 2, 3).apply { reverse() }) - assertArrayNotSameButEquals(floatArrayOf(3F, 2F, 1F), floatArrayOf(1F, 2F, 3F).apply { reverse() }) - assertArrayNotSameButEquals(doubleArrayOf(3.0, 2.0, 1.0), doubleArrayOf(1.0, 2.0, 3.0).apply { reverse() }) - assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').apply { reverse() }) - assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).apply { reverse() }) + + fun doTest(build: Iterable.() -> TArray, reverse: TArray.() -> Unit, snapshot: TArray.() -> List) { + val arrays = (0..4).map { n -> (1..n).build() } + for (array in arrays) { + val original = array.snapshot() + array.reverse() + val reversed = array.snapshot() + assertEquals(original.asReversed(), reversed) + } + } + + doTest(build = { map {it}.toIntArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toLong()}.toLongArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toByte()}.toByteArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toShort()}.toShortArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toFloat()}.toFloatArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toDouble()}.toDoubleArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {'a' + it}.toCharArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { reverse() }, snapshot = { toList() }) } + @test fun reversed() { expect(listOf(3, 2, 1)) { intArrayOf(1, 2, 3).reversed() } expect(listOf(3, 2, 1)) { byteArrayOf(1, 2, 3).reversed() } @@ -566,6 +579,7 @@ class ArraysTest { expect(listOf(3.0, 2.0, 1.0)) { doubleArrayOf(1.0, 2.0, 3.0).reversed() } expect(listOf('3', '2', '1')) { charArrayOf('1', '2', '3').reversed() } expect(listOf(false, false, true)) { booleanArrayOf(true, false, false).reversed() } + expect(listOf("3", "2", "1")) { arrayOf("1", "2", "3").reversed() } } @test fun reversedArray() { @@ -577,6 +591,7 @@ class ArraysTest { assertArrayNotSameButEquals(doubleArrayOf(3.0, 2.0, 1.0), doubleArrayOf(1.0, 2.0, 3.0).reversedArray()) assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').reversedArray()) assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).reversedArray()) + assertArrayNotSameButEquals(arrayOf("3", "2", "1"), arrayOf("1", "2", "3").reversedArray()) } @test fun drop() {