]> granicus.if.org Git - icinga2/blob - test/base-array.cpp
Implement support for performance data unit prefixes.
[icinga2] / test / base-array.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/array.h"
21 #include "base/objectlock.h"
22 #include <boost/test/unit_test.hpp>
23 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26
27 BOOST_AUTO_TEST_SUITE(base_array)
28
29 BOOST_AUTO_TEST_CASE(construct)
30 {
31         Array::Ptr array = make_shared<Array>();
32         BOOST_CHECK(array);
33         BOOST_CHECK(array->GetLength() == 0);
34 }
35
36 BOOST_AUTO_TEST_CASE(getset)
37 {
38         Array::Ptr array = make_shared<Array>();
39         array->Add(7);
40         array->Add(2);
41         array->Add(5);
42         BOOST_CHECK(array->GetLength() == 3);
43         BOOST_CHECK(array->Get(0) == 7);
44         BOOST_CHECK(array->Get(1) == 2);
45         BOOST_CHECK(array->Get(2) == 5);
46
47         array->Set(1, 9);
48         BOOST_CHECK(array->Get(1) == 9);
49
50         array->Remove(1);
51         BOOST_CHECK(array->GetLength() == 2);
52         BOOST_CHECK(array->Get(1) == 5);
53 }
54
55 BOOST_AUTO_TEST_CASE(remove)
56 {
57         Array::Ptr array = make_shared<Array>();
58         array->Add(7);
59         array->Add(2);
60         array->Add(5);
61
62         {
63                 ObjectLock olock(array);
64                 Array::Iterator it = array->Begin();
65                 array->Remove(it);
66         }
67
68         BOOST_CHECK(array->GetLength() == 2);
69         BOOST_CHECK(array->Get(0) == 2);
70 }
71
72 BOOST_AUTO_TEST_CASE(foreach)
73 {
74         Array::Ptr array = make_shared<Array>();
75         array->Add(7);
76         array->Add(2);
77         array->Add(5);
78
79         ObjectLock olock(array);
80
81         int n = 0;
82
83         BOOST_FOREACH(const Value& item, array) {
84                 BOOST_CHECK(n != 0 || item == 7);
85                 BOOST_CHECK(n != 1 || item == 2);
86                 BOOST_CHECK(n != 2 || item == 5);
87
88                 n++;
89         }
90 }
91
92 BOOST_AUTO_TEST_CASE(clone)
93 {
94         Array::Ptr array = make_shared<Array>();
95         array->Add(7);
96         array->Add(2);
97         array->Add(5);
98
99         Array::Ptr clone = array->ShallowClone();
100
101         BOOST_CHECK(clone->GetLength() == 3);
102         BOOST_CHECK(clone->Get(0) == 7);
103         BOOST_CHECK(clone->Get(1) == 2);
104         BOOST_CHECK(clone->Get(2) == 5);
105 }
106
107 BOOST_AUTO_TEST_CASE(serialize)
108 {
109         Array::Ptr array = make_shared<Array>();
110         array->Add(7);
111         array->Add(2);
112         array->Add(5);
113
114         String json = Value(array).Serialize();
115         BOOST_CHECK(json.GetLength() > 0);
116
117         Array::Ptr deserialized = Value::Deserialize(json);
118         BOOST_CHECK(deserialized);
119         BOOST_CHECK(deserialized->GetLength() == 3);
120         BOOST_CHECK(deserialized->Get(0) == 7);
121         BOOST_CHECK(deserialized->Get(1) == 2);
122         BOOST_CHECK(deserialized->Get(2) == 5);
123 }
124
125 BOOST_AUTO_TEST_SUITE_END()