I cannot understand problem with argument of the function

mrd denis.feklushkin at gmail.com
Tue Sep 17 20:31:13 PDT 2013


// Simple function called for unsigned integers:
static
ubyte[] packVarint(T)( T value )
if( isIntegral!T && isUnsigned!T )
out( arr )
{
     T d;
     size_t size = d.unpackVarint( &arr[0] );

     import std.stdio;
     import std.conv;

     writeln( "out contract, type=", typeid(T), " isUnsigned=", 
isUnsigned!T, " arg=", value, " result=", arr );
     stdout.flush;

     assert( size == arr.length );
     assert( d == value );
}
body
{
     import std.stdio;
     import std.conv;
     writeln( "value inside of body: ", value );
     stdout.flush;

     ubyte[] res;

     immutable ubyte maximal = 0b_1000_0000;

     while( value >= maximal )
     {
         res ~= cast( ubyte )( value | maximal );
         value >>= 7;
     }

     res ~= cast( ubyte ) value;

     return res;
}
unittest
{
     auto v = packVarint!ulong( 300 );
     assert( v.length == 2 );
     assert( v == [ 0b_10101100, 0b_00000010 ] );
}


output:
value inside of body: 1 // arg=1
out contract, type=uint isUnsigned=true arg=1 result=[1] // works 
for arg=1 !
value inside of body: 2
out contract, type=uint isUnsigned=true arg=2 result=[2]
value inside of body: 1
out contract, type=uint isUnsigned=true arg=1 result=[1]
value inside of body: 2
out contract, type=uint isUnsigned=true arg=2 result=[2]
value inside of body: 12
out contract, type=ulong isUnsigned=true arg=12 result=[12]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 14
out contract, type=ulong isUnsigned=true arg=14 result=[14]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 12
out contract, type=ulong isUnsigned=true arg=12 result=[12]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 16
out contract, type=ulong isUnsigned=true arg=16 result=[16]
value inside of body: 32
out contract, type=ulong isUnsigned=true arg=32 result=[32]
value inside of body: 0
out contract, type=uint isUnsigned=true arg=0 result=[0]
value inside of body: 5
out contract, type=uint isUnsigned=true arg=5 result=[5]
value inside of body: 2
out contract, type=uint isUnsigned=true arg=2 result=[2]
value inside of body: 1
out contract, type=uint isUnsigned=true arg=1 result=[1]
value inside of body: 10
out contract, type=ulong isUnsigned=true arg=10 result=[10]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 2
out contract, type=uint isUnsigned=true arg=2 result=[2]
value inside of body: 3
out contract, type=ulong isUnsigned=true arg=3 result=[3]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 1 // argument=1
out contract, type=ulong isUnsigned=true arg=1 result=[1] // also 
successful result
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 3
out contract, type=ulong isUnsigned=true arg=3 result=[3]
value inside of body: 30
out contract, type=ulong isUnsigned=true arg=30 result=[30]
value inside of body: 18446744073709551615 // WTF???!!!
out contract, type=ulong isUnsigned=true arg=1 result=[255, 255, 
255, 255, 255, 255, 255, 255, 255, 1] // unsuccessful packing 
with previously many times used arg=1
core.exception.AssertError at compression.pb_encoding(105): 
Assertion failure
----------------
./main(_d_assertm+0x16) [0x81b4316]
./main() [0x8179d47]
./main(ubyte[] 
compression.pb_encoding.packVarint!(ulong).packVarint(ulong)+0x149) 
[0x8179899]
./main(ubyte[] 
compression.pb_encoding.packVarint!(long).packVarint(inout(long))+0x18) 
[0x81796a8]


More information about the Digitalmars-d-learn mailing list