mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-01-14 01:47:54 +08:00
Value::compare() is now const and has an actual implementation with unit tests.
This commit is contained in:
parent
e3cc0f004b
commit
1837a1c508
3
NEWS.txt
3
NEWS.txt
@ -90,6 +90,9 @@
|
||||
- Fixed Value::operator <= implementation (had the semantic of operator >=).
|
||||
Found when addigin unit tests for comparison operators.
|
||||
|
||||
- Value::compare() is now const and has an actual implementation with
|
||||
unit tests.
|
||||
|
||||
* License
|
||||
|
||||
- See file LICENSE for details. Basically JsonCpp is now licensed under
|
||||
|
@ -256,7 +256,7 @@ namespace Json {
|
||||
bool operator ==( const Value &other ) const;
|
||||
bool operator !=( const Value &other ) const;
|
||||
|
||||
int compare( const Value &other );
|
||||
int compare( const Value &other ) const;
|
||||
|
||||
const char *asCString() const;
|
||||
std::string asString() const;
|
||||
|
@ -524,35 +524,16 @@ Value::type() const
|
||||
|
||||
|
||||
int
|
||||
Value::compare( const Value &other )
|
||||
Value::compare( const Value &other ) const
|
||||
{
|
||||
/*
|
||||
int typeDelta = other.type_ - type_;
|
||||
switch ( type_ )
|
||||
{
|
||||
case nullValue:
|
||||
|
||||
return other.type_ == type_;
|
||||
case intValue:
|
||||
if ( other.type_.isNumeric()
|
||||
case uintValue:
|
||||
case realValue:
|
||||
case booleanValue:
|
||||
break;
|
||||
case stringValue,
|
||||
break;
|
||||
case arrayValue:
|
||||
delete value_.array_;
|
||||
break;
|
||||
case objectValue:
|
||||
delete value_.map_;
|
||||
default:
|
||||
JSON_ASSERT_UNREACHABLE;
|
||||
}
|
||||
*/
|
||||
return 0; // unreachable
|
||||
if ( *this < other )
|
||||
return -1;
|
||||
if ( *this > other )
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Value::operator <( const Value &other ) const
|
||||
{
|
||||
@ -594,7 +575,7 @@ Value::operator <( const Value &other ) const
|
||||
default:
|
||||
JSON_ASSERT_UNREACHABLE;
|
||||
}
|
||||
return 0; // unreachable
|
||||
return false; // unreachable
|
||||
}
|
||||
|
||||
bool
|
||||
@ -656,7 +637,7 @@ Value::operator ==( const Value &other ) const
|
||||
default:
|
||||
JSON_ASSERT_UNREACHABLE;
|
||||
}
|
||||
return 0; // unreachable
|
||||
return false; // unreachable
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -256,6 +256,12 @@ ValueTest::checkIs( const Json::Value &value, const IsCheck &check )
|
||||
}
|
||||
|
||||
|
||||
JSONTEST_FIXTURE( ValueTest, compareNull )
|
||||
{
|
||||
JSONTEST_ASSERT_PRED( checkIsEqual( Json::Value(), Json::Value() ) );
|
||||
}
|
||||
|
||||
|
||||
JSONTEST_FIXTURE( ValueTest, compareInt )
|
||||
{
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( 0, 10 ) );
|
||||
@ -347,6 +353,19 @@ JSONTEST_FIXTURE( ValueTest, compareObject )
|
||||
}
|
||||
|
||||
|
||||
JSONTEST_FIXTURE( ValueTest, compareType )
|
||||
{
|
||||
// object of different type are ordered according to their type
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(), Json::Value(1) ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1), Json::Value(1u) ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1u), Json::Value(1.0) ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1.0), Json::Value("a") ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value("a"), Json::Value(true) ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(true), Json::Value(Json::arrayValue) ) );
|
||||
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(Json::arrayValue), Json::Value(Json::objectValue) ) );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ValueTest::checkIsLess( const Json::Value &x, const Json::Value &y )
|
||||
{
|
||||
@ -360,6 +379,8 @@ ValueTest::checkIsLess( const Json::Value &x, const Json::Value &y )
|
||||
JSONTEST_ASSERT( !(y <= x) );
|
||||
JSONTEST_ASSERT( !(x > y) );
|
||||
JSONTEST_ASSERT( !(y < x) );
|
||||
JSONTEST_ASSERT( x.compare( y ) < 0 );
|
||||
JSONTEST_ASSERT( y.compare( x ) >= 0 );
|
||||
}
|
||||
|
||||
|
||||
@ -376,6 +397,8 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
|
||||
JSONTEST_ASSERT( !(y < x) );
|
||||
JSONTEST_ASSERT( !(x > y) );
|
||||
JSONTEST_ASSERT( !(y > x) );
|
||||
JSONTEST_ASSERT( x.compare( y ) == 0 );
|
||||
JSONTEST_ASSERT( y.compare( x ) == 0 );
|
||||
}
|
||||
|
||||
|
||||
@ -394,6 +417,7 @@ int main( int argc, const char *argv[] )
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareNull );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareInt );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareUInt );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareDouble );
|
||||
@ -401,5 +425,6 @@ int main( int argc, const char *argv[] )
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareBoolean );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject );
|
||||
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType );
|
||||
return runner.runCommandLine( argc, argv );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user