GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 56.5% 35 / 0 / 62
Functions: 77.8% 7 / 0 / 9
Branches: 50.0% 47 / 0 / 94

src/sdd/Helpers.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 "Helpers.hpp"
7 #include <boost/lexical_cast.hpp>
8 #include <pugixml.hpp>
9
10 namespace openstudio {
11
12 namespace sdd {
13
14 // Helper to make a vector of pugi::xml_node of all children
15 5 std::vector<pugi::xml_node> makeVectorOfChildren(const pugi::xml_node& root) {
16 5 std::vector<pugi::xml_node> result;
17
6/6
✓ Branch 1 taken 5 times.
✓ Branch 6 taken 30 times.
✓ Branch 9 taken 30 times.
✓ Branch 12 taken 35 times.
✓ Branch 14 taken 30 times.
✓ Branch 15 taken 5 times.
65 for (const pugi::xml_node& child : root.children()) {
18
1/1
✓ Branch 1 taken 30 times.
30 result.push_back(child);
19 }
20 5 return result;
21 ✗ }
22
23 // Helper to make a vector of pugi::xml_node matching a tag
24 1621 std::vector<pugi::xml_node> makeVectorOfChildren(const pugi::xml_node& root, const char* tagName) {
25 1621 std::vector<pugi::xml_node> result;
26
6/6
✓ Branch 1 taken 1621 times.
✓ Branch 6 taken 4307 times.
✓ Branch 9 taken 4307 times.
✓ Branch 12 taken 5928 times.
✓ Branch 14 taken 4307 times.
✓ Branch 15 taken 1621 times.
10235 for (const pugi::xml_node& child : root.children(tagName)) {
27
1/1
✓ Branch 1 taken 4307 times.
4307 result.push_back(child);
28 }
29 1621 return result;
30 ✗ }
31
32 // Helper to make a vector of pugi::xml_node matching a tag: any level
33 1 std::vector<pugi::xml_node> makeVectorOfChildrenRecursive(const pugi::xml_node& root, const std::string& tagName) {
34 1 std::vector<pugi::xml_node> result;
35 // We use xpath '//tagName'
36
7/7
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
✓ Branch 18 taken 25 times.
✓ Branch 19 taken 1 time.
27 for (const pugi::xpath_node& n : root.select_nodes((std::string("//") + tagName).c_str())) {
37
2/2
✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
25 result.push_back(n.node());
38 1 }
39 1 return result;
40 ✗ }
41
42 // Lexical cast the text() of a node as a double
43 816 boost::optional<double> lexicalCastToDouble(const pugi::xml_node& element) {
44 816 boost::optional<double> result;
45
3/3
✓ Branch 1 taken 816 times.
✓ Branch 3 taken 581 times.
✓ Branch 4 taken 235 times.
816 if (element) {
46 try {
47
4/4
✓ Branch 1 taken 581 times.
✓ Branch 4 taken 581 times.
✓ Branch 7 taken 581 times.
✓ Branch 10 taken 581 times.
581 result = boost::lexical_cast<double>(element.text().as_string());
48 ✗ } catch (const boost::bad_lexical_cast&) {
49 // LOG(Error, "Cannot convert element text to double");
50 ✗ }
51 }
52 816 return result;
53 }
54
55 // Lexical cast the text() of a node as an int
56 106 boost::optional<int> lexicalCastToInt(const pugi::xml_node& element) {
57 106 boost::optional<int> result;
58
3/3
✓ Branch 1 taken 106 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 50 times.
106 if (element) {
59 try {
60
4/4
✓ Branch 1 taken 56 times.
✓ Branch 4 taken 56 times.
✓ Branch 7 taken 56 times.
✓ Branch 10 taken 56 times.
56 result = boost::lexical_cast<int>(element.text().as_string());
61 ✗ } catch (const boost::bad_lexical_cast&) {
62 // LOG(Error, "Cannot convert element text to double");
63 ✗ }
64 }
65 106 return result;
66 }
67
68 // Lexical cast the text() of an attribute as a double
69 ✗ boost::optional<double> lexicalCastToDouble(const pugi::xml_attribute& attr) {
70 ✗ boost::optional<double> result;
71 ✗ if (attr) {
72 try {
73 ✗ result = boost::lexical_cast<double>(attr.value());
74 ✗ } catch (const boost::bad_lexical_cast&) {
75 // LOG(Error, "Cannot convert attribute value to double");
76 ✗ }
77 }
78 ✗ return result;
79 }
80
81 // Lexical cast the text() of an attribute as an int
82 217 boost::optional<int> lexicalCastToInt(const pugi::xml_attribute& attr) {
83 217 boost::optional<int> result;
84
2/3
✓ Branch 1 taken 217 times.
✓ Branch 3 taken 217 times.
✗ Branch 4 not taken.
217 if (attr) {
85 try {
86
3/3
✓ Branch 1 taken 217 times.
✓ Branch 4 taken 217 times.
✓ Branch 7 taken 217 times.
217 result = boost::lexical_cast<int>(attr.value());
87 ✗ } catch (const boost::bad_lexical_cast&) {
88 // LOG(Error, "Cannot convert attribute value to integer");
89 ✗ }
90 }
91 217 return result;
92 }
93
94 // Lexical cast the text() of an attribute as an unsigned int (and don't return if a negative int is there)
95 ✗ boost::optional<unsigned> lexicalCastToUnsigned(const pugi::xml_attribute& attr) {
96 ✗ boost::optional<unsigned> result;
97 ✗ if (attr) {
98 try {
99 ✗ boost::optional<int> _int = boost::lexical_cast<int>(attr.value());
100 ✗ if (_int && (_int.get() >= 0)) {
101 ✗ result = static_cast<unsigned>(_int.get());
102 }
103 ✗ } catch (const boost::bad_lexical_cast&) {
104 // LOG(Error, "Cannot convert attribute value to integer");
105 ✗ }
106 }
107 ✗ return result;
108 }
109
110 165 pugi::xml_node getProjectElement(const pugi::xml_node& element) {
111
112 // root() returns the root that has the SDDXML element as the only child, which is the one we want (has 'Proj')
113
3/3
✓ Branch 1 taken 165 times.
✓ Branch 4 taken 165 times.
✓ Branch 7 taken 165 times.
165 pugi::xml_node result = element.root().first_child().child("Proj");
114
2/3
✓ Branch 1 taken 165 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 165 times.
165 if (!result) {
115 ✗ LOG_FREE(Error, "openstudio.sdd.Helpers",
116 "Couldn't find the 'Proj' element in the tree from the supplied element " << "(name()='" << element.name() << "', text()='"
117 << element.text().as_string() << "').");
118 ✗ OS_ASSERT(false);
119 }
120 165 return result;
121 }
122
123 } // namespace sdd
124 } // namespace openstudio
125