LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE import) carry the CNCL line instead of the LGPL notice. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
194 lines
4.5 KiB
C
194 lines
4.5 KiB
C
/*
|
|
* test_model.c -- the tests of model.c (make check)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "model.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
calc_is(const char *e, double want)
|
|
{
|
|
double v;
|
|
|
|
return mx_calc(e, &v) == 0 && v > want - 1e-9 && v < want + 1e-9;
|
|
}
|
|
|
|
static int
|
|
calc_fails(const char *e)
|
|
{
|
|
double v;
|
|
|
|
return mx_calc(e, &v) != 0;
|
|
}
|
|
|
|
static void
|
|
test_match(void)
|
|
{
|
|
CHECK(mx_match("", "anything") > 0);
|
|
CHECK(mx_match("dtf", "dtfile") > 0);
|
|
CHECK(mx_match("DTF", "dtfile") > 0);
|
|
CHECK(mx_match("xyz", "dtfile") == 0);
|
|
CHECK(mx_match("fd", "dtfile") == 0); /* order matters */
|
|
/* the start beats the middle, a word start beats a letter inside */
|
|
CHECK(mx_match("term", "dtterm") < mx_match("term", "terminal"));
|
|
CHECK(mx_match("fm", "file manager") > mx_match("fm", "pathfmt"));
|
|
/* letters in a row beat scattered ones */
|
|
CHECK(mx_match("cal", "calendar") > mx_match("cal", "cxaxl"));
|
|
}
|
|
|
|
static void
|
|
test_calc(void)
|
|
{
|
|
char buf[64];
|
|
|
|
CHECK(calc_is("1+2", 3));
|
|
CHECK(calc_is("=2*3+4", 10));
|
|
CHECK(calc_is("2*(3+4)", 14));
|
|
CHECK(calc_is("= -3 + 5", 2));
|
|
CHECK(calc_is("2^3^2", 512)); /* right associative */
|
|
CHECK(calc_is("7 % 4", 3));
|
|
CHECK(calc_is("=10/4", 2.5));
|
|
CHECK(calc_fails("1/0"));
|
|
CHECK(calc_fails("12")); /* a number is no calculation */
|
|
CHECK(calc_fails("firefox"));
|
|
CHECK(calc_fails("=2+"));
|
|
CHECK(calc_fails("=(1+2"));
|
|
CHECK(calc_fails("-5")); /* a leading minus alone */
|
|
mx_calc_format(2.5, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "2.5") == 0);
|
|
mx_calc_format(3.0, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "3") == 0);
|
|
mx_calc_format(-0.0, buf, sizeof buf);
|
|
CHECK(strcmp(buf, "0") == 0);
|
|
}
|
|
|
|
static void
|
|
test_snippets(const char *dir)
|
|
{
|
|
char path[1024];
|
|
FILE *f;
|
|
MxSnippet *s;
|
|
size_t n;
|
|
char *u;
|
|
|
|
u = mx_unescape("a\\nb\\tc\\\\d");
|
|
CHECK(u != NULL && strcmp(u, "a\nb\tc\\d") == 0);
|
|
free(u);
|
|
|
|
(void)snprintf(path, sizeof path, "%s/snippets", dir);
|
|
CHECK(mx_snippets_load(path, &s, &n) == 0 && n == 0); /* no file */
|
|
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fputs("# a comment\n", f);
|
|
fputs("addr\tRua Nova 1\\n1000 Lisboa\n", f);
|
|
fputs("no tab on this line\n", f);
|
|
fputs("\tno keyword\n", f);
|
|
fputs("sig\t-- \\nKonrad\r\n", f);
|
|
fclose(f);
|
|
CHECK(mx_snippets_load(path, &s, &n) == 0);
|
|
CHECK(n == 2);
|
|
if (n == 2) {
|
|
CHECK(strcmp(s[0].key, "addr") == 0);
|
|
CHECK(strcmp(s[0].text, "Rua Nova 1\n1000 Lisboa") == 0);
|
|
CHECK(strcmp(s[1].key, "sig") == 0);
|
|
CHECK(strcmp(s[1].text, "-- \nKonrad") == 0);
|
|
}
|
|
mx_snippets_free(s, n);
|
|
(void)unlink(path);
|
|
}
|
|
|
|
static void
|
|
test_programs(const char *dir)
|
|
{
|
|
char d1[1024], d2[1024], f[1100], p[2200];
|
|
char **v;
|
|
size_t n, i;
|
|
FILE *fp;
|
|
const char *names[] = { "beta", "alpha", ".hidden" };
|
|
|
|
(void)snprintf(d1, sizeof d1, "%s/bin1", dir);
|
|
(void)snprintf(d2, sizeof d2, "%s/bin2", dir);
|
|
(void)mkdir(d1, 0755);
|
|
(void)mkdir(d2, 0755);
|
|
for (i = 0; i < 3; i++) {
|
|
(void)snprintf(f, sizeof f, "%s/%s", d1, names[i]);
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0755);
|
|
}
|
|
(void)snprintf(f, sizeof f, "%s/alpha", d2); /* a duplicate */
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0755);
|
|
(void)snprintf(f, sizeof f, "%s/notexec", d2);
|
|
fp = fopen(f, "w");
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
(void)chmod(f, 0644);
|
|
|
|
(void)snprintf(p, sizeof p, "%s:%s:/nonexistent", d1, d2);
|
|
CHECK(mx_path_programs(p, &v, &n) == 0);
|
|
CHECK(n == 2);
|
|
if (n == 2) {
|
|
CHECK(strcmp(v[0], "alpha") == 0);
|
|
CHECK(strcmp(v[1], "beta") == 0);
|
|
}
|
|
mx_strv_free(v, n);
|
|
}
|
|
|
|
/* remove what test_programs made; test_snippets removes its own file */
|
|
static void
|
|
cleanup(const char *dir)
|
|
{
|
|
const char *files[] = { "bin1/alpha", "bin1/beta", "bin1/.hidden",
|
|
"bin2/alpha", "bin2/notexec", "bin1", "bin2" };
|
|
char p[1100];
|
|
size_t i;
|
|
|
|
for (i = 0; i < sizeof files / sizeof files[0]; i++) {
|
|
(void)snprintf(p, sizeof p, "%s/%s", dir, files[i]);
|
|
if (unlink(p) != 0)
|
|
(void)rmdir(p);
|
|
}
|
|
(void)rmdir(dir);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char dir[] = "/tmp/dtlaunch-test.XXXXXX";
|
|
|
|
if (mkdtemp(dir) == NULL) {
|
|
perror("mkdtemp");
|
|
return 2;
|
|
}
|
|
test_match();
|
|
test_calc();
|
|
test_snippets(dir);
|
|
test_programs(dir);
|
|
cleanup(dir);
|
|
printf("%s: %d failures\n", failures ? "FAIL" : "PASS", failures);
|
|
return failures ? 1 : 0;
|
|
}
|