31 json_ =
ReadFile(
"utf8.json",
true, &length_);
44 static FILE*
Open(
const char* filename) {
45 const char *paths[] = {
49 "../../bin/encodings",
50 "../../../bin/encodings" 53 for (
size_t i = 0; i <
sizeof(paths) /
sizeof(paths[0]); i++) {
54 sprintf(buffer,
"%s/%s", paths[i], filename);
55 FILE *fp = fopen(buffer,
"rb");
62 static char *
ReadFile(
const char* filename,
bool appendPath,
size_t* outLength) {
63 FILE *fp = appendPath ?
Open(filename) : fopen(filename,
"rb");
70 fseek(fp, 0, SEEK_END);
71 *outLength =
static_cast<size_t>(ftell(fp));
72 fseek(fp, 0, SEEK_SET);
73 char* buffer =
static_cast<char*
>(malloc(*outLength + 1));
74 size_t readLength = fread(buffer, 1, *outLength, fp);
75 buffer[readLength] =
'\0';
80 template <
typename FileEncoding,
typename MemoryEncoding>
85 FILE *fp =
Open(filename);
91 while (eis.
Peek() !=
'\0') {
92 unsigned expected, actual;
94 EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
95 EXPECT_EQ(expected, actual);
97 EXPECT_EQ(
'\0', s.
Peek());
104 char* data =
ReadFile(filename,
true, &size);
109 while (eis.
Peek() !=
'\0') {
110 unsigned expected, actual;
112 EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
113 EXPECT_EQ(expected, actual);
115 EXPECT_EQ(
'\0', s.
Peek());
117 EXPECT_EQ(size, eis.
Tell());
125 FILE *fp =
Open(filename);
126 ASSERT_TRUE(fp != 0);
129 EXPECT_EQ(expectHasBOM, eis.
HasBOM());
131 while (eis.
Peek() !=
'\0') {
132 unsigned expected, actual;
135 EXPECT_EQ(expected, actual);
137 EXPECT_EQ(
'\0', s.Peek());
144 char* data =
ReadFile(filename,
true, &size);
147 EXPECT_EQ(expectHasBOM, eis.
HasBOM());
150 while (eis.
Peek() !=
'\0') {
151 unsigned expected, actual;
154 EXPECT_EQ(expected, actual);
156 EXPECT_EQ(
'\0', s.Peek());
158 EXPECT_EQ(size, eis.
Tell());
162 template <
typename FileEncoding,
typename MemoryEncoding>
166 char filename[L_tmpnam];
172 while (s.
Peek() !=
'\0') {
174 EXPECT_TRUE(success);
178 EXPECT_TRUE(CompareFile(filename, expectedFilename));
187 while (s.
Peek() !=
'\0') {
189 EXPECT_TRUE(success);
192 EXPECT_TRUE(CompareBufferFile(mb.
GetBuffer(), mb.
GetSize(), expectedFilename));
199 char filename[L_tmpnam];
206 while (s.
Peek() !=
'\0') {
208 EXPECT_TRUE(success);
212 EXPECT_TRUE(CompareFile(filename, expectedFilename));
221 while (s.
Peek() !=
'\0') {
223 EXPECT_TRUE(success);
226 EXPECT_TRUE(CompareBufferFile(mb.
GetBuffer(), mb.
GetSize(), expectedFilename));
230 bool CompareFile(
const char* filename,
const char* expectedFilename) {
231 size_t actualLength, expectedLength;
232 char* actualBuffer =
ReadFile(filename,
false, &actualLength);
233 char* expectedBuffer =
ReadFile(expectedFilename,
true, &expectedLength);
234 bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
236 free(expectedBuffer);
240 bool CompareBufferFile(
const char* actualBuffer,
size_t actualLength,
const char* expectedFilename) {
241 size_t expectedLength;
242 char* expectedBuffer =
ReadFile(expectedFilename,
true, &expectedLength);
243 bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
244 free(expectedBuffer);
255 TestEncodedInputStream<UTF8<>,
UTF8<> >(
"utf8.json");
256 TestEncodedInputStream<UTF8<>,
UTF8<> >(
"utf8bom.json");
257 TestEncodedInputStream<UTF16LE<>,
UTF16<> >(
"utf16le.json");
258 TestEncodedInputStream<UTF16LE<>,
UTF16<> >(
"utf16lebom.json");
259 TestEncodedInputStream<UTF16BE<>,
UTF16<> >(
"utf16be.json");
260 TestEncodedInputStream<UTF16BE<>,
UTF16<> >(
"utf16bebom.json");
261 TestEncodedInputStream<UTF32LE<>,
UTF32<> >(
"utf32le.json");
262 TestEncodedInputStream<UTF32LE<>,
UTF32<> >(
"utf32lebom.json");
263 TestEncodedInputStream<UTF32BE<>,
UTF32<> >(
"utf32be.json");
264 TestEncodedInputStream<UTF32BE<>,
UTF32<> >(
"utf32bebom.json");
268 TestAutoUTFInputStream(
"utf8.json",
false);
269 TestAutoUTFInputStream(
"utf8bom.json",
true);
270 TestAutoUTFInputStream(
"utf16le.json",
false);
271 TestAutoUTFInputStream(
"utf16lebom.json",
true);
272 TestAutoUTFInputStream(
"utf16be.json",
false);
273 TestAutoUTFInputStream(
"utf16bebom.json",
true);
274 TestAutoUTFInputStream(
"utf32le.json",
false);
275 TestAutoUTFInputStream(
"utf32lebom.json",
true);
276 TestAutoUTFInputStream(
"utf32be.json",
false);
277 TestAutoUTFInputStream(
"utf32bebom.json",
true);
281 const char json[] =
"{ }";
284 EXPECT_FALSE(eis.
HasBOM());
290 TestEncodedOutputStream<UTF8<>,
UTF8<> >(
"utf8.json",
false);
291 TestEncodedOutputStream<UTF8<>, UTF8<> >(
"utf8bom.json",
true);
292 TestEncodedOutputStream<UTF16LE<>,
UTF16<> >(
"utf16le.json",
false);
293 TestEncodedOutputStream<UTF16LE<>, UTF16<> >(
"utf16lebom.json",
true);
294 TestEncodedOutputStream<UTF16BE<>, UTF16<> >(
"utf16be.json",
false);
295 TestEncodedOutputStream<UTF16BE<>, UTF16<> >(
"utf16bebom.json",
true);
296 TestEncodedOutputStream<UTF32LE<>,
UTF32<> >(
"utf32le.json",
false);
297 TestEncodedOutputStream<UTF32LE<>, UTF32<> >(
"utf32lebom.json",
true);
298 TestEncodedOutputStream<UTF32BE<>, UTF32<> >(
"utf32be.json",
false);
299 TestEncodedOutputStream<UTF32BE<>, UTF32<> >(
"utf32bebom.json",
true);
303 TestAutoUTFOutputStream(
kUTF8,
false,
"utf8.json");
304 TestAutoUTFOutputStream(
kUTF8,
true,
"utf8bom.json");
305 TestAutoUTFOutputStream(
kUTF16LE,
false,
"utf16le.json");
306 TestAutoUTFOutputStream(
kUTF16LE,
true,
"utf16lebom.json");
307 TestAutoUTFOutputStream(
kUTF16BE,
false,
"utf16be.json");
308 TestAutoUTFOutputStream(
kUTF16BE,
true,
"utf16bebom.json");
309 TestAutoUTFOutputStream(
kUTF32LE,
false,
"utf32le.json");
310 TestAutoUTFOutputStream(
kUTF32LE,
true,
"utf32lebom.json");
311 TestAutoUTFOutputStream(
kUTF32BE,
false,
"utf32be.json");
312 TestAutoUTFOutputStream(
kUTF32BE,
true,
"utf32bebom.json");
UTFType
Runtime-specified UTF encoding type of a stream.
Represents an in-memory input byte stream.
Represents an in-memory output byte stream.
Output byte stream wrapper with statically bound encoding.
FILE * TempFile(char *filename)
Wrapper of C file stream for input using fread().
virtual ~EncodedStreamTest()
void TestEncodedInputStream(const char *filename)
void TestAutoUTFInputStream(const char *filename, bool expectHasBOM)
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
TEST_F(EncodedStreamTest, EncodedInputStream)
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
static char * ReadFile(const char *filename, Allocator &allocator)
bool CompareFile(const char *filename, const char *expectedFilename)
bool CompareBufferFile(const char *actualBuffer, size_t actualLength, const char *expectedFilename)
const Ch * GetBuffer() const
static FILE * Open(const char *filename)
static bool Open(FileStreamType &fs, const char *filename)
static char * ReadFile(const char *filename, bool appendPath, size_t *outLength)
File byte stream for input using fread().
void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename)
void TestEncodedOutputStream(const char *expectedFilename, bool putBOM)