Skip to content

Commit

Permalink
[UT](test) add BE UT test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangstar333 committed Jun 4, 2024
1 parent df144a3 commit 07b8622
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 0 deletions.
77 changes: 77 additions & 0 deletions be/test/vec/core/accurate_comparison_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>

#include <string>

#include "gtest/gtest_pred_impl.h"
#include "vec/core/accurate_comparison.h"

namespace doris::vectorized {
TEST(VAccurateComparison, TestsOP) {
ASSERT_TRUE(accurate::equalsOp(static_cast<Float32>(123), static_cast<UInt64>(123)));
ASSERT_TRUE(accurate::lessOp(static_cast<Float32>(123), static_cast<UInt64>(124)));
ASSERT_TRUE(accurate::lessOp(static_cast<Float32>(-1), static_cast<UInt64>(1)));
ASSERT_TRUE(accurate::lessOp(static_cast<Int64>(-1), static_cast<UInt64>(1)));
ASSERT_TRUE(!accurate::equalsOp(static_cast<Int64>(-1), static_cast<UInt64>(-1)));
ASSERT_TRUE(accurate::equalsOp(-0., 0));
ASSERT_TRUE(accurate::lessOp(-0., 1));
ASSERT_TRUE(accurate::lessOp(-0.5, 1));
ASSERT_TRUE(accurate::lessOp(0.5, 1));
ASSERT_TRUE(accurate::equalsOp(1.0, 1));
ASSERT_TRUE(accurate::greaterOp(1.1, 1));
ASSERT_TRUE(accurate::greaterOp(11.1, 1));
ASSERT_TRUE(accurate::greaterOp(11.1, 11));
ASSERT_TRUE(accurate::lessOp(-11.1, 11));
ASSERT_TRUE(accurate::lessOp(-11.1, -11));
ASSERT_TRUE(accurate::lessOp(-1.1, -1));
ASSERT_TRUE(accurate::greaterOp(-1.1, -2));
ASSERT_TRUE(accurate::greaterOp(1000., 100));
ASSERT_TRUE(accurate::greaterOp(-100., -1000));
ASSERT_TRUE(accurate::lessOp(100., 1000));
ASSERT_TRUE(accurate::lessOp(-1000., -100));

ASSERT_TRUE(accurate::lessOp(-std::numeric_limits<Float64>::infinity(), 0));
ASSERT_TRUE(accurate::lessOp(-std::numeric_limits<Float64>::infinity(), 1000));
ASSERT_TRUE(accurate::lessOp(-std::numeric_limits<Float64>::infinity(), -1000));
ASSERT_TRUE(accurate::greaterOp(std::numeric_limits<Float64>::infinity(), 0));
ASSERT_TRUE(accurate::greaterOp(std::numeric_limits<Float64>::infinity(), 1000));
ASSERT_TRUE(accurate::greaterOp(std::numeric_limits<Float64>::infinity(), -1000));

ASSERT_TRUE(accurate::lessOp(1, 1e100));
ASSERT_TRUE(accurate::lessOp(-1, 1e100));
ASSERT_TRUE(accurate::lessOp(-1e100, 1));
ASSERT_TRUE(accurate::lessOp(-1e100, -1));

ASSERT_TRUE(accurate::equalsOp(static_cast<UInt64>(9223372036854775808ULL), static_cast<Float64>(9223372036854775808ULL)));
ASSERT_TRUE(accurate::equalsOp(static_cast<UInt64>(9223372036854775808ULL), static_cast<Float32>(9223372036854775808ULL)));

ASSERT_TRUE(accurate::greaterOp(static_cast<UInt64>(9223372036854776000ULL), static_cast<Float64>(9223372036854776000ULL)));
ASSERT_TRUE(accurate::lessOp(static_cast<UInt64>(9223372000000000000ULL), static_cast<Float32>(9223372000000000000ULL)));

ASSERT_TRUE(accurate::equalsOp(static_cast<Float32>(9223372036854775808ULL), static_cast<Float64>(9223372036854775808ULL)));

ASSERT_TRUE(accurate::lessOp(static_cast<UInt8>(255), 300));
ASSERT_TRUE(accurate::lessOp(static_cast<UInt8>(255), static_cast<Int16>(300)));
ASSERT_TRUE(accurate::notEqualsOp(static_cast<UInt8>(255), 44));
ASSERT_TRUE(accurate::notEqualsOp(static_cast<UInt8>(255), static_cast<Int16>(44)));
}

} // namespace doris::vectorized
46 changes: 46 additions & 0 deletions be/test/vec/core/field_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>

