aboutsummaryrefslogtreecommitdiff
path: root/test/test_common.cpp
blob: a74d07e9d2e44b4e97ec6f6e086d797b32f2a4a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "catch.hpp"
#include "common.h"
#include <unordered_map>

TEST_CASE("load file", "[common]") {
  line_view file{"\n111\n2222\n33333\n\n", 17};
  const char* lines[] = {"\n", "111\n", "2222\n", "33333\n", "\n"};
  int i = 0;
  per_line(
      file,
      [&i](line_view l, const char* lns[]) {
        REQUIRE(l == lns[i++]);
        return true;
      },
      lines);
}

TEST_CASE("char count", "[common]") {
  line_view line{"accad1\n", 7};
  ascii_count ac{line};
  REQUIRE(ac['\n'] == 1);
  REQUIRE(ac['a'] == 2);
  REQUIRE(ac['c'] == 2);
  REQUIRE(ac['d'] == 1);
  REQUIRE(ac['1'] == 1);
}

TEST_CASE("line_view hash", "[common]") {
  std::unordered_map<line_view, int> h{{"abc", 1}, {"xyz", 2}};
  REQUIRE(h["abc"] == 1);
  REQUIRE(h["xyz"] == 2);
  REQUIRE(h.find("ab") == h.end());
}

TEST_CASE("common divisor", "[common]") {
  REQUIRE(gcd(5,5) == 5);
  REQUIRE(gcd(1,0) == 1);
  REQUIRE(gcd(0,1) == 1);
  REQUIRE(gcd(3,2) == 1);
  REQUIRE(gcd(12,8) == 4);
  REQUIRE(gcd(9,6) == 3);
}