Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed Mar 7, 2025
1 parent 5a90743 commit c361f61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
77 changes: 36 additions & 41 deletions be/test/testutil/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,79 +199,74 @@ void load_data_from_csv(const vectorized::DataTypeSerDeSPtrs serders,
std::cout << "loading data done, file: " << file_path << ", row count: " << columns[0]->size()
<< std::endl;
}
void check_or_generate_res_file(const std::string& res_file_path,
const std::vector<std::vector<std::string>>& res_column, bool is_binary) {
}

//// this is very helpful function to check data in column against expected results according different function in assert function
//// such as run regress tests
//// if FLAGS_gen_out is true, we will generate a file for check data, otherwise we will read the file to check data
//// so the key point is we should how we write assert callback function to check data,
/// and when check data is generated, we should check result to statisfy the semantic of the function
void check_or_generate_res_file(const std::string& res_file_path,
const std::vector<std::vector<std::string>>& res, bool is_binary) {
const std::vector<std::string>& res_column, bool is_binary) {
if (FLAGS_gen_out) {
std::ofstream res_file(res_file_path);
std::cout << "gen check data: " << res.size() << " with file: " << res_file_path
std::cout << "gen check data: " << res_column.size() << " rows with file: " << res_file_path
<< std::endl;
if (!res_file.is_open()) {
throw std::ios_base::failure("Failed to open file.");
}

for (const auto& row : res) {
for (size_t i = 0; i < row.size(); ++i) {
auto cell = row[i];
if (is_binary) {
size_t data_size = cell.size();
res_file.write((char*)&data_size, sizeof(data_size));
res_file.write(cell.data(), data_size);
} else {
res_file << cell;
if (i < row.size() - 1) {
res_file << ";"; // Add semicolon between columns
}
for (size_t i = 0; i < res_column.size(); ++i) {
auto cell = res_column[i];
if (is_binary) {
size_t data_size = cell.size();
res_file.write((char*)&data_size, sizeof(data_size));
res_file.write(cell.data(), data_size);
} else {
res_file << cell;
if (i < res_column.size() - 1) {
res_file << "\n";
}
}
}

res_file.close();
} else {
// we read generate file to check result
std::cout << "check data: " << res.size() << " with file: " << res_file_path << std::endl;
std::cout << "check data: " << res_column.size() << " rows with file: " << res_file_path
<< std::endl;
std::ifstream file(res_file_path);
if (!file) {
throw doris::Exception(ErrorCode::INVALID_ARGUMENT, "can not open the file: {} ",
res_file_path);
}

if (is_binary) {
for (const auto& row : res) {
for (const auto& cell : row) {
size_t real_size = cell.size();
size_t expect_row_size = 0;
file.read((char*)&expect_row_size, sizeof(expect_row_size));
EXPECT_EQ(real_size, expect_row_size);
std::string expected_data;
expected_data.resize(expect_row_size);
file.read((char*)expected_data.data(), expect_row_size);
EXPECT_EQ(cell, expected_data);
}
for (const auto& cell : res_column) {
size_t real_size = cell.size();
size_t expect_row_size = 0;
file.read((char*)&expect_row_size, sizeof(expect_row_size));
EXPECT_EQ(real_size, expect_row_size);
std::string expected_data;
expected_data.resize(expect_row_size);
file.read((char*)expected_data.data(), expect_row_size);
EXPECT_EQ(cell, expected_data);
}
} else {
size_t file_line_count = 0;
std::string line;
std::vector<std::vector<std::string>> assert_res;
while (std::getline(file, line)) {
std::vector<std::string> row;
std::stringstream lineStream(line);
std::string value;
while (std::getline(lineStream, value, ';')) {
row.push_back(value);
}
assert_res.push_back(row);
while (getline(file, line)) {
file_line_count++;
}
EXPECT_EQ(file_line_count, res_column.size());
file.clear();
file.seekg(0, std::ios::beg);

// we just do check here
for (size_t i = 0; i < res.size(); ++i) {
for (size_t j = 0; j < res[i].size(); ++j) {
EXPECT_EQ(res[i][j], assert_res[i][j]);
}
size_t line_idx = 0;
while (std::getline(file, line)) {
EXPECT_EQ(line, res_column[line_idx]) << "line mismatch: " << line_idx;
line_idx++;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions be/test/testutil/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void load_columns_data_from_file(vectorized::MutableColumns& columns,
void load_data_from_csv(const vectorized::DataTypeSerDeSPtrs serders,
vectorized::MutableColumns& columns, const std::string& file_path,
const char spliter = ';', const std::set<int> idxes = {0});
void check_or_generate_res_file(const std::string& res_file_path,
const std::vector<std::string>& res_column, bool is_binary);
void check_or_generate_res_file(const std::string& res_file_path,
const std::vector<std::vector<std::string>>& res,
bool is_binary = false);
Expand Down

0 comments on commit c361f61

Please sign in to comment.