From 4068274fcbca4feceb2822fbb60feb41f553a24f Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Thu, 27 Dec 2018 00:12:17 +0100 Subject: [PATCH 1/2] Add fold(Right) and reduce(Right) tests --- test/collection/iterable_extensions_test.dart | 59 +++++++++++++++++++ test/collection/list_extensions_test.dart | 34 +++++++++++ test/collection/list_test.dart | 27 +++++++++ 3 files changed, 120 insertions(+) diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 064b1ced..2f64d1d4 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -587,6 +587,39 @@ void testIterable(KIterable Function() emptyIterable, }); }); + group("fold", () { + test("fold division", () { + final iterable = iterableOf([1.0, 2.0, 3.0]); + final result = iterable.fold(1.0, (double acc, it) => it / acc); + expect(result, closeTo(1.5, 0.00001)); + }); + + test("operation must be non null", () { + final e = catchException( + () => emptyIterable().fold("foo", null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + + group("foldIndexed", () { + test("foldIndexed division", () { + final list = iterableOf([1.0, 2.0, 3.0, 4.0]); + var i = 0; + final result = list.foldIndexed(1.0, (index, double acc, it) { + expect(index, i); + i++; + return it / acc; + }); + expect(result, closeTo(2.666666, 0.00001)); + }); + + test("operation must be non null", () { + final e = catchException( + () => emptyIterable().foldIndexed("foo", null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + group("groupBy", () { if (ordered) { test("basic", () { @@ -1052,6 +1085,32 @@ void testIterable(KIterable Function() emptyIterable, }); }); + group("reduceIndexed", () { + test("reduceIndexed", () { + var i = 1; + final result = + iterableOf([1, 2, 3, 4]).reduceIndexed((index, int acc, it) { + expect(index, i); + i++; + return it + acc; + }); + expect(result, 10); + }); + + test("empty throws", () { + expect( + () => emptyIterable() + .reduceIndexed((index, int acc, it) => it + acc), + throwsUnsupportedError); + }); + + test("reduceIndexed doesn't allow null as operation", () { + final iterable = emptyIterable(); + var e = catchException(() => iterable.reduceIndexed(null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + group("reversed", () { test("mutliple", () { final result = iterableOf([1, 2, 3, 4]).reversed(); diff --git a/test/collection/list_extensions_test.dart b/test/collection/list_extensions_test.dart index a63485e2..7df9adff 100644 --- a/test/collection/list_extensions_test.dart +++ b/test/collection/list_extensions_test.dart @@ -52,6 +52,39 @@ void main() { }); }); + group("foldRight", () { + test("foldRight division", () { + final list = listOf([1.0, 2.0, 3.0]); + final result = list.foldRight(1.0, (it, double acc) => acc / it); + expect(result, closeTo(0.1666666, 0.00001)); + }); + + test("operation must be non null", () { + final e = + catchException(() => listOf().foldRight("foo", null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + + group("foldRightIndexed", () { + test("foldRight division", () { + final list = listOf([1.0, 2.0, 3.0, 4.0]); + var i = 3; + final result = list.foldRightIndexed(1.0, (index, it, double acc) { + expect(index, i); + i--; + return acc / it; + }); + expect(result, closeTo(0.0416666, 0.00001)); + }); + + test("operation must be non null", () { + final e = catchException( + () => listOf().foldRightIndexed("foo", null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + group("first", () { test("get first element", () { expect(listOf(["a", "b"]).first(), "a"); @@ -99,6 +132,7 @@ void main() { expect(e.message, allOf(contains("null"), contains("defaultValue"))); }); }); + group("getOrNull", () { test("get item", () { final list = listOf(["a", "b", "c"]); diff --git a/test/collection/list_test.dart b/test/collection/list_test.dart index ecf90d32..ac8010c0 100644 --- a/test/collection/list_test.dart +++ b/test/collection/list_test.dart @@ -115,6 +115,33 @@ void main() { }); }); + group("reduceRightIndexed", () { + test("reduceRightIndexed", () { + var i = 2; + final result = + listOf([1, 2, 3, 4]).reduceRightIndexed((index, it, int acc) { + expect(index, i); + i--; + return it + acc; + }); + expect(result, 10); + }); + + test("empty throws", () { + expect( + () => emptyList() + .reduceRightIndexed((index, it, int acc) => it + acc), + throwsUnsupportedError); + }); + + test("reduceRightIndexed doesn't allow null as operation", () { + final list = emptyList(); + var e = + catchException(() => list.reduceRightIndexed(null)); + expect(e.message, allOf(contains("null"), contains("operation"))); + }); + }); + test("sublist works ", () { final list = listOf(["a", "b", "c"]); final subList = list.subList(1, 3); From 061c4cd5529fc689df3f9c5f1d51f301bf8515d6 Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Thu, 27 Dec 2018 00:30:46 +0100 Subject: [PATCH 2/2] Add better fold tests --- test/collection/iterable_extensions_test.dart | 42 ++++++++++++------- test/collection/list_extensions_test.dart | 34 +++++++++------ 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 2f64d1d4..ed90889d 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -588,11 +588,18 @@ void testIterable(KIterable Function() emptyIterable, }); group("fold", () { - test("fold division", () { - final iterable = iterableOf([1.0, 2.0, 3.0]); - final result = iterable.fold(1.0, (double acc, it) => it / acc); - expect(result, closeTo(1.5, 0.00001)); - }); + if (ordered) { + test("fold division", () { + final iterable = iterableOf([ + [1, 2], + [3, 4], + [5, 6] + ]); + final result = iterable.fold( + listOf(), (KList acc, it) => acc + listOf(it)); + expect(result, listOf([1, 2, 3, 4, 5, 6])); + }); + } test("operation must be non null", () { final e = catchException( @@ -602,16 +609,23 @@ void testIterable(KIterable Function() emptyIterable, }); group("foldIndexed", () { - test("foldIndexed division", () { - final list = iterableOf([1.0, 2.0, 3.0, 4.0]); - var i = 0; - final result = list.foldIndexed(1.0, (index, double acc, it) { - expect(index, i); - i++; - return it / acc; + if (ordered) { + test("foldIndexed division", () { + final iterable = iterableOf([ + [1, 2], + [3, 4], + [5, 6] + ]); + var i = 0; + final result = + iterable.foldIndexed(listOf(), (index, KList acc, it) { + expect(index, i); + i++; + return acc + listOf(it); + }); + expect(result, listOf([1, 2, 3, 4, 5, 6])); }); - expect(result, closeTo(2.666666, 0.00001)); - }); + } test("operation must be non null", () { final e = catchException( diff --git a/test/collection/list_extensions_test.dart b/test/collection/list_extensions_test.dart index 7df9adff..a72118e2 100644 --- a/test/collection/list_extensions_test.dart +++ b/test/collection/list_extensions_test.dart @@ -54,33 +54,43 @@ void main() { group("foldRight", () { test("foldRight division", () { - final list = listOf([1.0, 2.0, 3.0]); - final result = list.foldRight(1.0, (it, double acc) => acc / it); - expect(result, closeTo(0.1666666, 0.00001)); + final iterable = listOf([ + [1, 2], + [3, 4], + [5, 6] + ]); + final result = iterable.foldRight( + listOf(), (it, KList acc) => acc + listOf(it)); + expect(result, listOf([5, 6, 3, 4, 1, 2])); }); test("operation must be non null", () { - final e = - catchException(() => listOf().foldRight("foo", null)); + final e = catchException( + () => emptyList().foldRight("foo", null)); expect(e.message, allOf(contains("null"), contains("operation"))); }); }); group("foldRightIndexed", () { - test("foldRight division", () { - final list = listOf([1.0, 2.0, 3.0, 4.0]); - var i = 3; - final result = list.foldRightIndexed(1.0, (index, it, double acc) { + test("foldRightIndexed division", () { + final iterable = listOf([ + [1, 2], + [3, 4], + [5, 6] + ]); + var i = 2; + final result = + iterable.foldRightIndexed(listOf(), (index, it, KList acc) { expect(index, i); i--; - return acc / it; + return acc + listOf(it); }); - expect(result, closeTo(0.0416666, 0.00001)); + expect(result, listOf([5, 6, 3, 4, 1, 2])); }); test("operation must be non null", () { final e = catchException( - () => listOf().foldRightIndexed("foo", null)); + () => emptyList().foldRightIndexed("foo", null)); expect(e.message, allOf(contains("null"), contains("operation"))); }); });