Skip to content

Commit 5861442

Browse files
committed
Test internals of LinExpr
1 parent 7bc469f commit 5861442

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

include/scippp/lin_expr.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#include "scippp/var.hpp"
77

8+
// just for tests, see test_linexpr.cpp
9+
namespace LinExpression { // NOLINT(readability-identifier-naming)
10+
struct CheckInternalsUsingFriendStruct;
11+
}
812
namespace scippp {
913

1014
/**
@@ -13,6 +17,7 @@ namespace scippp {
1317
*/
1418
class LinExpr {
1519
friend class Model;
20+
friend struct LinExpression::CheckInternalsUsingFriendStruct; // just for tests, see test_linexpr.cpp
1621
//! Constant term.
1722
double m_constant { 0.0 };
1823
//! Variables in the linear combination.

test/test_linexpr.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <boost/test/unit_test.hpp>
22

3+
#include <algorithm>
4+
35
#include "scippp/model.hpp"
46
#include "scippp/solving_statistics.hpp"
57

@@ -98,4 +100,28 @@ BOOST_AUTO_TEST_CASE(AddInitializerList)
98100
BOOST_TEST(model.getSolvingStatistic(statistics::PRIMALBOUND) == 1);
99101
}
100102

103+
BOOST_AUTO_TEST_CASE(CheckInternalsUsingFriendStruct)
104+
{
105+
const double VAL { 2.0 };
106+
for (size_t num { 1 }; num < 4; ++num) {
107+
Model m("m");
108+
const auto VARS = m.addVars("x", num);
109+
110+
LinExpr lDef(VARS);
111+
BOOST_TEST(lDef.getConstant() == 0);
112+
BOOST_TEST(lDef.m_constant == 0);
113+
BOOST_TEST(lDef.m_vars.size() == num);
114+
BOOST_TEST(lDef.m_coeffs.size() == num);
115+
BOOST_TEST(all_of(lDef.m_coeffs.begin(), lDef.m_coeffs.end(), [](double d) { return d == 1; }));
116+
117+
const vector COEFF(num, VAL);
118+
LinExpr lCoeff(VARS, COEFF);
119+
BOOST_TEST(lCoeff.getConstant() == 0);
120+
BOOST_TEST(lCoeff.m_constant == 0);
121+
BOOST_TEST(lCoeff.m_vars.size() == num);
122+
BOOST_TEST(lCoeff.m_coeffs.size() == num);
123+
BOOST_TEST(all_of(lCoeff.m_coeffs.begin(), lCoeff.m_coeffs.end(), [&VAL](double d) { return d == VAL; }));
124+
}
125+
}
126+
101127
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)