Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3196

Show
Ignore:
Timestamp:
02/16/08 01:32:13 (10 months ago)
Author:
DRK
Message:

Fixed Variant, and added some unit tests to make sure there aren't regressions. Closes #654

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/core/Variant.d

    r3145 r3196  
    273273            else static if( isPointer!(T) ) 
    274274            { 
    275                 this.value.ptr = cast(void*)T
     275                this.value.ptr = cast(void*)value
    276276            } 
    277277            else static if( isObject!(T) ) 
    278278            { 
    279                 this.value.obj = T
     279                this.value.obj = value
    280280            } 
    281281            else 
     
    571571        assert( v.isEmpty, v.type.toString ); 
    572572 
     573        // Test basic integer storage and implicit casting support 
    573574        v = 42; 
    574575        assert( v.isA!(int), v.type.toString ); 
     
    580581        assert( v.get!(ulong) == 42uL ); 
    581582 
     583        // Test clearing 
    582584        v.clear; 
    583585        assert( v.isA!(void), v.type.toString ); 
    584586        assert( v.isEmpty, v.type.toString ); 
    585587 
     588        // Test strings 
    586589        v = "Hello, World!"c; 
    587590        assert( v.isA!(char[]), v.type.toString ); 
     
    589592        assert( v.get!(char[]) == "Hello, World!" ); 
    590593 
     594        // Test array storage 
    591595        v = [1,2,3,4,5]; 
    592596        assert( v.isA!(int[]), v.type.toString ); 
    593597        assert( v.get!(int[]) == [1,2,3,4,5] ); 
    594598 
     599        // Test pointer storage 
     600        v = &v; 
     601        assert( v.isA!(Variant*), v.type.toString ); 
     602        assert( !v.isImplicitly!(int*), v.type.toString ); 
     603        // NB: we *should* be able to implicitly cast any pointer to a void*; 
     604        // I'm just not sure how to do it right now.  This test will catch 
     605        // once it works, and remind us to switch the assert around. 
     606        assert( !v.isImplicitly!(void*), "see above comment in source" ); 
     607        assert( v.get!(Variant*) == &v ); 
     608 
     609        // Test object storage 
     610        { 
     611            scope o = new Object; 
     612            v = o; 
     613            assert( v.isA!(Object), v.type.toString ); 
     614            assert( v.get!(Object) is o ); 
     615        } 
     616 
     617        // Test doubles and implicit casting 
    595618        v = 3.1413; 
    596619        assert( v.isA!(double), v.type.toString ); 
     
    599622        assert( v.get!(double) == 3.1413 ); 
    600623 
     624        // Test storage transitivity 
    601625        auto u = Variant(v); 
    602626        assert( u.isA!(double), u.type.toString ); 
    603627        assert( u.get!(double) == 3.1413 ); 
    604628 
     629        // Test operators 
    605630        v = 38; 
    606631        assert( v + 4 == 42 ); 
     
    628653        assert( "abc" ~ Variant("def") == "abcdef" ); 
    629654 
     655        // Test op= operators 
    630656        v = 38; v += 4; assert( v == 42 ); 
    631657        v = 38; v -= 4; assert( v == 34 ); 
     
    641667        v = "abc"; v ~= "def"; assert( v == "abcdef" ); 
    642668 
     669        // Test comparison 
    643670        assert( Variant(0) < Variant(42) ); 
    644671        assert( Variant(42) > Variant(0) ); 
     
    647674        assert( Variant("bar") == Variant("bar") ); 
    648675        assert( Variant("foo") != Variant("bar") ); 
     676 
     677        // Test variants as AA keys 
    649678        { 
    650679            auto v1 = Variant(42); 
     
    661690            assert( hash[v3] == 2 ); 
    662691        } 
     692 
     693        // Test AA storage 
    663694        { 
    664695            int[char[]] hash;