1 #include "llvm/Support/CheckedArithmetic.h"
2 #include "gtest/gtest.h"
3
4 using namespace llvm;
5
6 namespace {
7
TEST(CheckedArithmetic,CheckedAdd)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));
15 }
16
TEST(CheckedArithmetic,CheckedAddSmall)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));
24 }
25
TEST(CheckedArithmetic,CheckedMul)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));
33 }
34
TEST(CheckedArithmetic,CheckedMulAdd)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));
42 }
43
TEST(CheckedArithmetic,CheckedMulSmall)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));
51 }
52
TEST(CheckedArithmetic,CheckedMulAddSmall)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));
60 }
61
TEST(CheckedArithmetic,CheckedAddUnsigned)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));
67 }
68
TEST(CheckedArithmetic,CheckedMulUnsigned)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));
74 }
75
TEST(CheckedArithmetic,CheckedMulAddUnsigned)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));
81 }
82
83
84 } // namespace
85