|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include "vec/core/types.h" |
| 21 | +#include "vec/io/io_helper.h" |
| 22 | +#include "vec/io/reader_buffer.h" |
| 23 | + |
| 24 | +namespace doris::vectorized { |
| 25 | + |
| 26 | +struct CheckOverFlowTest : public testing::Test { |
| 27 | + void SetUp() override { |
| 28 | + // This function is called before each test. |
| 29 | + } |
| 30 | + |
| 31 | + void TearDown() override { |
| 32 | + // This function is called after each test. |
| 33 | + } |
| 34 | + |
| 35 | + Int128 to_i128(std::string str) { |
| 36 | + ReadBuffer rb = ReadBuffer(str.data(), str.size()); |
| 37 | + Int128 val; |
| 38 | + EXPECT_TRUE(read_int_text_impl(val, rb)); |
| 39 | + return val; |
| 40 | + }; |
| 41 | + |
| 42 | + wide::Int256 to_i256(std::string str) { return wide::Int256::_impl::from_str(str.c_str()); }; |
| 43 | +}; |
| 44 | + |
| 45 | +TEST_F(CheckOverFlowTest, test_overflow_int128) { |
| 46 | + { |
| 47 | + Int128 a = to_i128("-15687000000000000000000"); |
| 48 | + Int128 b = to_i128("11000000000000000"); |
| 49 | + Int128 c; |
| 50 | + EXPECT_TRUE(common::mul_overflow(a, b, c)); |
| 51 | + } |
| 52 | + |
| 53 | + { |
| 54 | + Int128 a = to_i128("-15687000000000000000000"); |
| 55 | + Int128 b = to_i128("-11000000000000000"); |
| 56 | + Int128 c; |
| 57 | + EXPECT_TRUE(common::mul_overflow(a, b, c)); |
| 58 | + } |
| 59 | + |
| 60 | + { |
| 61 | + Int128 a = to_i128("1000"); |
| 62 | + Int128 b = to_i128("12000"); |
| 63 | + Int128 c; |
| 64 | + EXPECT_FALSE(common::mul_overflow(a, b, c)); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +TEST_F(CheckOverFlowTest, test_overflow_int256) { |
| 69 | + { |
| 70 | + wide::Int256 a = |
| 71 | + to_i256("-11579208923731619542357098500868790785326998466564056403945758400791"); |
| 72 | + wide::Int256 b = to_i256("1157920892373161954235709850086879078532699846656405640394575"); |
| 73 | + wide::Int256 c; |
| 74 | + EXPECT_TRUE(common::mul_overflow(a, b, c)); |
| 75 | + } |
| 76 | + |
| 77 | + { |
| 78 | + wide::Int256 a = to_i256("-1157920892373161954235709850086879078532699846656405640394"); |
| 79 | + wide::Int256 b = to_i256("-1157920892373161954235709850086879078532699846656405640394"); |
| 80 | + wide::Int256 c; |
| 81 | + EXPECT_TRUE(common::mul_overflow(a, b, c)); |
| 82 | + } |
| 83 | + |
| 84 | + { |
| 85 | + wide::Int256 a = to_i256("1000"); |
| 86 | + wide::Int256 b = to_i256("12000"); |
| 87 | + wide::Int256 c; |
| 88 | + EXPECT_FALSE(common::mul_overflow(a, b, c)); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace doris::vectorized |
0 commit comments