~quf/libcs3tio

c67069e08119b3c1782f9e04a3d88c4485c983d0 — Lukas Himbert 8 months ago 1e11516
test program: check for duplicate header names
2 files changed, 56 insertions(+), 1 deletions(-)

M compile.sh
A test/duplicates.cpp
M compile.sh => compile.sh +1 -1
@@ 18,7 18,7 @@ done

ar cr build/output/libcs3tio.a build/obj/*.o build/obj-ksp/*.o

for x in compiles tio roundtrip; do
for x in compiles duplicates tio roundtrip; do
  "$CXX" -std=c++20 -W -Wall -Werror -Wfatal-errors -fsanitize=address,undefined -I src test/$x.cpp build/output/libcs3tio.a -o build/test/$x
  #build/test/$x
done

A test/duplicates.cpp => test/duplicates.cpp +55 -0
@@ 0,0 1,55 @@
// checks for header names that occur in more than one tbl file.

#include <cs3tio.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>

typedef std::set<std::string, std::less<>> set_type;
typedef std::map<std::string, set_type, std::less<>> map_type;

void insert(map_type &header_names, const std::string_view header_name, const std::string_view tbl_name) {
  auto it = header_names.find(header_name);
  if (it == header_names.end()) {
    auto [it_ins, success] = header_names.emplace(std::pair{std::string(header_name), set_type()});
    if (!success) {
      throw std::logic_error("inserting a key that's not present should be successful");
    }
    it = it_ins;
  }
  it->second.emplace(std::string(tbl_name));
}

int main(int argc, char *argv[]) {
  if (argc != 2) {
    throw std::runtime_error("expected directory name as single cli argument");
  }
  auto dir = std::filesystem::path(argv[1]);

  map_type header_names; // maps header names to the tbl files where they occur
  for (const auto &file : std::filesystem::recursive_directory_iterator(dir)) {
    if (file.is_regular_file() && file.path().extension() == ".tbl") {
      auto f = std::ifstream(file.path());
      auto fn = file.path().filename().string();
      auto tbl = cs3tio::load_tbl(f);
      for (auto const &header : tbl.headers) {
        insert(header_names, header, fn);
      }
      for (auto const &entry : tbl.entries) {
        insert(header_names, header_name(entry), fn);
      }
    }
  }

  for (const auto &pair : header_names) {
    if (pair.second.size() > 1) {
      std::cout << "- '" << pair.first << "':" << std::endl;
      for (const auto &fn : pair.second) {
        std::cout << "  '" << fn << "'" << std::endl;
      }
    }
  }
}