Add asserts for the "compiles" tests

This commit is contained in:
Dragos Carp 2015-11-16 03:04:07 +01:00
parent c17a621bf2
commit 75cbbef0ab
9 changed files with 52 additions and 17 deletions

View file

@ -765,14 +765,14 @@ unittest
// Issue #10130 - map of iota with const step. // Issue #10130 - map of iota with const step.
const step = 2; const step = 2;
map!(i => i)(iota(0, 10, step)); assert(map!(i => i)(iota(0, 10, step)).walkLength == 5);
// Need these to all by const to repro the float case, due to the // Need these to all by const to repro the float case, due to the
// CommonType template used in the float specialization of iota. // CommonType template used in the float specialization of iota.
const floatBegin = 0.0; const floatBegin = 0.0;
const floatEnd = 1.0; const floatEnd = 1.0;
const floatStep = 0.02; const floatStep = 0.02;
map!(i => i)(iota(floatBegin, floatEnd, floatStep)); assert(map!(i => i)(iota(floatBegin, floatEnd, floatStep)).walkLength == 50);
} }
@safe unittest @safe unittest

View file

@ -574,11 +574,13 @@ void fill(Range, Value)(Range range, Value value)
int[] a = [1, 2, 3]; int[] a = [1, 2, 3];
immutable(int) b = 0; immutable(int) b = 0;
a.fill(b); a.fill(b);
assert(a == [0, 0, 0]);
} }
{ {
double[] a = [1, 2, 3]; double[] a = [1, 2, 3];
immutable(int) b = 0; immutable(int) b = 0;
a.fill(b); a.fill(b);
assert(a == [0, 0, 0]);
} }
} }
@ -982,7 +984,10 @@ unittest
class S5; class S5;
S5 s51; S5 s51;
move(s51, s51); S5 s52 = s51;
S5 s53;
move(s52, s53);
assert(s53 is s51);
} }
/// Ditto /// Ditto
@ -1092,17 +1097,24 @@ unittest
class S5; class S5;
S5 s51; S5 s51;
s51 = move(s51); S5 s52 = s51;
S5 s53;
s53 = move(s52);
assert(s53 is s51);
} }
unittest unittest
{ {
static struct S { ~this() @system { } } static struct S { int n = 0; ~this() @system { n = 0; } }
S a, b; S a, b;
static assert(!__traits(compiles, () @safe { move(a, b); })); static assert(!__traits(compiles, () @safe { move(a, b); }));
static assert(!__traits(compiles, () @safe { move(a); })); static assert(!__traits(compiles, () @safe { move(a); }));
a.n = 1;
() @trusted { move(a, b); }(); () @trusted { move(a, b); }();
assert(a.n == 0);
a.n = 1;
() @trusted { move(a); }(); () @trusted { move(a); }();
assert(a.n == 0);
} }
unittest//Issue 6217 unittest//Issue 6217

View file

@ -377,7 +377,7 @@ unittest
import std.typecons; import std.typecons;
static assert(!__traits(compiles, [ tuple("foo", "bar", "baz") ].assocArray())); static assert(!__traits(compiles, [ tuple("foo", "bar", "baz") ].assocArray()));
static assert(!__traits(compiles, [ tuple("foo") ].assocArray())); static assert(!__traits(compiles, [ tuple("foo") ].assocArray()));
[ tuple("foo", "bar") ].assocArray(); assert([ tuple("foo", "bar") ].assocArray() == ["foo": "bar"]);
} }
// Issue 13909 // Issue 13909
@ -386,7 +386,7 @@ unittest
import std.typecons; import std.typecons;
auto a = [tuple!(const string, string)("foo", "bar")]; auto a = [tuple!(const string, string)("foo", "bar")];
auto b = [tuple!(string, const string)("foo", "bar")]; auto b = [tuple!(string, const string)("foo", "bar")];
assocArray(a); assert(assocArray(a) == [cast(const(string)) "foo": "bar"]);
static assert(!__traits(compiles, assocArray(b))); static assert(!__traits(compiles, assocArray(b)));
} }

View file

