fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / rapidjson-1.1.0 / test / unittest / prettywritertest.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 #include "rapidjson/reader.h"
17 #include "rapidjson/prettywriter.h"
18 #include "rapidjson/stringbuffer.h"
19 #include "rapidjson/filewritestream.h"
20
21 using namespace rapidjson;
22
23 static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
24 static const char kPrettyJson[] =
25 "{\n"
26 "    \"hello\": \"world\",\n"
27 "    \"t\": true,\n"
28 "    \"f\": false,\n"
29 "    \"n\": null,\n"
30 "    \"i\": 123,\n"
31 "    \"pi\": 3.1416,\n"
32 "    \"a\": [\n"
33 "        1,\n"
34 "        2,\n"
35 "        3,\n"
36 "        -1\n"
37 "    ],\n"
38 "    \"u64\": 1234567890123456789,\n"
39 "    \"i64\": -1234567890123456789\n"
40 "}";
41
42 static const char kPrettyJson_FormatOptions_SLA[] =
43 "{\n"
44 "    \"hello\": \"world\",\n"
45 "    \"t\": true,\n"
46 "    \"f\": false,\n"
47 "    \"n\": null,\n"
48 "    \"i\": 123,\n"
49 "    \"pi\": 3.1416,\n"
50 "    \"a\": [1, 2, 3, -1],\n"
51 "    \"u64\": 1234567890123456789,\n"
52 "    \"i64\": -1234567890123456789\n"
53 "}";
54
55 TEST(PrettyWriter, Basic) {
56     StringBuffer buffer;
57     PrettyWriter<StringBuffer> writer(buffer);
58     Reader reader;
59     StringStream s(kJson);
60     reader.Parse(s, writer);
61     EXPECT_STREQ(kPrettyJson, buffer.GetString());
62 }
63
64 TEST(PrettyWriter, FormatOptions) {
65     StringBuffer buffer;
66     PrettyWriter<StringBuffer> writer(buffer);
67     writer.SetFormatOptions(kFormatSingleLineArray);
68     Reader reader;
69     StringStream s(kJson);
70     reader.Parse(s, writer);
71     EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
72 }
73
74 TEST(PrettyWriter, SetIndent) {
75     StringBuffer buffer;
76     PrettyWriter<StringBuffer> writer(buffer);
77     writer.SetIndent('\t', 1);
78     Reader reader;
79     StringStream s(kJson);
80     reader.Parse(s, writer);
81     EXPECT_STREQ(
82         "{\n"
83         "\t\"hello\": \"world\",\n"
84         "\t\"t\": true,\n"
85         "\t\"f\": false,\n"
86         "\t\"n\": null,\n"
87         "\t\"i\": 123,\n"
88         "\t\"pi\": 3.1416,\n"
89         "\t\"a\": [\n"
90         "\t\t1,\n"
91         "\t\t2,\n"
92         "\t\t3,\n"
93         "\t\t-1\n"
94         "\t],\n"
95         "\t\"u64\": 1234567890123456789,\n"
96         "\t\"i64\": -1234567890123456789\n"
97         "}",
98         buffer.GetString());
99 }
100
101 TEST(PrettyWriter, String) {
102     StringBuffer buffer;
103     PrettyWriter<StringBuffer> writer(buffer);
104     EXPECT_TRUE(writer.StartArray());
105     EXPECT_TRUE(writer.String("Hello\n"));
106     EXPECT_TRUE(writer.EndArray());
107     EXPECT_STREQ("[\n    \"Hello\\n\"\n]", buffer.GetString());
108 }
109
110 #if RAPIDJSON_HAS_STDSTRING
111 TEST(PrettyWriter, String_STDSTRING) {
112     StringBuffer buffer;
113     PrettyWriter<StringBuffer> writer(buffer);
114     EXPECT_TRUE(writer.StartArray());
115     EXPECT_TRUE(writer.String(std::string("Hello\n")));
116     EXPECT_TRUE(writer.EndArray());
117     EXPECT_STREQ("[\n    \"Hello\\n\"\n]", buffer.GetString());
118 }
119 #endif
120
121 #include <sstream>
122
123 class OStreamWrapper {
124 public:
125     typedef char Ch;
126
127     OStreamWrapper(std::ostream& os) : os_(os) {}
128
129     Ch Peek() const { assert(false); return '\0'; }
130     Ch Take() { assert(false); return '\0'; }
131     size_t Tell() const { return 0; }
132
133     Ch* PutBegin() { assert(false); return 0; }
134     void Put(Ch c) { os_.put(c); }
135     void Flush() { os_.flush(); }
136     size_t PutEnd(Ch*) { assert(false); return 0; }
137
138 private:
139     OStreamWrapper(const OStreamWrapper&);
140     OStreamWrapper& operator=(const OStreamWrapper&);
141
142     std::ostream& os_;
143 };
144
145 // For covering PutN() generic version
146 TEST(PrettyWriter, OStreamWrapper) {
147     StringStream s(kJson);
148     
149     std::stringstream ss;
150     OStreamWrapper os(ss);
151     
152     PrettyWriter<OStreamWrapper> writer(os);
153
154     Reader reader;
155     reader.Parse(s, writer);
156     
157     std::string actual = ss.str();
158     EXPECT_STREQ(kPrettyJson, actual.c_str());
159 }
160
161 // For covering FileWriteStream::PutN()
162 TEST(PrettyWriter, FileWriteStream) {
163     char filename[L_tmpnam];
164     FILE* fp = TempFile(filename);
165     char buffer[16];
166     FileWriteStream os(fp, buffer, sizeof(buffer));
167     PrettyWriter<FileWriteStream> writer(os);
168     Reader reader;
169     StringStream s(kJson);
170     reader.Parse(s, writer);
171     fclose(fp);
172
173     fp = fopen(filename, "rb");
174     fseek(fp, 0, SEEK_END);
175     size_t size = static_cast<size_t>(ftell(fp));
176     fseek(fp, 0, SEEK_SET);
177     char* json = static_cast<char*>(malloc(size + 1));
178     size_t readLength = fread(json, 1, size, fp);
179     json[readLength] = '\0';
180     fclose(fp);
181     remove(filename);
182     EXPECT_STREQ(kPrettyJson, json);
183     free(json);
184 }
185
186 TEST(PrettyWriter, RawValue) {
187     StringBuffer buffer;
188     PrettyWriter<StringBuffer> writer(buffer);
189     writer.StartObject();
190     writer.Key("a");
191     writer.Int(1);
192     writer.Key("raw");
193     const char json[] = "[\"Hello\\nWorld\", 123.456]";
194     writer.RawValue(json, strlen(json), kArrayType);
195     writer.EndObject();
196     EXPECT_TRUE(writer.IsComplete());
197     EXPECT_STREQ(
198         "{\n"
199         "    \"a\": 1,\n"
200         "    \"raw\": [\"Hello\\nWorld\", 123.456]\n" // no indentation within raw value
201         "}",
202         buffer.GetString());
203 }