File: clamped_uint.cpp

package info (click to toggle)
iqtree 2.0.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,700 kB
  • sloc: cpp: 142,571; ansic: 57,789; sh: 275; python: 242; makefile: 95
file content (32 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download | duplicates (2)
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>::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>::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