summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/rtc_base/strings/json_unittest.cc
blob: 82d26f179ed0e2a2ae295ea47260828cc6fd9bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 *  Copyright 2009 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/strings/json.h"

#include <vector>

#include "test/gtest.h"

namespace rtc {

static Json::Value in_s("foo");
static Json::Value in_sn("99");
static Json::Value in_si("-99");
static Json::Value in_sb("true");
static Json::Value in_sd("1.2");
static Json::Value in_n(12);
static Json::Value in_i(-12);
static Json::Value in_u(34U);
static Json::Value in_b(true);
static Json::Value in_d(1.2);
static Json::Value big_sn("12345678901234567890");
static Json::Value big_si("-12345678901234567890");
static Json::Value big_u(0xFFFFFFFF);
static Json::Value bad_a(Json::arrayValue);
static Json::Value bad_o(Json::objectValue);

TEST(JsonTest, GetString) {
  std::string out;
  EXPECT_TRUE(GetStringFromJson(in_s, &out));
  EXPECT_EQ("foo", out);
  EXPECT_TRUE(GetStringFromJson(in_sn, &out));
  EXPECT_EQ("99", out);
  EXPECT_TRUE(GetStringFromJson(in_si, &out));
  EXPECT_EQ("-99", out);
  EXPECT_TRUE(GetStringFromJson(in_i, &out));
  EXPECT_EQ("-12", out);
  EXPECT_TRUE(GetStringFromJson(in_n, &out));
  EXPECT_EQ("12", out);
  EXPECT_TRUE(GetStringFromJson(in_u, &out));
  EXPECT_EQ("34", out);
  EXPECT_TRUE(GetStringFromJson(in_b, &out));
  EXPECT_EQ("true", out);
  // Not supported here yet.
  EXPECT_FALSE(GetStringFromJson(bad_a, &out));
  EXPECT_FALSE(GetStringFromJson(bad_o, &out));
}

TEST(JsonTest, GetInt) {
  int out;
  EXPECT_TRUE(GetIntFromJson(in_sn, &out));
  EXPECT_EQ(99, out);
  EXPECT_TRUE(GetIntFromJson(in_si, &out));
  EXPECT_EQ(-99, out);
  EXPECT_TRUE(GetIntFromJson(in_n, &out));
  EXPECT_EQ(12, out);
  EXPECT_TRUE(GetIntFromJson(in_i, &out));
  EXPECT_EQ(-12, out);
  EXPECT_TRUE(GetIntFromJson(in_u, &out));
  EXPECT_EQ(34, out);
  EXPECT_TRUE(GetIntFromJson(in_b, &out));
  EXPECT_EQ(1, out);
  EXPECT_FALSE(GetIntFromJson(in_s, &out));
  EXPECT_FALSE(GetIntFromJson(big_sn, &out));
  EXPECT_FALSE(GetIntFromJson(big_si, &out));
  EXPECT_FALSE(GetIntFromJson(big_u, &out));
  EXPECT_FALSE(GetIntFromJson(bad_a, &out));
  EXPECT_FALSE(GetIntFromJson(bad_o, &out));
}

TEST(JsonTest, GetUInt) {
  unsigned int out;
  EXPECT_TRUE(GetUIntFromJson(in_sn, &out));
  EXPECT_EQ(99U, out);
  EXPECT_TRUE(GetUIntFromJson(in_n, &out));
  EXPECT_EQ(12U, out);
  EXPECT_TRUE(GetUIntFromJson(in_u, &out));
  EXPECT_EQ(34U, out);
  EXPECT_TRUE(GetUIntFromJson(in_b, &out));
  EXPECT_EQ(1U, out);
  EXPECT_TRUE(GetUIntFromJson(big_u, &out));
  EXPECT_EQ(0xFFFFFFFFU, out);
  EXPECT_FALSE(GetUIntFromJson(in_s, &out));
  // TODO(bugs.webrtc.org/9804): Fail reading negative strings.
  // EXPECT_FALSE(GetUIntFromJson(in_si, &out));
  EXPECT_FALSE(GetUIntFromJson(in_i, &out));
  EXPECT_FALSE(GetUIntFromJson(big_sn, &out));
  EXPECT_FALSE(GetUIntFromJson(big_si, &out));
  EXPECT_FALSE(GetUIntFromJson(bad_a, &out));
  EXPECT_FALSE(GetUIntFromJson(bad_o, &out));
}

TEST(JsonTest, GetBool) {
  bool out;
  EXPECT_TRUE(GetBoolFromJson(in_sb, &out));
  EXPECT_EQ(true, out);
  EXPECT_TRUE(GetBoolFromJson(in_n, &out));
  EXPECT_EQ(true, out);
  EXPECT_TRUE(GetBoolFromJson(in_i, &out));
  EXPECT_EQ(true, out);
  EXPECT_TRUE(GetBoolFromJson(in_u, &out));
  EXPECT_EQ(true, out);
  EXPECT_TRUE(GetBoolFromJson(in_b, &out));
  EXPECT_EQ(true, out);
  EXPECT_TRUE(GetBoolFromJson(big_u, &out));
  EXPECT_EQ(true, out);
  EXPECT_FALSE(GetBoolFromJson(in_s, &out));
  EXPECT_FALSE(GetBoolFromJson(in_sn, &out));
  EXPECT_FALSE(GetBoolFromJson(in_si, &out));
  EXPECT_FALSE(GetBoolFromJson(big_sn, &out));
  EXPECT_FALSE(GetBoolFromJson(big_si, &out));
  EXPECT_FALSE(GetBoolFromJson(bad_a, &out));
  EXPECT_FALSE(GetBoolFromJson(bad_o, &out));
}

TEST(JsonTest, GetDouble) {
  double out;
  EXPECT_TRUE(GetDoubleFromJson(in_sn, &out));
  EXPECT_EQ(99, out);
  EXPECT_TRUE(GetDoubleFromJson(in_si, &out));
  EXPECT_EQ(-99, out);
  EXPECT_TRUE(GetDoubleFromJson(in_sd, &out));
  EXPECT_EQ(1.2, out);
  EXPECT_TRUE(GetDoubleFromJson(in_n, &out));
  EXPECT_EQ(12, out);
  EXPECT_TRUE(GetDoubleFromJson(in_i, &out));
  EXPECT_EQ(-12, out);
  EXPECT_TRUE(GetDoubleFromJson(in_u, &out));
  EXPECT_EQ(34, out);
  EXPECT_TRUE(GetDoubleFromJson(in_b, &out));
  EXPECT_EQ(1, out);
  EXPECT_TRUE(GetDoubleFromJson(in_d, &out));
  EXPECT_EQ(1.2, out);
  EXPECT_FALSE(GetDoubleFromJson(in_s, &out));
}

TEST(JsonTest, GetFromArray) {
  Json::Value a, out;
  a.append(in_s);
  a.append(in_i);
  a.append(in_u);
  a.append(in_b);
  EXPECT_TRUE(GetValueFromJsonArray(a, 0, &out));
  EXPECT_TRUE(GetValueFromJsonArray(a, 3, &out));
  EXPECT_FALSE(GetValueFromJsonArray(a, 99, &out));
  EXPECT_FALSE(GetValueFromJsonArray(a, 0xFFFFFFFF, &out));
}

TEST(JsonTest, GetFromObject) {
  Json::Value o, out;
  o["string"] = in_s;
  o["int"] = in_i;
  o["uint"] = in_u;
  o["bool"] = in_b;
  EXPECT_TRUE(GetValueFromJsonObject(o, "int", &out));
  EXPECT_TRUE(GetValueFromJsonObject(o, "bool", &out));
  EXPECT_FALSE(GetValueFromJsonObject(o, "foo", &out));
  EXPECT_FALSE(GetValueFromJsonObject(o, "", &out));
}

namespace {
template <typename T>
std::vector<T> VecOf3(const T& a, const T& b, const T& c) {
  std::vector<T> in;
  in.push_back(a);
  in.push_back(b);
  in.push_back(c);
  return in;
}
template <typename T>
Json::Value JsonVecOf3(const T& a, const T& b, const T& c) {
  Json::Value in(Json::arrayValue);
  in.append(a);
  in.append(b);
  in.append(c);
  return in;
}
}  // unnamed namespace

TEST(JsonTest, ValueVectorToFromArray) {
  std::vector<Json::Value> in = VecOf3<Json::Value>("a", "b", "c");
  Json::Value out = ValueVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i].asString(), out[i].asString());
  }
  Json::Value inj = JsonVecOf3<Json::Value>("a", "b", "c");
  EXPECT_EQ(inj, out);
  std::vector<Json::Value> outj;
  EXPECT_TRUE(JsonArrayToValueVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

TEST(JsonTest, IntVectorToFromArray) {
  std::vector<int> in = VecOf3<int>(1, 2, 3);
  Json::Value out = IntVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i], out[i].asInt());
  }
  Json::Value inj = JsonVecOf3<int>(1, 2, 3);
  EXPECT_EQ(inj, out);
  std::vector<int> outj;
  EXPECT_TRUE(JsonArrayToIntVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

TEST(JsonTest, UIntVectorToFromArray) {
  std::vector<unsigned int> in = VecOf3<unsigned int>(1, 2, 3);
  Json::Value out = UIntVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i], out[i].asUInt());
  }
  Json::Value inj = JsonVecOf3<unsigned int>(1, 2, 3);
  EXPECT_EQ(inj, out);
  std::vector<unsigned int> outj;
  EXPECT_TRUE(JsonArrayToUIntVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

TEST(JsonTest, StringVectorToFromArray) {
  std::vector<std::string> in = VecOf3<std::string>("a", "b", "c");
  Json::Value out = StringVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i], out[i].asString());
  }
  Json::Value inj = JsonVecOf3<std::string>("a", "b", "c");
  EXPECT_EQ(inj, out);
  std::vector<std::string> outj;
  EXPECT_TRUE(JsonArrayToStringVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

TEST(JsonTest, BoolVectorToFromArray) {
  std::vector<bool> in = VecOf3<bool>(false, true, false);
  Json::Value out = BoolVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i], out[i].asBool());
  }
  Json::Value inj = JsonVecOf3<bool>(false, true, false);
  EXPECT_EQ(inj, out);
  std::vector<bool> outj;
  EXPECT_TRUE(JsonArrayToBoolVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

TEST(JsonTest, DoubleVectorToFromArray) {
  std::vector<double> in = VecOf3<double>(1.0, 2.0, 3.0);
  Json::Value out = DoubleVectorToJsonArray(in);
  EXPECT_EQ(in.size(), out.size());
  for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
    EXPECT_EQ(in[i], out[i].asDouble());
  }
  Json::Value inj = JsonVecOf3<double>(1.0, 2.0, 3.0);
  EXPECT_EQ(inj, out);
  std::vector<double> outj;
  EXPECT_TRUE(JsonArrayToDoubleVector(inj, &outj));
  for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
    EXPECT_EQ(in[i], outj[i]);
  }
}

}  // namespace rtc