-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
executable file
·83 lines (70 loc) · 2.04 KB
/
test.cpp
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
#include "PTree.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
struct Car {
string color;
unsigned int price;
};
int main() {
PTree<string> pt;
// normal get
string name("Alice");
pt.put("person.name", name);
string nameOut = pt.get("person.name"); // assignment syntax
assert(name == nameOut);
string nameEx(pt.get<string>("person.name")); // explicit syntax, need convert explicitly
assert(name == nameEx);
assert(pt.isValidPath("person.name"));
// const get
const PTree<string>& cpt = pt;
string nameOutC = cpt.get("person.name");
assert(name == nameOutC);
string nameExC;
nameExC = cpt.get<string>("person.name");
assert(name == nameExC);
const string& nameRefC = cpt.get("person.name");
assert(name == nameRefC);
// ref get
string& nameRef = pt.get("person.name");
nameRef = "Bob";
nameOut = pt.get<string>("person.name");
assert(nameRef == nameOut);
// other types
int age = 25;
pt.put("person.age", age);
int ageOut(pt.get("person.age"));
assert(age == ageOut);
float height = 1.65;
pt.put("person.height", height);
float heightOut(pt.get("person.height"));
assert(height == heightOut);
Car car;
car.color = "black";
car.price = 50000;
pt.put("person.car", car);
Car carOut(pt.get("person.car"));
assert(car.color == carOut.color);
assert(car.price == carOut.price);
// push as vector
int income = 100;
pt.push("person.deposit", income++);
pt.push("person.deposit", income++);
pt.push("person.deposit", income++);
const std::vector<int>& sum = pt.get("person.deposit");
assert(sum.size() == 3);
// del
string error;
pt.del("person.name");
try {
pt.get("person.name");
} catch (const PTreeError& e) {
error = e.what();
cout << "catched PTreeError: " << error << endl;
}
assert(!error.empty());
assert(!pt.isValidPath("person.name"));
cout << "all test passed" << endl;
return 0;
}