1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
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
8 // http://opensource.org/licenses/MIT
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.
17 #include "rapidjson/ostreamwrapper.h"
18 #include "rapidjson/encodedstream.h"
19 #include "rapidjson/document.h"
23 using namespace rapidjson;
26 template <typename StringStreamType>
27 static void TestStringStream() {
28 typedef typename StringStreamType::char_type Ch;
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++)
36 for (size_t i = 0; i < 3; i++)
37 EXPECT_EQ(s[i], oss.str()[i]);
40 TEST(OStreamWrapper, ostringstream) {
41 TestStringStream<ostringstream>();
44 TEST(OStreamWrapper, stringstream) {
45 TestStringStream<stringstream>();
48 TEST(OStreamWrapper, wostringstream) {
49 TestStringStream<wostringstream>();
52 TEST(OStreamWrapper, wstringstream) {
53 TestStringStream<wstringstream>();
56 TEST(OStreamWrapper, cout) {
57 OStreamWrapper os(cout);
58 const char* s = "Hello World!\n";
64 template <typename FileStreamType>
65 static void TestFileStream() {
66 char filename[L_tmpnam];
67 FILE* fp = TempFile(filename);
70 const char* s = "Hello World!\n";
72 ofstream ofs(filename, ios::out | ios::binary);
73 BasicOStreamWrapper<ofstream> osw(ofs);
74 for (const char* p = s; *p; p++)
79 fp = fopen(filename, "r");
80 for (const char* p = s; *p; p++)
81 EXPECT_EQ(*p, static_cast<char>(fgetc(fp)));
85 TEST(OStreamWrapper, ofstream) {
86 TestFileStream<ofstream>();
89 TEST(OStreamWrapper, fstream) {
90 TestFileStream<fstream>();