@ -1186,6 +1186,7 @@ public:
const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
//immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); //immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
st.year = 12; st.year = 12;
assert(st.year == 12);
static assert(!__traits(compiles, cst.year = 12)); static assert(!__traits(compiles, cst.year = 12));
//static assert(!__traits(compiles, ist.year = 12)); //static assert(!__traits(compiles, ist.year = 12));
} }
@ -1282,6 +1283,7 @@ public:
const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
//immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); //immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
st.yearBC = 12; st.yearBC = 12;
assert(st.yearBC == 12);
static assert(!__traits(compiles, cst.yearBC = 12)); static assert(!__traits(compiles, cst.yearBC = 12));
//static assert(!__traits(compiles, ist.yearBC = 12)); //static assert(!__traits(compiles, ist.yearBC = 12));
} }
@ -6839,6 +6841,7 @@ public:
const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); const cst = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
//immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33)); //immutable ist = SysTime(DateTime(1999, 7, 6, 12, 30, 33));
st.dayOfYear = 12; st.dayOfYear = 12;
assert(st.dayOfYear == 12);
static assert(!__traits(compiles, cst.dayOfYear = 12)); static assert(!__traits(compiles, cst.dayOfYear = 12));
//static assert(!__traits(compiles, ist.dayOfYear = 12)); //static assert(!__traits(compiles, ist.dayOfYear = 12));
} }
@ -9567,6 +9570,7 @@ public:
const cdate = Date(0, 7, 6); const cdate = Date(0, 7, 6);
immutable idate = Date(0, 7, 6); immutable idate = Date(0, 7, 6);
date.yearBC = 7; date.yearBC = 7;
assert(date.yearBC == 7);
static assert(!__traits(compiles, cdate.yearBC = 7)); static assert(!__traits(compiles, cdate.yearBC = 7));
static assert(!__traits(compiles, idate.yearBC = 7)); static assert(!__traits(compiles, idate.yearBC = 7));
} }
@ -12210,6 +12214,7 @@ public:
const cdate = Date(1999, 7, 6); const cdate = Date(1999, 7, 6);
immutable idate = Date(1999, 7, 6); immutable idate = Date(1999, 7, 6);
date.dayOfGregorianCal = 187; date.dayOfGregorianCal = 187;
assert(date.dayOfGregorianCal == 187);
static assert(!__traits(compiles, cdate.dayOfGregorianCal = 187)); static assert(!__traits(compiles, cdate.dayOfGregorianCal = 187));
static assert(!__traits(compiles, idate.dayOfGregorianCal = 187)); static assert(!__traits(compiles, idate.dayOfGregorianCal = 187));
} }
@ -15039,6 +15044,7 @@ public:
const cdt = DateTime(1999, 7, 6, 12, 30, 33); const cdt = DateTime(1999, 7, 6, 12, 30, 33);
immutable idt = DateTime(1999, 7, 6, 12, 30, 33); immutable idt = DateTime(1999, 7, 6, 12, 30, 33);
dt.yearBC = 12; dt.yearBC = 12;
assert(dt.yearBC == 12);
static assert(!__traits(compiles, cdt.yearBC = 12)); static assert(!__traits(compiles, cdt.yearBC = 12));
static assert(!__traits(compiles, idt.yearBC = 12)); static assert(!__traits(compiles, idt.yearBC = 12));
} }
@ -15077,6 +15083,7 @@ public:
const cdt = DateTime(1999, 7, 6, 12, 30, 33); const cdt = DateTime(1999, 7, 6, 12, 30, 33);
immutable idt = DateTime(1999, 7, 6, 12, 30, 33); immutable idt = DateTime(1999, 7, 6, 12, 30, 33);
dt.yearBC = 12; dt.yearBC = 12;
assert(dt.yearBC == 12);
static assert(!__traits(compiles, cdt.yearBC = 12)); static assert(!__traits(compiles, cdt.yearBC = 12));
static assert(!__traits(compiles, idt.yearBC = 12)); static assert(!__traits(compiles, idt.yearBC = 12));
} }
@ -16542,6 +16549,7 @@ public:
const cdt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)); const cdt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33));
immutable idt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33)); immutable idt = DateTime(Date(1999, 7, 6), TimeOfDay(12, 30, 33));
dt.dayOfYear = 12; dt.dayOfYear = 12;
assert(dt.dayOfYear == 12);
static assert(!__traits(compiles, cdt.dayOfYear = 12)); static assert(!__traits(compiles, cdt.dayOfYear = 12));
static assert(!__traits(compiles, idt.dayOfYear = 12)); static assert(!__traits(compiles, idt.dayOfYear = 12));
} }

View file

@ -983,9 +983,13 @@ unittest
{ {
enum abc = 1; enum abc = 1;
alias a = Alias!(123); alias a = Alias!(123);
static assert(a == 123);
alias b = Alias!(abc); alias b = Alias!(abc);
static assert(b == 1);
alias c = Alias!(int); alias c = Alias!(int);
static assert(c.stringof == "(int)");
alias d = Alias!(1, abc, int); alias d = Alias!(1, abc, int);
static assert(d[0] == 1 && d[1] == 1 && d[2].stringof == "int");
} }

View file

@ -1554,9 +1554,9 @@ auto chainPath(Ranges...)(auto ref Ranges ranges)
unittest unittest
{ {
chainPath(TestAliasedString(null), TestAliasedString(null), TestAliasedString(null)); assert(chainPath(TestAliasedString(null), TestAliasedString(null), TestAliasedString(null)).empty);
chainPath(TestAliasedString(null), TestAliasedString(null), ""); assert(chainPath(TestAliasedString(null), TestAliasedString(null), "").empty);
chainPath(TestAliasedString(null), "", TestAliasedString(null)); assert(chainPath(TestAliasedString(null), "", TestAliasedString(null)).empty);
static struct S { string s; } static struct S { string s; }
static assert(!__traits(compiles, chainPath(TestAliasedString(null), S(""), TestAliasedString(null)))); static assert(!__traits(compiles, chainPath(TestAliasedString(null), S(""), TestAliasedString(null))));
} }

