mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
Merge pull request #4431 from wilzbach/document_std_algorithm_test
add external imports to documented unittests in std.algorithm
This commit is contained in:
commit
e59c06b410
4 changed files with 23 additions and 0 deletions
|
@ -1870,6 +1870,7 @@ unittest
|
|||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
import std.typecons : tuple;
|
||||
import std.range.primitives;
|
||||
|
||||
// Grouping by particular attribute of each element:
|
||||
auto range =
|
||||
|
|
|
@ -1487,6 +1487,7 @@ if (isInputRange!InputRange &&
|
|||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
import std.container : SList;
|
||||
import std.range.primitives : empty;
|
||||
|
||||
assert(find("hello, world", ',') == ", world");
|
||||
assert(find([1, 2, 3, 5], 4) == []);
|
||||
|
@ -1736,6 +1737,8 @@ if (isForwardRange!R1 && isForwardRange!R2
|
|||
@safe unittest
|
||||
{
|
||||
import std.container : SList;
|
||||
import std.range.primitives : empty;
|
||||
import std.typecons : Tuple;
|
||||
|
||||
assert(find("hello, world", "World").empty);
|
||||
assert(find("hello, world", "wo") == "world");
|
||||
|
@ -2125,6 +2128,7 @@ if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles))))
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.typecons : tuple;
|
||||
int[] a = [ 1, 4, 2, 3 ];
|
||||
assert(find(a, 4) == [ 4, 2, 3 ]);
|
||||
assert(find(a, [ 1, 4 ]) == [ 1, 4, 2, 3 ]);
|
||||
|
@ -2257,6 +2261,7 @@ Range1 find(Range1, alias pred, Range2)(
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.range.primitives : empty;
|
||||
int[] a = [ -1, 0, 1, 2, 3, 4, 5 ];
|
||||
int[] b = [ 1, 2, 3 ];
|
||||
|
||||
|
@ -2528,6 +2533,7 @@ if (isForwardRange!R1 && isForwardRange!R2
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.range.primitives : empty;
|
||||
// Needle is found; s is replaced by the substring following the first
|
||||
// occurrence of the needle.
|
||||
string s = "abcdef";
|
||||
|
@ -2786,6 +2792,8 @@ if (isForwardRange!R1 && isForwardRange!R2)
|
|||
///
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
import std.range.primitives : empty;
|
||||
|
||||
auto a = "Carl Sagan Memorial Station";
|
||||
auto r = findSplit(a, "Velikovsky");
|
||||
import std.typecons : isTuple;
|
||||
|
@ -2811,6 +2819,8 @@ if (isForwardRange!R1 && isForwardRange!R2)
|
|||
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
import std.range.primitives : empty;
|
||||
|
||||
auto a = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
|
||||
auto r = findSplit(a, [9, 1]);
|
||||
assert(!r);
|
||||
|
@ -3036,6 +3046,7 @@ if (isInputRange!Range && !isInfinite!Range &&
|
|||
unittest
|
||||
{
|
||||
import std.conv : text;
|
||||
import std.typecons : tuple;
|
||||
|
||||
debug(std_algorithm) scope(success)
|
||||
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
||||
|
@ -3170,6 +3181,7 @@ auto minElement(alias map = "a", Range, RangeElementType = ElementType!Range)
|
|||
@safe pure unittest
|
||||
{
|
||||
import std.range : enumerate;
|
||||
import std.typecons : tuple;
|
||||
|
||||
assert([2, 1, 4, 3].minElement == 1);
|
||||
|
||||
|
@ -3260,6 +3272,7 @@ auto maxElement(alias map = "a", Range, RangeElementType = ElementType!Range)
|
|||
@safe pure unittest
|
||||
{
|
||||
import std.range : enumerate;
|
||||
import std.typecons : tuple;
|
||||
assert([2, 1, 4, 3].maxElement == 4);
|
||||
|
||||
// allows to get the index of an element too
|
||||
|
@ -3794,6 +3807,8 @@ bool startsWith(alias pred, R)(R doesThisStart)
|
|||
assert(startsWith("abc", "x", "aa", "ab") == 3);
|
||||
assert(startsWith("abc", "x", "aaa", "sab") == 0);
|
||||
assert(startsWith("abc", "x", "aaa", "a", "sab") == 3);
|
||||
|
||||
import std.typecons : Tuple;
|
||||
alias C = Tuple!(int, "x", int, "y");
|
||||
assert(startsWith!"a.x == b"([ C(1,1), C(1,2), C(2,2) ], [1, 1]));
|
||||
assert(startsWith!"a.x == b"([ C(1,1), C(2,1), C(2,2) ], [1, 1], [1, 2], [1, 3]) == 2);
|
||||
|
|
|
@ -523,6 +523,7 @@ auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges)
|
|||
|
||||
pure @safe nothrow @nogc unittest
|
||||
{
|
||||
import std.range.primitives : isForwardRange;
|
||||
int[2] A = [1,2];
|
||||
auto C = cartesianProduct(A[], A[], A[]);
|
||||
assert(isForwardRange!(typeof(C)));
|
||||
|
@ -954,6 +955,7 @@ SetDifference!(less, R1, R2) setDifference(alias less = "a < b", R1, R2)
|
|||
@safe unittest
|
||||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
import std.range.primitives : isForwardRange;
|
||||
|
||||
int[] a = [ 1, 2, 4, 5, 7, 9 ];
|
||||
int[] b = [ 0, 1, 2, 4, 7, 8 ];
|
||||
|
@ -1243,6 +1245,7 @@ setSymmetricDifference(alias less = "a < b", R1, R2)
|
|||
@safe unittest
|
||||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
import std.range.primitives : isForwardRange;
|
||||
|
||||
int[] a = [ 1, 2, 4, 5, 7, 9 ];
|
||||
int[] b = [ 0, 1, 2, 4, 7, 8 ];
|
||||
|
|
|
@ -501,6 +501,8 @@ Range partition(alias predicate,
|
|||
{
|
||||
import std.algorithm.searching : count, find;
|
||||
import std.conv : text;
|
||||
import std.range.primitives : empty;
|
||||
import std.algorithm.mutation : SwapStrategy;
|
||||
|
||||
auto Arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
auto arr = Arr.dup;
|
||||
|
@ -933,6 +935,7 @@ template multiSort(less...) //if (less.length > 1)
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.algorithm.mutation : SwapStrategy;
|
||||
static struct Point { int x, y; }
|
||||
auto pts1 = [ Point(0, 0), Point(5, 5), Point(0, 1), Point(0, 2) ];
|
||||
auto pts2 = [ Point(0, 0), Point(0, 1), Point(0, 2), Point(5, 5) ];
|
||||
|
@ -1185,6 +1188,7 @@ sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable,
|
|||
unittest
|
||||
{
|
||||
// Showcase stable sorting
|
||||
import std.algorithm.mutation : SwapStrategy;
|
||||
string[] words = [ "aBc", "a", "abc", "b", "ABC", "c" ];
|
||||
sort!("toUpper(a) < toUpper(b)", SwapStrategy.stable)(words);
|
||||
assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue