Skip to content

Commit 081418a

Browse files
authored
Add documented UTs for lightConst/lightImmutable (#457)
* Add documented UTs for lightConst/lightImmutable * UT fixups
1 parent bb5e949 commit 081418a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

source/mir/ndslice/slice.d

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,28 @@ public:
982982
return ret;
983983
}
984984

985+
static if (doUnittest)
986+
///
987+
@safe pure nothrow
988+
version(mir_ndslice_test) unittest {
989+
import mir.algorithm.iteration: equal;
990+
991+
immutable Slice!(int*, 1) x = [1, 2].sliced;
992+
auto y = x.lightImmutable;
993+
// this._iterator is copied to the new slice (i.e. both point to the same underlying data)
994+
assert(x._iterator == y._iterator);
995+
assert(x[0] == 1);
996+
assert(x[1] == 2);
997+
assert(y[0] == 1);
998+
assert(y[1] == 2);
999+
// Outer immutable is moved to iteration type
1000+
static assert(is(typeof(y) == Slice!(immutable(int)*, 1)));
1001+
// meaning that y can be modified, even if its elements can't
1002+
y.popFront;
1003+
// even if x can't be modified
1004+
//x.popFront; //error
1005+
}
1006+
9851007
/// Returns: Mutable slice over const data.
9861008
Slice!(LightConstOf!Iterator, N, kind, staticMap!(LightConstOf, Labels)) lightConst()() return scope const @property @trusted
9871009
{
@@ -997,6 +1019,26 @@ public:
9971019
return this.lightImmutable;
9981020
}
9991021

1022+
static if (doUnittest)
1023+
///
1024+
@safe pure nothrow
1025+
version(mir_ndslice_test) unittest {
1026+
import mir.algorithm.iteration: equal;
1027+
1028+
const Slice!(int*, 1) x = [1, 2].sliced;
1029+
auto y = x.lightConst;
1030+
// this._iterator is copied to the new slice (i.e. both point to the same underlying data)
1031+
assert(x._iterator == y._iterator);
1032+
assert(x.equal([1, 2]));
1033+
assert(y.equal([1, 2]));
1034+
// Outer const is moved to iteration type
1035+
static assert(is(typeof(y) == Slice!(const(int)*, 1)));
1036+
// meaning that y can be modified, even if its elements can't
1037+
y.popFront;
1038+
// even if x can't be modified
1039+
//x.popFront; //error
1040+
}
1041+
10001042
/// Label for the dimensions 'd'. By default returns the row label.
10011043
Slice!(Labels[d])
10021044
label(size_t d = 0)() @property

0 commit comments

Comments
 (0)