[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / Support / CheckedArithmeticTest.cpp
blob18eee4c447e419bbc6fc0b6b1a6d65b488c5fe08
1 #include "llvm/Support/CheckedArithmetic.h"
2 #include "gtest/gtest.h"
4 using namespace llvm;
6 namespace {
8 TEST(CheckedArithmetic, CheckedAdd) {
9 const int64_t Max = std::numeric_limits<int64_t>::max();
10 const int64_t Min = std::numeric_limits<int64_t>::min();
11 EXPECT_EQ(checkedAdd<int64_t>(Max, Max), None);
12 EXPECT_EQ(checkedAdd<int64_t>(Min, -1), None);
13 EXPECT_EQ(checkedAdd<int64_t>(Max, 1), None);
14 EXPECT_EQ(checkedAdd<int64_t>(10, 1), Optional<int64_t>(11));
17 TEST(CheckedArithmetic, CheckedAddSmall) {
18 const int16_t Max = std::numeric_limits<int16_t>::max();
19 const int16_t Min = std::numeric_limits<int16_t>::min();
20 EXPECT_EQ(checkedAdd<int16_t>(Max, Max), None);
21 EXPECT_EQ(checkedAdd<int16_t>(Min, -1), None);
22 EXPECT_EQ(checkedAdd<int16_t>(Max, 1), None);
23 EXPECT_EQ(checkedAdd<int16_t>(10, 1), Optional<int64_t>(11));
26 TEST(CheckedArithmetic, CheckedMul) {
27 const int64_t Max = std::numeric_limits<int64_t>::max();
28 const int64_t Min = std::numeric_limits<int64_t>::min();
29 EXPECT_EQ(checkedMul<int64_t>(Max, 2), None);
30 EXPECT_EQ(checkedMul<int64_t>(Max, Max), None);
31 EXPECT_EQ(checkedMul<int64_t>(Min, 2), None);
32 EXPECT_EQ(checkedMul<int64_t>(10, 2), Optional<int64_t>(20));
35 TEST(CheckedArithmetic, CheckedMulAdd) {
36 const int64_t Max = std::numeric_limits<int64_t>::max();
37 const int64_t Min = std::numeric_limits<int64_t>::min();
38 EXPECT_EQ(checkedMulAdd<int64_t>(Max, 1, 2), None);
39 EXPECT_EQ(checkedMulAdd<int64_t>(1, 1, Max), None);
40 EXPECT_EQ(checkedMulAdd<int64_t>(1, -1, Min), None);
41 EXPECT_EQ(checkedMulAdd<int64_t>(10, 2, 3), Optional<int64_t>(23));
44 TEST(CheckedArithmetic, CheckedMulSmall) {
45 const int16_t Max = std::numeric_limits<int16_t>::max();
46 const int16_t Min = std::numeric_limits<int16_t>::min();
47 EXPECT_EQ(checkedMul<int16_t>(Max, 2), None);
48 EXPECT_EQ(checkedMul<int16_t>(Max, Max), None);
49 EXPECT_EQ(checkedMul<int16_t>(Min, 2), None);
50 EXPECT_EQ(checkedMul<int16_t>(10, 2), Optional<int16_t>(20));
53 TEST(CheckedArithmetic, CheckedMulAddSmall) {
54 const int16_t Max = std::numeric_limits<int16_t>::max();
55 const int16_t Min = std::numeric_limits<int16_t>::min();
56 EXPECT_EQ(checkedMulAdd<int16_t>(Max, 1, 2), None);
57 EXPECT_EQ(checkedMulAdd<int16_t>(1, 1, Max), None);
58 EXPECT_EQ(checkedMulAdd<int16_t>(1, -1, Min), None);
59 EXPECT_EQ(checkedMulAdd<int16_t>(10, 2, 3), Optional<int16_t>(23));
62 TEST(CheckedArithmetic, CheckedAddUnsigned) {
63 const uint64_t Max = std::numeric_limits<uint64_t>::max();
64 EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max), None);
65 EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1), None);
66 EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1), Optional<uint64_t>(11));
69 TEST(CheckedArithmetic, CheckedMulUnsigned) {
70 const uint64_t Max = std::numeric_limits<uint64_t>::max();
71 EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2), llvm::None);
72 EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max), llvm::None);
73 EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2), Optional<uint64_t>(20));
76 TEST(CheckedArithmetic, CheckedMulAddUnsigned) {
77 const uint64_t Max = std::numeric_limits<uint64_t>::max();
78 EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(Max, 1, 2), None);
79 EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(1, 1, Max), None);
80 EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(10, 2, 3), Optional<uint64_t>(23));
84 } // namespace