#include <string>

#include "gtest/gtest_pred_impl.h"
#include "vec/core/field.h"
#include "vec/core/types.h"

namespace doris::vectorized {
TEST(VFieldTest, field_string) {
Field f;

f = Field {String {"Hello, world (1)"}};
ASSERT_EQ(f.get<String>(), "Hello, world (1)");
f = Field {String {"Hello, world (2)"}};
ASSERT_EQ(f.get<String>(), "Hello, world (2)");
f = Field {Array {Field {String {"Hello, world (3)"}}}};
ASSERT_EQ(f.get<Array>()[0].get<String>(), "Hello, world (3)");
f = String {"Hello, world (4)"};
ASSERT_EQ(f.get<String>(), "Hello, world (4)");
f = Array {Field {String {"Hello, world (5)"}}};
ASSERT_EQ(f.get<Array>()[0].get<String>(), "Hello, world (5)");
f = Array {String {"Hello, world (6)"}};
ASSERT_EQ(f.get<Array>()[0].get<String>(), "Hello, world (6)");
}

} // namespace doris::vectorized
153 changes: 153 additions & 0 deletions be/test/vec/core/get_common_type_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>

#include <string>

#include "gtest/gtest_pred_impl.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/data_type_nothing.h"
#include "vec/data_types/data_type_number.h"
#include "vec/data_types/data_type_string.h"
#include "vec/data_types/data_type_time_v2.h"
#include "vec/data_types/get_least_supertype.h"

namespace doris::vectorized {

static bool operator==(const IDataType& left, const IDataType& right) {
return left.equals(right);
}

} // namespace doris::vectorized

using namespace doris::vectorized;

static DataTypePtr typeFromString(const std::string& str) {
if (str == "Nothing") {
return std::make_shared<DataTypeNothing>();
} else if (str == "UInt8") {
return std::make_shared<DataTypeUInt8>();
} else if (str == "UInt16") {
return std::make_shared<DataTypeUInt32>();
} else if (str == "UInt32") {
return std::make_shared<DataTypeUInt32>();
} else if (str == "UInt64") {
return std::make_shared<DataTypeUInt64>();
} else if (str == "Int8") {
return std::make_shared<DataTypeInt8>();
} else if (str == "Int16") {
return std::make_shared<DataTypeInt16>();
} else if (str == "Int32") {
return std::make_shared<DataTypeInt32>();
} else if (str == "Int64") {
return std::make_shared<DataTypeInt64>();
} else if (str == "Float32") {
return std::make_shared<DataTypeFloat32>();
} else if (str == "Float64") {
return std::make_shared<DataTypeFloat64>();
} else if (str == "Date") {
return std::make_shared<DataTypeDateV2>();
} else if (str == "DateTime") {
return std::make_shared<DataTypeDateTimeV2>();
} else if (str == "String") {
return std::make_shared<DataTypeString>();
}
return nullptr;
}

static auto typesFromString(const std::string& str) {
std::istringstream data_types_stream(str); // STYLE_CHECK_ALLOW_STD_STRING_STREAM
DataTypes data_types;
std::string data_type;
while (data_types_stream >> data_type) {
data_types.push_back(typeFromString(data_type));
}

return data_types;
}

struct TypesTestCase {
const char* from_types = nullptr;
const char* expected_type = nullptr;
};

std::ostream& operator<<(std::ostream& ostr, const TypesTestCase& test_case) {
ostr << "TypesTestCase{\"" << test_case.from_types << "\", ";
if (test_case.expected_type) {
ostr << "\"" << test_case.expected_type << "\"";
} else {
ostr << "nullptr";
}
return ostr << "}";
}

class TypeTest : public ::testing::TestWithParam<TypesTestCase> {
public:
void SetUp() override {
const auto& p = GetParam();
from_types = typesFromString(p.from_types);

if (p.expected_type) {
expected_type = typeFromString(p.expected_type);
} else {
expected_type.reset();
}
}

DataTypes from_types;
DataTypePtr expected_type;
};

class LeastSuperTypeTest : public TypeTest {};

TEST_P(LeastSuperTypeTest, getLeastSupertype) {
DataTypePtr result_type;
if (this->expected_type) {
get_least_supertype(this->from_types, &result_type);
std::cout << std::endl
<< " " << this->expected_type->get_name() << " " << result_type->get_name()
<< std::endl;
ASSERT_EQ(*(this->expected_type), *result_type);
} else {
EXPECT_ANY_THROW(get_least_supertype(this->from_types, &result_type));
}
}

INSTANTIATE_TEST_SUITE_P(data_type, LeastSuperTypeTest,
::testing::ValuesIn(std::initializer_list<TypesTestCase> {
{"Nothing", "Nothing"},
{"UInt8", "UInt8"},
{"UInt8 UInt8", "UInt8"},
{"Int8 Int8", "Int8"},
{"UInt8 Int8", "Int16"},
{"UInt8 Int16", "Int16"},
{"UInt8 UInt32 UInt64", "UInt64"},
{"Int8 Int32 Int64", "Int64"},
{"UInt8 UInt32 Int64", "Int64"},
{"Float32 Float64", "Float64"},
{"Float32 UInt16 Int32", "Float64"},
{"Float32 Int16 UInt32", "Float64"},
{"Date Date", "Date"},
{"DateTime DateTime", "DateTime"},
{"String String String", "String"},
{"Int8 String", nullptr},
{"Int64 UInt64", nullptr},
{"Float32 UInt64", nullptr},
{"Float64 Int64", nullptr}}));
83 changes: 83 additions & 0 deletions be/test/vec/core/number_traits_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>

#include <string>

#include "gtest/gtest_pred_impl.h"
#include "vec/core/types.h"
#include "vec/data_types/number_traits.h"

namespace doris::vectorized {

static std::string getTypeString(UInt8) {
return "UInt8";
}
static std::string getTypeString(UInt32) {
return "UInt32";
}
static std::string getTypeString(UInt64) {
return "UInt64";
}
static std::string getTypeString(Int8) {
return "Int8";
}
static std::string getTypeString(Int16) {
return "Int16";
}
static std::string getTypeString(Int32) {
return "Int32";
}
static std::string getTypeString(Int64) {
return "Int64";
}
static std::string getTypeString(Float32) {
return "Float32";
}
static std::string getTypeString(Float64) {
return "Float64";
}
void test() {
std::cout<<getTypeString(UInt8(0))<<" "<<getTypeString(UInt16(0))<<" "<<getTypeString(UInt32(0))<<" "<<getTypeString(UInt64(0))<<" "<<getTypeString(Float32(0))<<" ";
}

TEST(VNumberTraits, ResultOfAdditionMultiplication)
{
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfAdditionMultiplication<UInt8, UInt8>::Type()), "Int32");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfAdditionMultiplication<UInt8, Int32>::Type()), "Int64");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfAdditionMultiplication<UInt8, Float32>::Type()), "Float64");
}

TEST(NumberTraits, ResultOfSubtraction)
{
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfSubtraction<UInt8, UInt8>::Type()), "Int16");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfSubtraction<UInt16, UInt8>::Type()), "Int32");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfSubtraction<UInt16, Int8>::Type()), "Int32");
}

TEST(NumberTraits, Others)
{
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfFloatingPointDivision<UInt16, Int16>::Type()), "Float64");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfFloatingPointDivision<UInt32, Int16>::Type()), "Float64");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfIntegerDivision<UInt8, Int16>::Type()), "Int8");
ASSERT_EQ(getTypeString(doris::vectorized::NumberTraits::ResultOfModulo<UInt32, Int8>::Type()), "UInt8");
}

} // namespace doris::vectorized

0 comments on commit 07b8622

Please sign in to comment.