]> granicus.if.org Git - icinga2/blob - test/base-array.cpp
Apply clang-tidy fix 'modernize-use-auto'
[icinga2] / test / base-array.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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 <BoostTestTargetConfig.h>
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 = new Array();
32         BOOST_CHECK(array);
33         BOOST_CHECK(array->GetLength() == 0);
34 }
35
36 BOOST_AUTO_TEST_CASE(getset)
37 {
38         Array::Ptr array = new 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(resize)
56 {
57         Array::Ptr array = new Array();
58         array->Resize(2);
59         BOOST_CHECK(array->GetLength() == 2);
60         BOOST_CHECK(array->Get(0) == Empty);
61         BOOST_CHECK(array->Get(1) == Empty);
62 }
63
64 BOOST_AUTO_TEST_CASE(insert)
65 {
66         Array::Ptr array = new Array();
67
68         array->Insert(0, 11);
69         array->Insert(1, 22);
70         BOOST_CHECK(array->GetLength() == 2);
71         BOOST_CHECK(array->Get(1) == 22);
72
73         array->Insert(0, 33);
74         BOOST_CHECK(array->GetLength() == 3);
75         BOOST_CHECK(array->Get(0) == 33);
76         BOOST_CHECK(array->Get(1) == 11);
77
78         array->Insert(1, 44);
79         BOOST_CHECK(array->GetLength() == 4);
80         BOOST_CHECK(array->Get(0) == 33);
81         BOOST_CHECK(array->Get(1) == 44);
82         BOOST_CHECK(array->Get(2) == 11);
83 }
84
85 BOOST_AUTO_TEST_CASE(remove)
86 {
87         Array::Ptr array = new Array();
88         array->Add(7);
89         array->Add(2);
90         array->Add(5);
91
92         {
93                 ObjectLock olock(array);
94                 auto it = array->Begin();
95                 array->Remove(it);
96         }
97
98         BOOST_CHECK(array->GetLength() == 2);
99         BOOST_CHECK(array->Get(0) == 2);
100
101         array->Clear();
102         BOOST_CHECK(array->GetLength() == 0);
103 }
104
105 BOOST_AUTO_TEST_CASE(foreach)
106 {
107         Array::Ptr array = new Array();
108         array->Add(7);
109         array->Add(2);
110         array->Add(5);
111
112         ObjectLock olock(array);
113
114         int n = 0;
115
116         for (const Value& item : array) {
117                 BOOST_CHECK(n != 0 || item == 7);
118                 BOOST_CHECK(n != 1 || item == 2);
119                 BOOST_CHECK(n != 2 || item == 5);
120
121                 n++;
122         }
123 }
124
125 BOOST_AUTO_TEST_CASE(clone)
126 {
127         Array::Ptr array = new Array();
128         array->Add(7);
129         array->Add(2);
130         array->Add(5);
131
132         Array::Ptr clone = array->ShallowClone();
133
134         BOOST_CHECK(clone->GetLength() == 3);
135         BOOST_CHECK(clone->Get(0) == 7);
136         BOOST_CHECK(clone->Get(1) == 2);
137         BOOST_CHECK(clone->Get(2) == 5);
138 }
139
140 BOOST_AUTO_TEST_CASE(json)
141 {
142         Array::Ptr array = new Array();
143         array->Add(7);
144         array->Add(2);
145         array->Add(5);
146
147         String json = JsonEncode(array);
148         BOOST_CHECK(json.GetLength() > 0);
149
150         Array::Ptr deserialized = JsonDecode(json);
151         BOOST_CHECK(deserialized);
152         BOOST_CHECK(deserialized->GetLength() == 3);
153         BOOST_CHECK(deserialized->Get(0) == 7);
154         BOOST_CHECK(deserialized->Get(1) == 2);
155         BOOST_CHECK(deserialized->Get(2) == 5);
156 }
157
158 BOOST_AUTO_TEST_SUITE_END()