mirror of
https://github.com/dlang/phobos.git
synced 2025-05-03 08:30:33 +03:00
dedicated sort5 unittest
- test all permutations of 5 integers - remove scope success assertion in sort5 - move shortSort test below shortSort as it now tests only one function
This commit is contained in:
parent
5901459850
commit
a0eec9d86b
1 changed files with 25 additions and 14 deletions
|
@ -1352,15 +1352,30 @@ private void shortSort(alias less, Range)(Range r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.random : Random, uniform;
|
||||||
|
|
||||||
|
debug(std_algorithm) scope(success)
|
||||||
|
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
||||||
|
|
||||||
|
auto rnd = Random(1);
|
||||||
|
auto a = new int[uniform(100, 200, rnd)];
|
||||||
|
foreach (ref e; a)
|
||||||
|
{
|
||||||
|
e = uniform(-100, 100, rnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
shortSort!(binaryFun!("a < b"), int[])(a);
|
||||||
|
assert(isSorted(a));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Sorts the first 5 elements exactly of range r.
|
Sorts the first 5 elements exactly of range r.
|
||||||
*/
|
*/
|
||||||
private void sort5(alias lt, Range)(Range r)
|
private void sort5(alias lt, Range)(Range r)
|
||||||
{
|
{
|
||||||
assert(r.length >= 5);
|
assert(r.length >= 5);
|
||||||
version(unittest) scope(success)
|
|
||||||
assert(!lt(r[1], r[0]) && !lt(r[2], r[1])
|
|
||||||
&& !lt(r[3], r[2]) && !lt(r[4], r[3]));
|
|
||||||
|
|
||||||
import std.algorithm : swapAt;
|
import std.algorithm : swapAt;
|
||||||
|
|
||||||
|
@ -1411,20 +1426,16 @@ private void sort5(alias lt, Range)(Range r)
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.random : Random, uniform;
|
import std.algorithm.iteration : permutations;
|
||||||
|
import std.algorithm.mutation : copy;
|
||||||
|
|
||||||
debug(std_algorithm) scope(success)
|
int[5] buf;
|
||||||
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
foreach (per; iota(5).permutations)
|
||||||
|
|
||||||
auto rnd = Random(1);
|
|
||||||
auto a = new int[uniform(100, 200, rnd)];
|
|
||||||
foreach (ref e; a)
|
|
||||||
{
|
{
|
||||||
e = uniform(-100, 100, rnd);
|
per.copy(buf[]);
|
||||||
|
sort5!((a, b) => a < b)(buf[]);
|
||||||
|
assert(buf[].isSorted);
|
||||||
}
|
}
|
||||||
|
|
||||||
shortSort!(binaryFun!("a < b"), int[])(a);
|
|
||||||
assert(isSorted(a));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort
|
// sort
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue