Skip to content

Commit

Permalink
Merge pull request #272 from ABRG-Models/issue/219_constexpr_units
Browse files Browse the repository at this point in the history
Adds constexpr tests of the range class
  • Loading branch information
sebjameswml authored Oct 21, 2024
2 parents a3d2304 + 7421c39 commit 99a73ce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ add_test(testScale testScale)
add_executable(testrange testrange.cpp)
add_test(testrange testrange)

add_executable(testrange_constexpr testrange_constexpr.cpp)
add_test(testrange_constexpr testrange_constexpr)

# Test the colour mapping
add_executable(testColourMap testColourMap.cpp)
add_test(testColourMap testColourMap)
Expand Down
74 changes: 74 additions & 0 deletions tests/testrange_constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <morph/range.h>
#include <morph/mathconst.h>

constexpr morph::range<float> test_update()
{
morph::range<float> r{2.0f, 4.0f};
r.update (1.0f);
r.update (5.0f);
return r;
}

constexpr morph::range<float> test_update_and_search_init()
{
morph::range<float> r;
r.search_init();
r.update (1.0f);
r.update (5.0f);
return r;
}

constexpr bool test_update_and_includes()
{
morph::range<float> r(2.0f, 4.0f); // also test 2 arg constructor
r.update (1.0f);
r.update (5.0f);
int rtn = 0;
if (r.includes (3.0f) == false) { --rtn; }
if (r.includes (0.5f) == true) { --rtn; }

return (rtn == 0 ? true : false);
}

constexpr float test_span()
{
morph::range<float> r{2.0f, 4.0f};
return r.span();
}

constexpr float test_set()
{
morph::range<float> r;
r.set (56.0f, 59.0f);
return r.span();
}

int main()
{
int rtn = 0;

constexpr morph::range<float> r1 = test_update();
if (r1.min == 1.0f && r1.max == 5.0f) {
// good
} else {
--rtn;
}
constexpr morph::range<float> r2 = test_update_and_search_init();
if (r2.min == 1.0f && r2.max == 5.0f) {
// good
} else {
--rtn;
}
constexpr bool tres = test_update_and_includes();
if (tres == false) { --rtn; }

constexpr float spn = test_span();
if (spn != 2.0f) { --rtn; }

constexpr float spn2 = test_set();
if (spn2 != 3.0f) { --rtn; }

std::cout << "Test " << (rtn == 0 ? "Passed" : "Failed") << std::endl;
return rtn;
}

0 comments on commit 99a73ce

Please sign in to comment.