encodedstreamtest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "unittest.h"
  2. #include "rapidjson/filereadstream.h"
  3. #include "rapidjson/filewritestream.h"
  4. #include "rapidjson/encodedstream.h"
  5. #include "rapidjson/stringbuffer.h"
  6. using namespace rapidjson;
  7. class EncodedStreamTest : public ::testing::Test {
  8. public:
  9. virtual void SetUp() {
  10. json_ = ReadFile("utf8.json", true, &length_);
  11. }
  12. virtual void TearDown() {
  13. free(json_);
  14. }
  15. protected:
  16. static FILE* Open(const char* filename) {
  17. char buffer[1024];
  18. sprintf(buffer, "encodings/%s", filename);
  19. FILE *fp = fopen(buffer, "rb");
  20. if (!fp) {
  21. sprintf(buffer, "../../bin/encodings/%s", filename);
  22. fp = fopen(buffer, "rb");
  23. }
  24. return fp;
  25. }
  26. static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) {
  27. FILE *fp = appendPath ? Open(filename) : fopen(filename, "rb");
  28. if (!fp) {
  29. *outLength = 0;
  30. return 0;
  31. }
  32. fseek(fp, 0, SEEK_END);
  33. *outLength = (size_t)ftell(fp);
  34. fseek(fp, 0, SEEK_SET);
  35. char* buffer = (char*)malloc(*outLength + 1);
  36. fread(buffer, 1, *outLength, fp);
  37. buffer[*outLength] = '\0';
  38. fclose(fp);
  39. return buffer;
  40. }
  41. template <typename FileEncoding, typename MemoryEncoding>
  42. void TestEncodedInputStream(const char* filename) {
  43. char buffer[16];
  44. FILE *fp = Open(filename);
  45. ASSERT_TRUE(fp != 0);
  46. FileReadStream fs(fp, buffer, sizeof(buffer));
  47. EncodedInputStream<FileEncoding, FileReadStream> eis(fs);
  48. StringStream s(json_);
  49. while (eis.Peek() != '\0') {
  50. unsigned expected, actual;
  51. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  52. EXPECT_TRUE(MemoryEncoding::Decode(eis, &actual));
  53. EXPECT_EQ(expected, actual);
  54. }
  55. EXPECT_EQ('\0', s.Peek());
  56. fclose(fp);
  57. }
  58. void TestAutoUTFInputStream(const char *filename) {
  59. char buffer[16];
  60. FILE *fp = Open(filename);
  61. ASSERT_TRUE(fp != 0);
  62. FileReadStream fs(fp, buffer, sizeof(buffer));
  63. AutoUTFInputStream<unsigned, FileReadStream> eis(fs);
  64. StringStream s(json_);
  65. while (eis.Peek() != '\0') {
  66. unsigned expected, actual;
  67. EXPECT_TRUE(UTF8<>::Decode(s, &expected));
  68. EXPECT_TRUE(AutoUTF<unsigned>::Decode(eis, &actual));
  69. EXPECT_EQ(expected, actual);
  70. }
  71. EXPECT_EQ('\0', s.Peek());
  72. fclose(fp);
  73. }
  74. template <typename FileEncoding, typename MemoryEncoding>
  75. void TestEncodedOutputStream(const char* expectedFilename, bool putBOM) {
  76. char filename[L_tmpnam];
  77. tmpnam(filename);
  78. FILE *fp = fopen(filename, "wb");
  79. char buffer[16];
  80. FileWriteStream os(fp, buffer, sizeof(buffer));
  81. EncodedOutputStream<FileEncoding, FileWriteStream> eos(os, putBOM);
  82. StringStream s(json_);
  83. while (s.Peek() != '\0') {
  84. bool success = Transcoder<UTF8<>, MemoryEncoding>::Transcode(s, eos);
  85. EXPECT_TRUE(success);
  86. }
  87. eos.Flush();
  88. fclose(fp);
  89. EXPECT_TRUE(CompareFile(filename, expectedFilename));
  90. remove(filename);
  91. }
  92. bool CompareFile(char * filename, const char* expectedFilename) {
  93. size_t actualLength, expectedLength;
  94. char* actualBuffer = ReadFile(filename, false, &actualLength);
  95. char* expectedBuffer = ReadFile(expectedFilename, true, &expectedLength);
  96. bool ret = (expectedLength == actualLength) && memcmp(expectedBuffer, actualBuffer, actualLength) == 0;
  97. free(actualBuffer);
  98. free(expectedBuffer);
  99. return ret;
  100. }
  101. void TestAutoUTFOutputStream(UTFType type, bool putBOM, const char *expectedFilename) {
  102. char filename[L_tmpnam];
  103. tmpnam(filename);
  104. FILE *fp = fopen(filename, "wb");
  105. char buffer[16];
  106. FileWriteStream os(fp, buffer, sizeof(buffer));
  107. AutoUTFOutputStream<unsigned, FileWriteStream> eos(os, type, putBOM);
  108. StringStream s(json_);
  109. while (s.Peek() != '\0') {
  110. bool success = Transcoder<UTF8<>, AutoUTF<unsigned> >::Transcode(s, eos);
  111. EXPECT_TRUE(success);
  112. }
  113. eos.Flush();
  114. fclose(fp);
  115. EXPECT_TRUE(CompareFile(filename, expectedFilename));
  116. remove(filename);
  117. }
  118. const char* filename_;
  119. char *json_;
  120. size_t length_;
  121. };
  122. TEST_F(EncodedStreamTest, EncodedInputStream) {
  123. TestEncodedInputStream<UTF8<>, UTF8<> >("utf8.json");
  124. TestEncodedInputStream<UTF8<>, UTF8<> >("utf8bom.json");
  125. TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16le.json");
  126. TestEncodedInputStream<UTF16LE<>, UTF16<> >("utf16lebom.json");
  127. TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16be.json");
  128. TestEncodedInputStream<UTF16BE<>, UTF16<> >("utf16bebom.json");
  129. TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32le.json");
  130. TestEncodedInputStream<UTF32LE<>, UTF32<> >("utf32lebom.json");
  131. TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32be.json");
  132. TestEncodedInputStream<UTF32BE<>, UTF32<> >("utf32bebom.json");
  133. }
  134. TEST_F(EncodedStreamTest, AutoUTFInputStream) {
  135. TestAutoUTFInputStream("utf8.json");
  136. TestAutoUTFInputStream("utf8bom.json");
  137. TestAutoUTFInputStream("utf16le.json");
  138. TestAutoUTFInputStream("utf16lebom.json");
  139. TestAutoUTFInputStream("utf16be.json");
  140. TestAutoUTFInputStream("utf16bebom.json");
  141. TestAutoUTFInputStream("utf32le.json");
  142. TestAutoUTFInputStream("utf32lebom.json");
  143. TestAutoUTFInputStream("utf32be.json");
  144. TestAutoUTFInputStream("utf32bebom.json");
  145. }
  146. TEST_F(EncodedStreamTest, EncodedOutputStream) {
  147. TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8.json", false);
  148. TestEncodedOutputStream<UTF8<>, UTF8<> >("utf8bom.json", true);
  149. TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16le.json", false);
  150. TestEncodedOutputStream<UTF16LE<>, UTF16<> >("utf16lebom.json",true);
  151. TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16be.json", false);
  152. TestEncodedOutputStream<UTF16BE<>, UTF16<> >("utf16bebom.json",true);
  153. TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32le.json", false);
  154. TestEncodedOutputStream<UTF32LE<>, UTF32<> >("utf32lebom.json",true);
  155. TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32be.json", false);
  156. TestEncodedOutputStream<UTF32BE<>, UTF32<> >("utf32bebom.json",true);
  157. }
  158. TEST_F(EncodedStreamTest, AutoUTFOutputStream) {
  159. TestAutoUTFOutputStream(kUTF8, false, "utf8.json");
  160. TestAutoUTFOutputStream(kUTF8, true, "utf8bom.json");
  161. TestAutoUTFOutputStream(kUTF16LE, false, "utf16le.json");
  162. TestAutoUTFOutputStream(kUTF16LE, true, "utf16lebom.json");
  163. TestAutoUTFOutputStream(kUTF16BE, false, "utf16be.json");
  164. TestAutoUTFOutputStream(kUTF16BE, true, "utf16bebom.json");
  165. TestAutoUTFOutputStream(kUTF32LE, false, "utf32le.json");
  166. TestAutoUTFOutputStream(kUTF32LE, true, "utf32lebom.json");
  167. TestAutoUTFOutputStream(kUTF32BE, false, "utf32be.json");
  168. TestAutoUTFOutputStream(kUTF32BE, true, "utf32bebom.json");
  169. }