mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
has_public_example: std.array
This commit is contained in:
parent
8180178986
commit
b6512b94f4
2 changed files with 88 additions and 2 deletions
88
std/array.d
88
std/array.d
|
@ -212,6 +212,18 @@ if (isNarrowString!String)
|
|||
return cast(typeof(return)) str.toUTF32;
|
||||
}
|
||||
|
||||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.range.primitives : isRandomAccessRange;
|
||||
|
||||
assert("Hello D".array == "Hello D"d);
|
||||
static assert(isRandomAccessRange!string == false);
|
||||
|
||||
assert("Hello D"w.array == "Hello D"d);
|
||||
static assert(isRandomAccessRange!dstring == true);
|
||||
}
|
||||
|
||||
@system unittest
|
||||
{
|
||||
// @system due to array!string
|
||||
|
@ -534,7 +546,7 @@ if (isDynamicArray!T && allSatisfy!(isIntegral, I) && hasIndirections!(ElementEn
|
|||
return arrayAllocImpl!(false, T, ST)(sizes);
|
||||
}
|
||||
|
||||
///
|
||||
/// ditto
|
||||
auto uninitializedArray(T, I...)(I sizes) nothrow @trusted
|
||||
if (isDynamicArray!T && allSatisfy!(isIntegral, I) && !hasIndirections!(ElementEncodingType!T))
|
||||
{
|
||||
|
@ -588,6 +600,18 @@ if (isDynamicArray!T && allSatisfy!(isIntegral, I))
|
|||
return arrayAllocImpl!(true, T, ST)(sizes);
|
||||
}
|
||||
|
||||
///
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
import std.algorithm.comparison : equal;
|
||||
import std.range : repeat;
|
||||
|
||||
auto arr = minimallyInitializedArray!(int[])(42);
|
||||
assert(arr.length == 42);
|
||||
// Elements aren't necessarily initialized to 0
|
||||
assert(!arr.equal(0.repeat(42)));
|
||||
}
|
||||
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
cast(void) minimallyInitializedArray!(int[][][][][])();
|
||||
|
@ -1535,6 +1559,15 @@ if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(range.front))))
|
|||
return range.splitter!isTerminator.array;
|
||||
}
|
||||
|
||||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.uni : isWhite;
|
||||
assert("Learning,D,is,fun".split(",") == ["Learning", "D", "is", "fun"]);
|
||||
assert("Learning D is fun".split!isWhite == ["Learning", "D", "is", "fun"]);
|
||||
assert("Learning D is fun".split(" D ") == ["Learning", "is fun"]);
|
||||
}
|
||||
|
||||
@safe unittest
|
||||
{
|
||||
import std.algorithm.comparison : cmp;
|
||||
|
@ -3212,6 +3245,23 @@ if (isDynamicArray!A)
|
|||
}
|
||||
}
|
||||
|
||||
///
|
||||
@system pure nothrow
|
||||
unittest
|
||||
{
|
||||
int[] a = [1, 2];
|
||||
auto app2 = appender(&a);
|
||||
assert(app2.data == [1, 2]);
|
||||
assert(a == [1, 2]);
|
||||
app2 ~= 3;
|
||||
app2 ~= [4, 5, 6];
|
||||
assert(app2.data == [1, 2, 3, 4, 5, 6]);
|
||||
assert(a == [1, 2, 3, 4, 5, 6]);
|
||||
|
||||
app2.reserve(5);
|
||||
assert(app2.capacity >= 5);
|
||||
}
|
||||
|
||||
/++
|
||||
Convenience function that returns an $(LREF Appender) instance,
|
||||
optionally initialized with $(D array).
|
||||
|
@ -3334,6 +3384,25 @@ Appender!(E[]) appender(A : E[], E)(auto ref A array)
|
|||
});
|
||||
}
|
||||
|
||||
///
|
||||
@safe pure nothrow
|
||||
unittest
|
||||
{
|
||||
auto w = appender!string;
|
||||
// pre-allocate space for at least 10 elements (this avoids costly reallocations)
|
||||
w.reserve(10);
|
||||
assert(w.capacity >= 10);
|
||||
|
||||
w.put('a'); // single elements
|
||||
w.put("bc"); // multiple elements
|
||||
|
||||
// use the append syntax
|
||||
w ~= 'd';
|
||||
w ~= "ef";
|
||||
|
||||
assert(w.data == "abcdef");
|
||||
}
|
||||
|
||||
@safe pure nothrow unittest
|
||||
{
|
||||
{
|
||||
|
@ -3597,6 +3666,23 @@ RefAppender!(E[]) appender(P : E[]*, E)(P arrayPtr)
|
|||
return RefAppender!(E[])(arrayPtr);
|
||||
}
|
||||
|
||||
///
|
||||
@system pure nothrow
|
||||
unittest
|
||||
{
|
||||
int[] a = [1, 2];
|
||||
auto app2 = appender(&a);
|
||||
assert(app2.data == [1, 2]);
|
||||
assert(a == [1, 2]);
|
||||
app2 ~= 3;
|
||||
app2 ~= [4, 5, 6];
|
||||
assert(app2.data == [1, 2, 3, 4, 5, 6]);
|
||||
assert(a == [1, 2, 3, 4, 5, 6]);
|
||||
|
||||
app2.reserve(5);
|
||||
assert(app2.capacity >= 5);
|
||||
}
|
||||
|
||||
@system unittest
|
||||
{
|
||||
import std.exception;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue