-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_boost_json.cpp
More file actions
73 lines (62 loc) · 1.74 KB
/
test_boost_json.cpp
File metadata and controls
73 lines (62 loc) · 1.74 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
#include <boost/format.hpp>
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <fstream>
#include <iostream>
#include <libnotify.hpp>
#include <sstream>
#include <string>
#include <unistd.h>
using namespace std;
struct coordinate_t {
double x;
double y;
double z;
auto operator<=>(const coordinate_t&) const = default;
friend ostream& operator<< (ostream &out, const coordinate_t &point) {
out << "coordinate_t {x: " << point.x
<< ", y: " << point.y
<< ", z: " << point.z << "}";
return out;
}
};
string read_file(const string& filename) {
ifstream f(filename);
if (!f) {
return {};
}
return string(istreambuf_iterator<char>(f),
istreambuf_iterator<char>());
}
coordinate_t calc(const string& text) {
auto x = 0.0, y = 0.0, z = 0.0;
auto len = 0;
auto mr = boost::json::monotonic_resource();
auto jv = boost::json::parse(text, &mr);
auto &obj = jv.get_object();
for (auto& v: obj.at("coordinates").get_array()) {
len += 1;
auto& coord = v.get_object();
x += coord.at("x").get_double();
y += coord.at("y").get_double();
z += coord.at("z").get_double();
}
return coordinate_t(x / len, y / len, z / len);
}
int main() {
auto right = coordinate_t(2.0, 0.5, 0.25);
for (auto v : {
"{\"coordinates\":[{\"x\":2.0,\"y\":0.5,\"z\":0.25}]}",
"{\"coordinates\":[{\"y\":0.5,\"x\":2.0,\"z\":0.25}]}"}) {
auto left = calc(v);
if (left != right) {
cerr << left << " != " << right << endl;
exit(EXIT_FAILURE);
}
}
const auto& text = read_file("/tmp/1.json");
notify(str(boost::format("C++/g++ (Boost.JSON)\t%d") % getpid()));
const auto& results = calc(text);
notify("stop");
cout << results << endl;
}