Fixed bugs in asInt64 and asUInt64.

This commit is contained in:
Aaron Jacobs 2011-05-25 03:16:49 +00:00
parent 3c9fdeb859
commit 4f081b50e6
2 changed files with 63 additions and 2 deletions

View File

@ -766,7 +766,7 @@ Value::asInt64() const
return value_.uint_;
case realValue:
JSON_ASSERT_MESSAGE( value_.real_ >= minInt64 && value_.real_ <= maxInt64, "Real out of Int64 range" );
return Int( value_.real_ );
return Int64( value_.real_ );
case booleanValue:
return value_.bool_ ? 1 : 0;
case stringValue:
@ -794,7 +794,7 @@ Value::asUInt64() const
return value_.uint_;
case realValue:
JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt64, "Real out of UInt64 range" );
return UInt( value_.real_ );
return UInt64( value_.real_ );
case booleanValue:
return value_.bool_ ? 1 : 0;
case stringValue:

View File

@ -427,6 +427,67 @@ JSONTEST_FIXTURE( ValueTest, integers )
JSONTEST_ASSERT( double(kuint64max) == val.asDouble());
JSONTEST_ASSERT( float(kuint64max) == val.asFloat());
#else // ifdef JSON_NO_INT64
// 2^40 (signed constructor arg)
val = Json::Value(1LL << 40);
checks = IsCheck();
checks.isInt_ = true;
checks.isNumeric_ = true;
checks.isIntegral_ = true;
JSONTEST_ASSERT_PRED( checkIs( val, checks ) );
JSONTEST_ASSERT( (1LL << 40) == val.asInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestInt());
JSONTEST_ASSERT( (1LL << 40) == val.asUInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestUInt());
JSONTEST_ASSERT( (1LL << 40) == val.asDouble());
JSONTEST_ASSERT( (1LL << 40) == val.asFloat());
// 2^40 (unsigned constructor arg)
val = Json::Value(1ULL << 40);
checks = IsCheck();
checks.isUInt_ = true;
checks.isNumeric_ = true;
checks.isIntegral_ = true;
JSONTEST_ASSERT_PRED( checkIs( val, checks ) );
JSONTEST_ASSERT( (1LL << 40) == val.asInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestInt());
JSONTEST_ASSERT( (1LL << 40) == val.asUInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestUInt());
JSONTEST_ASSERT( (1LL << 40) == val.asDouble());
JSONTEST_ASSERT( (1LL << 40) == val.asFloat());
// 2^40 (floating-point constructor arg)
val = Json::Value((1LL << 40) / 1.0);
checks = IsCheck();
checks.isDouble_ = true;
checks.isNumeric_ = true;
JSONTEST_ASSERT_PRED( checkIs( val, checks ) );
JSONTEST_ASSERT( (1LL << 40) == val.asInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestInt());
JSONTEST_ASSERT( (1LL << 40) == val.asUInt64());
JSONTEST_ASSERT( (1LL << 40) == val.asLargestUInt());
JSONTEST_ASSERT( (1LL << 40) == val.asDouble());
JSONTEST_ASSERT( (1LL << 40) == val.asFloat());
// -2^40
val = Json::Value(-(1LL << 40));
checks = IsCheck();
checks.isInt_ = true;
checks.isNumeric_ = true;
checks.isIntegral_ = true;
JSONTEST_ASSERT_PRED( checkIs( val, checks ) );
JSONTEST_ASSERT( -(1LL << 40) == val.asInt64());
JSONTEST_ASSERT( -(1LL << 40) == val.asLargestInt());
JSONTEST_ASSERT( -(1LL << 40) == val.asDouble());
JSONTEST_ASSERT( -(1LL << 40) == val.asFloat());
// int64 max
val = Json::Value(Json::Int64(kint64max));