Value::compare() is now const and has an actual implementation with unit tests.

This commit is contained in:
Baptiste Lepilleur 2011-05-02 20:11:48 +00:00
parent e3cc0f004b
commit 1837a1c508
4 changed files with 38 additions and 29 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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 );
}