Skip to content

Commit 07d4c37

Browse files
committed
Add thorough unittest to std.algorithm.remove
1 parent 424aec1 commit 07d4c37

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

std/algorithm/mutation.d

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,69 @@ if (isBidirectionalRange!Range
19441944
[ 1, 3, 3, 4, 5, 5, 6 ]);
19451945
}
19461946

1947+
@safe unittest
1948+
{
1949+
import std.algorithm.comparison : min;
1950+
import std.algorithm.searching : all, any;
1951+
import std.algorithm.sorting : isStrictlyMonotonic;
1952+
import std.array : array;
1953+
import std.meta : AliasSeq;
1954+
import std.range : iota, only;
1955+
import std.typecons : Tuple;
1956+
alias S = Tuple!(int[2]);
1957+
S[] soffsets;
1958+
foreach (start; 0..5)
1959+
foreach (end; min(start+1,5) .. 5)
1960+
soffsets ~= S([start,end]);
1961+
alias D = Tuple!(int[2],int[2]);
1962+
D[] doffsets;
1963+
foreach (start1; 0..10)
1964+
foreach (end1; min(start1+1,10) .. 10)
1965+
foreach (start2; end1 ..10)
1966+
foreach (end2; min(start2+1,10) .. 10)
1967+
doffsets ~= D([start1,end1],[start2,end2]);
1968+
alias T = Tuple!(int[2],int[2],int[2]);
1969+
T[] toffsets;
1970+
foreach (start1; 0..15)
1971+
foreach (end1; min(start1+1,15) .. 15)
1972+
foreach (start2; end1..15)
1973+
foreach (end2; min(start2+1,15) .. 15)
1974+
foreach (start3; end2..15)
1975+
foreach (end3; min(start3+1,15) .. 15)
1976+
toffsets ~= T([start1,end1],[start2,end2],[start3,end3]);
1977+
1978+
static void verify(O...)(int[] r, int len, int removed, bool stable, O offsets)
1979+
{
1980+
assert(r.length == len - removed);
1981+
assert(!stable || r.isStrictlyMonotonic);
1982+
assert(r.all!(e => all!(o => e < o[0] || e >= o[1])(offsets.only)));
1983+
}
1984+
1985+
foreach (offsets; AliasSeq!(soffsets,doffsets,toffsets))
1986+
foreach (os; offsets)
1987+
{
1988+
int len = 5*os.length;
1989+
auto w = iota(0, len).array;
1990+
auto x = w.dup;
1991+
auto y = w.dup;
1992+
auto z = w.dup;
1993+
alias pred = e => any!(o => o[0] <= e && e < o[1])(only(os.expand));
1994+
w = w.remove!(SwapStrategy.unstable)(os.expand);
1995+
x = x.remove!(SwapStrategy.stable)(os.expand);
1996+
y = y.remove!(pred, SwapStrategy.unstable);
1997+
z = z.remove!(pred, SwapStrategy.stable);
1998+
int removed;
1999+
foreach (o; os)
2000+
removed += o[1] - o[0];
2001+
verify(w, len, removed, false, os[]);
2002+
verify(x, len, removed, true, os[]);
2003+
verify(y, len, removed, false, os[]);
2004+
verify(z, len, removed, true, os[]);
2005+
assert(w == y);
2006+
assert(x == z);
2007+
}
2008+
}
2009+
19472010
// reverse
19482011
/**
19492012
Reverses $(D r) in-place. Performs $(D r.length / 2) evaluations of $(D

0 commit comments

Comments
 (0)