Skip to content

Commit

Permalink
Value::compare() is now const and has an actual implementation with u…
Browse files Browse the repository at this point in the history
…nit tests.
  • Loading branch information
blep committed May 2, 2011
1 parent e3cc0f0 commit 1837a1c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
3 changes: 3 additions & 0 deletions NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 9 additions & 28 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -594,7 +575,7 @@ Value::operator <( const Value &other ) const
default:
JSON_ASSERT_UNREACHABLE;
}
return 0; // unreachable
return false; // unreachable
}

bool
Expand Down Expand Up @@ -656,7 +637,7 @@ Value::operator ==( const Value &other ) const
default:
JSON_ASSERT_UNREACHABLE;
}
return 0; // unreachable
return false; // unreachable
}

bool
Expand Down
25 changes: 25 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 );
}


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


Expand All @@ -394,12 +417,14 @@ 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 );
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareString );
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 );
}

0 comments on commit 1837a1c

Please sign in to comment.