<commit_before>#include "base_test.hpp"

#include <algorithm>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include "../lib/storage/storage_manager.hpp"

namespace opossum {

void BaseTest::EXPECT_TABLE_EQ(const Table &tleft, const Table &tright, bool order_sensitive) {
  EXPECT_TRUE(_table_equal(tleft, tright, order_sensitive));
}

void BaseTest::ASSERT_TABLE_EQ(const Table &tleft, const Table &tright, bool order_sensitive) {
  ASSERT_TRUE(_table_equal(tleft, tright, order_sensitive));
}

void BaseTest::EXPECT_TABLE_EQ(std::shared_ptr<const Table> tleft, std::shared_ptr<const Table> tright,
                               bool order_sensitive) {
  EXPECT_TABLE_EQ(*tleft, *tright, order_sensitive);
}

void BaseTest::ASSERT_TABLE_EQ(std::shared_ptr<const Table> tleft, std::shared_ptr<const Table> tright,
                               bool order_sensitive) {
  ASSERT_TABLE_EQ(*tleft, *tright, order_sensitive);
}

BaseTest::Matrix BaseTest::_table_to_matrix(const Table &t) {
  // initialize matrix with table sizes
  Matrix matrix(t.row_count(), std::vector<AllTypeVariant>(t.col_count()));

  // set values
  unsigned row_offset = 0;
  for (ChunkID chunk_id = 0; chunk_id < t.chunk_count(); chunk_id++) {
    const Chunk &chunk = t.get_chunk(chunk_id);

    for (size_t col_id = 0; col_id < t.col_count(); ++col_id) {
      std::shared_ptr<BaseColumn> column = chunk.get_column(col_id);

      for (ChunkOffset chunk_offset = 0; chunk_offset < chunk.size(); ++chunk_offset) {
        matrix[row_offset + chunk_offset][col_id] = (*column)[chunk_offset];
      }
    }
    row_offset += chunk.size();
  }

  return matrix;
}

void BaseTest::_print_matrix(const BaseTest::Matrix &m) {
  std::cout << "-------------" << std::endl;
  for (unsigned row = 0; row < m.size(); row++) {
    for (unsigned col = 0; col < m[row].size(); col++) {
      std::cout << std::setw(8) << m[row][col] << " ";
    }
    std::cout << std::endl;
  }
  std::cout << "-------------" << std::endl;
}

::testing::AssertionResult BaseTest::_table_equal(const Table &tleft, const Table &tright, bool order_sensitive) {
  Matrix left = _table_to_matrix(tleft);
  Matrix right = _table_to_matrix(tright);
  // compare schema of tables<commit_msg>Fixing matrix conversion of empty tables<commit_after>#include "base_test.hpp"

#include <algorithm>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include "../lib/storage/storage_manager.hpp"

namespace opossum {

void BaseTest::EXPECT_TABLE_EQ(const Table &tleft, const Table &tright, bool order_sensitive) {
  EXPECT_TRUE(_table_equal(tleft, tright, order_sensitive));
}

void BaseTest::ASSERT_TABLE_EQ(const Table &tleft, const Table &tright, bool order_sensitive) {
  ASSERT_TRUE(_table_equal(tleft, tright, order_sensitive));
}

void BaseTest::EXPECT_TABLE_EQ(std::shared_ptr<const Table> tleft, std::shared_ptr<const Table> tright,
                               bool order_sensitive) {
  EXPECT_TABLE_EQ(*tleft, *tright, order_sensitive);
}

void BaseTest::ASSERT_TABLE_EQ(std::shared_ptr<const Table> tleft, std::shared_ptr<const Table> tright,
                               bool order_sensitive) {
  ASSERT_TABLE_EQ(*tleft, *tright, order_sensitive);
}

BaseTest::Matrix BaseTest::_table_to_matrix(const Table &t) {
  // initialize matrix with table sizes
  Matrix matrix(t.row_count(), std::vector<AllTypeVariant>(t.col_count()));

  // set values
  unsigned row_offset = 0;
  for (ChunkID chunk_id = 0; chunk_id < t.chunk_count(); chunk_id++) {
    const Chunk &chunk = t.get_chunk(chunk_id);

    // an empty table's chunk might be missing actual columns
    if (chunk.size() == 0) continue;

    for (size_t col_id = 0; col_id < t.col_count(); ++col_id) {
      std::shared_ptr<BaseColumn> column = chunk.get_column(col_id);

      for (ChunkOffset chunk_offset = 0; chunk_offset < chunk.size(); ++chunk_offset) {
        matrix[row_offset + chunk_offset][col_id] = (*column)[chunk_offset];
      }
    }
    row_offset += chunk.size();
  }

  return matrix;
}

void BaseTest::_print_matrix(const BaseTest::Matrix &m) {
  std::cout << "-------------" << std::endl;
  for (unsigned row = 0; row < m.size(); row++) {
    for (unsigned col = 0; col < m[row].size(); col++) {
      std::cout << std::setw(8) << m[row][col] << " ";
    }
    std::cout << std::endl;
  }
  std::cout << "-------------" << std::endl;
}

::testing::AssertionResult BaseTest::_table_equal(const Table &tleft, const Table &tright, bool order_sensitive) {
  Matrix left = _table_to_matrix(tleft);
  Matrix right = _table_to_matrix(tright);
  // compare schema of tables
{'fraction_non_alphanumeric': 0.15323787134002992, 'fraction_numerical': 0.003205813207950417, 'mean_word_length': 4.114754098360656, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5359014', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1544, 'n_words': 345}