Skip to content

Commit

Permalink
Update util_io so that integration tests work. Change cluster parser …
Browse files Browse the repository at this point in the history
…to read additional new lines.
  • Loading branch information
srvasude committed Jul 30, 2024
1 parent 0fcd214 commit a1af74b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will run integration tests.

name: Integration Tests
on:
push:
branches: [ main ]
pull_request:
jobs:
run_tests:
name: Run integration tests
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checking out repository
uses: actions/checkout@v4
- name: Install dependencies
run : |
sudo apt-get update && sudo apt-get install -yq clang clang-format
- name: Build everything
run: |
cd cxx && bazel build tests:all
- name: Test C++
run: |
./bazel-bin/tests/test_hirm_animals && \
./bazel-bin/tests/test_irm_two_relations && \
./bazel-bin/tests/test_misc
7 changes: 6 additions & 1 deletion cxx/tests/test_hirm_animals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cassert>
#include <random>
#include <iostream>

#include "hirm.hh"
#include "util_hash.hh"
Expand Down Expand Up @@ -36,7 +37,11 @@ int main(int argc, char** argv) {
[](const auto r) { return r->get_data().size(); }, relation);
}
}
assert(n_obs_unary == std::size(observations_unary));
size_t total_num_observations = 0;
for (const auto& [relname, observations] : observations_unary) {
total_num_observations += observations.size();
}
assert(n_obs_unary == total_num_observations);

hirm.transition_cluster_assignments_all(&prng);
hirm.transition_cluster_assignments_all(&prng);
Expand Down
28 changes: 20 additions & 8 deletions cxx/util_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,15 @@ void to_txt(std::ostream& fp, const HIRM& hirm, const T_encoding& encoding) {
}
fp << "\n";
}
fp << "\n";
fp << "\n\n";
// Write the IRMs.
int j = 0;
for (const auto& [table, rcs] : tables) {
const IRM* const irm = hirm.irms.at(table);
fp << "irm=" << table << "\n";
to_txt(fp, *irm, encoding);
if (j < std::ssize(tables) - 1) {
fp << "\n\n";
fp << "\n";
j += 1;
}
}
Expand Down Expand Up @@ -454,6 +454,10 @@ load_clusters_irm(const std::string& path) {
std::map<std::string, std::map<int, std::vector<std::string>>> clusters;
std::string line;
while (std::getline(fp, line)) {
// Ignore the new lines that are there for readability.
if (line.size() == 0) {
continue;
}
std::istringstream stream(line);

std::string domain;
Expand Down Expand Up @@ -504,9 +508,23 @@ load_clusters_hirm(const std::string& path) {

std::string line;
int irmc = 0;
// Only reset the IRM cluster when we see two newlines in a row.
bool start_of_irm_separator = false;

while (std::getline(fp, line)) {
std::istringstream stream(line);
// Skip a newline.
if (line.size() == 0) {
if (start_of_irm_separator) {
irmc = -1;
start_of_irm_separator = false;
} else {
start_of_irm_separator = true;
}
continue;
}

start_of_irm_separator = false;

std::string first;
stream >> first;
Expand All @@ -524,12 +542,6 @@ load_clusters_hirm(const std::string& path) {
continue;
}

// Skip a new line.
if (first.size() == 0) {
irmc = -1;
continue;
}

// Parse an irm= line.
if (first.rfind("irm=", 0) == 0) {
assert(irmc == -1);
Expand Down

0 comments on commit a1af74b

Please sign in to comment.