Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-defined double output precision #3

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
DoublePrecision: add a unit test for the precision handling
  • Loading branch information
pah committed Apr 1, 2014
commit 2a41653474551de34bb93088bd2a97fab00f2abf
56 changes: 56 additions & 0 deletions test/unittest/writertest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "unittest.h"
#include "rapidjson/document.h"
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
@@ -56,6 +57,61 @@ TEST(Writer, String) {
TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]");
}

TEST(Writer,DoublePrecision) {
const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]";

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);

const int kDefaultDoublePrecision = 6;
// handling the double precision
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
writer.SetDoublePrecision(17);
EXPECT_EQ(writer.GetDoublePrecision(), 17);
writer.SetDoublePrecision(-1); // negative equivalent to reset
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
writer.SetDoublePrecision(1);
writer.SetDoublePrecision(); // reset again
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);

{ // write with explicitly increased precision
StringStream s(json);
Reader reader;
reader.Parse<0>(s, writer.SetDoublePrecision(12));
EXPECT_EQ(writer.GetDoublePrecision(), 12);
EXPECT_STREQ(json, buffer.GetString());
buffer.Clear();
}
{ // explicit individual double precisions
writer.SetDoublePrecision(2)
.StartArray()
.Double(1.2345,5)
.Double(1.2345678,9)
.Double(0.123456789012,12)
.Double(1234567.8,8)
.EndArray();

EXPECT_EQ(writer.GetDoublePrecision(), 2);
EXPECT_STREQ(json, buffer.GetString());
buffer.Clear();
}
{ // write with default precision (output with precision loss)
Document d;
d.Parse<0>(json);
d.Accept(writer.SetDoublePrecision());

// parsed again to avoid platform-dependent floating point outputs
// (e.g. width of exponents)
d.Parse<0>(buffer.GetString());
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
EXPECT_DOUBLE_EQ(d[0u].GetDouble(), 1.2345);
EXPECT_DOUBLE_EQ(d[1u].GetDouble(), 1.23457);
EXPECT_DOUBLE_EQ(d[2u].GetDouble(), 0.123457);
EXPECT_DOUBLE_EQ(d[3u].GetDouble(), 1234570);
buffer.Clear();
}
}

TEST(Writer, Transcode) {
// UTF8 -> UTF16 -> UTF8
StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3], \"dollar\":\"\x24\", \"cents\":\"\xC2\xA2\", \"euro\":\"\xE2\x82\xAC\", \"gclef\":\"\xF0\x9D\x84\x9E\" } ");