Commit 3b1a995d authored by miloyip@gmail.com's avatar miloyip@gmail.com

Added EncodedOutputStream and AutoUTFOutputStream with unit tests

git-svn-id: https://rapidjson.googlecode.com/svn/trunk@42 c5894555-1306-4e8d-425f-1f6f381ee07c
parent 77255c6a
...@@ -12,12 +12,11 @@ public: ...@@ -12,12 +12,11 @@ public:
typedef typename Encoding::Ch Ch; typedef typename Encoding::Ch Ch;
EncodedInputStream(InputStream& is) : is_(is) { EncodedInputStream(InputStream& is) : is_(is) {
Encoding::TakeBOM(is_); current_ = Encoding::TakeBOM(is_);
Read();
} }
Ch Peek() const { return current_; } Ch Peek() const { return current_; }
Ch Take() { Ch c = current_; Read(); return c; } Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; }
size_t Tell() const { is_.Tell(); } size_t Tell() const { is_.Tell(); }
// Not implemented // Not implemented
...@@ -27,13 +26,37 @@ public: ...@@ -27,13 +26,37 @@ public:
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private: private:
void Read() {
current_ = Encoding::Take(is_);
}
InputStream& is_; InputStream& is_;
Ch current_; Ch current_;
}; };
//! Adapts an output byte stream with an specified encoding.
template <typename Encoding, typename OutputStream>
class EncodedOutputStream {
public:
typedef typename Encoding::Ch Ch;
EncodedOutputStream(OutputStream& os, bool putBOM = true) : os_(os) {
if (putBOM)
Encoding::PutBOM(os_);
}
void Put(Ch c) { Encoding::Put(os_, c); }
void Flush() { os_.Flush(); }
// Not implemented
Ch Peek() const { RAPIDJSON_ASSERT(false); }
Ch Take() { RAPIDJSON_ASSERT(false); }
size_t Tell() const { RAPIDJSON_ASSERT(false); }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private:
OutputStream& os_;
};
#define ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
template <typename CharType, typename InputStream> template <typename CharType, typename InputStream>
class AutoUTFInputStream { class AutoUTFInputStream {
public: public:
...@@ -58,33 +81,34 @@ private: ...@@ -58,33 +81,34 @@ private:
friend struct AutoUTF<Ch>; friend struct AutoUTF<Ch>;
void TakeBOM(InputStream& is) { void TakeBOM(InputStream& is) {
#define TAKE() is.Take() #define ASSUME(x) if ((unsigned char)is.Peek() != x) break; is.Take()
#define PEEK(x) if ((unsigned char)is.Peek() != x) break
switch ((unsigned char)is.Peek()) { switch ((unsigned char)is.Peek()) {
case 0x00: TAKE(); PEEK(0x00); TAKE(); PEEK(0xFE); TAKE(); PEEK(0xFF); type_ = kUTF32BE; return; case 0x00: is.Take(); ASSUME(0x00); ASSUME(0xFE); ASSUME(0xFF); type_ = kUTF32BE; break;
case 0xEF: TAKE(); PEEK(0xBB); TAKE(); PEEK(0xBF); TAKE(); type_ = kUTF8; return; case 0xEF: is.Take(); ASSUME(0xBB); ASSUME(0xBF); type_ = kUTF8; break;
case 0xFE: TAKE(); PEEK(0xFF); TAKE(); type_ = kUTF16BE; return; case 0xFE: is.Take(); ASSUME(0xFF); type_ = kUTF16BE; break;
case 0xFF: TAKE(); PEEK(0xFE); TAKE(); case 0xFF: is.Take(); ASSUME(0xFE);
if (is.Peek() == 0x00) { if (is.Peek() == 0x00) {
TAKE(); PEEK(0x00); TAKE(); type_ = kUTF32LE; return; is.Take(); ASSUME(0x00); type_ = kUTF32LE; break;
} }
type_ = kUTF16LE; type_ = kUTF16LE;
return;
} }
#undef TAKE #undef ASSUME
#undef PEEK // RUntime check whether the size of character type is sufficient. It only perform checks with assertion.
switch (type_) {
case kUTF16LE:
case kUTF16BE:
RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
break;
case kUTF32LE:
case kUTF32BE:
RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
break;
}
} }
void Read() { void Read() {
typedef Ch (*TakeFunc)(InputStream& is); typedef Ch (*TakeFunc)(InputStream& is);
static const TakeFunc f[] = { static const TakeFunc f[] = { ENCODINGS_FUNC(Take) };
UTF8<Ch>::Take,
UTF16LE<Ch>::Take,
UTF16BE<Ch>::Take,
UTF32LE<Ch>::Take,
UTF32BE<Ch>::Take,
};
current_ = f[type_](is_); current_ = f[type_](is_);
} }
...@@ -93,6 +117,59 @@ private: ...@@ -93,6 +117,59 @@ private:
Ch current_; Ch current_;
}; };
template <typename CharType, typename OutputStream>
class AutoUTFOutputStream {
public:
typedef CharType Ch;
AutoUTFOutputStream(OutputStream& os, UTFType type, bool putBOM) : os_(os), type_(type) {
// RUntime check whether the size of character type is sufficient. It only perform checks with assertion.
switch (type_) {
case kUTF16LE:
case kUTF16BE:
RAPIDJSON_ASSERT(sizeof(Ch) >= 2);
break;
case kUTF32LE:
case kUTF32BE:
RAPIDJSON_ASSERT(sizeof(Ch) >= 4);
break;
}
if (putBOM)
PutBOM();
}
void Put(Ch c) {
typedef void (*PutFunc)(OutputStream&, Ch);
static const PutFunc f[] = { ENCODINGS_FUNC(Put) };
f[type_](os_, c);
}
void Flush() { os_.Flush(); }
// Not implemented
Ch Peek() const { RAPIDJSON_ASSERT(false); }
Ch Take() { RAPIDJSON_ASSERT(false); }
size_t Tell() const { RAPIDJSON_ASSERT(false); }
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private:
friend struct AutoUTF<Ch>;
void PutBOM() {
typedef void (*PutBOMFunc)(OutputStream&);
static const PutBOMFunc f[] = { ENCODINGS_FUNC(PutBOM) };
f[type_](os_);
}
OutputStream& os_;
UTFType type_;
};
#undef ENCODINGS_FUNC
} // namespace rapidjson } // namespace rapidjson
#endif // RAPIDJSON_FILESTREAM_H_ #endif // RAPIDJSON_FILESTREAM_H_
...@@ -141,19 +141,31 @@ struct UTF8 { ...@@ -141,19 +141,31 @@ struct UTF8 {
} }
template <typename InputStream> template <typename InputStream>
static void TakeBOM(InputStream& is) { static CharType TakeBOM(InputStream& is) {
if ((unsigned char)is.Peek() != 0xEF) return; Ch c = Take(is);
is.Take(); if ((unsigned char)c != 0xEFu) return c;
if ((unsigned char)is.Peek() != 0xBB) return; c = is.Take();
is.Take(); if ((unsigned char)c != 0xBBu) return c;
if ((unsigned char)is.Peek() != 0xBF) return; c = is.Take();
is.Take(); if ((unsigned char)c != 0xBFu) return c;
c = is.Take();
return c;
} }
template <typename InputStream> template <typename InputStream>
RAPIDJSON_FORCEINLINE static Ch Take(InputStream& is) { RAPIDJSON_FORCEINLINE static Ch Take(InputStream& is) {
return is.Take(); return is.Take();
} }
template <typename OutputStream>
static void PutBOM(OutputStream& os) {
os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu);
}
template <typename OutputStream>
static void Put(OutputStream& os, Ch c) {
os.Put(c);
}
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -217,11 +229,9 @@ struct UTF16 { ...@@ -217,11 +229,9 @@ struct UTF16 {
template<typename CharType = wchar_t> template<typename CharType = wchar_t>
struct UTF16LE : UTF16<CharType> { struct UTF16LE : UTF16<CharType> {
template <typename InputStream> template <typename InputStream>
static void TakeBOM(InputStream& is) { static CharType TakeBOM(InputStream& is) {
if ((unsigned char)is.Peek() != 0xFF) return; CharType c = Take(is);
is.Take(); return (unsigned short)c == 0xFEFFu ? Take(is) : c;
if ((unsigned char)is.Peek() != 0xFE) return;
is.Take();
} }
template <typename InputStream> template <typename InputStream>
...@@ -230,16 +240,25 @@ struct UTF16LE : UTF16<CharType> { ...@@ -230,16 +240,25 @@ struct UTF16LE : UTF16<CharType> {
c |= (unsigned char)is.Take() << 8; c |= (unsigned char)is.Take() << 8;
return c; return c;
} }
template <typename OutputStream>
static void PutBOM(OutputStream& os) {
os.Put(0xFFu); os.Put(0xFEu);
}
template <typename OutputStream>
static void Put(OutputStream& os, Ch c) {
os.Put(c & 0xFFu);
os.Put((c >> 8) & 0xFFu);
}
}; };
template<typename CharType = wchar_t> template<typename CharType = wchar_t>
struct UTF16BE : UTF16<CharType> { struct UTF16BE : UTF16<CharType> {
template <typename InputStream> template <typename InputStream>
static void TakeBOM(InputStream& is) { static CharType TakeBOM(InputStream& is) {
if ((unsigned char)is.Peek() != 0xFE) return; CharType c = Take(is);
is.Take(); return (unsigned short)c == 0xFEFFu ? Take(is) : c;
if ((unsigned char)is.Peek() != 0xFF) return;
is.Take();
} }
template <typename InputStream> template <typename InputStream>
...@@ -248,6 +267,17 @@ struct UTF16BE : UTF16<CharType> { ...@@ -248,6 +267,17 @@ struct UTF16BE : UTF16<CharType> {
c |= (unsigned char)is.Take(); c |= (unsigned char)is.Take();
return c; return c;
} }
template <typename OutputStream>
static void PutBOM(OutputStream& os) {
os.Put(0xFEu); os.Put(0xFFu);
}
template <typename OutputStream>
static void Put(OutputStream& os, Ch c) {
os.Put((c >> 8) & 0xFFu);
os.Put(c & 0xFFu);
}
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -286,15 +316,9 @@ struct UTF32 { ...@@ -286,15 +316,9 @@ struct UTF32 {
template<typename CharType = unsigned> template<typename CharType = unsigned>
struct UTF32LE : UTF32<CharType> { struct UTF32LE : UTF32<CharType> {
template <typename InputStream> template <typename InputStream>
static void TakeBOM(InputStream& is) { static CharType TakeBOM(InputStream& is) {
if ((unsigned char)is.Peek() != 0xFF) return; CharType c = Take(is);
is.Take(); return (unsigned)c == 0x0000FEFFu ? Take(is) : c;
if ((unsigned char)is.Peek() != 0xFE) return;
is.Take();
if ((unsigned char)is.Peek() != 0x00) return;
is.Take();
if ((unsigned char)is.Peek() != 0x00) return;
is.Take();
} }
template <typename InputStream> template <typename InputStream>
...@@ -305,20 +329,27 @@ struct UTF32LE : UTF32<CharType> { ...@@ -305,20 +329,27 @@ struct UTF32LE : UTF32<CharType> {
c |= (unsigned char)is.Take() << 24; c |= (unsigned char)is.Take() << 24;
return c; return c;
} }
template <typename OutputStream>
static void PutBOM(OutputStream& os) {
os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u);
}
template <typename OutputStream>
static void Put(OutputStream& os, Ch c) {
os.Put(c & 0xFFu);
os.Put((c >> 8) & 0xFFu);
os.Put((c >> 16) & 0xFFu);
os.Put((c >> 24) & 0xFFu);
}
}; };
template<typename CharType = unsigned> template<typename CharType = unsigned>
struct UTF32BE : UTF32<CharType> { struct UTF32BE : UTF32<CharType> {
template <typename InputStream> template <typename InputStream>
static void TakeBOM(InputStream& is) { static CharType TakeBOM(InputStream& is) {
if ((unsigned char)is.Peek() != 0x00) return; CharType c = Take(is);
is.Take(); return (unsigned)c == 0x0000FEFFu ? Take(is) : c;
if ((unsigned char)is.Peek() != 0x00) return;
is.Take();
if ((unsigned char)is.Peek() != 0xFE) return;
is.Take();
if ((unsigned char)is.Peek() != 0xFF) return;
is.Take();
} }
template <typename InputStream> template <typename InputStream>
...@@ -329,6 +360,19 @@ struct UTF32BE : UTF32<CharType> { ...@@ -329,6 +360,19 @@ struct UTF32BE : UTF32<CharType> {
c |= (unsigned char)is.Take(); c |= (unsigned char)is.Take();
return c; return c;
} }
template <typename OutputStream>
static void PutBOM(OutputStream& os) {
os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu);
}
template <typename OutputStream>
static void Put(OutputStream& os, Ch c) {
os.Put((c >> 24) & 0xFFu);
os.Put((c >> 16) & 0xFFu);
os.Put((c >> 8) & 0xFFu);
os.Put(c & 0xFFu);
}
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -347,44 +391,30 @@ template<typename CharType> ...@@ -347,44 +391,30 @@ template<typename CharType>
struct AutoUTF { struct AutoUTF {
typedef CharType Ch; typedef CharType Ch;
#define ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
template<typename OutputStream> template<typename OutputStream>
RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) {
typedef void (*EncodeFunc)(OutputStream&, unsigned); typedef void (*EncodeFunc)(OutputStream&, unsigned);
static const EncodeFunc f[] = { static const EncodeFunc f[] = { ENCODINGS_FUNC(Encode) };
UTF8<Ch>::Encode,
UTF16<Ch>::Encode,
UTF16<Ch>::Encode,
UTF32<Ch>::Encode,
UTF32<Ch>::Encode,
};
(*f[os.type_])(os, codepoint); (*f[os.type_])(os, codepoint);
} }
template <typename InputStream> template <typename InputStream>
RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) {
typedef bool (*DecodeFunc)(InputStream&, unsigned*); typedef bool (*DecodeFunc)(InputStream&, unsigned*);
static const DecodeFunc f[] = { static const DecodeFunc f[] = { ENCODINGS_FUNC(Decode) };
UTF8<Ch>::Decode,
UTF16<Ch>::Decode,
UTF16<Ch>::Decode,
UTF32<Ch>::Decode,
UTF32<Ch>::Decode,
};
return (*f[is.type_])(is, codepoint); return (*f[is.type_])(is, codepoint);
} }
template <typename InputStream, typename OutputStream> template <typename InputStream, typename OutputStream>
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) {
typedef bool (*ValidateFunc)(InputStream&, unsigned*); typedef bool (*ValidateFunc)(InputStream&, unsigned*);
static const ValidateFunc f[] = { static const ValidateFunc f[] = { ENCODINGS_FUNC(Validate) };
UTF8<Ch>::Decode,
UTF16<Ch>::Decode,
UTF16<Ch>::Decode,
UTF32<Ch>::Decode,
UTF32<Ch>::Decode,
};
return (*f[is.type_])(is, os); return (*f[is.type_])(is, os);
} }
#undef ENCODINGS_FUNC
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
...@@ -7,7 +7,16 @@ using namespace rapidjson; ...@@ -7,7 +7,16 @@ using namespace rapidjson;
class EncodingsTest : public ::testing::Test { class EncodingsTest : public ::testing::Test {
public: public:
FILE* Open(const char* filename) { virtual void SetUp() {
json_ = ReadFile("utf8.json", true, &length_);
}
virtual void TearDown() {
free(json_);
}
protected:
static FILE* Open(const char* filename) {
char buffer[1024]; char buffer[1024];
sprintf(buffer, "encodings/%s", filename); sprintf(buffer, "encodings/%s", filename);
FILE *fp = fopen(buffer, "rb"); FILE *fp = fopen(buffer, "rb");
...@@ -18,105 +27,158 @@ public: ...@@ -18,105 +27,158 @@ public:
return fp; return fp;
} }
virtual void SetUp() { static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) {
FILE *fp = Open("utf8.json"); FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb");
ASSERT_TRUE(fp != 0);
if (!fp) {
*outLength = 0;
return 0;
}
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
length_ = (size_t)ftell(fp); *outLength = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
json_ = (char*)malloc(length_ + 1); char* buffer = (char*)malloc(*outLength + 1);
fread(json_, 1, length_, fp); fread(buffer, 1, *outLength, fp);
json_[length_] = '\0'; buffer[*outLength] = '\0';
fclose(fp); fclose(fp);
return buffer;
} }
virtual void TearDown() { template <typename FileEncoding, typename MemoryEncoding>
free(json_); void TestEncodedInputStream(const char* filename) {
char buffer[16];
FILE *fp = Open(filename);
ASSERT_TRUE(fp != 0);
FileReadStream fs(fp, buffer, sizeof(buffer));
EncodedInputStream<FileEncoding, FileReadStream> eis(fs);
StringStream s(json_);
while (eis.Peek() != '\0') {
unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
fclose(fp);
}
void TestAutoUTFInputStream(const char *filename) {
char buffer[16];
FILE *fp = Open(filename);
ASSERT_TRUE(fp != 0);
FileReadStream fs(fp, buffer, sizeof(buffer));
AutoUTFInputStream<unsigned, FileReadStream> eis(fs);
StringStream s(json_);
while (eis.Peek() != '\0') {
unsigned expected, actual;
EXPECT_TRUE(UTF8<>::Decode(s, &expected));
EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
fclose(fp);
}
template <typename FileEncoding, typename MemoryEncoding>
void TestEncodedOutputStream(const char* expectedFilename, bool putBOM) {
char filename[L_tmpnam];
tmpnam(filename);
FILE *fp = fopen(filename, "wb");
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM);
StringStream s(json_);
while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
EXPECT_TRUE(success);
}
eos.Flush();
fclose(fp);
EXPECT_TRUE(CompareFile(filename, expectedFilename));
remove(filename);
}
bool CompareFile(char * filename, const char* expectedFilename) {
size_t actualLength, expectedLength;
char* actualBuffer = ReadFile(filename, false, &actualLength);
char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
free(actualBuffer);
free(expectedBuffer);
return ret;
}
void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) {
char filename[L_tmpnam];
tmpnam(filename);
FILE *fp = fopen(filename, "wb");
char buffer[16];
FileWriteStream os(fp, buffer, sizeof(buffer));
AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
StringStream s(json_);
while (s.Peek() != '\0') {
bool success = Transcoder<UTF8<>, AutoUTF<unsigned>>::Transcode(s, eos);
EXPECT_TRUE(success);
}
eos.Flush();
fclose(fp);
EXPECT_TRUE(CompareFile(filename, expectedFilename));
remove(filename);
} }
protected:
const char* filename_; const char* filename_;
char *json_; char *json_;
size_t length_; size_t length_;
}; };
TEST_F(EncodingsTest, EncodedInputStream_UTF8BOM) { TEST_F(EncodingsTest, EncodedInputStream) {
char buffer[16]; TestEncodedInputStream<UTF8<>, UTF8<>>("utf8.json");
FILE *fp = Open("utf8bom.json"); TestEncodedInputStream<UTF8<>, UTF8<>>("utf8bom.json");
ASSERT_TRUE(fp != 0); TestEncodedInputStream<UTF16LE<>, UTF16<>>("utf16le.json");
FileReadStream fs(fp, buffer, sizeof(buffer)); TestEncodedInputStream<UTF16LE<>, UTF16<>>("utf16lebom.json");
EncodedInputStream<UTF8<>, FileReadStream> eis(fs); TestEncodedInputStream<UTF16BE<>, UTF16<>>("utf16be.json");
StringStream s(json_); TestEncodedInputStream<UTF16BE<>, UTF16<>>("utf16bebom.json");
TestEncodedInputStream<UTF32LE<>, UTF32<>>("utf32le.json");
while (eis.Peek() != '\0') { TestEncodedInputStream<UTF32LE<>, UTF32<>>("utf32lebom.json");
unsigned expected, actual; TestEncodedInputStream<UTF32BE<>, UTF32<>>("utf32be.json");
UTF8<>::Decode(s, &expected); TestEncodedInputStream<UTF32BE<>, UTF32<>>("utf32bebom.json");
UTF8<>::Decode(eis, &actual);
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
fclose(fp);
} }
TEST_F(EncodingsTest, EncodedInputStream_UTF16LEBOM) { TEST_F(EncodingsTest, AutoUTFInputStream) {
char buffer[16]; TestAutoUTFInputStream("utf8.json");
FILE *fp = Open("utf16lebom.json"); TestAutoUTFInputStream("utf8bom.json");
ASSERT_TRUE(fp != 0); TestAutoUTFInputStream("utf16lebom.json");
FileReadStream fs(fp, buffer, sizeof(buffer)); TestAutoUTFInputStream("utf16bebom.json");
EncodedInputStream<UTF16LE<>, FileReadStream> eis(fs); TestAutoUTFInputStream("utf32lebom.json");
StringStream s(json_); TestAutoUTFInputStream("utf32bebom.json");
while (eis.Peek() != '\0') {
unsigned expected, actual;
UTF8<>::Decode(s, &expected);
UTF16<>::Decode(eis, &actual);
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
fclose(fp);
} }
TEST_F(EncodingsTest, EncodedInputStream_UTF16BEBOM) { TEST_F(EncodingsTest, EncodedOutputStream) {
char buffer[16]; TestEncodedOutputStream<UTF8<>, UTF8<>>("utf8.json", false);
FILE *fp = Open("utf16bebom.json"); TestEncodedOutputStream<UTF8<>, UTF8<>>("utf8bom.json", true);
ASSERT_TRUE(fp != 0); TestEncodedOutputStream<UTF16LE<>, UTF16<>>("utf16le.json", false);
FileReadStream fs(fp, buffer, sizeof(buffer)); TestEncodedOutputStream<UTF16LE<>, UTF16<>>("utf16lebom.json", true);
EncodedInputStream<UTF16BE<>, FileReadStream> eis(fs); TestEncodedOutputStream<UTF16BE<>, UTF16<>>("utf16be.json", false);
StringStream s(json_); TestEncodedOutputStream<UTF16BE<>, UTF16<>>("utf16bebom.json", true);
TestEncodedOutputStream<UTF32LE<>, UTF32<>>("utf32le.json", false);
while (eis.Peek() != '\0') { TestEncodedOutputStream<UTF32LE<>, UTF32<>>("utf32lebom.json", true);
unsigned expected, actual; TestEncodedOutputStream<UTF32BE<>, UTF32<>>("utf32be.json", false);
UTF8<>::Decode(s, &expected); TestEncodedOutputStream<UTF32BE<>, UTF32<>>("utf32bebom.json", true);
UTF16<>::Decode(eis, &actual);
EXPECT_EQ(expected, actual);
}
EXPECT_EQ('\0', s.Peek());
fclose(fp);
} }
TEST_F(EncodingsTest, AutoUTFInputStream) { TEST_F(EncodingsTest, AutoUTFOutputStream) {
#define TEST_FILE(filename) \ TestAutoUTFOutputStream(kUTF8, false, "utf8.json");
{ \ TestAutoUTFOutputStream(kUTF8, true, "utf8bom.json");
char buffer[16]; \ TestAutoUTFOutputStream(kUTF16LE, false, "utf16le.json");
FILE *fp = Open(filename); \ TestAutoUTFOutputStream(kUTF16LE, true, "utf16lebom.json");
ASSERT_TRUE(fp != 0); \ TestAutoUTFOutputStream(kUTF16BE, false, "utf16be.json");
FileReadStream fs(fp, buffer, sizeof(buffer)); \ TestAutoUTFOutputStream(kUTF16BE, true, "utf16bebom.json");
AutoUTFInputStream<wchar_t, FileReadStream> eis(fs); \ TestAutoUTFOutputStream(kUTF32LE, false, "utf32le.json");
StringStream s(json_); \ TestAutoUTFOutputStream(kUTF32LE, true, "utf32lebom.json");
while (eis.Peek() != '\0') { \ TestAutoUTFOutputStream(kUTF32BE, false, "utf32be.json");
unsigned expected, actual; \ TestAutoUTFOutputStream(kUTF32BE, true, "utf32bebom.json");
UTF8<>::Decode(s, &expected); \
AutoUTF<wchar_t>::Decode(eis, &actual); \
EXPECT_EQ(expected, actual); \
} \
EXPECT_EQ('\0', s.Peek()); \
fclose(fp); \
}
TEST_FILE("utf8.json");
TEST_FILE("utf8bom.json");
TEST_FILE("utf16lebom.json");
TEST_FILE("utf16bebom.json");
#undef TEST_FILE
} }
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