commit 308d8d99267d1915fedf5f06532e0c1ccca1f5f6
parent 6eb4dd815927cb9bd913a744c2a9574360cd45ec
Author: Walther Chen <walther.chen@gmail.com>
Date: Fri, 1 Nov 2024 09:35:42 -0400
test module, factor out expect_equal_slices_sorted, fix it
Diffstat:
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/justfile b/justfile
@@ -6,7 +6,7 @@
# redirects outputs to stderr
@build problem *args="":
- c3c compile -O3 {{problem}}.c3 util.c3 1>&2
+ c3c compile -O3 {{problem}}.c3 util.c3 test.c3 1>&2
# using compile-run prints a bunch of logs
run problem *args="":
diff --git a/test.c3 b/test.c3
@@ -0,0 +1,14 @@
+module test @test;
+
+macro void expect_equal_slices_sorted(expected, xs) {
+ assert(xs.len == expected.len, "Expected %s, found %s", expected, xs);
+ foreach (x : xs) {
+ bool found = false;
+ foreach (exp : expected) {
+ if (exp == x) {
+ found = true;
+ }
+ }
+ assert(found, "Expected %s, found %s", expected, xs);
+ }
+}
diff --git a/util.c3 b/util.c3
@@ -4,6 +4,8 @@ import std::core::string;
import std::io;
import std::sort;
+import test;
+
def FrequencyTable = HashMap(<String, int>);
// counts of kmers in a text.
@@ -192,14 +194,7 @@ fn void test_frequent_words_with_mismatches() @test {
};
foreach (t : tests) {
String[] matches = frequent_words_with_mismatches(t.text, t.k, t.d);
- assert(matches.len == t.expected.len, "Expected %s, found %s", t.expected, matches);
- bool found = false;
- foreach (match : matches) {
- foreach (exp : t.expected) {
- found = true;
- }
- }
- assert(found, "Expected %s, found %s", t.expected, matches);
+ test::expect_equal_slices_sorted(t.expected, matches);
}
}
@@ -288,13 +283,8 @@ fn void test_neighbors() @test {
};
foreach (t : tests) {
String[] matches = neighbors(t.text, t.d);
- assert(matches.len == t.expected.len, "Expected %s, found %s", t.expected, matches);
- bool found = false;
- foreach (match : matches) {
- foreach (exp : t.expected) {
- found = true;
- }
- }
- assert(found, "Expected %s, found %s", t.expected, matches);
+ test::expect_equal_slices_sorted(t.expected, matches);
}
}
+
+