test/wcc: run the unit loops fully in process
000_smoke drops its popen ww -V leg (driver version parity moves to 949_driver_flagargs); 400_w6c feeds its sources through lex/parse/ check/cgen into open_memstream instead of spawning w6c. The five unit binaries now hold zero subprocess calls.
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
* 000_smoke — Phase 0 smoke test.
|
* 000_smoke — Phase 0 smoke test.
|
||||||
*
|
*
|
||||||
* Asserts:
|
* Asserts arena allocation and growth through libwcc, plus the configured
|
||||||
* - libwcc symbols (newarena/amalloc/freearena, fatal/errorf/warnf)
|
* version constant. Driver behavior belongs to the driver integration tests.
|
||||||
* are linkable.
|
|
||||||
* - `ww -V` exits 0 and prints the configured version.
|
|
||||||
*/
|
*/
|
||||||
#include "ww.h"
|
#include "ww.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -44,44 +42,11 @@ test_version(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
test_ww_minus_v(void)
|
|
||||||
{
|
|
||||||
const char *bin = getenv("BIN");
|
|
||||||
if (bin == NULL)
|
|
||||||
bin = "out/bin";
|
|
||||||
char cmd[512];
|
|
||||||
snprintf(cmd, sizeof cmd, "%s/ww -V", bin);
|
|
||||||
FILE *p = popen(cmd, "r");
|
|
||||||
if (p == NULL) {
|
|
||||||
fprintf(stderr, "smoke: popen %s\n", cmd);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
char buf[128];
|
|
||||||
size_t n = fread(buf, 1, sizeof buf - 1, p);
|
|
||||||
buf[n] = '\0';
|
|
||||||
int rc = pclose(p);
|
|
||||||
if (rc != 0) {
|
|
||||||
fprintf(stderr, "smoke: ww -V exit %d\n", rc);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (strstr(buf, "ww ") != buf) {
|
|
||||||
fprintf(stderr, "smoke: ww -V output not 'ww ...': %s\n", buf);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (strstr(buf, WW_VERSION) == NULL) {
|
|
||||||
fprintf(stderr, "smoke: ww -V missing version %s in: %s\n",
|
|
||||||
WW_VERSION, buf);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
test_arena();
|
test_arena();
|
||||||
test_version();
|
test_version();
|
||||||
test_ww_minus_v();
|
|
||||||
puts("smoke: ok");
|
puts("smoke: ok");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,91 +1,106 @@
|
|||||||
/*
|
/*
|
||||||
* 400_6c — codegen smoke tests. Each row supplies a tiny ww source
|
* 400_w6c — in-process codegen smoke tests. Each row supplies one tiny ww
|
||||||
* and a list of substrings expected in the emitted .s. We don't
|
* source and the assembly fragments it must emit. Full-file golden matches
|
||||||
* golden-match the whole file (too brittle); we just verify that
|
* would bind the test to unrelated output details.
|
||||||
* key opcodes and operand shapes show up.
|
|
||||||
*/
|
*/
|
||||||
|
#include "gc.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include "wwtestpkg.h"
|
#include "wwtestpkg.h"
|
||||||
|
|
||||||
static int
|
static char *
|
||||||
run6c(const char *src, char **out)
|
codegen(const char *src)
|
||||||
{
|
{
|
||||||
char path[64];
|
Arena *a = newarena();
|
||||||
snprintf(path, sizeof path, "/tmp/wwt_%d.ww", getpid());
|
char *wrapped = wwtest_wrap(src);
|
||||||
FILE *f = fopen(path, "wb");
|
Lex l;
|
||||||
if (f == NULL) return -1;
|
Parser p;
|
||||||
wwtest_fputs(src, f);
|
Checker c;
|
||||||
fclose(f);
|
Cg cg;
|
||||||
|
|
||||||
char cmd[256];
|
lexinit(&l, a, "<test>", wrapped, strlen(wrapped));
|
||||||
const char *bin = getenv("BIN");
|
parserinit(&p, a, &l);
|
||||||
if (!bin) bin = "out/bin";
|
Node *file = parsefile(&p);
|
||||||
snprintf(cmd, sizeof cmd, "%s/w6c %s 2>&1", bin, path);
|
if (l.errs || p.errs)
|
||||||
FILE *p = popen(cmd, "r");
|
goto fail;
|
||||||
if (p == NULL) { unlink(path); return -1; }
|
check_init(&c, a);
|
||||||
size_t cap = 4096, n = 0;
|
check_file(&c, file);
|
||||||
char *buf = malloc(cap);
|
if (c.errs)
|
||||||
int c;
|
goto fail;
|
||||||
while ((c = fgetc(p)) != EOF) {
|
|
||||||
if (n + 1 >= cap) { cap *= 2; buf = realloc(buf, cap); }
|
char *out = NULL;
|
||||||
buf[n++] = (char)c;
|
size_t outlen = 0;
|
||||||
|
FILE *f = open_memstream(&out, &outlen);
|
||||||
|
if (f == NULL)
|
||||||
|
goto fail;
|
||||||
|
cg_init(&cg, a);
|
||||||
|
cg_file(&cg, f, file);
|
||||||
|
if (fclose(f) != 0) {
|
||||||
|
free(out);
|
||||||
|
out = NULL;
|
||||||
}
|
}
|
||||||
buf[n] = '\0';
|
free(wrapped);
|
||||||
int rc = pclose(p);
|
freearena(a);
|
||||||
unlink(path);
|
return out;
|
||||||
*out = buf;
|
|
||||||
return rc;
|
fail:
|
||||||
|
free(wrapped);
|
||||||
|
freearena(a);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
struct row {
|
||||||
contains(const char *hay, const char *needle)
|
const char *src;
|
||||||
{
|
const char *needle[3];
|
||||||
return strstr(hay, needle) != NULL;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
struct row { const char *src; const char *needle; };
|
|
||||||
|
|
||||||
static const struct row rows[] = {
|
static const struct row rows[] = {
|
||||||
{ "fn main() i32 = { return 42; };", "MOVQ\t$42, AX" },
|
{ "fn main() i32 = { return 42; };", { "MOVQ\t$42, AX", "RET" } },
|
||||||
{ "fn main() i32 = { return 42; };", "RET" },
|
{ "fn id(x: i32) i32 = { return x; };", { "MOVQ\tDI, -8(BP)" } },
|
||||||
{ "fn id(x: i32) i32 = { return x; };","MOVQ\tDI, -8(BP)" },
|
{ "fn add(a: i32, b: i32) i32 = { return a + b; };", { "ADDQ\tBX, AX" } },
|
||||||
{ "fn add(a: i32, b: i32) i32 = { return a + b; };", "ADDQ\tBX, AX" },
|
{ "fn sub(a: i32, b: i32) i32 = { return a - b; };", { "SUBQ\tBX, AX" } },
|
||||||
{ "fn sub(a: i32, b: i32) i32 = { return a - b; };", "SUBQ\tBX, AX" },
|
{ "fn mul(a: i32, b: i32) i32 = { return a * b; };", { "IMULQ\tBX, AX" } },
|
||||||
{ "fn mul(a: i32, b: i32) i32 = { return a * b; };", "IMULQ\tBX, AX" },
|
{ "fn neg(x: i32) i32 = { return -x; };", { "NEGQ\tAX" } },
|
||||||
{ "fn neg(x: i32) i32 = { return -x; };", "NEGQ\tAX" },
|
{ "fn cmp(a: i32, b: i32) bool = { return a == b; };",
|
||||||
{ "fn cmp(a: i32, b: i32) bool = { return a == b; };", "CMPQ\tBX, AX" },
|
{ "CMPQ\tBX, AX", "JE" } },
|
||||||
{ "fn cmp(a: i32, b: i32) bool = { return a == b; };", "JE" },
|
|
||||||
{ "fn cond(x: i32) i32 = { if (x > 0) { return 1; }; return 0; };",
|
{ "fn cond(x: i32) i32 = { if (x > 0) { return 1; }; return 0; };",
|
||||||
"CMPQ\t$0, AX" },
|
{ "CMPQ\t$0, AX" } },
|
||||||
{ "fn lo() void = { for (let i: i32 = 0; i < 10; i += 1) { }; };", "JMP" },
|
{ "fn lo() void = { for (let i: i32 = 0; i < 10; i += 1) { }; };",
|
||||||
{ "fn callit() i32 = { return 1; };", "TEXT main.callit,$0" },
|
{ "JMP" } },
|
||||||
|
{ "fn callit() i32 = { return 1; };", { "TEXT main.callit,$0" } },
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
int n = sizeof rows / sizeof rows[0];
|
int n = sizeof rows / sizeof rows[0];
|
||||||
|
int nfrag = 0;
|
||||||
int fail = 0;
|
int fail = 0;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
char *out = NULL;
|
int want = 0;
|
||||||
int rc = run6c(rows[i].src, &out);
|
while (want < 3 && rows[i].needle[want] != NULL)
|
||||||
if (rc != 0) {
|
want++;
|
||||||
fprintf(stderr, "row %d: w6c rc=%d\n", i, rc);
|
nfrag += want;
|
||||||
fail++;
|
char *out = codegen(rows[i].src);
|
||||||
free(out);
|
if (out == NULL) {
|
||||||
|
fprintf(stderr, "row %d: frontend/codegen failed\n", i);
|
||||||
|
fail += want;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!contains(out, rows[i].needle)) {
|
for (int j = 0; j < want; j++) {
|
||||||
|
if (strstr(out, rows[i].needle[j]) != NULL)
|
||||||
|
continue;
|
||||||
fprintf(stderr, "row %d: missing '%s' in:\n%s\n",
|
fprintf(stderr, "row %d: missing '%s' in:\n%s\n",
|
||||||
i, rows[i].needle, out);
|
i, rows[i].needle[j], out);
|
||||||
fail++;
|
fail++;
|
||||||
}
|
}
|
||||||
free(out);
|
free(out);
|
||||||
}
|
}
|
||||||
if (fail) { fprintf(stderr, "%d/%d w6c tests failed\n", fail, n); return 1; }
|
if (fail) {
|
||||||
printf("w6c: %d/%d ok\n", n, n);
|
fprintf(stderr, "%d/%d w6c fragments failed\n", fail, nfrag);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("w6c: %d/%d fragments ok in %d sources\n", nfrag, nfrag, n);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user