src/utilities/core/System.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 "System.hpp" | ||
| 7 | |||
| 8 | #include <boost/chrono/duration.hpp> | ||
| 9 | #include <boost/thread.hpp> | ||
| 10 | #include <boost/numeric/ublas/lu.hpp> | ||
| 11 | |||
| 12 | #include <cstdlib> // std::getenv | ||
| 13 | |||
| 14 | namespace openstudio { | ||
| 15 | |||
| 16 | 2 | void System::msleep(int msecs) { | |
| 17 |
1/1✓ Branch 1 taken 2 times.
|
2 | boost::this_thread::sleep_for(boost::chrono::milliseconds(msecs)); |
| 18 | 2 | } | |
| 19 | |||
| 20 | ✗ | unsigned System::numberOfProcessors() { | |
| 21 | ✗ | unsigned numberOfProcessors = boost::thread::hardware_concurrency(); | |
| 22 | ✗ | if (numberOfProcessors < 1) { | |
| 23 | ✗ | numberOfProcessors = 1; | |
| 24 | } | ||
| 25 | ✗ | return numberOfProcessors; | |
| 26 | } | ||
| 27 | |||
| 28 | 3 | void System::testExceptions1() { | |
| 29 | try { | ||
| 30 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions1: Test 1" << '\n'; |
| 31 | 3 | throw std::exception(); | |
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::exception&) { |
| 33 | 3 | } | |
| 34 | |||
| 35 | try { | ||
| 36 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions1: Test 2" << '\n'; |
| 37 | 3 | throw std::exception(); | |
| 38 | 3 | } catch (...) { | |
| 39 | 3 | } | |
| 40 | 3 | } | |
| 41 | |||
| 42 | 3 | void System::testExceptions2() { | |
| 43 | try { | ||
| 44 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions2: Test 1" << '\n'; |
| 45 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw std::runtime_error("test"); |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::runtime_error&) { |
| 47 | 3 | } | |
| 48 | |||
| 49 | try { | ||
| 50 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions2: Test 2" << '\n'; |
| 51 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw std::runtime_error("test"); |
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::exception&) { |
| 53 | 3 | } | |
| 54 | |||
| 55 | try { | ||
| 56 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions2: Test 3" << '\n'; |
| 57 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw std::runtime_error("test"); |
| 58 | 3 | } catch (...) { | |
| 59 | 3 | } | |
| 60 | 3 | } | |
| 61 | |||
| 62 | 3 | void System::testExceptions5() { | |
| 63 | using namespace boost::numeric::ublas; | ||
| 64 | using namespace std; | ||
| 65 | |||
| 66 | struct BreakUBlas | ||
| 67 | { | ||
| 68 | 9 | static bool invertMatrix(matrix<double>& orig, matrix<double>& inverted) { | |
| 69 | using pmatrix = permutation_matrix<std::size_t>; | ||
| 70 |
1/1✓ Branch 1 taken 9 times.
|
9 | matrix<double> A(orig); |
| 71 |
1/1✓ Branch 2 taken 9 times.
|
9 | pmatrix pm(A.size1()); |
| 72 | |||
| 73 | // perform LU-factorization | ||
| 74 |
1/1✓ Branch 1 taken 9 times.
|
9 | int res = lu_factorize(A, pm); |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (res != 0) { |
| 76 | ✗ | return false; | |
| 77 | } | ||
| 78 | |||
| 79 |
2/2✓ Branch 2 taken 9 times.
✓ Branch 5 taken 9 times.
|
9 | inverted.assign(identity_matrix<double>(A.size1())); |
| 80 |
1/1✓ Branch 1 taken 9 times.
|
9 | lu_substitute(A, pm, inverted); |
| 81 | |||
| 82 | 9 | return true; | |
| 83 | 9 | }; | |
| 84 | |||
| 85 | 9 | static void breakIt() { | |
| 86 | 9 | constexpr std::array<std::array<double, 6>, 6> data{{ | |
| 87 | {15, 29700, 472042, 7.8021e+06, 1.32426e+08, 2.29091e+09}, | ||
| 88 | {29700, 1.32426e+08, 2.29091e+09, 4.01989e+10, 7.13142e+11, 1.27611e+13}, | ||
| 89 | {472042, 2.29091e+09, 4.01989e+10, 7.13142e+11, 1.27611e+13, 2.29941e+14}, | ||
| 90 | {7.8021e+06, 4.01989e+10, 7.13142e+11, 1.27611e+13, 2.29941e+14, 4.16694e+15}, | ||
| 91 | {1.32426e+08, 7.13142e+11, 1.27611e+13, 2.29941e+14, 4.16694e+15, 7.58705e+16}, | ||
| 92 | {2.29091e+09, 1.27611e+13, 2.29941e+14, 4.16694e+15, 7.58705e+16, 1.38694e+18}, | ||
| 93 | }}; | ||
| 94 | |||
| 95 |
1/1✓ Branch 1 taken 9 times.
|
9 | matrix<double> a(6, 6); |
| 96 |
2/2✓ Branch 1 taken 54 times.
✓ Branch 2 taken 9 times.
|
63 | for (unsigned i = 0; i < a.size1(); ++i) { |
| 97 |
2/2✓ Branch 1 taken 324 times.
✓ Branch 2 taken 54 times.
|
378 | for (unsigned j = 0; j < a.size2(); ++j) { |
| 98 |
1/1✓ Branch 3 taken 324 times.
|
324 | a(i, j) = data[i][j]; |
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 |
1/1✓ Branch 1 taken 9 times.
|
9 | matrix<double> b(a); |
| 103 |
1/1✓ Branch 1 taken 9 times.
|
9 | invertMatrix(a, b); |
| 104 | 9 | } | |
| 105 | }; | ||
| 106 | |||
| 107 | try { | ||
| 108 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions5: Test 1" << '\n'; |
| 109 |
1/1✓ Branch 1 taken 3 times.
|
3 | BreakUBlas::breakIt(); |
| 110 | assert(!"Exception Not Thrown"); | ||
| 111 | ✗ | } catch (boost::numeric::ublas::internal_logic&) { | |
| 112 | ✗ | } | |
| 113 | |||
| 114 | try { | ||
| 115 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions5: Test 2" << '\n'; |
| 116 |
1/1✓ Branch 1 taken 3 times.
|
3 | BreakUBlas::breakIt(); |
| 117 | assert(!"Exception Not Thrown"); | ||
| 118 | ✗ | } catch (std::logic_error&) { | |
| 119 | ✗ | } | |
| 120 | |||
| 121 | try { | ||
| 122 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions5: Test 3" << '\n'; |
| 123 |
1/1✓ Branch 1 taken 3 times.
|
3 | BreakUBlas::breakIt(); |
| 124 | assert(!"Exception Not Thrown"); | ||
| 125 | ✗ | } catch (std::exception&) { | |
| 126 | ✗ | } | |
| 127 | 3 | } | |
| 128 | |||
| 129 | 3 | void System::testExceptions3() { | |
| 130 | class MyException : public std::runtime_error | ||
| 131 | { | ||
| 132 | public: | ||
| 133 | 12 | MyException() : std::runtime_error("exception") {} | |
| 134 | MyException(const MyException&) = default; | ||
| 135 | MyException& operator=(const MyException&) = default; | ||
| 136 | MyException(MyException&&) = default; | ||
| 137 | MyException& operator=(MyException&&) = default; | ||
| 138 | 12 | virtual ~MyException() noexcept override = default; | |
| 139 | }; | ||
| 140 | |||
| 141 | try { | ||
| 142 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions3: Test 1" << '\n'; |
| 143 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException(); |
| 144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const MyException&) { |
| 145 | 3 | } | |
| 146 | |||
| 147 | try { | ||
| 148 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions3: Test 2" << '\n'; |
| 149 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException(); |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::runtime_error&) { |
| 151 | 3 | } | |
| 152 | |||
| 153 | try { | ||
| 154 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions3: Test 3" << '\n'; |
| 155 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException(); |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::exception&) { |
| 157 | 3 | } | |
| 158 | |||
| 159 | try { | ||
| 160 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions3: Test 4" << '\n'; |
| 161 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException(); |
| 162 | 3 | } catch (...) { | |
| 163 | 3 | } | |
| 164 | 3 | } | |
| 165 | |||
| 166 | 3 | void System::testExceptions4() { | |
| 167 | class MyException : public std::runtime_error | ||
| 168 | { | ||
| 169 | public: | ||
| 170 | 15 | MyException() : std::runtime_error("exception") {} | |
| 171 | MyException(const MyException&) = default; | ||
| 172 | MyException& operator=(const MyException&) = default; | ||
| 173 | MyException(MyException&&) = default; | ||
| 174 | MyException& operator=(MyException&&) = default; | ||
| 175 | 15 | virtual ~MyException() noexcept override = default; | |
| 176 | }; | ||
| 177 | |||
| 178 | class MyException2 : public MyException | ||
| 179 | { | ||
| 180 | public: | ||
| 181 | 15 | MyException2() = default; | |
| 182 | MyException2(const MyException2&) = default; | ||
| 183 | MyException2& operator=(const MyException2&) = default; | ||
| 184 | MyException2(MyException2&&) = default; | ||
| 185 | MyException2& operator=(MyException2&&) = default; | ||
| 186 | 15 | virtual ~MyException2() noexcept override = default; | |
| 187 | }; | ||
| 188 | |||
| 189 | try { | ||
| 190 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions4: Test 1" << '\n'; |
| 191 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException2(); |
| 192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const MyException2&) { |
| 193 | 3 | } | |
| 194 | |||
| 195 | try { | ||
| 196 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions4: Test 2" << '\n'; |
| 197 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException2(); |
| 198 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const MyException&) { |
| 199 | 3 | } | |
| 200 | |||
| 201 | try { | ||
| 202 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions4: Test 3" << '\n'; |
| 203 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException2(); |
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::runtime_error&) { |
| 205 | 3 | } | |
| 206 | |||
| 207 | try { | ||
| 208 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions4: Test 4" << '\n'; |
| 209 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException2(); |
| 210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } catch (const std::exception&) { |
| 211 | 3 | } | |
| 212 | |||
| 213 | try { | ||
| 214 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | std::cout << "testExceptions4: Test 5" << '\n'; |
| 215 |
1/1✓ Branch 2 taken 3 times.
|
3 | throw MyException2(); |
| 216 | 3 | } catch (...) { | |
| 217 | 3 | } | |
| 218 | 3 | } | |
| 219 | |||
| 220 | } // namespace openstudio | ||
| 221 |