summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2025-04-18 09:54:02 -0400
committerTavian Barnes <tavianator@tavianator.com>2025-04-18 09:54:02 -0400
commitb551e660dce8e232bfbc9c94fc753f6e26e72efb (patch)
tree94b410cba4b4d5b10cf550ffd57a20283da4560d
parent16649a99898b2739f4eb313ec156f3218809da20 (diff)
downloadbfs-b551e660dce8e232bfbc9c94fc753f6e26e72efb.tar.xz
tests/bfstd: Factor tests into separate functions
-rw-r--r--tests/bfstd.c67
1 files changed, 42 insertions, 25 deletions
diff --git a/tests/bfstd.c b/tests/bfstd.c
index a43783a..9630715 100644
--- a/tests/bfstd.c
+++ b/tests/bfstd.c
@@ -10,31 +10,8 @@
#include <stdlib.h>
#include <string.h>
-/** Check the result of xdirname()/xbasename(). */
-static void check_base_dir(const char *path, const char *dir, const char *base) {
- char *xdir = xdirname(path);
- bfs_everify(xdir, "xdirname()");
- bfs_check(strcmp(xdir, dir) == 0, "xdirname('%s') == '%s' (!= '%s')", path, xdir, dir);
- free(xdir);
-
- char *xbase = xbasename(path);
- bfs_everify(xbase, "xbasename()");
- bfs_check(strcmp(xbase, base) == 0, "xbasename('%s') == '%s' (!= '%s')", path, xbase, base);
- free(xbase);
-}
-
-/** Check the result of wordesc(). */
-static void check_wordesc(const char *str, const char *exp, enum wesc_flags flags) {
- char buf[256];
- char *end = buf + sizeof(buf);
- char *esc = wordesc(buf, end, str, flags);
-
- if (bfs_check(esc != end)) {
- bfs_check(strcmp(buf, exp) == 0, "wordesc('%s') == '%s' (!= '%s')", str, buf, exp);
- }
-}
-
-void check_bfstd(void) {
+/** asciilen() test cases. */
+static void check_asciilen(void) {
bfs_check(asciilen("") == 0);
bfs_check(asciilen("@") == 1);
bfs_check(asciilen("@@") == 2);
@@ -49,7 +26,23 @@ void check_bfstd(void) {
bfs_check(asciilen("@@@@@@@\xFF@@@@@@a\xFF@@@@@@@") == 7);
bfs_check(asciilen("@@@@@@@@\xFF@@@@@a\xFF@@@@@@@") == 8);
bfs_check(asciilen("@@@@@@@@@\xFF@@@@a\xFF@@@@@@@") == 9);
+}
+/** Check the result of xdirname()/xbasename(). */
+static void check_base_dir(const char *path, const char *dir, const char *base) {
+ char *xdir = xdirname(path);
+ bfs_everify(xdir, "xdirname()");
+ bfs_check(strcmp(xdir, dir) == 0, "xdirname('%s') == '%s' (!= '%s')", path, xdir, dir);
+ free(xdir);
+
+ char *xbase = xbasename(path);
+ bfs_everify(xbase, "xbasename()");
+ bfs_check(strcmp(xbase, base) == 0, "xbasename('%s') == '%s' (!= '%s')", path, xbase, base);
+ free(xbase);
+}
+
+/** xdirname()/xbasename() test cases. */
+static void check_basedirs(void) {
// From man 3p basename
check_base_dir("usr", ".", "usr");
check_base_dir("usr/", ".", "usr");
@@ -61,7 +54,21 @@ void check_bfstd(void) {
check_base_dir("/usr/lib", "/usr", "lib");
check_base_dir("//usr//lib//", "//usr", "lib");
check_base_dir("/home//dwc//test", "/home//dwc", "test");
+}
+/** Check the result of wordesc(). */
+static void check_wordesc(const char *str, const char *exp, enum wesc_flags flags) {
+ char buf[256];
+ char *end = buf + sizeof(buf);
+ char *esc = wordesc(buf, end, str, flags);
+
+ if (bfs_check(esc != end)) {
+ bfs_check(strcmp(buf, exp) == 0, "wordesc('%s') == '%s' (!= '%s')", str, buf, exp);
+ }
+}
+
+/** wordesc() test cases. */
+static void check_wordescs(void) {
check_wordesc("", "\"\"", WESC_SHELL);
check_wordesc("word", "word", WESC_SHELL);
check_wordesc("two words", "\"two words\"", WESC_SHELL);
@@ -80,7 +87,17 @@ void check_bfstd(void) {
check_wordesc("\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", WESC_SHELL | WESC_TTY);
check_wordesc("\xCB\x9Cuser", "\xCB\x9Cuser", WESC_SHELL);
}
+}
+/** xstrwidth() test cases. */
+static void check_strwidth(void) {
bfs_check(xstrwidth("Hello world") == 11);
bfs_check(xstrwidth("Hello\1world") == 10);
}
+
+void check_bfstd(void) {
+ check_asciilen();
+ check_basedirs();
+ check_wordescs();
+ check_strwidth();
+}