From d38c8d472d45671f2e37b7c83194780c50f4e62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Thu, 13 Aug 2015 23:43:09 +0200 Subject: [PATCH] Add some more tests. --- source/taggedalgebraic.d | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/taggedalgebraic.d b/source/taggedalgebraic.d index 3511e58..9d91d52 100644 --- a/source/taggedalgebraic.d +++ b/source/taggedalgebraic.d @@ -241,6 +241,7 @@ ref inout(T) get(T, U)(ref inout(TaggedAlgebraic!U) ta) ta += 12; assert(ta == 24); + assert(ta - 10 == 14); ta = ["foo" : "bar"]; assert(ta.typeID == TA.Type.dictionary); @@ -269,6 +270,9 @@ ref inout(T) get(T, U)(ref inout(TaggedAlgebraic!U) ta) ta = TA(12, TA.Type.count); assert(ta.typeID == TA.Type.count); assert(ta == 12); + + ta = null; + assert(ta.typeID == TA.Type.null_); } unittest { @@ -409,6 +413,44 @@ unittest { // postblit/destructor test assert(S.i == 0); } +unittest { + static struct S { + union U { + int i; + string s; + U[] a; + } + alias TA = TaggedAlgebraic!U; + TA p; + alias p this; + } + S s = S(S.TA("hello")); + assert(cast(string)s == "hello"); +} + +unittest { // multiple operator choices + union U { + int i; + double d; + } + alias TA = TaggedAlgebraic!U; + TA ta = 12; + static assert(is(typeof(ta + 10) == TA)); // ambiguous, could be int or double + assert((ta + 10).typeID == TA.Type.i); + assert(ta + 10 == 22); + static assert(is(typeof(ta + 10.5) == double)); + assert(ta + 10.5 == 22.5); +} + +/*unittest { + union U { int i; } + alias TA = TaggedAlgebraic!U; + + TA a = 1, b = 2; + assert(a + b == 3); + static assert(is(typeof(a + b) == int)); +}*/ + /// Convenience type that can be used for union fields that have no value (`void` is not allowed). struct Void {}