Commit 90fed587 authored by Chuanren Wu's avatar Chuanren Wu

add tests

parent 95654561
......@@ -11,7 +11,7 @@
template<class T> int clamp1(T x)
{
return x >= 1 ? static_cast<int>(x) : 1;
return x <= 1 ? static_cast<int>(x) : 1;
}
struct ConstraintExt
......@@ -131,7 +131,8 @@ static std::map<int, int> eliminateSide(
}
// make sure the sum is nTotal
m[*c.plus.cbegin()] = nTotal - std::accumulate(
std::next(c.plus.cbegin()), c.plus.cend(), 0);
std::next(c.plus.cbegin()), c.plus.cend(), 0,
[&m](int s, int i){ return s + m[i];} );
}
} else {
// the mirrored plus case
......@@ -170,7 +171,9 @@ static std::map<int, int> eliminateSide(
}
// make sure the sum is nTotal
m[*c.minus.cbegin()] = nTotal - std::accumulate(
std::next(c.minus.cbegin()), c.minus.cend(), 0);
std::next(c.minus.cbegin()), c.minus.cend(), 0,
[&m](int s, int i){ return s + m[i];} );
assert(m[*c.minus.cbegin()] > 0);
}
}
......
#include "../src/bc.h"
#include <algorithm>
#include <cmath>
#include <gtest/gtest.h>
TEST(BC, SingleSegment1)
......@@ -24,7 +25,7 @@ TEST(BC, SingleSegment1)
std::generate(vl.begin(), vl.end(), [](){ return 10.0;} );
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
EXPECT_LE(10, v[0]);
EXPECT_LE(10, v[1]);
EXPECT_LE(10, v[2]);
......@@ -57,7 +58,7 @@ TEST(BC, SingleSegment2)
vl.push_back(10000);
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
EXPECT_LE(10, v[0]);
EXPECT_LE(100, v[1]);
EXPECT_LE(1000, v[2]);
......@@ -90,7 +91,7 @@ TEST(BC, SingleSegment3)
vl.push_back(2);
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
EXPECT_LE(1, v[0]);
EXPECT_LE(1, v[1]);
EXPECT_LE(1, v[2]);
......@@ -99,6 +100,38 @@ TEST(BC, SingleSegment3)
EXPECT_EQ(v[1], v[3]);
}
TEST(BC, SingleSegment4)
{
std::vector<Constraint> cs;
{
Constraint c;
c.plus(0);
c.plus(1);
c.minus(2);
c.minus(3);
cs.push_back(c);
}
std::vector<double> vl(4);
for (vl[0] = 1; vl[0] <= 5; ++vl[0]) {
for (vl[1] = 1; vl[1] <= 5; ++vl[1]) {
for (vl[2] = 1; vl[2] <= 5; ++vl[2]) {
for (vl[3] = 1; vl[3] <= 5; ++vl[3]) {
auto v = discretizeImpl(vl, cs);
ASSERT_EQ(4, v.size())
<< "With equation: " << v[0] << " + " << v[1] << " = " << v[2] << " + " << v[3];
EXPECT_LE(1, v[0]);
EXPECT_LE(1, v[1]);
EXPECT_LE(1, v[2]);
EXPECT_LE(1, v[3]);
EXPECT_EQ(v[0]+v[1], v[2]+v[3]);
}
}
}
}
}
TEST(BC, TwoSegments1)
{
std::vector<Constraint> cs;
......@@ -146,7 +179,7 @@ TEST(BC, TwoSegments1)
vl.push_back(7);
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
EXPECT_LE(1, v[0]);
EXPECT_LE(2, v[1]);
EXPECT_LE(3, v[2]);
......@@ -220,7 +253,7 @@ TEST(BC, ThreeSegment1)
std::generate(vl.begin(), vl.end(), [](){ return 100.0;} );
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
for (int i = 0; i < 12; ++i) {
EXPECT_LE(10, v[i]);
}
......@@ -1226,5 +1259,5 @@ TEST(BC, ComplexSystem)
std::vector<double> vl(466);
std::generate(vl.begin(), vl.end(), [](){ return 1000.0;} );
auto v = discretizeImpl(vl, cs);
EXPECT_NE(0, v.size());
ASSERT_NE(0, v.size());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment