Menu

[r1911]: / framework / trunk / tests / json_test.cpp  Maximize  Restore  History

Download this file

224 lines (204 with data), 7.0 kB

  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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include <cppcms/json.h>
#include "test.h"
#include <iostream>
#include <limits>
#include <stdlib.h>
#include <sstream>
#include <iomanip>
using namespace cppcms;
using namespace std;
char const *jsn_str=
"{ \"t\" : [{},{},{},{ \"x\":1},[]],\"x\" : { \"o\" : { \"test\" : [ 10,20,true ], \"post\" : 13.01 }, \"yes\" : \"\\u05d0א\" }}";
class parsing_error : public std::runtime_error {
public:
parsing_error(std::string s) : std::runtime_error(s) {}
};
#define THROWS(X) do{ try { X; }catch(cppcms::json::bad_value_cast const &e){}\
catch(parsing_error const &e){}\
catch(...){\
std::ostringstream tmp; \
tmp << __FILE__ << " " << __LINE__ << " "#X " not throwed"; \
throw std::runtime_error(tmp.str()); } }while(0)
json::value Parse(std::string s,int line)
{
std::istringstream ss(s);
json::value v;
if(!v.load(ss,true)) {
std::ostringstream tmp;
tmp << "Parsing error of " << s << " in line " << line;
throw parsing_error(tmp.str());
}
return v;
}
std::string format(json::value const &v)
{
std::ostringstream ss;
ss<<v;
return ss.str();
}
#define parse(X) Parse(X,__LINE__)
int main()
{
try {
json::value v;
json::value const &vc=v;
TEST(v.type()==json::is_undefined);
TEST(v.is_undefined());
v=10;
TEST(v.type()==json::is_number);
TEST(v.number()==10);
TEST(v.get_value<int>()==10);
TEST(v.get_value<double>()==10);
THROWS(v.get_value<std::string>());
v="test";
TEST(v.type()==json::is_string);
TEST(v.str()=="test");
v=true;
TEST(v.type()==json::is_boolean);
TEST(v.boolean()==true);
v=json::null();
TEST(v.is_null());
TEST(v.type()==json::is_null);
v=json::array();
TEST(v.type()==json::is_array);
v=json::object();
TEST(v.type()==json::is_object);
TEST(v.find("x")==json::value());
TEST(v.type("x")==json::is_undefined);
TEST(v.get<std::string>("x","y")=="y");
THROWS(v.get<std::string>("x"));
THROWS(v.at("x"));
v["x"]=10;
TEST(v.find("x")==json::value(10));
TEST(v.at("x")==json::value(10));
TEST(v.type("x")==json::is_number);
TEST(v.get<std::string>("x","y")=="y");
THROWS(v.get<std::string>("x"));
v.set("x.y.z",10);
TEST(v["x"]["y"]["z"].number()==10);
TEST(v.get<int>("x.y.z")==10);
TEST(parse("[]")==json::array());
TEST(parse("{}")==json::object());
TEST(parse("true")==json::value(true));
TEST(parse("false")==json::value(false));
TEST(parse("10")==json::value(10));
TEST(parse("\"hello\"")==json::value("hello"));
TEST(parse("null")==json::null());
char const *s=
"{ \"t\" : [{},{},{},{ \"x\":1},[]],\"x\" : { \"o\" : { \"test\" : [ 10,20,true ], \"post\" : 13.01 }, \"yes\" : \"\\u05d0א\" }}";
v=parse(s);
TEST(v.type("t")==json::is_array);
TEST(v["t"].array().size()==5);
TEST(v["t"][0]==json::object());
TEST(v["t"][1]==json::object());
TEST(v["t"][2]==json::object());
TEST(v["t"][4]==json::array());
TEST(v["t"][3]["x"].number()==1);
TEST(v.type("x")==json::is_object);
TEST(v.get<std::string>("x.yes")=="אא");
// Test correct handing of surrogates
THROWS(parse("\"\\ud834\""));
THROWS(parse("\"\\udd1e\""));
THROWS(parse("\"\\ud834 \\udd1e\""));
TEST(parse("\"\\u05d0\\ud834\\udd1e x\"")==\xf0\x9d\x84\x9e x");
THROWS(parse("\"\xFF\xFF\"")); // Invalid UTF-8
TEST(parse("\"\\u05d0 x\"")=="א x"); // Correct read of 4 bytes
THROWS(parse("\"\\u05dx x\"")); // Correct read of 4 bytes
TEST(parse("[//Hello\n]")==json::array());
TEST(format("test")=="\"test\"");
TEST(format(10)=="10");
TEST(format(true)=="true");
TEST(format(false)=="false");
TEST(format(json::null())=="null");
TEST(format(json::object())=="{}");
TEST(format(json::array())=="[]");
v=json::value();
v["x"]="yes";
TEST(vc["x"].str()=="yes");
THROWS(vc["y"]);
THROWS(vc[1]);
v[2]="yes";
TEST(v[0]==json::null());
TEST(v[1]==json::null());
TEST(vc[2].str()=="yes");
TEST(v[2]=="yes");
TEST(v[3]==json::null());
THROWS(vc[4]);
THROWS(vc["x"]);
v=json::value();
v["x"]=10;
v["y"][1]="test";
v["y"][2]=json::array();
TEST(format(v)=="{\"x\":10,\"y\":[null,\"test\",[]]}");
std::string fl="123456789123456789123456789123456789";
fl = fl.substr(0,std::numeric_limits<double>::digits10);
double big_int=atof(fl.c_str());
TEST(format(big_int)==fl);
TEST(format(1277880000)=="1277880000");
TEST(format(-1277880000)=="-1277880000");
TEST(atoi(format(std::numeric_limits<int>::max()).c_str()) == std::numeric_limits<int>::max());
TEST(atoi(format(std::numeric_limits<int>::min()).c_str()) == std::numeric_limits<int>::min());
std::string fl2=fl.substr(0,1)+'.'+fl.substr(1);
big_int=atof(fl2.c_str());
TEST(format(big_int)==fl2);
TEST(format(-big_int)=="-" + fl2);
{
std::string tmp;
tmp = format(1.35e30);
TEST(tmp.substr(0,5)=="1.35e");
TEST(tmp.substr(tmp.size()-2)=="30");
tmp = format(big_int * 1e30);
TEST(tmp.substr(0,fl2.size()+1) == fl2+"e");
TEST(tmp.substr(tmp.size()-2)=="30");
}
v["x"]=10000000;
THROWS(v.get<short>("x"));
TEST(v.get<int>("x")==10000000);
v["x"]=-1;
THROWS(v.get<unsigned>("x"));
THROWS(v.get<unsigned short>("x"));
THROWS(v.get<unsigned long >("x"));
TEST(v.get<int>("x")==-1);
TEST(v.get<short>("x")==-1);
TEST(v.get<long>("x")==-1);
if(sizeof(long long) >= sizeof(double)) {
THROWS(v["x"]=std::numeric_limits<long long>::max());
}
if(std::numeric_limits<double>::max() != std::numeric_limits<float>::max()) {
double val = std::numeric_limits<double>::max() / 100;
v["x"]=val;
THROWS(v.get<float>("x"));
TEST(v.get<double>("x")==val);
TEST(v.get<long double>("x")==val);
}
if(std::numeric_limits<long double>::max() != std::numeric_limits<double>::max()) {
long double val = std::numeric_limits<long double>::max() / 100;
THROWS(v["x"]=val);
}
}
catch(std::exception const &e)
{
cerr<<"Failed:"<<e.what()<<endl;
return 1;
}
std::cout << "Passed" << std::endl;
return 0;
}
MongoDB Logo MongoDB