]> granicus.if.org Git - icinga2/blob - test/base-array.cpp
Improve test coverage for the array class
[icinga2] / test / base-array.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/json.hpp"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/foreach.hpp>
25
26 using namespace icinga;
27
28 BOOST_AUTO_TEST_SUITE(base_array)
29
30 BOOST_AUTO_TEST_CASE(construct)
31 {
32         Array::Ptr array = new Array();
33         BOOST_CHECK(array);
34         BOOST_CHECK(array->GetLength() == 0);
35 }
36
37 BOOST_AUTO_TEST_CASE(getset)
38 {
39         Array::Ptr array = new Array();
40         array->Add(7);
41         array->Add(2);
42         array->Add(5);
43         BOOST_CHECK(array->GetLength() == 3);
44         BOOST_CHECK(array->Get(0) == 7);
45         BOOST_CHECK(array->Get(1) == 2);
46         BOOST_CHECK(array->Get(2) == 5);
47
48         array->Set(1, 9);
49         BOOST_CHECK(array->Get(1) == 9);
50
51         array->Remove(1);
52         BOOST_CHECK(array->GetLength() == 2);
53         BOOST_CHECK(array->Get(1) == 5);
54 }
55
56 BOOST_AUTO_TEST_CASE(resize)
57 {
58         Array::Ptr array = new Array();
59         array->Resize(2);
60         BOOST_CHECK(array->GetLength() == 2);
61         BOOST_CHECK(array->Get(0) == Empty);
62         BOOST_CHECK(array->Get(1) == Empty);
63 }
64
65 BOOST_AUTO_TEST_CASE(insert)
66 {
67         Array::Ptr array = new Array();
68
69         array->Insert(0, 11);
70         array->Insert(1, 22);
71         BOOST_CHECK(array->GetLength() == 2);
72         BOOST_CHECK(array->Get(1) == 22);
73
74         array->Insert(0, 33);
75         BOOST_CHECK(array->GetLength() == 3);
76         BOOST_CHECK(array->Get(0) == 33);
77         BOOST_CHECK(array->Get(1) == 11);
78
79         array->Insert(1, 44);
80         BOOST_CHECK(array->GetLength() == 4);
81         BOOST_CHECK(array->Get(0) == 33);
82         BOOST_CHECK(array->Get(1) == 44);
83         BOOST_CHECK(array->Get(2) == 11);
84 }
85
86 BOOST_AUTO_TEST_CASE(remove)
87 {
88         Array::Ptr array = new Array();
89         array->Add(7);
90         array->Add(2);
91         array->Add(5);
92
93         {
94                 ObjectLock olock(array);
95                 Array::Iterator it = array->Begin();
96                 array->Remove(it);
97         }
98
99         BOOST_CHECK(array->GetLength() == 2);
100         BOOST_CHECK(array->Get(0) == 2);
101
102         array->Clear();
103         BOOST_CHECK(array->GetLength() == 0);
104 }
105
106 BOOST_AUTO_TEST_CASE(foreach)
107 {
108         Array::Ptr array = new Array();
109         array->Add(7);
110         array->Add(2);
111         array->Add(5);
112
113         ObjectLock olock(array);
114
115         int n = 0;
116
117         BOOST_FOREACH(const Value& item, array) {
118                 BOOST_CHECK(n != 0 || item == 7);
119                 BOOST_CHECK(n != 1 || item == 2);
120                 BOOST_CHECK(n != 2 || item == 5);
121
122                 n++;
123         }
124 }
125
126 BOOST_AUTO_TEST_CASE(clone)
127 {
128         Array::Ptr array = new Array();
129         array->Add(7);
130         array->Add(2);
131         array->Add(5);
132
133         Array::Ptr clone = array->ShallowClone();
134
135         BOOST_CHECK(clone->GetLength() == 3);
136         BOOST_CHECK(clone->Get(0) == 7);
137         BOOST_CHECK(clone->Get(1) == 2);
138         BOOST_CHECK(clone->Get(2) == 5);
139 }
140
141 BOOST_AUTO_TEST_CASE(json)
142 {
143         Array::Ptr array = new Array();
144         array->Add(7);
145         array->Add(2);
146         array->Add(5);
147
148         String json = JsonEncode(array);
149         BOOST_CHECK(json.GetLength() > 0);
150
151         Array::Ptr deserialized = JsonDecode(json);
152         BOOST_CHECK(deserialized);
153         BOOST_CHECK(deserialized->GetLength() == 3);
154         BOOST_CHECK(deserialized->Get(0) == 7);
155         BOOST_CHECK(deserialized->Get(1) == 2);
156         BOOST_CHECK(deserialized->Get(2) == 5);
157 }
158
159 BOOST_AUTO_TEST_SUITE_END()