Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: webrtc/base/json_unittest.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/json.cc ('k') | webrtc/base/keep_ref_until_done.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/json.h"
12
13 #include <vector>
14
15 #include "webrtc/base/gunit.h"
16
17 namespace rtc {
18
19 static Json::Value in_s("foo");
20 static Json::Value in_sn("99");
21 static Json::Value in_si("-99");
22 static Json::Value in_sb("true");
23 static Json::Value in_sd("1.2");
24 static Json::Value in_n(12);
25 static Json::Value in_i(-12);
26 static Json::Value in_u(34U);
27 static Json::Value in_b(true);
28 static Json::Value in_d(1.2);
29 static Json::Value big_sn("12345678901234567890");
30 static Json::Value big_si("-12345678901234567890");
31 static Json::Value big_u(0xFFFFFFFF);
32 static Json::Value bad_a(Json::arrayValue);
33 static Json::Value bad_o(Json::objectValue);
34
35 TEST(JsonTest, GetString) {
36 std::string out;
37 EXPECT_TRUE(GetStringFromJson(in_s, &out));
38 EXPECT_EQ("foo", out);
39 EXPECT_TRUE(GetStringFromJson(in_sn, &out));
40 EXPECT_EQ("99", out);
41 EXPECT_TRUE(GetStringFromJson(in_si, &out));
42 EXPECT_EQ("-99", out);
43 EXPECT_TRUE(GetStringFromJson(in_i, &out));
44 EXPECT_EQ("-12", out);
45 EXPECT_TRUE(GetStringFromJson(in_n, &out));
46 EXPECT_EQ("12", out);
47 EXPECT_TRUE(GetStringFromJson(in_u, &out));
48 EXPECT_EQ("34", out);
49 EXPECT_TRUE(GetStringFromJson(in_b, &out));
50 EXPECT_EQ("true", out);
51 // Not supported here yet.
52 EXPECT_FALSE(GetStringFromJson(bad_a, &out));
53 EXPECT_FALSE(GetStringFromJson(bad_o, &out));
54 }
55
56 TEST(JsonTest, GetInt) {
57 int out;
58 EXPECT_TRUE(GetIntFromJson(in_sn, &out));
59 EXPECT_EQ(99, out);
60 EXPECT_TRUE(GetIntFromJson(in_si, &out));
61 EXPECT_EQ(-99, out);
62 EXPECT_TRUE(GetIntFromJson(in_n, &out));
63 EXPECT_EQ(12, out);
64 EXPECT_TRUE(GetIntFromJson(in_i, &out));
65 EXPECT_EQ(-12, out);
66 EXPECT_TRUE(GetIntFromJson(in_u, &out));
67 EXPECT_EQ(34, out);
68 EXPECT_TRUE(GetIntFromJson(in_b, &out));
69 EXPECT_EQ(1, out);
70 EXPECT_FALSE(GetIntFromJson(in_s, &out));
71 EXPECT_FALSE(GetIntFromJson(big_sn, &out));
72 EXPECT_FALSE(GetIntFromJson(big_si, &out));
73 EXPECT_FALSE(GetIntFromJson(big_u, &out));
74 EXPECT_FALSE(GetIntFromJson(bad_a, &out));
75 EXPECT_FALSE(GetIntFromJson(bad_o, &out));
76 }
77
78 TEST(JsonTest, GetUInt) {
79 unsigned int out;
80 EXPECT_TRUE(GetUIntFromJson(in_sn, &out));
81 EXPECT_EQ(99U, out);
82 EXPECT_TRUE(GetUIntFromJson(in_n, &out));
83 EXPECT_EQ(12U, out);
84 EXPECT_TRUE(GetUIntFromJson(in_u, &out));
85 EXPECT_EQ(34U, out);
86 EXPECT_TRUE(GetUIntFromJson(in_b, &out));
87 EXPECT_EQ(1U, out);
88 EXPECT_TRUE(GetUIntFromJson(big_u, &out));
89 EXPECT_EQ(0xFFFFFFFFU, out);
90 EXPECT_FALSE(GetUIntFromJson(in_s, &out));
91 // TODO: Fail reading negative strings.
92 // EXPECT_FALSE(GetUIntFromJson(in_si, &out));
93 EXPECT_FALSE(GetUIntFromJson(in_i, &out));
94 EXPECT_FALSE(GetUIntFromJson(big_sn, &out));
95 EXPECT_FALSE(GetUIntFromJson(big_si, &out));
96 EXPECT_FALSE(GetUIntFromJson(bad_a, &out));
97 EXPECT_FALSE(GetUIntFromJson(bad_o, &out));
98 }
99
100 TEST(JsonTest, GetBool) {
101 bool out;
102 EXPECT_TRUE(GetBoolFromJson(in_sb, &out));
103 EXPECT_EQ(true, out);
104 EXPECT_TRUE(GetBoolFromJson(in_n, &out));
105 EXPECT_EQ(true, out);
106 EXPECT_TRUE(GetBoolFromJson(in_i, &out));
107 EXPECT_EQ(true, out);
108 EXPECT_TRUE(GetBoolFromJson(in_u, &out));
109 EXPECT_EQ(true, out);
110 EXPECT_TRUE(GetBoolFromJson(in_b, &out));
111 EXPECT_EQ(true, out);
112 EXPECT_TRUE(GetBoolFromJson(big_u, &out));
113 EXPECT_EQ(true, out);
114 EXPECT_FALSE(GetBoolFromJson(in_s, &out));
115 EXPECT_FALSE(GetBoolFromJson(in_sn, &out));
116 EXPECT_FALSE(GetBoolFromJson(in_si, &out));
117 EXPECT_FALSE(GetBoolFromJson(big_sn, &out));
118 EXPECT_FALSE(GetBoolFromJson(big_si, &out));
119 EXPECT_FALSE(GetBoolFromJson(bad_a, &out));
120 EXPECT_FALSE(GetBoolFromJson(bad_o, &out));
121 }
122
123 TEST(JsonTest, GetDouble) {
124 double out;
125 EXPECT_TRUE(GetDoubleFromJson(in_sn, &out));
126 EXPECT_EQ(99, out);
127 EXPECT_TRUE(GetDoubleFromJson(in_si, &out));
128 EXPECT_EQ(-99, out);
129 EXPECT_TRUE(GetDoubleFromJson(in_sd, &out));
130 EXPECT_EQ(1.2, out);
131 EXPECT_TRUE(GetDoubleFromJson(in_n, &out));
132 EXPECT_EQ(12, out);
133 EXPECT_TRUE(GetDoubleFromJson(in_i, &out));
134 EXPECT_EQ(-12, out);
135 EXPECT_TRUE(GetDoubleFromJson(in_u, &out));
136 EXPECT_EQ(34, out);
137 EXPECT_TRUE(GetDoubleFromJson(in_b, &out));
138 EXPECT_EQ(1, out);
139 EXPECT_TRUE(GetDoubleFromJson(in_d, &out));
140 EXPECT_EQ(1.2, out);
141 EXPECT_FALSE(GetDoubleFromJson(in_s, &out));
142 }
143
144 TEST(JsonTest, GetFromArray) {
145 Json::Value a, out;
146 a.append(in_s);
147 a.append(in_i);
148 a.append(in_u);
149 a.append(in_b);
150 EXPECT_TRUE(GetValueFromJsonArray(a, 0, &out));
151 EXPECT_TRUE(GetValueFromJsonArray(a, 3, &out));
152 EXPECT_FALSE(GetValueFromJsonArray(a, 99, &out));
153 EXPECT_FALSE(GetValueFromJsonArray(a, 0xFFFFFFFF, &out));
154 }
155
156 TEST(JsonTest, GetFromObject) {
157 Json::Value o, out;
158 o["string"] = in_s;
159 o["int"] = in_i;
160 o["uint"] = in_u;
161 o["bool"] = in_b;
162 EXPECT_TRUE(GetValueFromJsonObject(o, "int", &out));
163 EXPECT_TRUE(GetValueFromJsonObject(o, "bool", &out));
164 EXPECT_FALSE(GetValueFromJsonObject(o, "foo", &out));
165 EXPECT_FALSE(GetValueFromJsonObject(o, "", &out));
166 }
167
168 namespace {
169 template <typename T>
170 std::vector<T> VecOf3(const T& a, const T& b, const T& c) {
171 std::vector<T> in;
172 in.push_back(a);
173 in.push_back(b);
174 in.push_back(c);
175 return in;
176 }
177 template <typename T>
178 Json::Value JsonVecOf3(const T& a, const T& b, const T& c) {
179 Json::Value in(Json::arrayValue);
180 in.append(a);
181 in.append(b);
182 in.append(c);
183 return in;
184 }
185 } // unnamed namespace
186
187 TEST(JsonTest, ValueVectorToFromArray) {
188 std::vector<Json::Value> in = VecOf3<Json::Value>("a", "b", "c");
189 Json::Value out = ValueVectorToJsonArray(in);
190 EXPECT_EQ(in.size(), out.size());
191 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
192 EXPECT_EQ(in[i].asString(), out[i].asString());
193 }
194 Json::Value inj = JsonVecOf3<Json::Value>("a", "b", "c");
195 EXPECT_EQ(inj, out);
196 std::vector<Json::Value> outj;
197 EXPECT_TRUE(JsonArrayToValueVector(inj, &outj));
198 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
199 EXPECT_EQ(in[i], outj[i]);
200 }
201 }
202
203 TEST(JsonTest, IntVectorToFromArray) {
204 std::vector<int> in = VecOf3<int>(1, 2, 3);
205 Json::Value out = IntVectorToJsonArray(in);
206 EXPECT_EQ(in.size(), out.size());
207 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
208 EXPECT_EQ(in[i], out[i].asInt());
209 }
210 Json::Value inj = JsonVecOf3<int>(1, 2, 3);
211 EXPECT_EQ(inj, out);
212 std::vector<int> outj;
213 EXPECT_TRUE(JsonArrayToIntVector(inj, &outj));
214 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
215 EXPECT_EQ(in[i], outj[i]);
216 }
217 }
218
219 TEST(JsonTest, UIntVectorToFromArray) {
220 std::vector<unsigned int> in = VecOf3<unsigned int>(1, 2, 3);
221 Json::Value out = UIntVectorToJsonArray(in);
222 EXPECT_EQ(in.size(), out.size());
223 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
224 EXPECT_EQ(in[i], out[i].asUInt());
225 }
226 Json::Value inj = JsonVecOf3<unsigned int>(1, 2, 3);
227 EXPECT_EQ(inj, out);
228 std::vector<unsigned int> outj;
229 EXPECT_TRUE(JsonArrayToUIntVector(inj, &outj));
230 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
231 EXPECT_EQ(in[i], outj[i]);
232 }
233 }
234
235 TEST(JsonTest, StringVectorToFromArray) {
236 std::vector<std::string> in = VecOf3<std::string>("a", "b", "c");
237 Json::Value out = StringVectorToJsonArray(in);
238 EXPECT_EQ(in.size(), out.size());
239 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
240 EXPECT_EQ(in[i], out[i].asString());
241 }
242 Json::Value inj = JsonVecOf3<std::string>("a", "b", "c");
243 EXPECT_EQ(inj, out);
244 std::vector<std::string> outj;
245 EXPECT_TRUE(JsonArrayToStringVector(inj, &outj));
246 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
247 EXPECT_EQ(in[i], outj[i]);
248 }
249 }
250
251 TEST(JsonTest, BoolVectorToFromArray) {
252 std::vector<bool> in = VecOf3<bool>(false, true, false);
253 Json::Value out = BoolVectorToJsonArray(in);
254 EXPECT_EQ(in.size(), out.size());
255 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
256 EXPECT_EQ(in[i], out[i].asBool());
257 }
258 Json::Value inj = JsonVecOf3<bool>(false, true, false);
259 EXPECT_EQ(inj, out);
260 std::vector<bool> outj;
261 EXPECT_TRUE(JsonArrayToBoolVector(inj, &outj));
262 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
263 EXPECT_EQ(in[i], outj[i]);
264 }
265 }
266
267 TEST(JsonTest, DoubleVectorToFromArray) {
268 std::vector<double> in = VecOf3<double>(1.0, 2.0, 3.0);
269 Json::Value out = DoubleVectorToJsonArray(in);
270 EXPECT_EQ(in.size(), out.size());
271 for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
272 EXPECT_EQ(in[i], out[i].asDouble());
273 }
274 Json::Value inj = JsonVecOf3<double>(1.0, 2.0, 3.0);
275 EXPECT_EQ(inj, out);
276 std::vector<double> outj;
277 EXPECT_TRUE(JsonArrayToDoubleVector(inj, &outj));
278 for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
279 EXPECT_EQ(in[i], outj[i]);
280 }
281 }
282
283 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/json.cc ('k') | webrtc/base/keep_ref_until_done.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698