From 1bfa188d18785062179d0e760aca27e7af00b743 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 13:50:24 +0800 Subject: [PATCH] Improve encodings coverage --- test/unittest/encodingstest.cpp | 25 ++++++++++++++++ test/unittest/writertest.cpp | 52 ++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp index b3cbb76..4104880 100644 --- a/test/unittest/encodingstest.cpp +++ b/test/unittest/encodingstest.cpp @@ -423,3 +423,28 @@ TEST(EncodingsTest, UTF32) { } } } + +TEST(EncodingsTest, ASCII) { + StringBuffer os, os2; + for (unsigned codepoint = 0; codepoint < 128; codepoint++) { + os.Clear(); + ASCII<>::Encode(os, codepoint); + const ASCII<>::Ch* encodedStr = os.GetString(); + { + StringStream is(encodedStr); + unsigned decodedCodepoint; + bool result = ASCII<>::Decode(is, &decodedCodepoint); + if (!result || codepoint != decodedCodepoint) + std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; + } + + // Validate + { + StringStream is(encodedStr); + os2.Clear(); + bool result = ASCII<>::Validate(is, os2); + EXPECT_TRUE(result); + EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); + } + } +} diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 238aa79..4e08d7e 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -18,6 +18,7 @@ #include "rapidjson/reader.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" +#include "rapidjson/memorybuffer.h" using namespace rapidjson; @@ -107,35 +108,58 @@ TEST(Writer, Double) { } +// UTF8 -> TargetEncoding -> UTF8 +template +void TestTranscode(const char* json) { + StringStream s(json); + GenericStringBuffer buffer; + Writer, UTF8<>, TargetEncoding> writer(buffer); + Reader reader; + reader.Parse(s, writer); + + StringBuffer buffer2; + Writer writer2(buffer2); + GenericReader > reader2; + GenericStringStream s2(buffer.GetString()); + reader2.Parse(s2, writer2); + + EXPECT_STREQ(json, buffer2.GetString()); +} + TEST(Writer, Transcode) { const char json[] = "{\"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\"}"; // UTF8 -> UTF16 -> UTF8 - { - StringStream s(json); - StringBuffer buffer; - Writer, UTF8<> > writer(buffer); - GenericReader, UTF16<> > reader; - reader.Parse(s, writer); - EXPECT_STREQ(json, buffer.GetString()); - } + TestTranscode >(json); - // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8 + // UTF8 -> ASCII -> UTF8 + TestTranscode >(json); + + // UTF8 -> UTF16 -> UTF8 + TestTranscode >(json); + + // UTF8 -> UTF32 -> UTF8 + TestTranscode >(json); + + // UTF8 -> AutoUTF (UTF16BE) -> UTF8 { StringStream s(json); - StringBuffer buffer; - Writer, ASCII<> > writer(buffer); + MemoryBuffer buffer; + AutoUTFOutputStream os(buffer, kUTF16BE, true); + Writer, UTF8<>, AutoUTF > writer(os); Reader reader; reader.Parse(s, writer); StringBuffer buffer2; Writer writer2(buffer2); - GenericReader, UTF8<> > reader2; - StringStream s2(buffer.GetString()); - reader2.Parse(s2, writer2); + GenericReader, UTF8<> > reader2; + MemoryStream s2(buffer.GetBuffer(), buffer.GetSize()); + AutoUTFInputStream is(s2); + reader2.Parse(is, writer2); EXPECT_STREQ(json, buffer2.GetString()); } + } #include