From 93b1123581c5aab25d5bdcf68fa8e91406170acf Mon Sep 17 00:00:00 2001 From: Michael Polyntsov Date: Sun, 13 Mar 2022 02:03:55 +0300 Subject: [PATCH] Implement tests for ColumnTypedData --- tests/TestTypedColumnData.cpp | 103 ++++++++++++++++++++++++++++++++ tests/inputData/SimpleTypes.csv | 10 ++++ 2 files changed, 113 insertions(+) create mode 100644 tests/TestTypedColumnData.cpp create mode 100644 tests/inputData/SimpleTypes.csv diff --git a/tests/TestTypedColumnData.cpp b/tests/TestTypedColumnData.cpp new file mode 100644 index 0000000000..2a81f3d5f8 --- /dev/null +++ b/tests/TestTypedColumnData.cpp @@ -0,0 +1,103 @@ +#include +#include + +#include +#include + +#include "ColumnLayoutTypedRelationData.h" +#include "CSVParser.h" + +namespace tests { + +namespace fs = std::filesystem; +namespace mo = model; + +using mo::TypeId; + +struct TypeParsingParams { + std::vector expected; + std::string_view dataset; + char const sep; + bool const has_header; + TypeParsingParams(std::vector expected, + std::string_view dataset, + char const sep = ',', + bool const has_header = true) noexcept + : expected(std::move(expected)), dataset(dataset), + sep(sep), has_header(has_header) {} +}; + +class TestTypeParsing : public ::testing::TestWithParam {}; + +static inline std::vector CreateColumnData(std::string_view data, char sep, + bool has_header) { + auto const path = fs::current_path() / "inputData" / data; + CSVParser input_generator(path, sep, has_header); + std::unique_ptr relation_data = + model::ColumnLayoutTypedRelationData::CreateFrom(input_generator, true, -1, -1); + std::vector col_data = std::move(relation_data->GetColumnData()); + return col_data; +} + +TEST_P(TestTypeParsing, DefaultTest) { + TypeParsingParams const& p = GetParam(); + std::vector column_data = CreateColumnData(p.dataset, p.sep, p.has_header); + + ASSERT_EQ(column_data.size(), p.expected.size()); + + size_t i = 0; + for (mo::TypedColumnData const& col_data : column_data) { + EXPECT_EQ(col_data.GetTypeId(), p.expected[i]) << "Column index: " << i; + ++i; + } +} + +INSTANTIATE_TEST_SUITE_P( + TypeSystem, TestTypeParsing, + ::testing::Values( + TypeParsingParams({TypeId::kString, TypeId::kMixed, TypeId::kMixed}, + "WDC_appearances.csv"), + TypeParsingParams({TypeId::kString, TypeId::kString, TypeId::kString}, + "WDC_age.csv"), + TypeParsingParams({TypeId::kString, TypeId::kMixed, TypeId::kMixed, + TypeId::kMixed}, + "WDC_kepler.csv"), + TypeParsingParams({TypeId::kString, TypeId::kString, TypeId::kMixed, + TypeId::kMixed, TypeId::kMixed, TypeId::kString, + TypeId::kString, TypeId::kString}, + "WDC_satellites.csv"), + TypeParsingParams({TypeId::kString, TypeId::kString, TypeId::kInt, + TypeId::kInt, TypeId::kInt, TypeId::kInt, + TypeId::kInt, TypeId::kUndefined, TypeId::kUndefined, + TypeId::kUndefined, TypeId::kUndefined, TypeId::kInt, + TypeId::kInt, TypeId::kInt, TypeId::kInt, + TypeId::kInt, TypeId::kUndefined, TypeId::kUndefined}, + "CIPublicHighway700.csv"), + TypeParsingParams({TypeId::kInt, TypeId::kInt, TypeId::kMixed, + TypeId::kInt, TypeId::kInt, TypeId::kInt, + TypeId::kInt}, + "neighbors10k.csv"), + TypeParsingParams({TypeId::kDouble, TypeId::kDouble, TypeId::kDouble, + TypeId::kDouble, TypeId::kString}, + "iris.csv", ',', false), + TypeParsingParams({TypeId::kUndefined,TypeId::kUndefined,TypeId::kUndefined, + TypeId::kInt, TypeId::kString, TypeId::kDouble, + TypeId::kBigInt, TypeId::kMixed, TypeId::kBigInt, + TypeId::kMixed, TypeId::kInt}, + "SimpleTypes.csv"))); + +TEST(TypeSystem, SumColumnDoubles) { + std::vector col_data = CreateColumnData("iris.csv", ',', false); + ASSERT_EQ(col_data.size(), 5); + mo::TypedColumnData const& col = col_data.front(); + ASSERT_EQ(col.GetTypeId(), static_cast(TypeId::kDouble)); + mo::INumericType const& type = static_cast(col.GetType()); + std::unique_ptr sum(type.Allocate()); + for (std::byte const* value : col.GetData()) { + type.Add(sum.get(), value, sum.get()); + } + mo::Double expected = 876.5; + EXPECT_DOUBLE_EQ(type.GetValue(sum.get()), expected); +} + +} // namespace tests diff --git a/tests/inputData/SimpleTypes.csv b/tests/inputData/SimpleTypes.csv new file mode 100644 index 0000000000..9971846276 --- /dev/null +++ b/tests/inputData/SimpleTypes.csv @@ -0,0 +1,10 @@ +Undefined1,Undefined2,Undefined3,Int,String,Double,BigInt,IntAndDouble,BigIntWithInt,JustMixed,Int1 +NULL,NULL,,12341,asdb,3.142,33333333333333333333,111111,1,123,3123 +NULL,NULL,,12312341,justs0mestring,3.,33333333333173333333,3.123141,2,123.,2 +,NULL,,0,"some string within quotes",-3.142,82374045691542307589,3.,3,+202,, +NULL,NULL,,-1,"some string\" with quote",-2312.,100000000000000000000,1111,4,0,NULL +,NULL,,-10101,something,8321937193.0,NULL,7,170317031703170317031703,NULL,NULL +,NULL,,1111111111111111111,"something with \n",1.1,98765432112345678909876,8,6,suddenly,NULL +NULL,NULL,,17238917293719,le le le le le,2.0,170317031703170317031703,9,7,bigint,3 +NULL,NULL,,+123123828282,31a231234,+3.,1337733113377331133773311337,10,8,smallint,+3 +,NULL,,1337,sssssnake,+3.89,30000000000000000004,11,9,,-11