Functions
test_configuration.cxx File Reference
#include "WireCellUtil/Configuration.h"
#include "WireCellUtil/Persist.h"
#include "WireCellUtil/Testing.h"
#include <iostream>

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

int main ( void  )

Definition at line 10 of file test_configuration.cxx.

11 {
12  string json = R"(
13 {
14 "my_int": 1,
15 "my_float": 6.9,
16 "my_string": "hello",
17 "my_struct": { "x": 1, "y": 2, "z": 3 },
18 "my_array" : [ "one", "two", "three" ],
19 "my_array_dict" : [ {"a":1, "b":2}, {"a":10, "b":20}],
20 "data1" : { "data2": { "a":1, "b":2, "c":3 } }
21 }
22 )";
23  string extra_json = R"(
24 {
25 "my_int" : 2,
26 "data1" : { "data2": { "d":4 } },
27 "data3": { "data4" : 4 }
28 }
29 )";
30 
31 
33  Configuration extra_cfg = Persist::loads(extra_json);
34 
35  Assert(get(cfg,"my_int",0) == 1);
36  Assert(get(cfg,"my_float",0.0) == 6.9);
37 
38  Assert(string("hello") == convert<std::string>(cfg["my_string"]));
39 
40  cerr << "my_string=" << get(cfg,"my_string",string(""))<< endl;
41  Assert(get(cfg,"my_string",string("")) == string("hello"));
42 
43 
44  put(cfg, "a.b.c", 42);
45  cerr << cfg << endl;
46  int n42get = get(cfg, "a.b.c", 0);
47  Assert(42 == n42get);
48  int n42ind = cfg["a"]["b"]["c"].asInt();
49  Assert(42 == n42ind);
50 
51  Assert(get<int>(cfg,"my_struct.x") == 1);
52 
53  auto nums = get< vector<string> >(cfg, "my_array");
54  for (auto anum : nums) {
55  cerr << anum << endl;
56  }
57 
58  Configuration a = branch(cfg, "data1.data2.a");
59  Assert(convert<int>(a) == 1);
60 
62  update(other, cfg);
63  Configuration last = update(other, extra_cfg);
64  cerr << "other:\n" << other << endl;
65  cerr << "last:\n" << last << endl;
66  Assert(last["a"]["b"]["c"] == 42);
67  Assert(last["data3"]["data4"] == 4);
68 
69  return 0;
70 }
Configuration update(Configuration &a, Configuration &b)
Merge dictionary b into a, return a.
std::string string
Definition: nybbler.cc:12
void put(Configuration &cfg, const std::string &dotpath, const T &val)
Put value in configuration at the dotted path.
cfg
Definition: dbjson.py:29
#define Assert
Definition: Testing.h:7
Configuration branch(Configuration cfg, const std::string &dotpath)
Follow a dot.separated.path and return the branch there.
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
Json::Value Configuration
Definition: Configuration.h:50
int convert< int >(const Configuration &cfg, const int &def)
Definition: Configuration.h:74
QTextStream & endl(QTextStream &s)