Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit f2f44bd

Browse files
committed
More unittests
1 parent 40f6906 commit f2f44bd

File tree

1 file changed

+99
-32
lines changed

1 file changed

+99
-32
lines changed

src/rt/util/typeinfo.d

+99-32
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,30 @@ unittest
468468
// Predefined TypeInfos
469469
////////////////////////////////////////////////////////////////////////////////
470470

471+
// void
472+
class TypeInfo_v : TypeInfoGeneric!ubyte
473+
{
474+
const: nothrow: pure: @trusted:
475+
476+
override string toString() const pure nothrow @safe { return "void"; }
477+
478+
override size_t getHash(scope const void* p)
479+
{
480+
assert(0);
481+
}
482+
483+
override @property uint flags() nothrow pure
484+
{
485+
return 1;
486+
}
487+
488+
unittest
489+
{
490+
assert(typeid(void).toString == "void");
491+
assert(typeid(void).flags == 1);
492+
}
493+
}
494+
471495
// All integrals.
472496
class TypeInfo_b : TypeInfoGeneric!bool {}
473497
class TypeInfo_g : TypeInfoGeneric!byte {}
@@ -547,6 +571,17 @@ class TypeInfo_P : TypeInfoGeneric!(void*)
547571
override @property uint flags() nothrow pure const { return 1; }
548572
}
549573

574+
unittest
575+
{
576+
with (typeid(int*))
577+
{
578+
int x;
579+
int* p = &x;
580+
assert(getHash(&p) != 0);
581+
assert(flags == 1);
582+
}
583+
}
584+
550585
// Arrays of all integrals.
551586
class TypeInfo_Ab : TypeInfoArrayGeneric!bool {}
552587
class TypeInfo_Ag : TypeInfoArrayGeneric!byte {}
@@ -620,27 +655,16 @@ class TypeInfo_Ac : TypeInfoArrayGeneric!creal {}
620655
class TypeInfo_Av : TypeInfo_Ah
621656
{
622657
override string toString() const { return "void[]"; }
658+
623659
override @property inout(TypeInfo) next() inout
624660
{
625661
return cast(inout) typeid(void);
626662
}
627-
}
628-
629-
// void
630-
class TypeInfo_v : TypeInfoGeneric!ubyte
631-
{
632-
const: nothrow: pure: @trusted:
633-
634-
override string toString() const pure nothrow @safe { return "void"; }
635-
636-
override size_t getHash(scope const void* p)
637-
{
638-
assert(0);
639-
}
640663

641-
override @property uint flags() nothrow pure
664+
unittest
642665
{
643-
return 1;
666+
assert(typeid(void[]).toString == "void[]");
667+
assert(typeid(void[]).next == typeid(void));
644668
}
645669
}
646670

@@ -652,6 +676,11 @@ class TypeInfo_D : TypeInfoGeneric!(void delegate(int))
652676
{
653677
return 1;
654678
}
679+
680+
unittest
681+
{
682+
assert(typeid(int delegate(string)).flags == 1);
683+
}
655684
}
656685

657686
// typeof(null)
@@ -690,6 +719,20 @@ class TypeInfo_n : TypeInfo
690719
}
691720

692721
override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; }
722+
723+
unittest
724+
{
725+
with (typeid(typeof(null)))
726+
{
727+
assert(toString == "typeof(null)");
728+
assert(getHash(null) == 0);
729+
assert(equals(null, null));
730+
assert(compare(null, null) == 0);
731+
assert(tsize == typeof(null).sizeof);
732+
assert(initializer == new ubyte[(void*).sizeof]);
733+
assert(rtInfo == rtinfoNoPointers);
734+
}
735+
}
693736
}
694737

695738
// Object
@@ -718,22 +761,11 @@ class TypeInfo_C : TypeInfo
718761
{
719762
Object o1 = *cast(Object*)p1;
720763
Object o2 = *cast(Object*)p2;
721-
int c = 0;
722-
723-
// Regard null references as always being "less than"
724-
if (o1 !is o2)
725-
{
726-
if (o1)
727-
{
728-
if (!o2)
729-
c = 1;
730-
else
731-
c = o1.opCmp(o2);
732-
}
733-
else
734-
c = -1;
735-
}
736-
return c;
764+
if (o1 is o2)
765+
return 0; // includes the case null vs null
766+
if (int result = (o1 !is null) - (o2 !is null))
767+
return result;
768+
return o1.opCmp(o2);
737769
}
738770

739771
override @property size_t tsize() nothrow pure
@@ -743,11 +775,46 @@ class TypeInfo_C : TypeInfo
743775

744776
override const(void)[] initializer() const @trusted
745777
{
746-
return (cast(void *)null)[0 .. Object.sizeof];
778+
assert(0);
747779
}
748780

749781
override @property uint flags() nothrow pure
750782
{
751783
return 1;
752784
}
785+
786+
unittest
787+
{
788+
static class Bacon
789+
{
790+
int sizzle = 1;
791+
override int opCmp(Object rhs) const
792+
{
793+
if (auto rhsb = cast(Bacon) rhs)
794+
return (sizzle > rhsb.sizzle) - (sizzle < rhsb.sizzle);
795+
return 0;
796+
}
797+
}
798+
Object obj = new Bacon;
799+
Bacon obj2 = new Bacon;
800+
obj2.sizzle = 2;
801+
auto dummy = new Object;
802+
with (typeid(obj))
803+
{
804+
assert(toString[$ - 6 .. $] == ".Bacon");
805+
assert(getHash(&obj) != 0);
806+
assert(equals(&obj, &obj));
807+
assert(!equals(&obj, &obj2));
808+
assert(compare(&obj, &dummy) == 0);
809+
assert(compare(&obj, &obj) == 0);
810+
assert(compare(&obj, &obj2) == -1);
811+
assert(compare(&obj2, &obj) == 1);
812+
assert(tsize == Object.sizeof);
813+
assert(rtInfo == RTInfo!Bacon);
814+
assert(tsize == Object.sizeof);
815+
assert(initializer.ptr !is null);
816+
assert(initializer.length == __traits(classInstanceSize, Bacon));
817+
assert(flags == 1);
818+
}
819+
}
753820
}

0 commit comments

Comments
 (0)