View file

@ -3944,8 +3944,8 @@ pure unittest
import std.exception : assertThrown; import std.exception : assertThrown;
static struct S { @disable this(); } static struct S { @disable this(); }
zip((S[5]).init[]); assert(zip((S[5]).init[]).length == 5);
auto z = zip(StoppingPolicy.longest, cast(S[]) null, new int[1]); assert(zip(StoppingPolicy.longest, cast(S[]) null, new int[1]).length == 1);
assertThrown(zip(StoppingPolicy.longest, cast(S[]) null, new int[1]).front); assertThrown(zip(StoppingPolicy.longest, cast(S[]) null, new int[1]).front);
} }
@ -4212,10 +4212,6 @@ unittest
static assert(!__traits(compiles, { static assert(!__traits(compiles, {
foreach (ref a, ref b; lockstep(r1, r2)) { a++; } foreach (ref a, ref b; lockstep(r1, r2)) { a++; }
})); }));
foreach (a, ref b; lockstep(r1, r2))
{
a++;
}
} }
/** /**

View file

@ -445,6 +445,7 @@ unittest
int[] a = new int[10]; int[] a = new int[10];
static assert(!__traits(compiles, put(a, 1.0L))); static assert(!__traits(compiles, put(a, 1.0L)));
put(a, 1); put(a, 1);
assert(a.length == 9);
/* /*
* a[0] = 65; // OK * a[0] = 65; // OK
* a[0] = 'A'; // OK * a[0] = 'A'; // OK
@ -452,6 +453,7 @@ unittest
* put(a, "ABC"); // OK * put(a, "ABC"); // OK
*/ */
put(a, "ABC"); put(a, "ABC");
assert(a.length == 6);
} }
unittest unittest
@ -470,7 +472,9 @@ unittest
int[] b = new int[10]; int[] b = new int[10];
int c; int c;
put(b, c); put(b, c);
assert(b.length == 9);
put(a, b); put(a, b);
assert(a.length == 9);
static assert(!__traits(compiles, put(a, c))); static assert(!__traits(compiles, put(a, c)));
} }

View file

@ -1161,7 +1161,7 @@ unittest
foreach (v1; AliasSeq!(mv, cv, iv, wv, wcv)) foreach (v1; AliasSeq!(mv, cv, iv, wv, wcv))
foreach (v2; AliasSeq!(mv, cv, iv, wv, wcv)) foreach (v2; AliasSeq!(mv, cv, iv, wv, wcv))
{ {
cast(void) (v1 < v2); assert(!(v1 < v2));
} }
} }
{ {
@ -2179,6 +2179,10 @@ unittest
auto x2 = immutable Nullable!S1(sm); auto x2 = immutable Nullable!S1(sm);
auto x3 = Nullable!S1(si); auto x3 = Nullable!S1(si);
auto x4 = immutable Nullable!S1(si); auto x4 = immutable Nullable!S1(si);
assert(x1.val == 1);
assert(x2.val == 1);
assert(x3.val == 1);
assert(x4.val == 1);
} }
auto nm = 10; auto nm = 10;
@ -2191,6 +2195,8 @@ unittest
static assert(!__traits(compiles, { auto x2 = immutable Nullable!S2(sm); })); static assert(!__traits(compiles, { auto x2 = immutable Nullable!S2(sm); }));
static assert(!__traits(compiles, { auto x3 = Nullable!S2(si); })); static assert(!__traits(compiles, { auto x3 = Nullable!S2(si); }));
auto x4 = immutable Nullable!S2(si); auto x4 = immutable Nullable!S2(si);
assert(*x1.val == 10);
assert(*x4.val == 10);
} }
{ {
@ -2200,6 +2206,10 @@ unittest
auto x2 = immutable Nullable!S3(sm); auto x2 = immutable Nullable!S3(sm);
auto x3 = Nullable!S3(si); auto x3 = Nullable!S3(si);
auto x4 = immutable Nullable!S3(si); auto x4 = immutable Nullable!S3(si);
assert(*x1.val == 10);
assert(*x2.val == 10);
assert(*x3.val == 10);
assert(*x4.val == 10);
} }
} }
unittest unittest
@ -6553,6 +6563,7 @@ public:
C = 1<<2 C = 1<<2
} }
BitFlags!Enum flags1; BitFlags!Enum flags1;
assert(!(flags1 & (Enum.A | Enum.B | Enum.C)));
// You need to specify the $(D unsafe) parameter for enum with custom values // You need to specify the $(D unsafe) parameter for enum with custom values
enum UnsafeEnum enum UnsafeEnum