limit_test.cxx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // limit_test.cxx
  3. // limit
  4. //
  5. // Created by Sam Jaffe on 2/3/17.
  6. //
  7. #pragma once
  8. #include "math/limit.h"
  9. #include <cxxtest/TestSuite.h>
  10. class bound_number_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. using number_t = bound_number<int, -5, +7>;
  13. // Fails to compile because of well-ordered principle
  14. // void test_bounds_compiler_error() {
  15. // bound_number<int, 0, -1>(0);
  16. // }
  17. void test_number_construct_inbounds() {
  18. number_t n{0};
  19. TS_ASSERT_EQUALS(n, 0);
  20. }
  21. void test_number_validate_oob_low() {
  22. TS_ASSERT_THROWS((number_t{check_bounds, -6}), std::out_of_range);
  23. }
  24. void test_number_validate_oob_high() {
  25. TS_ASSERT_THROWS((number_t{check_bounds, +8}), std::out_of_range);
  26. }
  27. void test_number_cannot_construct_oob_low() {
  28. number_t n{-6};
  29. TS_ASSERT_EQUALS(n, number_t::min);
  30. }
  31. void test_number_cannot_construct_oob_high() {
  32. number_t n{10};
  33. TS_ASSERT_EQUALS(n, number_t::max);
  34. }
  35. void test_number_cannot_increment_above_limit() {
  36. number_t n{+7};
  37. TS_ASSERT_EQUALS(n++, number_t::max);
  38. TS_ASSERT_EQUALS(n, number_t::max);
  39. }
  40. void test_number_cannot_decrement_below_limit() {
  41. number_t n{-5};
  42. TS_ASSERT_EQUALS(n--, number_t::min);
  43. TS_ASSERT_EQUALS(n, number_t::min);
  44. }
  45. };