Commit 0bff4ffd authored by Milo Yip's avatar Milo Yip

Adds unit tests about streams.

parent b01093b1
......@@ -101,6 +101,44 @@ TEST(Document, ParseStream_EncodedInputStream) {
}
}
TEST(Document, ParseStream_AutoUTFInputStream) {
// Any -> UTF8
FILE* fp = OpenEncodedFile("utf32BE.json");
char buffer[256];
FileReadStream bis(fp, buffer, sizeof(buffer));
AutoUTFInputStream<unsigned, FileReadStream> eis(bis);
Document d;
d.ParseStream<0, AutoUTF<unsigned> >(eis);
EXPECT_FALSE(d.HasParseError());
fclose(fp);
char expected[] = "I can eat glass and it doesn't hurt me.";
Value& v = d["en"];
EXPECT_TRUE(v.IsString());
EXPECT_EQ(sizeof(expected) - 1, v.GetStringLength());
EXPECT_EQ(0, StrCmp(expected, v.GetString()));
// UTF8 -> UTF8 in memory
StringBuffer bos;
Writer<StringBuffer> writer(bos);
d.Accept(writer);
{
// Condense the original file and compare.
FILE *fp = OpenEncodedFile("utf8.json");
FileReadStream is(fp, buffer, sizeof(buffer));
Reader reader;
StringBuffer bos2;
Writer<StringBuffer> writer(bos2);
reader.Parse(is, writer);
EXPECT_EQ(bos.GetSize(), bos2.GetSize());
EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize()));
}
}
TEST(Document, Swap) {
Document d1;
Document::AllocatorType& a = d1.GetAllocator();
......
......@@ -662,6 +662,50 @@ TEST(Reader, CustomStringStream) {
EXPECT_EQ(20u, h.step_);
}
#include <sstream>
class IStreamWrapper {
public:
typedef char Ch;
IStreamWrapper(std::istream& is) : is_(is) {}
Ch Peek() const {
int c = is_.peek();
return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
}
Ch Take() {
int c = is_.get();
return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
}
size_t Tell() const { return (size_t)is_.tellg(); }
Ch* PutBegin() { assert(false); return 0; }
void Put(Ch) { assert(false); }
void Flush() { assert(false); }
size_t PutEnd(Ch*) { assert(false); return 0; }
private:
IStreamWrapper(const IStreamWrapper&);
IStreamWrapper& operator=(const IStreamWrapper&);
std::istream& is_;
};
TEST(Reader, Parse_IStreamWrapper_StringStream) {
const char* json = "[1,2,3,4]";
std::stringstream ss(json);
IStreamWrapper is(ss);
Reader reader;
ParseArrayHandler<4> h;
reader.ParseArray<0>(is, h);
EXPECT_FALSE(reader.HasParseError());
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
......@@ -121,3 +121,42 @@ TEST(Writer, Transcode) {
reader.Parse<0>(s, writer);
EXPECT_STREQ("{\"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\"}", buffer.GetString());
}
#include <sstream>
class OStreamWrapper {
public:
typedef char Ch;
OStreamWrapper(std::ostream& os) : os_(os) {}
Ch Peek() const { assert(false); return '\0'; }
Ch Take() { assert(false); return '\0'; }
size_t Tell() const { }
Ch* PutBegin() { assert(false); return 0; }
void Put(Ch c) { os_.put(c); }
void Flush() { os_.flush(); }
size_t PutEnd(Ch*) { assert(false); return 0; }
private:
OStreamWrapper(const OStreamWrapper&);
OStreamWrapper& operator=(const OStreamWrapper&);
std::ostream& os_;
};
TEST(Writer, OStreamWrapper) {
StringStream s("{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ");
std::stringstream ss;
OStreamWrapper os(ss);
Writer<OStreamWrapper> writer(os);
Reader reader;
reader.Parse<0>(s, writer);
std::string actual = ss.str();
EXPECT_STREQ("{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3]}", actual.c_str());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment