aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common.h22
-rw-r--r--test/CMakeLists.txt1
-rw-r--r--test/test_2015.cpp5
-rw-r--r--test/test_common.cpp15
4 files changed, 41 insertions, 2 deletions
diff --git a/src/common.h b/src/common.h
index 93da70b..35f9b0e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -1,18 +1,36 @@
#pragma once
+#include <iostream>
#include <stdlib.h>
#include <utility>
struct line_view {
const char* line;
size_t length;
+
+ friend std::ostream& operator<<(std::ostream& o, const line_view& lv) {
+ for (size_t i = 0; i < lv.length; i++) {
+ o << lv.line[i];
+ }
+ return o;
+ }
+
+ bool operator==(const char* l) const noexcept {
+ size_t i = 0;
+ while (i < this->length) {
+ if (l[i] != this->line[i]) {
+ return false;
+ }
+ i++;
+ }
+ return l[i] == '\0';
+ }
};
line_view load_file(const char*);
line_view next_line(line_view, size_t*);
-template <typename F, typename... Args>
-void per_line(line_view file, F&& f, Args&&... args) {
+template <typename F, typename... Args> void per_line(line_view file, F&& f, Args&&... args) {
size_t offset = 0;
line_view lv;
do {
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7410cdd..b91258c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,5 +1,6 @@
set(TEST_FILES
catch_main.cpp
+ test_common.cpp
test_2015.cpp
)
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index e69de29..67e25f0 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -0,0 +1,5 @@
+#include "catch.hpp"
+
+TEST_CASE("", "[]") {
+
+}
diff --git a/test/test_common.cpp b/test/test_common.cpp
new file mode 100644
index 0000000..141398b
--- /dev/null
+++ b/test/test_common.cpp
@@ -0,0 +1,15 @@
+#include "catch.hpp"
+#include "common.h"
+
+TEST_CASE("load file", "[]") {
+ line_view file{"\n\n111\n2222\n33333\n", 17};
+ const char* lines[] = {"\n", "\n", "111\n", "2222\n", "33333\n"};
+ int i = 0;
+ per_line(
+ file,
+ [&i](line_view l, const char* lns[]) {
+ REQUIRE(l == lns[i++]);
+ return true;
+ },
+ lines);
+}