| 1 |
// Tencent is pleased to support the open source community by making RapidJSON available. |
| 2 |
// |
| 3 |
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. |
| 4 |
// |
| 5 |
// Licensed under the MIT License (the "License"); you may not use this file except |
| 6 |
// in compliance with the License. You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://opensource.org/licenses/MIT |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 11 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 |
// specific language governing permissions and limitations under the License. |
| 14 |
|
| 15 |
#include "unittest.h" |
| 16 |
|
| 17 |
#include "rapidjson/ostreamwrapper.h" |
| 18 |
#include "rapidjson/encodedstream.h" |
| 19 |
#include "rapidjson/document.h" |
| 20 |
#include <sstream> |
| 21 |
#include <fstream> |
| 22 |
|
| 23 |
using namespace rapidjson; |
| 24 |
using namespace std; |
| 25 |
|
| 26 |
template <typename StringStreamType> |
| 27 |
static void TestStringStream() { |
| 28 |
typedef typename StringStreamType::char_type Ch; |
| 29 |
|
| 30 |
Ch s[] = { 'A', 'B', 'C', '\0' }; |
| 31 |
StringStreamType oss(s); |
| 32 |
BasicOStreamWrapper<StringStreamType> os(oss); |
| 33 |
for (size_t i = 0; i < 3; i++) |
| 34 |
os.Put(s[i]); |
| 35 |
os.Flush(); |
| 36 |
for (size_t i = 0; i < 3; i++) |
| 37 |
EXPECT_EQ(s[i], oss.str()[i]); |
| 38 |
} |
| 39 |
|
| 40 |
TEST(OStreamWrapper, ostringstream) { |
| 41 |
TestStringStream<ostringstream>(); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(OStreamWrapper, stringstream) { |
| 45 |
TestStringStream<stringstream>(); |
| 46 |
} |
| 47 |
|
| 48 |
TEST(OStreamWrapper, wostringstream) { |
| 49 |
TestStringStream<wostringstream>(); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(OStreamWrapper, wstringstream) { |
| 53 |
TestStringStream<wstringstream>(); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(OStreamWrapper, cout) { |
| 57 |
OStreamWrapper os(cout); |
| 58 |
const char* s = "Hello World!\n"; |
| 59 |
while (*s) |
| 60 |
os.Put(*s++); |
| 61 |
os.Flush(); |
| 62 |
} |
| 63 |
|
| 64 |
template <typename FileStreamType> |
| 65 |
static void TestFileStream() { |
| 66 |
char filename[L_tmpnam]; |
| 67 |
FILE* fp = TempFile(filename); |
| 68 |
fclose(fp); |
| 69 |
|
| 70 |
const char* s = "Hello World!\n"; |
| 71 |
{ |
| 72 |
FileStreamType ofs(filename, ios::out | ios::binary); |
| 73 |
BasicOStreamWrapper<FileStreamType> osw(ofs); |
| 74 |
for (const char* p = s; *p; p++) |
| 75 |
osw.Put(*p); |
| 76 |
osw.Flush(); |
| 77 |
} |
| 78 |
|
| 79 |
fp = fopen(filename, "r"); |
| 80 |
ASSERT_TRUE( fp != NULL ); |
| 81 |
for (const char* p = s; *p; p++) |
| 82 |
EXPECT_EQ(*p, static_cast<char>(fgetc(fp))); |
| 83 |
fclose(fp); |
| 84 |
} |
| 85 |
|
| 86 |
TEST(OStreamWrapper, ofstream) { |
| 87 |
TestFileStream<ofstream>(); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(OStreamWrapper, fstream) { |
| 91 |
TestFileStream<fstream>(); |
| 92 |
} |