Skip to content

Commit 1837a1c

Browse files
committed
Value::compare() is now const and has an actual implementation with unit tests.
1 parent e3cc0f0 commit 1837a1c

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

NEWS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
- Fixed Value::operator <= implementation (had the semantic of operator >=).
9191
Found when addigin unit tests for comparison operators.
9292

93+
- Value::compare() is now const and has an actual implementation with
94+
unit tests.
95+
9396
* License
9497

9598
- See file LICENSE for details. Basically JsonCpp is now licensed under

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ namespace Json {
256256
bool operator ==( const Value &other ) const;
257257
bool operator !=( const Value &other ) const;
258258

259-
int compare( const Value &other );
259+
int compare( const Value &other ) const;
260260

261261
const char *asCString() const;
262262
std::string asString() const;

src/lib_json/json_value.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -524,35 +524,16 @@ Value::type() const
524524

525525

526526
int
527-
Value::compare( const Value &other )
527+
Value::compare( const Value &other ) const
528528
{
529-
/*
530-
int typeDelta = other.type_ - type_;
531-
switch ( type_ )
532-
{
533-
case nullValue:
534-
535-
return other.type_ == type_;
536-
case intValue:
537-
if ( other.type_.isNumeric()
538-
case uintValue:
539-
case realValue:
540-
case booleanValue:
541-
break;
542-
case stringValue,
543-
break;
544-
case arrayValue:
545-
delete value_.array_;
546-
break;
547-
case objectValue:
548-
delete value_.map_;
549-
default:
550-
JSON_ASSERT_UNREACHABLE;
551-
}
552-
*/
553-
return 0; // unreachable
529+
if ( *this < other )
530+
return -1;
531+
if ( *this > other )
532+
return 1;
533+
return 0;
554534
}
555535

536+
556537
bool
557538
Value::operator <( const Value &other ) const
558539
{
@@ -594,7 +575,7 @@ Value::operator <( const Value &other ) const
594575
default:
595576
JSON_ASSERT_UNREACHABLE;
596577
}
597-
return 0; // unreachable
578+
return false; // unreachable
598579
}
599580

600581
bool
@@ -656,7 +637,7 @@ Value::operator ==( const Value &other ) const
656637
default:
657638
JSON_ASSERT_UNREACHABLE;
658639
}
659-
return 0; // unreachable
640+
return false; // unreachable
660641
}
661642

662643
bool

src/test_lib_json/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ ValueTest::checkIs( const Json::Value &value, const IsCheck &check )
256256
}
257257

258258

259+
JSONTEST_FIXTURE( ValueTest, compareNull )
260+
{
261+
JSONTEST_ASSERT_PRED( checkIsEqual( Json::Value(), Json::Value() ) );
262+
}
263+
264+
259265
JSONTEST_FIXTURE( ValueTest, compareInt )
260266
{
261267
JSONTEST_ASSERT_PRED( checkIsLess( 0, 10 ) );
@@ -347,6 +353,19 @@ JSONTEST_FIXTURE( ValueTest, compareObject )
347353
}
348354

349355

356+
JSONTEST_FIXTURE( ValueTest, compareType )
357+
{
358+
// object of different type are ordered according to their type
359+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(), Json::Value(1) ) );
360+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1), Json::Value(1u) ) );
361+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1u), Json::Value(1.0) ) );
362+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(1.0), Json::Value("a") ) );
363+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value("a"), Json::Value(true) ) );
364+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(true), Json::Value(Json::arrayValue) ) );
365+
JSONTEST_ASSERT_PRED( checkIsLess( Json::Value(Json::arrayValue), Json::Value(Json::objectValue) ) );
366+
}
367+
368+
350369
void
351370
ValueTest::checkIsLess( const Json::Value &x, const Json::Value &y )
352371
{
@@ -360,6 +379,8 @@ ValueTest::checkIsLess( const Json::Value &x, const Json::Value &y )
360379
JSONTEST_ASSERT( !(y <= x) );
361380
JSONTEST_ASSERT( !(x > y) );
362381
JSONTEST_ASSERT( !(y < x) );
382+
JSONTEST_ASSERT( x.compare( y ) < 0 );
383+
JSONTEST_ASSERT( y.compare( x ) >= 0 );
363384
}
364385

365386

@@ -376,6 +397,8 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
376397
JSONTEST_ASSERT( !(y < x) );
377398
JSONTEST_ASSERT( !(x > y) );
378399
JSONTEST_ASSERT( !(y > x) );
400+
JSONTEST_ASSERT( x.compare( y ) == 0 );
401+
JSONTEST_ASSERT( y.compare( x ) == 0 );
379402
}
380403

381404

@@ -394,12 +417,14 @@ int main( int argc, const char *argv[] )
394417
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
395418
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray );
396419
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat );
420+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareNull );
397421
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareInt );
398422
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareUInt );
399423
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareDouble );
400424
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareString );
401425
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareBoolean );
402426
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray );
403427
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject );
428+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType );
404429
return runner.runCommandLine( argc, argv );
405430
}

0 commit comments

Comments
 (0)