Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
ostreamwrappertest.cpp
Go to the documentation of this file.
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. All rights reserved.
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
19#include "rapidjson/document.h"
20#include <sstream>
21#include <fstream>
22
23using namespace rapidjson;
24using namespace std;
25
26template <typename StringStreamType>
27static void TestStringStream() {
28 typedef typename StringStreamType::char_type Ch;
29
30 Ch s[] = { 'A', 'B', 'C', '\0' };
31 StringStreamType oss(s);
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
40TEST(OStreamWrapper, ostringstream) {
41 TestStringStream<ostringstream>();
42}
43
44TEST(OStreamWrapper, stringstream) {
45 TestStringStream<stringstream>();
46}
47
48TEST(OStreamWrapper, wostringstream) {
49 TestStringStream<wostringstream>();
50}
51
52TEST(OStreamWrapper, wstringstream) {
53 TestStringStream<wstringstream>();
54}
55
57 OStreamWrapper os(cout);
58 const char* s = "Hello World!\n";
59 while (*s)
60 os.Put(*s++);
61 os.Flush();
62}
63
64template <typename FileStreamType>
65static 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);
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
86TEST(OStreamWrapper, ofstream) {
87 TestFileStream<ofstream>();
88}
89
91 TestFileStream<fstream>();
92}
const mie::Vuint & p
Definition bn.cpp:27
Wrapper of std::basic_ostream into RapidJSON's Stream concept.
os_t os
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define ASSERT_TRUE(condition)
Definition gtest.h:1901
#define Ch(x, y, z)
Definition hash_impl.h:17
main RapidJSON namespace
Definition name.hpp:106
FILE * TempFile(char *filename)
Definition unittest.h:80
char * s