diff --git a/std/typecons.d b/std/typecons.d index b366b451897..e3b527a6ee2 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -1134,19 +1134,6 @@ unittest /** Returns a $(D Tuple) object instantiated and initialized according to the arguments. - -Example: ----- -auto value = tuple(5, 6.7, "hello"); -assert(value[0] == 5); -assert(value[1] == 6.7); -assert(value[2] == "hello"); - -// Field names can be provided. -auto entry = tuple!("index", "value")(4, "Hello"); -assert(entry.index == 4); -assert(entry.value == "Hello"); ----- */ template tuple(Names...) @@ -1190,6 +1177,20 @@ template tuple(Names...) } } +/// +unittest +{ + auto value = tuple(5, 6.7, "hello"); + assert(value[0] == 5); + assert(value[1] == 6.7); + assert(value[2] == "hello"); + + // Field names can be provided. + auto entry = tuple!("index", "value")(4, "Hello"); + assert(entry.index == 4); + assert(entry.value == "Hello"); +} + /** Returns $(D true) if and only if $(D T) is an instance of the $(D Tuple) struct template. @@ -1206,6 +1207,7 @@ template isTuple(T) } } +/// unittest { static assert(isTuple!(Tuple!())); @@ -1213,7 +1215,10 @@ unittest static assert(isTuple!(Tuple!(int, real, string))); static assert(isTuple!(Tuple!(int, "x", real, "y"))); static assert(isTuple!(Tuple!(int, Tuple!(real), string))); +} +unittest +{ static assert(isTuple!(const Tuple!(int))); static assert(isTuple!(immutable Tuple!(int))); @@ -1489,14 +1494,6 @@ unittest /** Order the provided members to minimize size while preserving alignment. Returns a declaration to be mixed in. - -Example: ---- -struct Banner { - mixin(alignForSize!(byte[6], double)(["name", "height"])); -} ---- - Alignment is not always optimal for 80-bit reals, nor for structs declared as align(1). */ @@ -1528,6 +1525,14 @@ string alignForSize(E...)(string[] names...) return s; } +/// +unittest +{ + struct Banner { + mixin(alignForSize!(byte[6], double)(["name", "height"])); + } +} + unittest { enum x = alignForSize!(int[], char[3], short, double[5])("x", "y","z", "w"); @@ -1551,15 +1556,6 @@ the absence of a value. If default constructed, a $(D Nullable!T) object starts in the null state. Assigning it renders it non-null. Calling $(D nullify) can nullify it again. -Example: ----- -Nullable!int a; -assert(a.isNull); -a = 5; -assert(!a.isNull); -assert(a == 5); ----- - Practically $(D Nullable!T) stores a $(D T) and a $(D bool). */ struct Nullable(T) @@ -1638,6 +1634,16 @@ $(D this) must not be in the null state. alias get this; } +/// +unittest +{ + Nullable!int a; + assert(a.isNull); + a = 5; + assert(!a.isNull); + assert(a == 5); +} + unittest { import std.exception : assertThrown; @@ -4021,20 +4027,6 @@ autoInit == RefCountedAutoInitialize.no), user code must call either $(D refCountedStore.isInitialized) or $(D refCountedStore.ensureInitialized) before attempting to access the payload. Not doing so results in null pointer dereference. - -Example: ----- -// A pair of an $(D int) and a $(D size_t) - the latter being the -// reference count - will be dynamically allocated -auto rc1 = RefCounted!int(5); -assert(rc1 == 5); -// No more allocation, add just one extra reference count -auto rc2 = rc1; -// Reference semantics -rc2 = 42; -assert(rc1 == 42); -// the pair will be freed when rc1 and rc2 go out of scope ----- */ struct RefCounted(T, RefCountedAutoInitialize autoInit = RefCountedAutoInitialize.yes) @@ -4228,6 +4220,21 @@ assert(refCountedStore.isInitialized)). alias refCountedPayload this; } +/// +unittest +{ + // A pair of an $(D int) and a $(D size_t) - the latter being the + // reference count - will be dynamically allocated + auto rc1 = RefCounted!int(5); + assert(rc1 == 5); + // No more allocation, add just one extra reference count + auto rc2 = rc1; + // Reference semantics + rc2 = 42; + assert(rc1 == 42); + // the pair will be freed when rc1 and rc2 go out of scope +} + unittest { RefCounted!int* p; @@ -4313,30 +4320,6 @@ unittest /** Make proxy for $(D a). - -Example: ----- -struct MyInt -{ - private int value; - mixin Proxy!value; - - this(int n){ value = n; } -} - -MyInt n = 10; - -// Enable operations that original type has. -++n; -assert(n == 11); -assert(n * 2 == 22); - -void func(int n) { } - -// Disable implicit conversions to original type. -//int x = n; -//func(n); ----- */ mixin template Proxy(alias a) { @@ -4522,6 +4505,31 @@ mixin template Proxy(alias a) } } +/// +unittest +{ + struct MyInt + { + private int value; + mixin Proxy!value; + + this(int n){ value = n; } + } + + MyInt n = 10; + + // Enable operations that original type has. + ++n; + assert(n == 11); + assert(n * 2 == 22); + + void func(int n) { } + + // Disable implicit conversions to original type. + //int x = n; + //func(n); +} + unittest { static struct MyInt @@ -5584,8 +5592,8 @@ struct No enum opDispatch = Flag!name.no; } } -//template no(string name) { enum Flag!name no = Flag!name.no; } +/// unittest { Flag!"abc" flag1;