RobotsIO
Loading...
Searching...
No Matches
Utils/Parameters/main.cpp
1/*
2 * Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Utils/Parameters.h>
9#include <RobotsIO/Utils/ParametersExtractor.h>
10
11#include <TestParameters.h>
12
13#include <cstdlib>
14#include <cmath>
15#include <iostream>
16#include <string>
17
19{
20public:
23 { }
24
25 bool test()
26 {
27 /* Perform extraction */
28 extract_fields();
29
30 /* Check extraction correctness */
31 bool ok = true;
32 if (field0_ != -1)
33 {
34 std::cerr << "Expected field_0 to be -1, instead it is " << field0_ << std::endl;
35 ok = false;
36 }
37 if (field1_ != M_PI)
38 {
39 std::cerr << "Expected field_1 to be M_PI, instead it is " << field1_ << std::endl;
40 ok = false;
41 }
42 if (field2_ != true)
43 {
44 std::cerr << "Expected field_2 to be true, instead it is " << (field2_ ? "true" : "false") << std::endl;
45 ok = false;
46 }
47 if (field3_ != "This is a string.")
48 {
49 std::cerr << "Expected field_3 to be 'This is a string.', instead it is " << field3_ << std::endl;
50 ok = false;
51 }
52 if (field4_ != 1)
53 {
54 std::cerr << "Expected field_4 to be 1, instead it is " << field4_ << std::endl;
55 ok = false;
56 }
57
58 return ok;
59 }
60
61 void extract_field(const std::string& key, const int& value) override
62 {
63 field0_ = value;
64 }
65
66 void extract_field(const std::string& key, const double& value) override
67 {
68 field1_ = value;
69 }
70
71 void extract_field(const std::string& key, const bool& value) override
72 {
73 field2_ = value;
74 }
75
76 void extract_field(const std::string& key, const std::string& value) override
77 {
78 field3_ = value;
79 }
80
81 void extract_field(const std::string& key, const std::size_t& value) override
82 {
83 field4_ = value;
84 }
85
86private:
87 int field0_;
88
89 double field1_;
90
91 bool field2_;
92
93 std::string field3_;
94
95 std::size_t field4_;
96};
97
98
99int main(int argc, char** argv)
100{
101 TestParameters parameters;
102
103 std::cout << "Setting parameters." << std::endl;
104 parameters.field0(-1);
105 parameters.field1(M_PI);
106 parameters.field2(true);
107 parameters.field3("This is a string.");
108 parameters.field4(1);
109
110 std::cout << "Testing parameters using named accessors." << std::endl;
111 if (parameters.field0() != -1)
112 {
113 std::cerr << "Expected field_0 to be -1, instead it is " << parameters.field0() << std::endl;
114 return EXIT_FAILURE;
115 }
116 if (parameters.field1() != M_PI)
117 {
118 std::cerr << "Expected field_1 to be M_PI, instead it is " << parameters.field1() << std::endl;
119 return EXIT_FAILURE;
120 }
121 if (parameters.field2() != true)
122 {
123 std::cerr << "Expected field_2 to be true, instead it is " << (parameters.field2() ? "true" : "false") << std::endl;
124 return EXIT_FAILURE;
125 }
126 if (parameters.field3() != "This is a string.")
127 {
128 std::cerr << "Expected field_3 to be 'This is a string.', instead it is " << parameters.field3() << std::endl;
129 return EXIT_FAILURE;
130 }
131 if (parameters.field4() != 1)
132 {
133 std::cerr << "Expected field_4 to be 1, instead it is " << parameters.field4() << std::endl;
134 return EXIT_FAILURE;
135 }
136
137 std::cout << "Testing parameters using extractor." << std::endl;
138 ExtractionTest extraction_test(parameters);
139 if (!(extraction_test.test()))
140 return EXIT_FAILURE;
141
142 return EXIT_SUCCESS;
143}