# Changeset View

Changeset View

# Standalone View

Standalone View

# unittests/Support/CheckedArithmeticTest.cpp

- This file was added.

1 | #include "llvm/Support/CheckedArithmetic.h" | ||||
---|---|---|---|---|---|

2 | #include "gtest/gtest.h" | ||||

3 | | ||||

4 | using namespace llvm; | ||||

5 | | ||||

6 | namespace { | ||||

7 | | ||||

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), true); | ||||

12 | EXPECT_EQ(checkedAdd<int64_t>(Min, -1), true); | ||||

13 | EXPECT_EQ(checkedAdd<int64_t>(Max, 1), true); | ||||

14 | int64_t Out; | ||||

15 | EXPECT_EQ(checkedAdd<int64_t>(10, 1, &Out), false); | ||||

16 | EXPECT_EQ(Out, 11); | ||||

17 | } | ||||

18 | | ||||

19 | TEST(CheckedArithmetic, CheckedAddSmall) { | ||||

20 | const int16_t Max = std::numeric_limits<int16_t>::max(); | ||||

21 | const int16_t Min = std::numeric_limits<int16_t>::min(); | ||||

22 | EXPECT_EQ(checkedAdd<int16_t>(Max, Max), true); | ||||

23 | EXPECT_EQ(checkedAdd<int16_t>(Min, -1), true); | ||||

24 | EXPECT_EQ(checkedAdd<int16_t>(Max, 1), true); | ||||

25 | int16_t Out; | ||||

26 | EXPECT_EQ(checkedAdd<int16_t>(10, 1, &Out), false); | ||||

27 | EXPECT_EQ(Out, 11); | ||||

28 | } | ||||

29 | | ||||

30 | TEST(CheckedArithmetic, CheckedMul) { | ||||

31 | const int64_t Max = std::numeric_limits<int64_t>::max(); | ||||

32 | const int64_t Min = std::numeric_limits<int64_t>::min(); | ||||

33 | EXPECT_EQ(checkedMul<int64_t>(Max, 2), true); | ||||

34 | EXPECT_EQ(checkedMul<int64_t>(Max, Max), true); | ||||

35 | EXPECT_EQ(checkedMul<int64_t>(Min, 2), true); | ||||

36 | int64_t Out; | ||||

37 | EXPECT_EQ(checkedMul<int64_t>(10, 2, &Out), false); | ||||

38 | EXPECT_EQ(Out, 20); | ||||

39 | } | ||||

40 | | ||||

41 | TEST(CheckedArithmetic, CheckedMulSmall) { | ||||

42 | const int16_t Max = std::numeric_limits<int16_t>::max(); | ||||

43 | const int16_t Min = std::numeric_limits<int16_t>::min(); | ||||

44 | EXPECT_EQ(checkedMul<int16_t>(Max, 2), true); | ||||

45 | EXPECT_EQ(checkedMul<int16_t>(Max, Max), true); | ||||

46 | EXPECT_EQ(checkedMul<int16_t>(Min, 2), true); | ||||

47 | int16_t Out; | ||||

48 | EXPECT_EQ(checkedMul<int16_t>(10, 2, &Out), false); | ||||

49 | EXPECT_EQ(Out, 20); | ||||

50 | } | ||||

51 | | ||||

52 | TEST(CheckedArithmetic, CheckedAddUnsigned) { | ||||

53 | const uint64_t Max = std::numeric_limits<uint64_t>::max(); | ||||

54 | EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max), true); | ||||

55 | EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1), true); | ||||

56 | uint64_t Out; | ||||

57 | EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1, &Out), false); | ||||

58 | EXPECT_EQ(Out, (uint64_t)11); | ||||

59 | } | ||||

60 | | ||||

61 | TEST(CheckedArithmetic, CheckedMulUnsigned) { | ||||

62 | const uint64_t Max = std::numeric_limits<uint64_t>::max(); | ||||

63 | EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2), true); | ||||

64 | EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max), true); | ||||

65 | uint64_t Out; | ||||

66 | EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2, &Out), false); | ||||

67 | EXPECT_EQ(Out, (uint64_t)20); | ||||

68 | } | ||||

69 | | ||||

70 | | ||||

71 | } // namespace |