Skip to content

Commit f0b24e7

Browse files
committed
Fixed MSVS 2003, 2005 and 2008 tests execution by normalizing floating-point string representation using helper normalizeFloatingPointStr().
1 parent e807a76 commit f0b24e7

2 files changed

Lines changed: 77 additions & 10 deletions

File tree

src/jsontestrunner/main.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
# pragma warning( disable: 4996 ) // disable fopen deprecation warning
1616
#endif
1717

18+
static std::string
19+
normalizeFloatingPointStr( double value )
20+
{
21+
char buffer[32];
22+
sprintf( buffer, "%.16g", value );
23+
buffer[sizeof(buffer)-1] = 0;
24+
std::string s( buffer );
25+
std::string::size_type index = s.find_last_of( "eE" );
26+
if ( index != std::string::npos )
27+
{
28+
std::string::size_type hasSign = (s[index+1] == '+' || s[index+1] == '-') ? 1 : 0;
29+
std::string::size_type exponentStartIndex = index + 1 + hasSign;
30+
std::string normalized = s.substr( 0, exponentStartIndex );
31+
std::string::size_type indexDigit = s.find_first_not_of( '0', exponentStartIndex );
32+
std::string exponent = "0";
33+
if ( indexDigit != std::string::npos ) // There is an exponent different from 0
34+
{
35+
exponent = s.substr( indexDigit );
36+
}
37+
return normalized + exponent;
38+
}
39+
return s;
40+
}
41+
42+
1843
static std::string
1944
readInputTestFile( const char *path )
2045
{
@@ -34,7 +59,6 @@ readInputTestFile( const char *path )
3459
return text;
3560
}
3661

37-
3862
static void
3963
printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." )
4064
{
@@ -50,7 +74,7 @@ printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." )
5074
fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestUInt() ).c_str() );
5175
break;
5276
case Json::realValue:
53-
fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble() );
77+
fprintf( fout, "%s=%s\n", path.c_str(), normalizeFloatingPointStr(value.asDouble()).c_str() );
5478
break;
5579
case Json::stringValue:
5680
fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() );

src/test_lib_json/main.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,51 @@ struct ValueTest : JsonTest::TestCase
9797
void checkIsLess( const Json::Value &x, const Json::Value &y );
9898

9999
void checkIsEqual( const Json::Value &x, const Json::Value &y );
100+
101+
/// Normalize the representation of floating-point number by stripped leading 0 in exponent.
102+
static std::string normalizeFloatingPointStr( const std::string &s );
100103
};
101104

102105

106+
std::string
107+
ValueTest::normalizeFloatingPointStr( const std::string &s )
108+
{
109+
std::string::size_type index = s.find_last_of( "eE" );
110+
if ( index != std::string::npos )
111+
{
112+
std::string::size_type hasSign = (s[index+1] == '+' || s[index+1] == '-') ? 1 : 0;
113+
std::string::size_type exponentStartIndex = index + 1 + hasSign;
114+
std::string normalized = s.substr( 0, exponentStartIndex );
115+
std::string::size_type indexDigit = s.find_first_not_of( '0', exponentStartIndex );
116+
std::string exponent = "0";
117+
if ( indexDigit != std::string::npos ) // There is an exponent different from 0
118+
{
119+
exponent = s.substr( indexDigit );
120+
}
121+
return normalized + exponent;
122+
}
123+
return s;
124+
}
125+
126+
127+
JSONTEST_FIXTURE( ValueTest, checkNormalizeFloatingPointStr )
128+
{
129+
JSONTEST_ASSERT_STRING_EQUAL( "0.0", normalizeFloatingPointStr("0.0") );
130+
JSONTEST_ASSERT_STRING_EQUAL( "0e0", normalizeFloatingPointStr("0e0") );
131+
JSONTEST_ASSERT_STRING_EQUAL( "1234.0", normalizeFloatingPointStr("1234.0") );
132+
JSONTEST_ASSERT_STRING_EQUAL( "1234.0e0", normalizeFloatingPointStr("1234.0e0") );
133+
JSONTEST_ASSERT_STRING_EQUAL( "1234.0e+0", normalizeFloatingPointStr("1234.0e+0") );
134+
JSONTEST_ASSERT_STRING_EQUAL( "1234e-1", normalizeFloatingPointStr("1234e-1") );
135+
JSONTEST_ASSERT_STRING_EQUAL( "1234e10", normalizeFloatingPointStr("1234e10") );
136+
JSONTEST_ASSERT_STRING_EQUAL( "1234e10", normalizeFloatingPointStr("1234e010") );
137+
JSONTEST_ASSERT_STRING_EQUAL( "1234e+10", normalizeFloatingPointStr("1234e+010") );
138+
JSONTEST_ASSERT_STRING_EQUAL( "1234e-10", normalizeFloatingPointStr("1234e-010") );
139+
JSONTEST_ASSERT_STRING_EQUAL( "1234e+100", normalizeFloatingPointStr("1234e+100") );
140+
JSONTEST_ASSERT_STRING_EQUAL( "1234e-100", normalizeFloatingPointStr("1234e-100") );
141+
JSONTEST_ASSERT_STRING_EQUAL( "1234e+1", normalizeFloatingPointStr("1234e+001") );
142+
}
143+
144+
103145
JSONTEST_FIXTURE( ValueTest, memberCount )
104146
{
105147
JSONTEST_ASSERT_PRED( checkMemberCount(emptyArray_, 0) );
@@ -579,7 +621,7 @@ JSONTEST_FIXTURE( ValueTest, integers )
579621
JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble());
580622
JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat());
581623
JSONTEST_ASSERT_EQUAL(true, val.asBool());
582-
JSONTEST_ASSERT_STRING_EQUAL("1.04858e+06", val.asString());
624+
JSONTEST_ASSERT_STRING_EQUAL("1.04858e+6", normalizeFloatingPointStr(val.asString()));
583625

