@@ -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.
472496class TypeInfo_b : TypeInfoGeneric !bool {}
473497class 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.
551586class TypeInfo_Ab : TypeInfoArrayGeneric !bool {}
552587class TypeInfo_Ag : TypeInfoArrayGeneric !byte {}
@@ -620,27 +655,16 @@ class TypeInfo_Ac : TypeInfoArrayGeneric!creal {}
620655class 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- }
628663
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- }
640-
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
@@ -750,4 +782,37 @@ class TypeInfo_C : TypeInfo
750782 {
751783 return 1 ;
752784 }
785+
786+ unittest
787+ {
788+ 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+ with (typeid (obj))
802+ {
803+ assert (toString[$ - 6 .. $] == " .Bacon" );
804+ assert (getHash(&obj) != 0 );
805+ assert (equals(&obj, &obj));
806+ assert (! equals(&obj, &obj2));
807+ assert (compare(&obj, &obj) == 0 );
808+ assert (compare(&obj, &obj2) == - 1 );
809+ assert (compare(&obj2, &obj) == 1 );
810+ assert (tsize == Object .sizeof);
811+ assert (rtInfo != rtinfoNoPointers);
812+ assert (tsize == Object .sizeof);
813+ assert (initializer.ptr ! is null );
814+ assert (initializer.length == __traits(classInstanceSize, Bacon));
815+ assert (flags == 1 );
816+ }
817+ }
753818}
0 commit comments