@@ -468,6 +468,30 @@ unittest
468
468
// Predefined TypeInfos
469
469
// //////////////////////////////////////////////////////////////////////////////
470
470
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
+
471
495
// All integrals.
472
496
class TypeInfo_b : TypeInfoGeneric !bool {}
473
497
class TypeInfo_g : TypeInfoGeneric !byte {}
@@ -547,6 +571,17 @@ class TypeInfo_P : TypeInfoGeneric!(void*)
547
571
override @property uint flags() nothrow pure const { return 1 ; }
548
572
}
549
573
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
+
550
585
// Arrays of all integrals.
551
586
class TypeInfo_Ab : TypeInfoArrayGeneric !bool {}
552
587
class TypeInfo_Ag : TypeInfoArrayGeneric !byte {}
@@ -620,27 +655,16 @@ class TypeInfo_Ac : TypeInfoArrayGeneric!creal {}
620
655
class TypeInfo_Av : TypeInfo_Ah
621
656
{
622
657
override string toString () const { return " void[]" ; }
658
+
623
659
override @property inout (TypeInfo ) next() inout
624
660
{
625
661
return cast (inout ) typeid (void );
626
662
}
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
- }
640
663
641
- override @property uint flags() nothrow pure
664
+ unittest
642
665
{
643
- return 1 ;
666
+ assert (typeid (void []).toString == " void[]" );
667
+ assert (typeid (void []).next == typeid (void ));
644
668
}
645
669
}
646
670
@@ -652,6 +676,11 @@ class TypeInfo_D : TypeInfoGeneric!(void delegate(int))
652
676
{
653
677
return 1 ;
654
678
}
679
+
680
+ unittest
681
+ {
682
+ assert (typeid (int delegate (string )).flags == 1 );
683
+ }
655
684
}
656
685
657
686
// typeof(null)
@@ -690,6 +719,20 @@ class TypeInfo_n : TypeInfo
690
719
}
691
720
692
721
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
+ }
693
736
}
694
737
695
738
// Object
@@ -718,22 +761,11 @@ class TypeInfo_C : TypeInfo
718
761
{
719
762
Object o1 = * cast (Object * )p1;
720
763
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);
737
769
}
738
770
739
771
override @property size_t tsize() nothrow pure
@@ -743,11 +775,46 @@ class TypeInfo_C : TypeInfo
743
775
744
776
override const (void )[] initializer () const @trusted
745
777
{
746
- return ( cast ( void * ) null )[ 0 .. Object .sizeof] ;
778
+ assert ( 0 ) ;
747
779
}
748
780
749
781
override @property uint flags() nothrow pure
750
782
{
751
783
return 1 ;
752
784
}
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
+ }
753
820
}
0 commit comments