1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 11:42:57 +01:00
llvm-mirror/unittests/Support/CheckedArithmeticTest.cpp
George Karpenkov 2d8ec49fc8 Utility functions for checked arithmetic
Provide checkedAdd and checkedMul functions, providing checked
arithmetic on signed integers.

Differential Revision: https://reviews.llvm.org/D43704

llvm-svn: 326516
2018-03-02 00:30:43 +00:00

72 lines
2.4 KiB
C++

#include "llvm/Support/CheckedArithmetic.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(CheckedArithmetic, CheckedAdd) {
int64_t Out;
const int64_t Max = std::numeric_limits<int64_t>::max();
const int64_t Min = std::numeric_limits<int64_t>::min();
EXPECT_EQ(checkedAdd<int64_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedAdd<int64_t>(Min, -1, &Out), true);
EXPECT_EQ(checkedAdd<int64_t>(Max, 1, &Out), true);
EXPECT_EQ(checkedAdd<int64_t>(10, 1, &Out), false);
EXPECT_EQ(Out, 11);
}
TEST(CheckedArithmetic, CheckedAddSmall) {
int16_t Out;
const int16_t Max = std::numeric_limits<int16_t>::max();
const int16_t Min = std::numeric_limits<int16_t>::min();
EXPECT_EQ(checkedAdd<int16_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedAdd<int16_t>(Min, -1, &Out), true);
EXPECT_EQ(checkedAdd<int16_t>(Max, 1, &Out), true);
EXPECT_EQ(checkedAdd<int16_t>(10, 1, &Out), false);
EXPECT_EQ(Out, 11);
}
TEST(CheckedArithmetic, CheckedMul) {
int64_t Out;
const int64_t Max = std::numeric_limits<int64_t>::max();
const int64_t Min = std::numeric_limits<int64_t>::min();
EXPECT_EQ(checkedMul<int64_t>(Max, 2, &Out), true);
EXPECT_EQ(checkedMul<int64_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedMul<int64_t>(Min, 2, &Out), true);
EXPECT_EQ(checkedMul<int64_t>(10, 2, &Out), false);
EXPECT_EQ(Out, 20);
}
TEST(CheckedArithmetic, CheckedMulSmall) {
int16_t Out;
const int16_t Max = std::numeric_limits<int16_t>::max();
const int16_t Min = std::numeric_limits<int16_t>::min();
EXPECT_EQ(checkedMul<int16_t>(Max, 2, &Out), true);
EXPECT_EQ(checkedMul<int16_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedMul<int16_t>(Min, 2, &Out), true);
EXPECT_EQ(checkedMul<int16_t>(10, 2, &Out), false);
EXPECT_EQ(Out, 20);
}
TEST(CheckedArithmetic, CheckedAddUnsigned) {
uint64_t Out;
const uint64_t Max = std::numeric_limits<uint64_t>::max();
EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1, &Out), true);
EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1, &Out), false);
EXPECT_EQ(Out, uint64_t(11));
}
TEST(CheckedArithmetic, CheckedMulUnsigned) {
uint64_t Out;
const uint64_t Max = std::numeric_limits<uint64_t>::max();
EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2, &Out), true);
EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max, &Out), true);
EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2, &Out), false);
EXPECT_EQ(Out, uint64_t(20));
}
} // namespace