summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/kit/bigint.h28
-rw-r--r--source/kit/file.c4
-rw-r--r--source/kit/string_ref.h26
-rw-r--r--source/kit_test/test.c48
-rw-r--r--source/test/unittests/bigint.test.c77
-rw-r--r--source/test/unittests/file.test.c86
-rw-r--r--source/test/unittests/string_ref.test.c5
7 files changed, 185 insertions, 89 deletions
diff --git a/source/kit/bigint.h b/source/kit/bigint.h
index 26268a6..30233c6 100644
--- a/source/kit/bigint.h
+++ b/source/kit/bigint.h
@@ -99,8 +99,7 @@ static ptrdiff_t kit_bi_significant_bit_count(kit_bigint_t const x) {
: (byte & 0x08) != 0 ? 4
: (byte & 0x04) != 0 ? 3
: (byte & 0x02) != 0 ? 2
- : (byte & 0x01) != 0 ? 1
- : 0;
+ : 1;
return bytes * 8 + bits;
}
@@ -250,7 +249,7 @@ static kit_bigint_t kit_bi_mul(kit_bigint_t const x,
continue;
for (ptrdiff_t j = 0; i + j < KIT_BIGINT_SIZE / 4; j++) {
- if (y.v32[i] == 0)
+ if (y.v32[j] == 0)
continue;
uint64_t carry = ((uint64_t) x.v32[i]) * ((uint64_t) y.v32[j]);
@@ -294,7 +293,7 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x,
y = kit_bi_shl_uword(y, (kit_uword_t) shift);
while (shift >= 0) {
- if (kit_bi_compare(result.remainder, y) > 0) {
+ if (kit_bi_compare(result.remainder, y) >= 0) {
result.remainder = kit_bi_sub(result.remainder, y);
result.quotient.v8[shift / 8] |= (1u << (shift % 8));
}
@@ -432,6 +431,21 @@ static kit_bigint_t kit_bi_base58(kit_str_t const base58) {
# pragma GCC diagnostic pop
#endif
+#define KIT_BIN(static_str_) \
+ kit_bi_bin(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_DEC(static_str_) \
+ kit_bi_dec(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_HEX(static_str_) \
+ kit_bi_hex(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_BASE32(static_str_) \
+ kit_bi_base32(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
+#define KIT_BASE58(static_str_) \
+ kit_bi_base58(kit_str(sizeof(static_str_) - 1, (static_str_)))
+
#ifndef KIT_DISABLE_SHORT_NAMES
# define bigint_t kit_bigint_t
# define bi_uword kit_bi_uword
@@ -442,9 +456,15 @@ static kit_bigint_t kit_bi_base58(kit_str_t const base58) {
# define bi_neg kit_bi_neg
# define bi_sub kit_bi_sub
# define bi_mul kit_bi_mul
+# define bi_div kit_bi_div
# define hex_digit kit_hex_digit
# define bi_hex kit_bi_hex
# define bi_base58 kit_bi_base58
+# define BIN KIT_BIN
+# define DEC KIT_DEC
+# define HEX KIT_HEX
+# define BASE32 KIT_BASE32
+# define BASE58 KIT_BASE58
#endif
#ifdef __cplusplus
diff --git a/source/kit/file.c b/source/kit/file.c
index ff3a18d..d69bf76 100644
--- a/source/kit/file.c
+++ b/source/kit/file.c
@@ -24,9 +24,9 @@ static int is_delim(char const c) {
kit_string_t kit_path_norm(kit_str_t const path,
kit_allocator_t const alloc) {
- SZ(parent, "..");
+ str_t parent = SZ("..");
- kit_string_t norm;
+ string_t norm;
DA_INIT(norm, path.size, alloc);
assert(norm.size == path.size);
diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h
index 2f285dc..4b2cabd 100644
--- a/source/kit/string_ref.h
+++ b/source/kit/string_ref.h
@@ -13,11 +13,27 @@ typedef KIT_AR(char) kit_string_ref_t;
typedef kit_string_mut_t kit_out_str_t;
typedef kit_string_ref_t kit_str_t;
-#define KIT_SZ(name_, static_str_) \
- kit_str_t name_ = { \
- .size = (sizeof(static_str_) / sizeof((static_str_)[0])) - 1, \
- .values = (static_str_) \
- }
+#ifdef __GNUC__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+# pragma GCC push_options
+# pragma GCC optimize("O3")
+#endif
+
+static kit_str_t kit_str(ptrdiff_t const size,
+ char const *const static_string) {
+ kit_str_t const s = { .size = size, .values = static_string };
+ return s;
+}
+
+#ifdef __GNUC__
+# pragma GCC pop_options
+# pragma GCC diagnostic pop
+#endif
+
+#define KIT_SZ(static_str_) \
+ kit_str(sizeof(static_str_) - 1, (static_str_))
#ifndef KIT_DISABLE_SHORT_NAMES
# define string_mut_t kit_string_mut_t
diff --git a/source/kit_test/test.c b/source/kit_test/test.c
index 503a85b..1704343 100644
--- a/source/kit_test/test.c
+++ b/source/kit_test/test.c
@@ -1,12 +1,12 @@
#include "test.h"
+#define _GNU_SOURCE
+#include <setjmp.h>
+#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
-#include <setjmp.h>
-#include <signal.h>
-
kit_tests_list_t kit_tests_list = { 0 };
static void report(int i, int line, int ok) {
@@ -105,6 +105,8 @@ int kit_run_tests(int argc, char **argv) {
int line_width = 20;
int carriage_return = 1;
+ char const *specific_test = NULL;
+
for (int i = 0; i < argc; i++)
if (strcmp("--no-term-color", argv[i]) == 0)
no_color = 1;
@@ -112,25 +114,45 @@ int kit_run_tests(int argc, char **argv) {
carriage_return = 0;
else if (strcmp("--quiet", argv[i]) == 0)
quiet = 1;
+ else if (strcmp("--match", argv[i]) == 0)
+ specific_test = argv[++i];
- if (quiet)
- no_color = 1;
+ quiet && (no_color = 1);
- char const *file = NULL;
- ptrdiff_t file_root = -1;
+ if (specific_test != NULL) {
+ no_color || print_color(light);
+ quiet || printf("Run tests matching ");
+ no_color || print_color(white);
+ quiet || printf("*%s*", specific_test);
+ no_color || print_color(light);
+ quiet || printf("\n\n");
+ }
+
+ char const *file = NULL;
+ ptrdiff_t file_root = -1;
+ int tests_total = 0;
for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT;
i++) {
+ if (specific_test != NULL &&
+ strstr(kit_tests_list.tests[i].test_name, specific_test) ==
+ NULL)
+ continue;
+ tests_total++;
int const l = 2 + (int) strlen(kit_tests_list.tests[i].test_name);
if (line_width < l)
line_width = l;
}
- if (kit_tests_list.size > 0) {
+ if (tests_total > 0) {
char const *const s = kit_tests_list.tests[0].test_file;
for (int j = 1;
j < kit_tests_list.size && j < KIT_TESTS_SIZE_LIMIT; j++) {
+ if (specific_test != NULL &&
+ strstr(kit_tests_list.tests[j].test_name, specific_test) ==
+ NULL)
+ continue;
if (strcmp(s, kit_tests_list.tests[j].test_file) == 0)
continue;
int k = 0;
@@ -151,6 +173,10 @@ int kit_run_tests(int argc, char **argv) {
for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT;
i++) {
+ if (specific_test != NULL &&
+ strstr(kit_tests_list.tests[i].test_name, specific_test) ==
+ NULL)
+ continue;
if (file == NULL ||
strcmp(file, kit_tests_list.tests[i].test_file) != 0) {
if (file != NULL)
@@ -215,7 +241,7 @@ int kit_run_tests(int argc, char **argv) {
no_color || print_color(white);
quiet || printf("\n%d of %d tests passed.\n", success_count,
- kit_tests_list.size);
+ tests_total);
quiet || printf("%d of %d assertions passed.\n\n",
total_assertion_count - fail_assertion_count,
total_assertion_count);
@@ -225,6 +251,10 @@ int kit_run_tests(int argc, char **argv) {
if (status != 0) {
for (int i = 0;
i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) {
+ if (specific_test != NULL &&
+ strstr(kit_tests_list.tests[i].test_name, specific_test) ==
+ NULL)
+ continue;
if (kit_tests_list.tests[i].signal != 0) {
int signum = kit_tests_list.tests[i].signal;
if (signum >= 0 &&
diff --git a/source/test/unittests/bigint.test.c b/source/test/unittests/bigint.test.c
index 87bbc60..66cd87a 100644
--- a/source/test/unittests/bigint.test.c
+++ b/source/test/unittests/bigint.test.c
@@ -1,45 +1,74 @@
+#define KIT_BIGINT_SIZE 256
#include "../../kit/bigint.h"
#define KIT_TEST_FILE bigint
#include "../../kit_test/test.h"
-TEST("bigint hex add") {
- SZ(foo, "4242424242424242424242424242424242424242");
- SZ(bar, "1111111111111111111111111111111111111111");
- SZ(sum, "5353535353535353535353535353535353535353");
+static_assert(sizeof(bigint_t) == 256, "KIT_BIGINT_SIZE check");
- REQUIRE(bi_equal(bi_add(bi_hex(foo), bi_hex(bar)), bi_hex(sum)));
+TEST("bigint hex add") {
+ REQUIRE(bi_equal(
+ bi_add(HEX("4242424242424242424242424242424242424242"),
+ HEX("1111111111111111111111111111111111111111")),
+ HEX("5353535353535353535353535353535353535353")));
}
TEST("bigint hex sub") {
- SZ(foo, "4242424242424242424242424242424242424242");
- SZ(bar, "1111111111111111111111111111111111111111");
- SZ(dif, "3131313131313131313131313131313131313131");
-
- REQUIRE(bi_equal(bi_sub(bi_hex(foo), bi_hex(bar)), bi_hex(dif)));
+ REQUIRE(bi_equal(
+ bi_sub(HEX("4242424242424242424242424242424242424242"),
+ HEX("1111111111111111111111111111111111111111")),
+ HEX("3131313131313131313131313131313131313131")));
}
TEST("bigint base58") {
- SZ(foo, "31");
-
- REQUIRE(bi_equal(bi_base58(foo), bi_uword(58 * 2)));
+ REQUIRE(bi_equal(BASE58("31"), bi_uword(58 * 2)));
}
TEST("bigint base58 add") {
- SZ(foo, "4242424242424242424242424242424242424242");
- SZ(bar, "2222222222222222222222222222222222222222");
- SZ(sum, "5353535353535353535353535353535353535353");
-
- REQUIRE(bi_equal(bi_add(bi_base58(foo), bi_base58(bar)),
- bi_base58(sum)));
+ REQUIRE(bi_equal(
+ bi_add(BASE58("4242424242424242424242424242424242424242"),
+ BASE58("2222222222222222222222222222222222222222")),
+ BASE58("5353535353535353535353535353535353535353")));
}
TEST("bigint base58 sub") {
- SZ(foo, "42");
- SZ(bar, "22");
- SZ(dif, "31");
+ REQUIRE(bi_equal(
+ bi_sub(BASE58("4242424242424242424242424242424242424242"),
+ BASE58("2222222222222222222222222222222222222222")),
+ BASE58("3131313131313131313131313131313131313131")));
+}
+
+TEST("bigint base58 mul") {
+ REQUIRE(bi_equal(bi_mul(BASE58("2111111111111111111111"),
+ BASE58("foofoofoofoofoo")),
+ BASE58("foofoofoofoofoo111111111111111111111")));
+}
+
+TEST("bigint div") {
+ REQUIRE(
+ bi_equal(bi_div(HEX("100"), HEX("10")).quotient, HEX("10")));
+
+ REQUIRE(bi_equal(bi_div(bi_mul(BASE58("foofoofoofoofoofoo"),
+ BASE58("barbarbarbarbarbar")),
+ BASE58("barbarbarbarbarbar"))
+ .quotient,
+ BASE58("foofoofoofoofoofoo")));
+
+ REQUIRE(bi_equal(bi_div(bi_mul(BASE58("foofoofoofoofoofoofoofoo"),
+ BASE58("barbarbarbarbarbar")),
+ BASE58("barbarbarbarbarbar"))
+ .quotient,
+ BASE58("foofoofoofoofoofoofoofoo")));
- REQUIRE(bi_equal(bi_sub(bi_base58(foo), bi_base58(bar)),
- bi_base58(dif)));
+ REQUIRE(bi_equal(
+ bi_div(
+ bi_mul(BASE58("foofoofoofoofoofoofoofoofoofoofoofoofoofoofo"
+ "ofoofoofoo"),
+ BASE58("barbarbarbarbarbarbarbarbarbarbarbarbarbarba"
+ "rbar")),
+ BASE58("barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar"))
+ .quotient,
+ BASE58(
+ "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo")));
}
diff --git a/source/test/unittests/file.test.c b/source/test/unittests/file.test.c
index 5b37ab4..3b6d6d0 100644
--- a/source/test/unittests/file.test.c
+++ b/source/test/unittests/file.test.c
@@ -12,8 +12,8 @@
#endif
TEST("file path normalize one") {
- SZ(foo, "foo/bar/../baz");
- SZ(foo_norm, "foo" S_DELIM_ "baz");
+ str_t foo = SZ("foo/bar/../baz");
+ str_t foo_norm = SZ("foo" S_DELIM_ "baz");
string_t bar = path_norm(foo, kit_alloc_default());
@@ -23,8 +23,8 @@ TEST("file path normalize one") {
}
TEST("file path normalize two") {
- SZ(foo, "foo/bar/../../baz");
- SZ(foo_norm, "baz");
+ str_t foo = SZ("foo/bar/../../baz");
+ str_t foo_norm = SZ("baz");
string_t bar = path_norm(foo, kit_alloc_default());
@@ -34,8 +34,8 @@ TEST("file path normalize two") {
}
TEST("file path normalize parent") {
- SZ(foo, "../baz");
- SZ(foo_norm, ".." S_DELIM_ "baz");
+ str_t foo = SZ("../baz");
+ str_t foo_norm = SZ(".." S_DELIM_ "baz");
string_t bar = path_norm(foo, kit_alloc_default());
@@ -45,8 +45,8 @@ TEST("file path normalize parent") {
}
TEST("file path normalize double parent") {
- SZ(foo, "foo/../../baz");
- SZ(foo_norm, ".." S_DELIM_ "baz");
+ str_t foo = SZ("foo/../../baz");
+ str_t foo_norm = SZ(".." S_DELIM_ "baz");
string_t bar = path_norm(foo, kit_alloc_default());
@@ -56,8 +56,8 @@ TEST("file path normalize double parent") {
}
TEST("file path normalize windows delim") {
- SZ(foo, "foo\\bar\\..\\baz");
- SZ(foo_norm, "foo" S_DELIM_ "baz");
+ str_t foo = SZ("foo\\bar\\..\\baz");
+ str_t foo_norm = SZ("foo" S_DELIM_ "baz");
string_t bar = path_norm(foo, kit_alloc_default());
@@ -67,9 +67,9 @@ TEST("file path normalize windows delim") {
}
TEST("file path join no delim") {
- SZ(foo, "foo");
- SZ(bar, "bar");
- SZ(joined, "foo" S_DELIM_ "bar");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("bar");
+ str_t joined = SZ("foo" S_DELIM_ "bar");
string_t foobar = path_join(foo, bar, kit_alloc_default());
@@ -79,9 +79,9 @@ TEST("file path join no delim") {
}
TEST("file path join delim left") {
- SZ(foo, "foo/");
- SZ(bar, "bar");
- SZ(joined, "foo" S_DELIM_ "bar");
+ str_t foo = SZ("foo/");
+ str_t bar = SZ("bar");
+ str_t joined = SZ("foo" S_DELIM_ "bar");
string_t foobar = path_join(foo, bar, kit_alloc_default());
@@ -91,9 +91,9 @@ TEST("file path join delim left") {
}
TEST("file path join delim right") {
- SZ(foo, "foo");
- SZ(bar, "/bar");
- SZ(joined, "foo" S_DELIM_ "bar");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("/bar");
+ str_t joined = SZ("foo" S_DELIM_ "bar");
string_t foobar = path_join(foo, bar, kit_alloc_default());
@@ -103,9 +103,9 @@ TEST("file path join delim right") {
}
TEST("file path join delim both") {
- SZ(foo, "foo/");
- SZ(bar, "/bar");
- SZ(joined, "foo" S_DELIM_ "bar");
+ str_t foo = SZ("foo/");
+ str_t bar = SZ("/bar");
+ str_t joined = SZ("foo" S_DELIM_ "bar");
string_t foobar = path_join(foo, bar, kit_alloc_default());
@@ -123,9 +123,9 @@ TEST("file path user") {
}
TEST("file path index relative") {
- SZ(foobar, "foo/bar");
- SZ(foo, "foo");
- SZ(bar, "bar");
+ str_t foobar = SZ("foo/bar");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("bar");
REQUIRE(AR_EQUAL(path_index(foobar, 0), foo));
REQUIRE(AR_EQUAL(path_index(foobar, 1), bar));
@@ -133,9 +133,9 @@ TEST("file path index relative") {
}
TEST("file path index absolute") {
- SZ(foobar, "/foo/bar");
- SZ(foo, "foo");
- SZ(bar, "bar");
+ str_t foobar = SZ("/foo/bar");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("bar");
REQUIRE(AR_EQUAL(path_index(foobar, 0), foo));
REQUIRE(AR_EQUAL(path_index(foobar, 1), bar));
@@ -143,10 +143,10 @@ TEST("file path index absolute") {
}
TEST("file path index windows disk name") {
- SZ(foobar, "c:\\foo\\bar");
- SZ(disk, "c:");
- SZ(foo, "foo");
- SZ(bar, "bar");
+ str_t foobar = SZ("c:\\foo\\bar");
+ str_t disk = SZ("c:");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("bar");
REQUIRE(AR_EQUAL(path_index(foobar, 0), disk));
REQUIRE(AR_EQUAL(path_index(foobar, 1), foo));
@@ -155,10 +155,10 @@ TEST("file path index windows disk name") {
}
TEST("file path take relative") {
- SZ(foobar, "foo/bar/");
- SZ(foo, "foo");
- SZ(bar, "foo/bar");
- SZ(bar_end, "foo/bar/");
+ str_t foobar = SZ("foo/bar/");
+ str_t foo = SZ("foo");
+ str_t bar = SZ("foo/bar");
+ str_t bar_end = SZ("foo/bar/");
REQUIRE(AR_EQUAL(path_take(foobar, 0), foo));
REQUIRE(AR_EQUAL(path_take(foobar, 1), bar));
@@ -166,19 +166,19 @@ TEST("file path take relative") {
}
TEST("file path take absolute") {
- SZ(foobar, "/foo/bar");
- SZ(foo, "/foo");
- SZ(bar, "/foo/bar");
+ str_t foobar = SZ("/foo/bar");
+ str_t foo = SZ("/foo");
+ str_t bar = SZ("/foo/bar");
REQUIRE(AR_EQUAL(path_take(foobar, 0), foo));
REQUIRE(AR_EQUAL(path_take(foobar, 1), bar));
}
TEST("file path take windows disk name") {
- SZ(foobar, "c:\\foo\\bar");
- SZ(disk, "c:");
- SZ(foo, "c:\\foo");
- SZ(bar, "c:\\foo\\bar");
+ str_t foobar = SZ("c:\\foo\\bar");
+ str_t disk = SZ("c:");
+ str_t foo = SZ("c:\\foo");
+ str_t bar = SZ("c:\\foo\\bar");
REQUIRE(AR_EQUAL(path_take(foobar, 0), disk));
REQUIRE(AR_EQUAL(path_take(foobar, 1), foo));
diff --git a/source/test/unittests/string_ref.test.c b/source/test/unittests/string_ref.test.c
index 7c49996..5ec9414 100644
--- a/source/test/unittests/string_ref.test.c
+++ b/source/test/unittests/string_ref.test.c
@@ -4,7 +4,7 @@
#include "../../kit_test/test.h"
TEST("static string wrap") {
- SZ(ref, "foo bar");
+ str_t ref = SZ("foo bar");
REQUIRE(ref.size == 7);
REQUIRE(ref.values[0] == 'f');
@@ -17,6 +17,7 @@ TEST("static string wrap") {
}
TEST("string literal") {
- SZ(foo, "foo");
+ str_t foo = SZ("foo");
str_t bar = foo;
+ (void) bar;
}