From bfc9ac983eb8cc47ffffd327c15d6ed2bb87d8fd Mon Sep 17 00:00:00 2001 From: Ilya Yaroshenko Date: Sat, 16 Apr 2016 20:28:00 +0200 Subject: [PATCH] [ndslice] improve coverage --- std/experimental/ndslice/internal.d | 14 ++++++ std/experimental/ndslice/selection.d | 72 ++++++++++++++++++++++++++++ std/experimental/ndslice/slice.d | 12 +++-- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/std/experimental/ndslice/internal.d b/std/experimental/ndslice/internal.d index e82752cb6..6eb5700a6 100644 --- a/std/experimental/ndslice/internal.d +++ b/std/experimental/ndslice/internal.d @@ -159,6 +159,20 @@ bool isPermutation(size_t N)(auto ref in size_t[N] perm) return true; } +unittest +{ + assert(isPermutation([0, 1])); + // all numbers 0..N-1 need to be part of the permutation + assert(!isPermutation([1, 2])); + assert(!isPermutation([0, 2])); + // duplicates are not allowed + assert(!isPermutation([0, 1, 1])); + + size_t[0] emptyArr; + // empty permutations are not allowed either + assert(!isPermutation(emptyArr)); +} + bool isValidPartialPermutation(size_t N)(in size_t[] perm) { int[N] mask; diff --git a/std/experimental/ndslice/selection.d b/std/experimental/ndslice/selection.d index 39a6a4389..6a7dca189 100644 --- a/std/experimental/ndslice/selection.d +++ b/std/experimental/ndslice/selection.d @@ -134,6 +134,11 @@ template pack(K...) .byElement.front .byElement.front .shape.length == 2); + // test save + b.byElement.save.popFront; + static assert(b + .byElement.front + .shape.length == 3); } /++ @@ -303,6 +308,24 @@ Slice!(1, Range) diagonal(size_t N, Range)(auto ref Slice!(N, Range) slice) assert(iotaSlice(2, 3).diagonal == d); } +@safe @nogc pure nothrow unittest +{ + import std.algorithm.comparison: equal; + import std.range: only; + + // ------- + // | 0 1 | + // | 2 3 | + // | 4 5 | + // ------- + //-> + // | 0 3 | + + assert(iotaSlice(3, 2) + .diagonal + .equal(only(0, 3))); +} + /// ditto pure nothrow unittest { @@ -819,6 +842,20 @@ unittest assert(iotaSlice(3, 4, 5, 6, 7).pack!2.reshape(4, 3, 5)[0, 0, 0].shape == cast(size_t[2])[6, 7]); } +@safe pure unittest +{ + import std.experimental.ndslice.slice; + import std.range: iota; + import std.exception: assertThrown; + + auto e = 1.iotaSlice(1); + // resize to the wrong dimension + assertThrown!ReshapeException(e.reshape(2)); + e.popFront; + // test with an empty slice + assertThrown!ReshapeException(e.reshape(1)); +} + /// See_also: $(LREF reshape) class ReshapeException: SliceException { @@ -1173,6 +1210,18 @@ pure nothrow unittest [40, 43, 46, 49]]); } +pure nothrow unittest +{ + // test save + import std.range: dropOne; + import std.range: iota; + + auto elems = 12.iota.sliced(3, 4).byElement; + assert(elems.front == 0); + assert(elems.save.dropOne.front == 1); + assert(elems.front == 0); +} + /++ Random access and slicing +/ @@ -1478,6 +1527,17 @@ pure nothrow unittest elems.popFrontN(3); assert(elems.front == 5); +} + +/// Save +@safe @nogc pure nothrow unittest +{ + auto elems = iotaSlice(3, 4).byElementInStandardSimplex; + import std.range: dropOne, popFrontN; + elems.popFrontN(4); + + assert(elems.save.dropOne.front == 8); + assert(elems.front == 5); assert(elems.index == cast(size_t[2])[1, 1]); assert(elems.length == 2); } @@ -1534,6 +1594,18 @@ IndexSlice!N indexSlice(size_t N)(auto ref size_t[N] lengths) assert(cm[2, 1] == [3, 5]); } +@safe pure nothrow unittest +{ + // test save + import std.range: dropOne; + + auto im = indexSlice(7, 9); + auto imByElement = im.byElement; + assert(imByElement.front == [0, 0]); + assert(imByElement.save.dropOne.front == [0, 1]); + assert(imByElement.front == [0, 0]); +} + /++ Slice composed of indexes. See_also: $(LREF indexSlice) diff --git a/std/experimental/ndslice/slice.d b/std/experimental/ndslice/slice.d index 4af1772f5..350ab9ebd 100644 --- a/std/experimental/ndslice/slice.d +++ b/std/experimental/ndslice/slice.d @@ -310,12 +310,15 @@ pure nothrow @nogc unittest } alias S = Slice!(3, MyIota); - auto slice = MyIota().sliced(20, 10); import std.range.primitives; static assert(hasLength!S); static assert(isInputRange!S); static assert(isForwardRange!S == false); + + auto slice = MyIota().sliced(20, 10); + assert(slice[1, 2] == 12); + } /// Random access range primitives for slices over user defined types @@ -336,14 +339,17 @@ pure nothrow @nogc unittest } alias S = Slice!(3, MyIota); - auto slice = MyIota().sliced(20, 10); - import std.range.primitives; static assert(hasLength!S); static assert(hasSlicing!S); static assert(isForwardRange!S); static assert(isBidirectionalRange!S); static assert(isRandomAccessRange!S); + + auto slice = MyIota().sliced(20, 10); + assert(slice[1, 2] == 12); + auto sCopy = slice.save; + assert(slice[1, 2] == 12); } /// Slice tuple and flags