|
16 | 16 | MA 02110-1301, USA. */ |
17 | 17 |
|
18 | 18 | #include <gtest/gtest.h> // googletest header file |
19 | | -#include "mcs_basic_types.h" |
| 19 | +#include "arithmeticoperator.h" |
| 20 | +#include "exceptclasses.h" |
| 21 | +#include "constantcolumn.h" |
| 22 | +#include "parsetree.h" |
| 23 | +#include "rowgroup.h" |
| 24 | +#include <limits> |
| 25 | +#include <memory> |
20 | 26 |
|
21 | | -// using CSCDataType = execplan::CalpontSystemCatalog::ColDataType; |
| 27 | +using namespace execplan; |
| 28 | + |
| 29 | +class ArithmeticOperatorOverflowTest : public ::testing::Test |
| 30 | +{ |
| 31 | + protected: |
| 32 | + void SetUp() override |
| 33 | + { |
| 34 | + // Create a simple row for testing |
| 35 | + row.initToNull(); |
| 36 | + } |
| 37 | + |
| 38 | + rowgroup::Row row; |
| 39 | +}; |
| 40 | + |
| 41 | +// Test ArithmeticOperator overflow handling through public interface |
| 42 | +TEST_F(ArithmeticOperatorOverflowTest, SignedAdditionOverflow) |
| 43 | +{ |
| 44 | + ArithmeticOperator op("+"); |
| 45 | + CalpontSystemCatalog::ColType colType; |
| 46 | + colType.colDataType = CalpontSystemCatalog::BIGINT; |
| 47 | + colType.colWidth = 8; |
| 48 | + op.operationType(colType); |
| 49 | + |
| 50 | + // Create constant columns with overflow values (heap allocated for ParseTree) |
| 51 | + auto left = |
| 52 | + new ConstantColumn(static_cast<int64_t>(std::numeric_limits<int64_t>::max()), ConstantColumn::NUM); |
| 53 | + auto right = new ConstantColumn(static_cast<int64_t>(1), ConstantColumn::NUM); |
| 54 | + |
| 55 | + ParseTree leftTree(left); |
| 56 | + ParseTree rightTree(right); |
| 57 | + |
| 58 | + bool isNull = false; |
| 59 | + |
| 60 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 61 | + EXPECT_THROW({ op.getIntVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 62 | +} |
| 63 | + |
| 64 | +TEST_F(ArithmeticOperatorOverflowTest, SignedSubtractionUnderflow) |
| 65 | +{ |
| 66 | + ArithmeticOperator op("-"); |
| 67 | + CalpontSystemCatalog::ColType colType; |
| 68 | + colType.colDataType = CalpontSystemCatalog::BIGINT; |
| 69 | + colType.colWidth = 8; |
| 70 | + op.operationType(colType); |
| 71 | + |
| 72 | + // Create constant columns with underflow values (heap allocated for ParseTree) |
| 73 | + auto left = |
| 74 | + new ConstantColumn(static_cast<int64_t>(std::numeric_limits<int64_t>::min()), ConstantColumn::NUM); |
| 75 | + auto right = new ConstantColumn(static_cast<int64_t>(1), ConstantColumn::NUM); |
| 76 | + |
| 77 | + ParseTree leftTree(left); |
| 78 | + ParseTree rightTree(right); |
| 79 | + |
| 80 | + bool isNull = false; |
| 81 | + |
| 82 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 83 | + EXPECT_THROW({ op.getIntVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(ArithmeticOperatorOverflowTest, SignedMultiplicationOverflow) |
| 87 | +{ |
| 88 | + ArithmeticOperator op("*"); |
| 89 | + CalpontSystemCatalog::ColType colType; |
| 90 | + colType.colDataType = CalpontSystemCatalog::BIGINT; |
| 91 | + colType.colWidth = 8; |
| 92 | + op.operationType(colType); |
| 93 | + |
| 94 | + // Create constant columns with overflow values (heap allocated for ParseTree) |
| 95 | + auto left = new ConstantColumn(static_cast<int64_t>(16000000000000000LL), ConstantColumn::NUM); |
| 96 | + auto right = new ConstantColumn(static_cast<int64_t>(781), ConstantColumn::NUM); |
| 97 | + |
| 98 | + ParseTree leftTree(left); |
| 99 | + ParseTree rightTree(right); |
| 100 | + |
| 101 | + bool isNull = false; |
| 102 | + |
| 103 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 104 | + EXPECT_THROW({ op.getIntVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(ArithmeticOperatorOverflowTest, UnsignedAdditionOverflow) |
| 108 | +{ |
| 109 | + ArithmeticOperator op("+"); |
| 110 | + CalpontSystemCatalog::ColType colType; |
| 111 | + colType.colDataType = CalpontSystemCatalog::UBIGINT; |
| 112 | + colType.colWidth = 8; |
| 113 | + op.operationType(colType); |
| 114 | + |
| 115 | + // Create constant columns with overflow values (heap allocated for ParseTree) |
| 116 | + auto left = new ConstantColumn(static_cast<uint64_t>(std::numeric_limits<uint64_t>::max()), |
| 117 | + ConstantColumn::NUM, 0, 0); |
| 118 | + auto right = new ConstantColumn(static_cast<uint64_t>(1), ConstantColumn::NUM, 0, 0); |
| 119 | + |
| 120 | + ParseTree leftTree(left); |
| 121 | + ParseTree rightTree(right); |
| 122 | + |
| 123 | + bool isNull = false; |
| 124 | + |
| 125 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 126 | + EXPECT_THROW({ op.getUintVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(ArithmeticOperatorOverflowTest, UnsignedSubtractionUnderflow) |
| 130 | +{ |
| 131 | + ArithmeticOperator op("-"); |
| 132 | + CalpontSystemCatalog::ColType colType; |
| 133 | + colType.colDataType = CalpontSystemCatalog::UBIGINT; |
| 134 | + colType.colWidth = 8; |
| 135 | + op.operationType(colType); |
| 136 | + |
| 137 | + // Create constant columns with underflow values (heap allocated for ParseTree) |
| 138 | + auto left = new ConstantColumn(static_cast<uint64_t>(0), ConstantColumn::NUM, 0, 0); |
| 139 | + auto right = new ConstantColumn(static_cast<uint64_t>(1), ConstantColumn::NUM, 0, 0); |
| 140 | + |
| 141 | + ParseTree leftTree(left); |
| 142 | + ParseTree rightTree(right); |
| 143 | + |
| 144 | + bool isNull = false; |
| 145 | + |
| 146 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 147 | + EXPECT_THROW({ op.getUintVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 148 | +} |
| 149 | + |
| 150 | +TEST_F(ArithmeticOperatorOverflowTest, UnsignedMultiplicationOverflow) |
| 151 | +{ |
| 152 | + ArithmeticOperator op("*"); |
| 153 | + CalpontSystemCatalog::ColType colType; |
| 154 | + colType.colDataType = CalpontSystemCatalog::UBIGINT; |
| 155 | + colType.colWidth = 8; |
| 156 | + op.operationType(colType); |
| 157 | + |
| 158 | + // Create constant columns with overflow values (heap allocated for ParseTree) |
| 159 | + auto left = new ConstantColumn(static_cast<uint64_t>(std::numeric_limits<uint64_t>::max()), |
| 160 | + ConstantColumn::NUM, 0, 0); |
| 161 | + auto right = new ConstantColumn(static_cast<uint64_t>(2), ConstantColumn::NUM, 0, 0); |
| 162 | + |
| 163 | + ParseTree leftTree(left); |
| 164 | + ParseTree rightTree(right); |
| 165 | + |
| 166 | + bool isNull = false; |
| 167 | + |
| 168 | + // Should throw IDBExcept with ERR_FUNC_OUT_OF_RANGE_RESULT |
| 169 | + EXPECT_THROW({ op.getUintVal(row, isNull, &leftTree, &rightTree); }, logging::IDBExcept); |
| 170 | +} |
| 171 | + |
| 172 | +TEST_F(ArithmeticOperatorOverflowTest, ValidOperationsNoThrow) |
| 173 | +{ |
| 174 | + ArithmeticOperator addOp("+"); |
| 175 | + CalpontSystemCatalog::ColType colType; |
| 176 | + colType.colDataType = CalpontSystemCatalog::BIGINT; |
| 177 | + colType.colWidth = 8; |
| 178 | + addOp.operationType(colType); |
| 179 | + |
| 180 | + // Create constant columns with normal values (heap allocated for ParseTree) |
| 181 | + auto left = new ConstantColumn(static_cast<int64_t>(100), ConstantColumn::NUM); |
| 182 | + auto right = new ConstantColumn(static_cast<int64_t>(200), ConstantColumn::NUM); |
| 183 | + |
| 184 | + ParseTree leftTree(left); |
| 185 | + ParseTree rightTree(right); |
| 186 | + |
| 187 | + bool isNull = false; |
| 188 | + |
| 189 | + // Should not throw and return correct result |
| 190 | + EXPECT_NO_THROW({ |
| 191 | + int64_t result = addOp.getIntVal(row, isNull, &leftTree, &rightTree); |
| 192 | + EXPECT_EQ(result, 300LL); |
| 193 | + EXPECT_FALSE(isNull); |
| 194 | + }); |
| 195 | +} |
| 196 | + |
| 197 | +TEST_F(ArithmeticOperatorOverflowTest, DivisionByZero) |
| 198 | +{ |
| 199 | + ArithmeticOperator divOp("/"); |
| 200 | + CalpontSystemCatalog::ColType colType; |
| 201 | + colType.colDataType = CalpontSystemCatalog::BIGINT; |
| 202 | + colType.colWidth = 8; |
| 203 | + divOp.operationType(colType); |
| 204 | + |
| 205 | + // Create constant columns for division by zero (heap allocated for ParseTree) |
| 206 | + auto left = new ConstantColumn(static_cast<int64_t>(100), ConstantColumn::NUM); |
| 207 | + auto right = new ConstantColumn(static_cast<int64_t>(0), ConstantColumn::NUM); |
| 208 | + |
| 209 | + ParseTree leftTree(left); |
| 210 | + ParseTree rightTree(right); |
| 211 | + |
| 212 | + bool isNull = false; |
| 213 | + |
| 214 | + // Division by zero should set isNull to true, not throw |
| 215 | + EXPECT_NO_THROW({ |
| 216 | + divOp.getIntVal(row, isNull, &leftTree, &rightTree); |
| 217 | + EXPECT_TRUE(isNull); |
| 218 | + }); |
| 219 | +} |
22 | 220 |
|
23 | 221 | TEST(SimpleCheck, check1) |
24 | 222 | { |
|
0 commit comments