CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
jsonParser_test.cpp
Go to the documentation of this file.
1 #define BOOST_TEST_DYN_LINK
2 #include <boost/test/unit_test.hpp>
3 
6 
8 #include <boost/filesystem.hpp>
9 
10 using namespace CASM;
11 
12 std::string json_str =
13  R"({
14 "int" : 34,
15 "number" : 4.0023,
16 "string" : "hello",
17 "bool_true" : true,
18 "bool_false" : false,
19 "object" : {
20 "int" : 34,
21 "number" : 4.0023,
22 "string" : "hello",
23 "bool_true" : true,
24 "bool_false" : false
25 },
26 "uniform_array" : [1, 2, 3, 4],
27 "mixed_array" : [
28 "hello",
29 34,
30 4.0023,
31 {"int" : 34, "number" : 4.0023}
32 ]
33 })";
34 
35 
36 BOOST_AUTO_TEST_SUITE(jsonParserTest)
37 
39 
41 
42  BOOST_CHECK_EQUAL(true, json.is_obj());
43 
44  // test jsonParser::get<T>()
45  int i;
46  from_json(i, json["int"]);
47  BOOST_CHECK_EQUAL(i, 34);
48  BOOST_CHECK_EQUAL(34, json["int"].get<int>());
49 
50  double d;
51  from_json(d, json["number"]);
52  BOOST_CHECK_EQUAL(d, 4.0023);
53  BOOST_CHECK_EQUAL(4.0023, json["number"].get<double>());
54 }
55 
56 BOOST_AUTO_TEST_CASE(ArrayExtraTrailingComma) {
57 
58  std::string json_extra_trailing_comma =
59  R"({
60 "int" : 34,
61 "number" : 4.0023,
62 "string" : "hello",
63 "bool_true" : true,
64 "bool_false" : false,
65 "object" : {
66 "int" : 34,
67 "number" : 4.0023,
68 "string" : "hello",
69 "bool_true" : true,
70 "bool_false" : false
71 },
72 "uniform_array" : [1, 2, 3, 4,],
73 "mixed_array" : [
74 "hello",
75 34,
76 4.0023,
77 {"int" : 34, "number" : 4.0023}
78 ]
79 })";
80 
81  jsonParser json;
82 
83  json.read(json_extra_trailing_comma);
84 
85  BOOST_CHECK_EQUAL(json.read(json_extra_trailing_comma), false);
86 
87 
88  BOOST_CHECK_THROW(jsonParser::parse(json_extra_trailing_comma), std::runtime_error);
89 
90 }
91 
92 BOOST_AUTO_TEST_CASE(ArrayMissingComma) {
93 
94  std::string json_missing_comma =
95  R"({
96 "int" : 34,
97 "number" : 4.0023,
98 "string" : "hello",
99 "bool_true" : true,
100 "bool_false" : false,
101 "object" : {
102 "int" : 34,
103 "number" : 4.0023,
104 "string" : "hello",
105 "bool_true" : true,
106 "bool_false" : false
107 },
108 "uniform_array" : [1, 2 3, 4],
109 "mixed_array" : [
110 "hello",
111 34,
112 4.0023,
113 {"int" : 34, "number" : 4.0023}
114 ]
115 })";
116 
117  jsonParser json;
118 
119  json.read(json_missing_comma);
120 
121  BOOST_CHECK_EQUAL(json.read(json_missing_comma), false);
122 
123 
124  BOOST_CHECK_THROW(jsonParser::parse(json_missing_comma), std::runtime_error);
125 
126 }
127 
128 BOOST_AUTO_TEST_CASE(FindDiffTest) {
129 
131  jsonParser B {A};
132 
133  B["object"]["number"] = B["object"]["number"].get<double>() + 1e-8;
134 
135  BOOST_CHECK(A != B);
136  BOOST_CHECK(A.almost_equal(B, 1e-5));
137 
138  fs::path diff_point = find_diff(A, B);
139  BOOST_CHECK_EQUAL(diff_point, fs::path("object/number"));
140  BOOST_CHECK_EQUAL(A.at(diff_point), 4.0023);
141 
142  diff_point = find_diff(A, B, 1e-5);
143  BOOST_CHECK(diff_point.empty());
144 
145 }
146 
147 BOOST_AUTO_TEST_SUITE_END()
148 
149 //#include "../src/casm/casm_io/jsonParser.cc"
void from_json(ClexDescription &desc, const jsonParser &json)
boost::filesystem::path find_diff(const jsonParser &A, const jsonParser &B, boost::filesystem::path diff=boost::filesystem::path())
Return the location at which jsonParser 'A' != 'B' as a boost::filesystem::path.
Definition: jsonParser.cc:380
jsonParser & at(const fs::path &path)
Definition: jsonParser.cc:321
bool read(std::istream &stream)
Reads json from the stream.
Definition: jsonParser.cc:165
Main CASM namespace.
Definition: complete.cpp:8
BOOST_AUTO_TEST_CASE(Basic)
T get(Args...args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:729
bool is_obj() const
Check if object type.
Definition: jsonParser.cc:276
std::string json_str
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
Definition: jsonParser.hh:312
bool almost_equal(const jsonParser &B, double tol) const
Definition: jsonParser.cc:211