Skip to content

Commit

Permalink
Implement tests for ColumnTypedData
Browse files Browse the repository at this point in the history
  • Loading branch information
polyntsov authored and Mstrutov committed Mar 25, 2022
1 parent 90570f1 commit 93b1123
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
103 changes: 103 additions & 0 deletions tests/TestTypedColumnData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <filesystem>
#include <memory>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "ColumnLayoutTypedRelationData.h"
#include "CSVParser.h"

namespace tests {

namespace fs = std::filesystem;
namespace mo = model;

using mo::TypeId;

struct TypeParsingParams {
std::vector<mo::TypeId> expected;
std::string_view dataset;
char const sep;
bool const has_header;
TypeParsingParams(std::vector<mo::TypeId> 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<TypeParsingParams> {};

static inline std::vector<mo::TypedColumnData> 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<model::ColumnLayoutTypedRelationData> relation_data =
model::ColumnLayoutTypedRelationData::CreateFrom(input_generator, true, -1, -1);
std::vector<mo::TypedColumnData> col_data = std::move(relation_data->GetColumnData());
return col_data;
}

TEST_P(TestTypeParsing, DefaultTest) {
TypeParsingParams const& p = GetParam();
std::vector<mo::TypedColumnData> 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<mo::TypedColumnData> 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>(TypeId::kDouble));
mo::INumericType const& type = static_cast<mo::INumericType const&>(col.GetType());
std::unique_ptr<std::byte[]> 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<mo::Double>(sum.get()), expected);
}

} // namespace tests
10 changes: 10 additions & 0 deletions tests/inputData/SimpleTypes.csv
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 93b1123

Please sign in to comment.