mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
std.variant: Add Variant unittests for 'null' comparisons
Fix Issue #22647 - [std.variant.Variant] Cannot compare types compliant with null comparison with 'null' Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
This commit is contained in:
parent
5f14dc0305
commit
b901b5fb50
1 changed files with 36 additions and 0 deletions
|
@ -1622,6 +1622,42 @@ pure nothrow @nogc
|
||||||
assert(v != b);
|
assert(v != b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://issues.dlang.org/show_bug.cgi?id=22647
|
||||||
|
// Can compare with 'null'
|
||||||
|
@system unittest
|
||||||
|
{
|
||||||
|
static struct Bar
|
||||||
|
{
|
||||||
|
int* ptr;
|
||||||
|
alias ptr this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Foo {}
|
||||||
|
int* iptr;
|
||||||
|
int[] arr;
|
||||||
|
|
||||||
|
Variant v = Foo.init; // 'null'
|
||||||
|
assert(v != null); // can only compare objects with 'null' by using 'is'
|
||||||
|
|
||||||
|
v = iptr;
|
||||||
|
assert(v == null); // pointers can be compared with 'null'
|
||||||
|
|
||||||
|
v = arr;
|
||||||
|
assert(v == null); // arrays can be compared with 'null'
|
||||||
|
|
||||||
|
v = "";
|
||||||
|
assert(v == null); // strings are arrays, an empty string is considered 'null'
|
||||||
|
|
||||||
|
v = Bar.init;
|
||||||
|
assert(v == null); // works with alias this
|
||||||
|
|
||||||
|
v = [3];
|
||||||
|
assert(v != null);
|
||||||
|
assert(v > null);
|
||||||
|
assert(v >= null);
|
||||||
|
assert(!(v < null));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
_Algebraic data type restricted to a closed set of possible
|
_Algebraic data type restricted to a closed set of possible
|
||||||
types. It's an alias for $(LREF VariantN) with an
|
types. It's an alias for $(LREF VariantN) with an
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue