fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / rapidjson-1.1.0 / test / unittest / ostreamwrappertest.cpp
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
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         ofstream ofs(filename, ios::out | ios::binary);
73         BasicOStreamWrapper<ofstream> 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     for (const char* p = s; *p; p++)
81         EXPECT_EQ(*p, static_cast<char>(fgetc(fp)));
82     fclose(fp);
83 }
84
85 TEST(OStreamWrapper, ofstream) {
86     TestFileStream<ofstream>();
87 }
88
89 TEST(OStreamWrapper, fstream) {
90     TestFileStream<fstream>();
91 }