Line data Source code
1 : /*
2 : * File: TestProperties.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on Mar 5, 2016, 4:53:48 PM
6 : *
7 : * ForBES is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * ForBES is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU Lesser General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "TestProperties.h"
22 :
23 :
24 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestProperties);
25 :
26 4 : TestProperties::TestProperties() {
27 4 : }
28 :
29 8 : TestProperties::~TestProperties() {
30 8 : }
31 :
32 4 : void TestProperties::setUp() {
33 4 : }
34 :
35 4 : void TestProperties::tearDown() {
36 4 : }
37 :
38 1 : void TestProperties::testDouble() {
39 1 : Properties stats;
40 :
41 1 : const double first_value = 34.59210;
42 1 : const double second_value = -52.132;
43 1 : double *a = new double;
44 1 : *a = first_value;
45 :
46 : /* Add a value to Properties under `key` */
47 2 : std::string key = "key-1";
48 1 : bool status = stats.add_property<double>(key, Properties::DOUBLE_TYPE, a);
49 1 : _ASSERT(status);
50 :
51 : /* Retrieve the value under `key` */
52 : int type; // type
53 : void * value; // actual value
54 1 : status = stats.get_typed_property(key, type, value);
55 1 : _ASSERT(status); // make sure the key-value-pair exists
56 1 : _ASSERT_EQ(Properties::DOUBLE_TYPE, type); // make sure the retrieved value is of type double
57 :
58 1 : double * dbl_val = static_cast<double*> (value); // cast as double*
59 1 : _ASSERT_EQ(a, dbl_val);
60 1 : _ASSERT_EQ(first_value, *dbl_val);
61 1 : _ASSERT_EQ(first_value, *a);
62 :
63 : /* Now add again the same key (update) */
64 1 : *a = second_value;
65 1 : status = stats.add_property<double>(key, Properties::DOUBLE_TYPE, a);
66 1 : _ASSERT_NOT(status);
67 :
68 1 : status = stats.get_typed_property(key, type, value);
69 1 : dbl_val = static_cast<double*> (value); // cast as double*
70 1 : _ASSERT(status); // make sure the key-value-pair exists
71 1 : _ASSERT_EQ(Properties::DOUBLE_TYPE, type); // make sure the retrieved value is of type double
72 1 : _ASSERT_EQ(second_value, *dbl_val);
73 :
74 2 : delete a;
75 1 : }
76 :
77 1 : void TestProperties::testInteger() {
78 1 : Properties * stats = new Properties();
79 1 : int i = 365;
80 1 : bool how = stats->add_property<int>("i", Properties::INT_TYPE, &i);
81 1 : _ASSERT(how);
82 1 : how = stats->add_property<int>("i", Properties::INT_TYPE, &i);
83 1 : _ASSERT_NOT(how);
84 :
85 : int * i_retrieved;
86 1 : how = stats->get_property<>("i", i_retrieved);
87 1 : _ASSERT(how);
88 1 : _ASSERT_NEQ(NULL, i_retrieved);
89 1 : _ASSERT_EQ(i_retrieved, &i);
90 1 : _ASSERT_EQ(*i_retrieved, i);
91 :
92 1 : how = stats->get_property<>("asdf-qwerty", i_retrieved);
93 1 : _ASSERT_NOT(how);
94 :
95 1 : delete stats;
96 1 : }
97 :
98 1 : void TestProperties::testNotExisting() {
99 1 : Properties * stats = new Properties();
100 :
101 1 : double a = 345.678;
102 1 : stats->add_property<>("a", Properties::DOUBLE_TYPE, &a);
103 :
104 1 : size_t q = 122;
105 1 : stats->add_property("q", Properties::SIZE_T_TYPE, &q);
106 :
107 1 : unsigned long s = 4564564;
108 1 : stats->add_property("s", Properties::CUSTOM_TYPE, &s);
109 :
110 1 : void * val = NULL;
111 1 : bool found = stats->get_property("chung", val);
112 1 : _ASSERT_NOT(found);
113 1 : _ASSERT_EQ(static_cast<void*> (NULL), val);
114 :
115 1 : _ASSERT_EQ(static_cast<size_t> (3), stats->size());
116 :
117 :
118 1 : delete stats;
119 1 : }
120 :
121 1 : void TestProperties::testVector() {
122 1 : Properties * stats = new Properties();
123 :
124 1 : std::vector<double> vec;
125 1 : vec.push_back(1.23);
126 1 : vec.push_back(2.34);
127 1 : vec.push_back(3.45);
128 :
129 2 : string key = "key-vec-1";
130 :
131 1 : stats->add_property<std::vector<double> >(key, Properties::VECTOR_TYPE, &vec);
132 :
133 1 : std::vector<double> * found = NULL;
134 1 : bool found_status = stats->get_property<std::vector<double> >(key, found);
135 1 : _ASSERT(found_status);
136 1 : _ASSERT_NEQ(NULL, found);
137 4 : for (size_t i = 0; i < vec.size(); i++) {
138 3 : _ASSERT_EQ(vec[i], found->at(i));
139 : }
140 :
141 :
142 : /* here, let's assume we don't know the type of the value of `key` */
143 1 : int type_found = 0;
144 : void * obj;
145 1 : found_status = stats->get_typed_property(key, type_found, obj);
146 1 : _ASSERT(found_status);
147 1 : _ASSERT_EQ(Properties::VECTOR_TYPE, type_found);
148 1 : _ASSERT_NEQ(NULL, found);
149 :
150 1 : std::vector<double>* obj_as_vec = static_cast<std::vector<double>*>(obj);
151 1 : obj_as_vec->push_back(10.34);
152 1 : obj_as_vec->push_back(11.01);
153 1 : obj_as_vec->push_back(-8.75);
154 :
155 1 : _ASSERT_EQ(static_cast<size_t>(6), vec.size());
156 1 : _ASSERT_EQ(-8.75, vec[5]);
157 :
158 2 : delete stats;
159 4 : }
160 :
161 :
162 :
163 :
|