GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 80.0% 160 / 0 / 200
Functions: 93.9% 31 / 0 / 33
Branches: 76.8% 152 / 0 / 198

src/utilities/idf/IdfExtensibleGroup.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "IdfExtensibleGroup.hpp"
7 #include "IdfObject_Impl.hpp"
8
9 #include "../core/Assert.hpp"
10
11 namespace openstudio {
12
13 // GETTERS
14
15 3356 StringVector IdfExtensibleGroup::fields(bool returnDefault) const {
16 3356 StringVector result;
17
3/3
✓ Branch 1 taken 3356 times.
✓ Branch 3 taken 3396 times.
✓ Branch 4 taken 3356 times.
6752 for (unsigned i = 0, n = numFields(); i < n; ++i) {
18
1/1
✓ Branch 1 taken 3396 times.
3396 OptionalString str = getString(i, returnDefault);
19
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 3396 times.
✗ Branch 4 not taken.
3396 OS_ASSERT(str);
20
2/2
✓ Branch 1 taken 3396 times.
✓ Branch 4 taken 3396 times.
3396 result.push_back(*str);
21 3396 }
22 3356 return result;
23 ✗ }
24
25 234083 StringVector IdfExtensibleGroup::fieldsWithHandles(bool returnDefault) const {
26 234083 StringVector result;
27
1/1
✓ Branch 1 taken 234083 times.
234083 const unsigned n = numFields();
28
1/1
✓ Branch 1 taken 234083 times.
234083 result.reserve(n);
29
2/2
✓ Branch 0 taken 698041 times.
✓ Branch 1 taken 234083 times.
932124 for (unsigned i = 0; i < n; ++i) {
30
1/1
✓ Branch 1 taken 698041 times.
698041 OptionalString str = getField(i, returnDefault);
31
3/3
✓ Branch 1 taken 698041 times.
✓ Branch 4 taken 698041 times.
✓ Branch 7 taken 698041 times.
698041 result.push_back(str.get_value_or(""));
32 698041 }
33 234083 return result;
34 ✗ }
35
36 104083 StringVector IdfExtensibleGroup::fieldComments(bool returnDefault) const {
37 104083 StringVector result;
38
3/3
✓ Branch 1 taken 104083 times.
✓ Branch 3 taken 310596 times.
✓ Branch 4 taken 104083 times.
414679 for (unsigned i = 0, n = numFields(); i < n; ++i) {
39
1/1
✓ Branch 1 taken 310596 times.
310596 OptionalString str = fieldComment(i, returnDefault);
40
1/2
✓ Branch 0 taken 310596 times.
✗ Branch 1 not taken.
310596 if (str) {
41
2/2
✓ Branch 1 taken 310596 times.
✓ Branch 4 taken 310596 times.
310596 result.push_back(*str);
42 } else {
43 ✗ break;
44 }
45 310596 }
46 104083 return result;
47 ✗ }
48
49 310842 OptionalString IdfExtensibleGroup::fieldComment(unsigned fieldIndex, bool returnDefault) const {
50
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 310842 times.
310842 if (!isValid(fieldIndex)) {
51 ✗ return boost::none;
52 }
53 310842 return m_impl->fieldComment(mf_toIndex(fieldIndex), returnDefault);
54 }
55
56 31 bool IdfExtensibleGroup::isEmpty(unsigned fieldIndex) const {
57
1/1
✓ Branch 1 taken 31 times.
31 OptionalString test = this->getString(fieldIndex, false);
58
6/7
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 31 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 27 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 27 times.
31 if (!test || test.get().empty()) {
59 4 return true;
60 }
61 27 return false;
62 31 }
63
64 252244 OptionalString IdfExtensibleGroup::getString(unsigned fieldIndex, bool returnDefault) const {
65
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 252243 times.
252244 if (!isValid(fieldIndex)) {
66 1 return boost::none;
67 }
68 252243 return m_impl->getString(mf_toIndex(fieldIndex), returnDefault);
69 }
70
71 698500 boost::optional<std::string> IdfExtensibleGroup::getField(unsigned index, bool returnDefault) const {
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 698500 times.
698500 if (!isValid(index)) {
73 ✗ return boost::none;
74 }
75 698500 return m_impl->getField(mf_toIndex(index), returnDefault);
76 }
77
78 3176182 OptionalDouble IdfExtensibleGroup::getDouble(unsigned fieldIndex, bool returnDefault) const {
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3176182 times.
3176182 if (!isValid(fieldIndex)) {
80 ✗ return boost::none;
81 }
82 3176182 return m_impl->getDouble(mf_toIndex(fieldIndex), returnDefault);
83 }
84
85 1429 OptionalUnsigned IdfExtensibleGroup::getUnsigned(unsigned fieldIndex, bool returnDefault) const {
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1429 times.
1429 if (!isValid(fieldIndex)) {
87 ✗ return boost::none;
88 }
89 1429 return m_impl->getUnsigned(mf_toIndex(fieldIndex), returnDefault);
90 }
91
92 1498495 OptionalInt IdfExtensibleGroup::getInt(unsigned fieldIndex, bool returnDefault) const {
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1498495 times.
1498495 if (!isValid(fieldIndex)) {
94 ✗ return boost::none;
95 }
96 1498495 return m_impl->getInt(mf_toIndex(fieldIndex), returnDefault);
97 }
98
99 // SETTERS
100
101 247 bool IdfExtensibleGroup::setFieldComment(unsigned fieldIndex, const std::string& cmnt) {
102
2/4
✓ Branch 1 taken 247 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 247 times.
✗ Branch 7 not taken.
247 return isValid(fieldIndex) && m_impl->setFieldComment(mf_toIndex(fieldIndex), cmnt);
103 }
104
105 ✗ bool IdfExtensibleGroup::setFields(const std::vector<std::string>& values) {
106 ✗ bool result = setFields(values, true);
107 ✗ if (result) {
108 ✗ m_impl->emitChangeSignals();
109 }
110 ✗ return result;
111 }
112
113 23085 bool IdfExtensibleGroup::setFields(const std::vector<std::string>& values, bool checkValidity) {
114
4/7
✓ Branch 1 taken 23085 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 23085 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 23085 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 23085 times.
23085 if ((values.empty()) || (values.size() != numFields())) {
115 ✗ return false;
116 }
117
118
1/1
✓ Branch 1 taken 23085 times.
23085 unsigned n = numFields();
119 // save oldValues for rollback
120 23085 StringVector oldValues;
121
2/2
✓ Branch 0 taken 68902 times.
✓ Branch 1 taken 23085 times.
91987 for (unsigned i = 0; i < n; ++i) {
122
1/1
✓ Branch 1 taken 68902 times.
68902 OptionalString oValue = getString(i);
123
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 68902 times.
✗ Branch 4 not taken.
68902 OS_ASSERT(oValue);
124
2/2
✓ Branch 1 taken 68902 times.
✓ Branch 4 taken 68902 times.
68902 oldValues.push_back(*oValue);
125 68902 }
126
127 // try to set values
128 23085 bool ok = true;
129
2/2
✓ Branch 0 taken 68902 times.
✓ Branch 1 taken 23084 times.
91986 for (unsigned i = 0; i < n; ++i) {
130
1/1
✓ Branch 2 taken 68902 times.
68902 ok = setString(i, values[i], checkValidity);
131
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 68901 times.
68902 if (!ok) {
132 1 break;
133 }
134 }
135
136
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 23084 times.
23085 if (!ok) {
137 // rollback
138
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 for (unsigned i = 0; i < n; ++i) {
139
1/1
✓ Branch 2 taken 1 time.
1 bool rollbackOk = setString(i, oldValues[i], false);
140
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
1 OS_ASSERT(rollbackOk);
141 }
142 }
143
144 23085 return ok;
145 23085 }
146
147 31058 bool IdfExtensibleGroup::setString(unsigned fieldIndex, const std::string& value) {
148 31058 bool result = setString(fieldIndex, value, true);
149
2/2
✓ Branch 0 taken 31053 times.
✓ Branch 1 taken 5 times.
31058 if (result) {
150 31053 m_impl->emitChangeSignals();
151 }
152 31058 return result;
153 }
154
155 99979 bool IdfExtensibleGroup::setString(unsigned fieldIndex, const std::string& value, bool checkValidity) {
156 // private method for internal use
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 99979 times.
99979 if (!isValid(fieldIndex)) {
158 ✗ return false;
159 }
160 99979 return m_impl->setString(mf_toIndex(fieldIndex), value, checkValidity);
161 }
162
163 69937 bool IdfExtensibleGroup::setDouble(unsigned fieldIndex, double value) {
164
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 69935 times.
69937 if (!isValid(fieldIndex)) {
165 2 return false;
166 }
167 69935 return m_impl->setDouble(mf_toIndex(fieldIndex), value);
168 }
169
170 1356 bool IdfExtensibleGroup::setUnsigned(unsigned fieldIndex, unsigned value) {
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1356 times.
1356 if (!isValid(fieldIndex)) {
172 ✗ return false;
173 }
174 1356 return m_impl->setUnsigned(mf_toIndex(fieldIndex), value);
175 }
176
177 2598 bool IdfExtensibleGroup::setInt(unsigned fieldIndex, int value) {
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2598 times.
2598 if (!isValid(fieldIndex)) {
179 ✗ return false;
180 }
181 2598 return m_impl->setInt(mf_toIndex(fieldIndex), value);
182 }
183
184 3 IdfExtensibleGroup IdfExtensibleGroup::pushClone() const {
185
2/3
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (empty()) {
186 ✗ detail::IdfObject_ImplPtr p;
187 ✗ return {p, 0};
188 ✗ }
189
190
1/1
✓ Branch 1 taken 3 times.
3 StringVector values = fieldsWithHandles();
191
2/4
✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 7 not taken.
3 OS_ASSERT(values.size() == numFields());
192
1/1
✓ Branch 2 taken 3 times.
3 return m_impl->pushExtensibleGroup(values);
193 3 }
194
195 6 IdfExtensibleGroup IdfExtensibleGroup::insertClone(unsigned groupIndex) const {
196
2/3
✓ Branch 1 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
6 if (empty()) {
197 ✗ detail::IdfObject_ImplPtr p;
198 ✗ return {p, 0};
199 ✗ }
200
201
1/1
✓ Branch 1 taken 6 times.
6 StringVector values = fieldsWithHandles();
202
2/4
✓ Branch 2 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 7 not taken.
6 OS_ASSERT(values.size() == numFields());
203
1/1
✓ Branch 2 taken 6 times.
6 return m_impl->insertExtensibleGroup(groupIndex, values);
204 6 }
205
206 // QUERIES
207
208 17412032 bool IdfExtensibleGroup::empty() const {
209
6/6
✓ Branch 1 taken 17411921 times.
✓ Branch 2 taken 111 times.
✓ Branch 5 taken 106911 times.
✓ Branch 6 taken 17305010 times.
✓ Branch 7 taken 107022 times.
✓ Branch 8 taken 17305010 times.
17412032 if (!m_impl || (m_index >= m_impl->numFields())) {
210 107022 return true;
211 }
212 17305010 return false;
213 }
214
215 7064851 unsigned IdfExtensibleGroup::groupIndex() const {
216
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 7064848 times.
7064851 if (empty()) {
217
13/13
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 25 taken 3 times.
✓ Branch 28 taken 3 times.
✓ Branch 31 taken 3 times.
✓ Branch 34 taken 3 times.
✓ Branch 38 taken 3 times.
✓ Branch 41 taken 3 times.
3 LOG_AND_THROW("This extensible group is invalid, and so does not have an index.");
218 }
219
2/2
✓ Branch 2 taken 7064848 times.
✓ Branch 5 taken 7064848 times.
7064848 return m_impl->iddObject().extensibleIndex(m_index).group;
220 };
221
222 2593819 unsigned IdfExtensibleGroup::numFields() const {
223
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2593818 times.
2593819 if (empty()) {
224 1 return 0;
225 }
226
2/2
✓ Branch 2 taken 2593818 times.
✓ Branch 5 taken 2593818 times.
2593818 unsigned n = m_impl->iddObject().properties().numExtensible;
227
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2593818 times.
2593818 if (m_index + n > m_impl->numFields()) {
228 ✗ OS_ASSERT(false);
229 ✗ return 0;
230 }
231 2593818 return n;
232 }
233
234 6155323 bool IdfExtensibleGroup::isValid(unsigned fieldIndex) const {
235
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6155320 times.
6155323 if (empty()) {
236 3 return false;
237 }
238
4/4
✓ Branch 2 taken 6155320 times.
✓ Branch 5 taken 6155320 times.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 6155317 times.
6155320 if (fieldIndex >= m_impl->iddObject().properties().numExtensible) {
239 3 return false;
240 }
241
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 6155317 times.
6155317 OS_ASSERT(m_index + fieldIndex < m_impl->numFields());
242 6155317 return true;
243 }
244
245 1 std::vector<unsigned> IdfExtensibleGroup::objectListFields() const {
246 1 UnsignedVector result;
247
2/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 if (!empty()) {
248
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 result = mf_subsetAndToFieldIndices(m_impl->objectListFields());
249 }
250 1 return result;
251 ✗ }
252
253 ✗ std::vector<unsigned> IdfExtensibleGroup::dataFields() const {
254 ✗ UnsignedVector result;
255 ✗ if (!empty()) {
256 ✗ result = mf_subsetAndToFieldIndices(m_impl->dataFields());
257 }
258 ✗ return result;
259 ✗ }
260
261 1 std::vector<unsigned> IdfExtensibleGroup::requiredFields() const {
262 1 UnsignedVector result;
263
2/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 if (!empty()) {
264
3/3
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 8 taken 1 time.
1 IddFieldVector iddExtFields = m_impl->iddObject().extensibleGroup();
265
3/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
3 for (unsigned fieldIndex = 0, n = numFields(); fieldIndex < n; ++fieldIndex) {
266
2/3
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 if (iddExtFields[fieldIndex].properties().required) {
267
1/1
✓ Branch 1 taken 2 times.
2 result.push_back(fieldIndex);
268 }
269 }
270 1 }
271 1 return result;
272 ✗ }
273
274 2 bool IdfExtensibleGroup::operator==(const IdfExtensibleGroup& other) const {
275
3/4
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
2 return (m_impl == other.m_impl) && (m_index == other.m_index);
276 }
277
278 1 bool IdfExtensibleGroup::operator!=(const IdfExtensibleGroup& other) const {
279 1 return !(*this == other);
280 }
281
282 // PRIVATE
283
284 6560253 IdfExtensibleGroup::IdfExtensibleGroup(std::shared_ptr<detail::IdfObject_Impl> impl, unsigned index) : m_impl(impl), m_index(index) {
285 // impl is passed in as null in many cases
286 //OS_ASSERT(m_impl);
287 6560253 }
288
289 6553109 unsigned IdfExtensibleGroup::mf_toIndex(unsigned fieldIndex) const {
290 // only call after checking isValid(fieldIndex)
291 try {
292
4/4
✓ Branch 2 taken 6553109 times.
✓ Branch 5 taken 6553109 times.
✓ Branch 8 taken 6553109 times.
✓ Branch 11 taken 6553109 times.
6553109 return m_impl->iddObject().index(ExtensibleIndex(groupIndex(), fieldIndex));
293 ✗ } catch (...) {
294 ✗ }
295 ✗ OS_ASSERT(false);
296 ✗ return 0;
297 }
298
299 133626 std::vector<unsigned> IdfExtensibleGroup::mf_indices() const {
300 133626 UnsignedVector result;
301
2/3
✓ Branch 1 taken 133626 times.
✓ Branch 3 taken 133626 times.
✗ Branch 4 not taken.
133626 if (!empty()) {
302
3/3
✓ Branch 1 taken 133626 times.
✓ Branch 3 taken 397674 times.
✓ Branch 4 taken 133626 times.
531300 for (unsigned i = 0, n = numFields(); i < n; ++i) {
303
2/2
✓ Branch 1 taken 397674 times.
✓ Branch 4 taken 397674 times.
397674 result.push_back(mf_toIndex(i));
304 }
305 }
306 133626 return result;
307 ✗ }
308
309 3 UnsignedVector IdfExtensibleGroup::mf_subsetAndToFieldIndices(UnsignedVector indices) const {
310 3 UnsignedVector result;
311
2/4
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 6 not taken.
3 OS_ASSERT(!empty());
312 // downselect and convert to only this ExtensibleGroup
313
2/2
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 3 times.
6 for (unsigned index : indices) {
314
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (index >= m_index) {
315 3 unsigned fieldIndex = index - m_index;
316
3/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
3 if (isValid(fieldIndex)) {
317
1/1
✓ Branch 1 taken 2 times.
2 result.push_back(fieldIndex);
318 }
319 }
320 }
321 3 return result;
322 ✗ }
323
324 } // namespace openstudio
325