1 #include "llvm/Support/CheckedArithmetic.h"
2 #include "gtest/gtest.h"
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
), std::nullopt
);
12 EXPECT_EQ(checkedAdd
<int64_t>(Min
, -1), std::nullopt
);
13 EXPECT_EQ(checkedAdd
<int64_t>(Max
, 1), std::nullopt
);
14 EXPECT_EQ(checkedAdd
<int64_t>(10, 1), std::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
), std::nullopt
);
21 EXPECT_EQ(checkedAdd
<int16_t>(Min
, -1), std::nullopt
);
22 EXPECT_EQ(checkedAdd
<int16_t>(Max
, 1), std::nullopt
);
23 EXPECT_EQ(checkedAdd
<int16_t>(10, 1), std::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), std::nullopt
);
30 EXPECT_EQ(checkedMul
<int64_t>(Max
, Max
), std::nullopt
);
31 EXPECT_EQ(checkedMul
<int64_t>(Min
, 2), std::nullopt
);
32 EXPECT_EQ(checkedMul
<int64_t>(10, 2), std::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), std::nullopt
);
39 EXPECT_EQ(checkedMulAdd
<int64_t>(1, 1, Max
), std::nullopt
);
40 EXPECT_EQ(checkedMulAdd
<int64_t>(1, -1, Min
), std::nullopt
);
41 EXPECT_EQ(checkedMulAdd
<int64_t>(10, 2, 3), std::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), std::nullopt
);
48 EXPECT_EQ(checkedMul
<int16_t>(Max
, Max
), std::nullopt
);
49 EXPECT_EQ(checkedMul
<int16_t>(Min
, 2), std::nullopt
);
50 EXPECT_EQ(checkedMul
<int16_t>(10, 2), std::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), std::nullopt
);
57 EXPECT_EQ(checkedMulAdd
<int16_t>(1, 1, Max
), std::nullopt
);
58 EXPECT_EQ(checkedMulAdd
<int16_t>(1, -1, Min
), std::nullopt
);
59 EXPECT_EQ(checkedMulAdd
<int16_t>(10, 2, 3), std::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
), std::nullopt
);
65 EXPECT_EQ(checkedAddUnsigned
<uint64_t>(Max
, 1), std::nullopt
);
66 EXPECT_EQ(checkedAddUnsigned
<uint64_t>(10, 1), std::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), std::nullopt
);
72 EXPECT_EQ(checkedMulUnsigned
<uint64_t>(Max
, Max
), std::nullopt
);
73 EXPECT_EQ(checkedMulUnsigned
<uint64_t>(10, 2), std::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), std::nullopt
);
79 EXPECT_EQ(checkedMulAddUnsigned
<uint64_t>(1, 1, Max
), std::nullopt
);
80 EXPECT_EQ(checkedMulAddUnsigned
<uint64_t>(10, 2, 3), std::optional
<uint64_t>(23));