diff --git a/NEWS.txt b/NEWS.txt index 9b5d69c..71edd19 100644 --- a/NEWS.txt +++ b/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 diff --git a/include/json/value.h b/include/json/value.h index b629d8c..c9c7e1d 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -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; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index b8de89c..ec3fb2e 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -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 diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 004702b..3275219 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -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 ); }