aboutsummaryrefslogtreecommitdiff
path: root/tests/test_cli.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_cli.c')
-rw-r--r--tests/test_cli.c76
1 files changed, 36 insertions, 40 deletions
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 58cbad3..8d56f09 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -13,68 +13,69 @@
#include <sys/wait.h>
#include <unistd.h>
-static char g_dir[1024]; /* fixture model dir */
-static char g_out[1100]; /* captured stdout */
-static char g_err[1100]; /* captured stderr */
-static char g_in[1100]; /* stdin source */
+static char g_dir[1024]; /* fixture model dir */
+static char g_out[1100]; /* captured stdout */
+static char g_err[1100]; /* captured stderr */
+static char g_in[1100]; /* stdin source */
static const char *g_cli;
static int g_failures;
/* Run the CLI with sh-formatted args; returns its exit status (-1 on a
* harness error). stdin_text == NULL feeds /dev/null. */
-static int run_cli(const char *args, const char *stdin_text)
-{
+static int run_cli(const char *args, const char *stdin_text) {
if (stdin_text) {
FILE *f = fopen(g_in, "w");
- if (!f) return -1;
+ if (!f)
+ return -1;
fputs(stdin_text, f);
fclose(f);
}
char cmd[4096];
- int n = snprintf(cmd, sizeof(cmd), "'%s' %s > '%s' 2> '%s' < '%s'",
- g_cli, args, g_out, g_err,
+ int n = snprintf(cmd, sizeof(cmd), "'%s' %s > '%s' 2> '%s' < '%s'", g_cli, args, g_out, g_err,
stdin_text ? g_in : "/dev/null");
- if (n < 0 || (size_t)n >= sizeof(cmd)) return -1;
+ if (n < 0 || (size_t)n >= sizeof(cmd))
+ return -1;
int st = system(cmd);
- if (st == -1 || !WIFEXITED(st)) return -1;
+ if (st == -1 || !WIFEXITED(st))
+ return -1;
return WEXITSTATUS(st);
}
-static void expect(int cond, const char *what)
-{
+static void expect(int cond, const char *what) {
if (!cond) {
fprintf(stderr, "FAIL: %s\n", what);
g_failures++;
}
}
-static int file_contains(const char *path, const char *needle)
-{
+static int file_contains(const char *path, const char *needle) {
static char buf[1 << 16];
FILE *f = fopen(path, "rb");
- if (!f) return 0;
+ if (!f)
+ return 0;
size_t n = fread(buf, 1, sizeof(buf) - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
-static int count_lines(const char *path)
-{
+static int count_lines(const char *path) {
FILE *f = fopen(path, "rb");
- if (!f) return -1;
+ if (!f)
+ return -1;
int lines = 0, c;
while ((c = fgetc(f)) != EOF)
- if (c == '\n') lines++;
+ if (c == '\n')
+ lines++;
fclose(f);
return lines;
}
/* First stdout line must hold exactly `dim` finite floats. */
-static int stdout_is_embedding(int dim)
-{
+static int stdout_is_embedding(int dim) {
FILE *f = fopen(g_out, "r");
- if (!f) return 0;
+ if (!f)
+ return 0;
int count = 0;
float v;
while (fscanf(f, "%f", &v) == 1) {
@@ -88,8 +89,7 @@ static int stdout_is_embedding(int dim)
return count == dim;
}
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
g_cli = argc > 1 ? argv[1] : "./embed";
tm_dims_t dims = {4, 2, 1, 2, 8, TF_VOCAB_SIZE};
@@ -109,13 +109,11 @@ int main(int argc, char **argv)
/* Help and missing-argument handling. */
expect(run_cli("-h", NULL) == 0, "-h exits 0");
expect(run_cli("", NULL) == 1, "no arguments exits 1");
- expect(run_cli("-d /nonexistent-embed-dir hi", NULL) == 1,
- "missing model dir exits 1");
+ expect(run_cli("-d /nonexistent-embed-dir hi", NULL) == 1, "missing model dir exits 1");
/* -V reports the program's base name, not the path it was invoked by. */
expect(run_cli("-V", NULL) == 0, "-V exits 0");
- expect(!file_contains(g_out, "tests/"),
- "-V strips the directory from the program name");
+ expect(!file_contains(g_out, "tests/"), "-V strips the directory from the program name");
/* Mode 1: one text prints one embedding line. */
snprintf(args, sizeof(args), "-d '%s' 'hello world'", g_dir);
@@ -123,11 +121,9 @@ int main(int argc, char **argv)
expect(stdout_is_embedding(dims.hidden), "single text prints dim floats");
/* Thread count. */
- snprintf(args, sizeof(args),
- "-d '%s' -t 2 -v 'hello world'", g_dir);
+ snprintf(args, sizeof(args), "-d '%s' -t 2 -v 'hello world'", g_dir);
expect(run_cli(args, NULL) == 0, "-t 2 -v exits 0");
- expect(file_contains(g_err, "Using 2 CPU thread(s)"),
- "-v reports the thread count");
+ expect(file_contains(g_err, "Using 2 CPU thread(s)"), "-v reports the thread count");
/* Mode 2+: several texts print a bare numeric matrix, one row per line,
* with no titles or labels. Two texts give a 2x2 with a 1.0 diagonal. */
@@ -152,8 +148,7 @@ int main(int argc, char **argv)
/* -e appends the raw embeddings (label-free) after the matrix: three
* matrix rows, a blank separator, then three embedding rows. */
- snprintf(args, sizeof(args),
- "-d '%s' -e -b 1 'hello' 'world' 'held'", g_dir);
+ snprintf(args, sizeof(args), "-d '%s' -e -b 1 'hello' 'world' 'held'", g_dir);
expect(run_cli(args, NULL) == 0, "-e -b 1 with three texts exits 0");
expect(count_lines(g_out) == 7, "-e prints matrix then raw embeddings");
@@ -167,16 +162,17 @@ int main(int argc, char **argv)
snprintf(args, sizeof(args), "-d '%s' --stream", g_dir);
expect(run_cli(args, "hello\nworld\n") == 0, "--stream exits 0");
expect(count_lines(g_out) == 2, "--stream writes one line per input");
- expect(file_contains(g_out, "{\"embedding\":["),
- "--stream writes embedding JSON");
+ expect(file_contains(g_out, "{\"embedding\":["), "--stream writes embedding JSON");
snprintf(args, sizeof(args), "-d '%s' --stream -b 2 -v", g_dir);
- expect(run_cli(args, "hello\nworld\nheld\n") == 0,
- "--stream -b 2 exits 0");
+ expect(run_cli(args, "hello\nworld\nheld\n") == 0, "--stream -b 2 exits 0");
expect(count_lines(g_out) == 3, "--stream -b 2 covers the tail batch");
/* Flag validation: missing model dir must exit 1. */
- static const struct { const char *args; const char *what; } bad[] = {
+ static const struct {
+ const char *args;
+ const char *what;
+ } bad[] = {
{"-d '' t", "empty model dir"},
};
for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++)