File: clamped_uint.cpp

package info (click to toggle)
terraphast 0.0%2Bgit20200413.8af2e4c%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 792 kB
  • sloc: cpp: 5,923; sh: 83; ansic: 55; makefile: 25
file content (32 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <catch.hpp>

#include <terraces/clamped_uint.hpp>

#include <limits>
#include <terraces/errors.hpp>

namespace terraces {
namespace tests {

TEST_CASE("clamped_uint", "[checked_uint]") {
	auto max = std::numeric_limits<index_t>::max();
	CHECK((clamped_uint{10} + clamped_uint{417}).value() == 10 + 417);
	CHECK((clamped_uint{10} * clamped_uint{417}).value() == 10 * 417);
	CHECK((clamped_uint{max} + clamped_uint{1}).is_clamped());
	CHECK((clamped_uint{max} + clamped_uint{1}).value() == max);
	CHECK((clamped_uint{max / 2} * clamped_uint{3}).is_clamped());
	CHECK((clamped_uint{max / 2} * clamped_uint{3}).value() == max);
}

TEST_CASE("overflow_except_uint", "[checked_uint]") {
	auto max = std::numeric_limits<index_t>::max();
	CHECK((overflow_except_uint{10} + overflow_except_uint{417}).value() == 10 + 417);
	CHECK((overflow_except_uint{10} * overflow_except_uint{417}).value() == 10 * 417);
	CHECK_THROWS_AS(overflow_except_uint{max} + overflow_except_uint{1},
	                terraces::tree_count_overflow_error);
	CHECK_THROWS_AS(overflow_except_uint{max / 2} * overflow_except_uint{3},
	                terraces::tree_count_overflow_error);
}

} // namespace tests
} // namespace terraces