584626
// -2^20
585627
val = Json::Value(-(1 << 20));
@@ -819,7 +861,7 @@ JSONTEST_FIXTURE( ValueTest, integers )
819861
JSONTEST_ASSERT_EQUAL((1LL << 40), val.asDouble());
820862
JSONTEST_ASSERT_EQUAL((1LL << 40), val.asFloat());
821863
JSONTEST_ASSERT_EQUAL(true, val.asBool());
822-
JSONTEST_ASSERT_STRING_EQUAL("1.09951e+12", val.asString());
864+
JSONTEST_ASSERT_STRING_EQUAL("1.09951e+12", normalizeFloatingPointStr(val.asString()));
823865

824866
// -2^40
825867
val = Json::Value(-(1LL << 40));
@@ -892,7 +934,7 @@ JSONTEST_FIXTURE( ValueTest, integers )
892934
JSONTEST_ASSERT_EQUAL(9223372036854775808ULL, val.asDouble());
893935
JSONTEST_ASSERT_EQUAL(9223372036854775808ULL, val.asFloat());
894936
JSONTEST_ASSERT_EQUAL(true, val.asBool());
895-
JSONTEST_ASSERT_STRING_EQUAL("9.22337e+18", val.asString());
937+
JSONTEST_ASSERT_STRING_EQUAL("9.22337e+18", normalizeFloatingPointStr(val.asString()));
896938

897939
// int64 min
898940
val = Json::Value(Json::Int64(kint64min));
@@ -939,7 +981,7 @@ JSONTEST_FIXTURE( ValueTest, integers )
939981
JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asDouble());
940982
JSONTEST_ASSERT_EQUAL(-9223372036854775808.0, val.asFloat());
941983
JSONTEST_ASSERT_EQUAL(true, val.asBool());
942-
JSONTEST_ASSERT_STRING_EQUAL("-9.22337e+18", val.asString());
984+
JSONTEST_ASSERT_STRING_EQUAL("-9.22337e+18", normalizeFloatingPointStr(val.asString()));
943985

944986
// uint64 max
945987
val = Json::Value(Json::UInt64(kuint64max));
@@ -982,7 +1024,7 @@ JSONTEST_FIXTURE( ValueTest, integers )
9821024
JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asDouble());
9831025
JSONTEST_ASSERT_EQUAL(18446744073709551616.0, val.asFloat());
9841026
JSONTEST_ASSERT_EQUAL(true, val.asBool());
985-
JSONTEST_ASSERT_STRING_EQUAL("1.84467e+19", val.asString());
1027+
JSONTEST_ASSERT_STRING_EQUAL("1.84467e+19", normalizeFloatingPointStr(val.asString()));
9861028
#endif
9871029
}
9881030

@@ -1073,7 +1115,7 @@ JSONTEST_FIXTURE( ValueTest, nonIntegers )
10731115
JSONTEST_ASSERT_EQUAL(2147483647U, val.asLargestUInt());
10741116
#endif
10751117
JSONTEST_ASSERT_EQUAL(true, val.asBool());
1076-
JSONTEST_ASSERT_EQUAL("2.14748e+09", val.asString());
1118+
JSONTEST_ASSERT_EQUAL("2.14748e+9", normalizeFloatingPointStr(val.asString()));
10771119

10781120
// A bit under int32 min
10791121
val = Json::Value(kint32min - 0.5);
@@ -1100,7 +1142,7 @@ JSONTEST_FIXTURE( ValueTest, nonIntegers )
11001142
JSONTEST_ASSERT_EQUAL(-2147483648LL, val.asLargestInt());
11011143
#endif
11021144
JSONTEST_ASSERT_EQUAL(true, val.asBool());
1103-
JSONTEST_ASSERT_EQUAL("-2.14748e+09", val.asString());
1145+
JSONTEST_ASSERT_EQUAL("-2.14748e+9", normalizeFloatingPointStr(val.asString()));
11041146

11051147
// A bit over uint32 max
11061148
val = Json::Value(kuint32max + 0.5);
@@ -1128,7 +1170,7 @@ JSONTEST_FIXTURE( ValueTest, nonIntegers )
11281170
JSONTEST_ASSERT_EQUAL(4294967295ULL, val.asLargestUInt());
11291171
#endif
11301172
JSONTEST_ASSERT_EQUAL(true, val.asBool());
1131-
JSONTEST_ASSERT_EQUAL("4.29497e+09", val.asString());
1173+
JSONTEST_ASSERT_EQUAL("4.29497e+9", normalizeFloatingPointStr(val.asString()));
11321174
}
11331175

11341176

@@ -1351,6 +1393,7 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
13511393
int main( int argc, const char *argv[] )
13521394
{
13531395
JsonTest::Runner runner;
1396+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, checkNormalizeFloatingPointStr );
13541397
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, memberCount );
13551398
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, objects );
13561399
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, arrays );

0 commit comments

Comments
 (0)