From 4f1418bb724ca0f1bf4d807a410ccf3830d90cec Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 28 Jan 2016 18:03:33 +0300 Subject: [PATCH] Verify index expectations for reduceIndexed/reduceRightIndexed --- .../stdlib/test/collections/CollectionTest.kt | 18 ++++++++++++++++++ libraries/stdlib/test/text/StringTest.kt | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index cea31cb58f1..26e43925fc4 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -223,6 +223,15 @@ class CollectionTest { list.reduceIndexed { index, a, b -> if (index == 3) a else a + b } } + expect(5) { + listOf(2, 3).reduceIndexed { index, acc: Number, e -> + assertEquals(1, index) + assertEquals(2, acc) + assertEquals(3, e) + acc.toInt() + e + } + } + assertTrue(assertFails { arrayListOf().reduceIndexed { index, a, b -> a + b } } is UnsupportedOperationException) @@ -234,6 +243,15 @@ class CollectionTest { list.reduceRightIndexed { index, a, b -> if (index == 0) b else a + b } } + expect(1) { + listOf(2, 3).reduceRightIndexed { index, e, acc: Number -> + assertEquals(0, index) + assertEquals(3, acc) + assertEquals(2, e) + acc.toInt() - e + } + } + assertTrue(assertFails { arrayListOf().reduceRightIndexed { index, a, b -> a + b } } is UnsupportedOperationException) diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 30cad8b35f2..7da4f4742d1 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -863,6 +863,15 @@ class StringTest { // get the 3rd character assertEquals('c', arg1("bacfd").reduceIndexed { index, v, c -> if (index == 2) c else v }) + expect('c') { + "ab".reduceIndexed { index, acc, e -> + assertEquals(1, index) + assertEquals('a', acc) + assertEquals('b', e) + e + (e - acc) + } + } + assertTrue(assertFails { arg1("").reduceIndexed { index, a, b -> '\n' } } is UnsupportedOperationException) @@ -872,6 +881,15 @@ class StringTest { // get the 3rd character assertEquals('c', arg1("bacfd").reduceRightIndexed { index, c, v -> if (index == 2) c else v }) + expect('c') { + "ab".reduceRightIndexed { index, e, acc -> + assertEquals(0, index) + assertEquals('b', acc) + assertEquals('a', e) + acc + (acc - e) + } + } + assertTrue(assertFails { arg1("").reduceRightIndexed { index, a, b -> '\n' } } is UnsupportedOperationException)