equality and comparison
Algebraic!(float) a, b; static assert(a.hasFixedSize); a = 1.0f; assert(a._tix != a.Ix.init); b = 1.0f; assert(b._tix != b.Ix.init); assert(a._tix == b._tix); assert((cast(ubyte*)&a)[0 .. a.sizeof] == (cast(ubyte*)&b)[0 .. b.sizeof]); assert(a == b); /+ TODO: this errors with dmd master +/
alias C = Algebraic!(float, double); C a = 1.0; const C b = 2.0; const C c = 2.0f; const C d = 1.0f; assert(a.commonValue == 1); assert(b.commonValue == 2); assert(c.commonValue == 2); assert(d.commonValue == 1); // nothrow comparison possible assert(a < b); assert(a < c); assert(a == d); static assert(!a.hasFixedSize); static assert(a.allowsAssignmentFrom!float); static assert(a.allowsAssignmentFrom!double); static assert(!a.allowsAssignmentFrom!string); a.clear(); assert(!a.hasValue); assert(a.peek!float is null); assert(a.peek!double is null); assert(a.currentSize == 0);
aliasing traits
import std.traits : hasAliasing; static assert(!hasAliasing!(Algebraic!(long, double))); static assert(!hasAliasing!(Algebraic!(long, string))); static assert(!hasAliasing!(Algebraic!(long, immutable(double)*))); static assert(hasAliasing!(Algebraic!(long, double*)));
equality and comparison
Algebraic!(int) a, b; static assert(a.hasFixedSize); a = 1; b = 1; assert(a == b);
equality and comparison
Algebraic!(float) a, b; static assert(a.hasFixedSize); a = 1.0f; b = 1.0f; assert(a == b);
equality and comparison
Algebraic!(float, double, string) a, b; static assert(!a.hasFixedSize); a = 1.0f; b = 1.0f; assert(a == b); a = 1.0f; b = 2.0f; assert(a != b); assert(a < b); assert(b > a); a = "alpha"; b = "alpha"; assert(a == b); a = "a"; b = "b"; assert(a != b); assert(a < b); assert(b > a);
AA keys
alias C = Algebraic!(float, double); static assert(!C.hasFixedSize); string[C] a; a[C(1.0f)] = "1.0f"; a[C(2.0)] = "2.0"; assert(a[C(1.0f)] == "1.0f"); assert(a[C(2.0)] == "2.0");
verify nothrow comparisons
alias C = Algebraic!(int, float, double); static assert(!C.hasFixedSize); assert(C(1.0) < 2); assert(C(1.0) < 2.0); assert(C(1.0) < 2.0); static assert(!__traits(compiles, { C(1.0) < 'a'; })); // cannot compare with char static assert(!__traits(compiles, { C(1.0) < "a"; })); // cannot compare with string
TODO
// alias C = Algebraic!(int, float, double); // alias D = Algebraic!(float, double); // assert(C(1) < D(2.0)); // assert(C(1) < D(1.0)); // static assert(!__traits(compiles, { C(1.0) < "a"; })); // cannot compare with string
if types have CommonType comparison is nothrow @nogc
alias C = Algebraic!(short, int, long, float, double); static assert(!C.hasFixedSize); assert(C(1) != C(2.0)); assert(C(1) == C(1.0));
if types have CommonType then comparison is nothrow @nogc
alias C = Algebraic!(short, int, long, float, double); static assert(!C.hasFixedSize); assert(C(1) != C(2.0)); assert(C(1) == C(1.0));
import nxt.container.static_array : MutableStringN; alias String15 = MutableStringN!(15); String15 s; String15 t = s; assert(t == s); alias V = Algebraic!(String15, string); V v = String15("first"); assert(v.peek!String15); assert(!v.peek!string); v = String15("second"); assert(v.peek!String15); assert(!v.peek!string); v = "third"; assert(!v.peek!String15); assert(v.peek!string); auto w = v; assert(v == w); w.clear(); assert(!v.isNull); assert(w.isNull); w = v; assert(!w.isNull); v = V.init; assert(v == V.init);
check default values
import nxt.container.static_array : MutableStringN; alias String15 = MutableStringN!(15); alias V = Algebraic!(String15, string); V _; assert(_._tix == V.Ix.init); assert(V.init._tix == V.Ix.init); /+ TODO: import nxt.bit_traits : isInitAllZeroBits; +/ /+ TODO: static assert(isInitAllZeroBits!(V)); +/
Algebraic type exception.