diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4bc5db --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +calc +case_* +casegen +out \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e46d5d3 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +.PHONY: clean + +out: calc case_all + ./calc < case_all > out + +case_all: case_add case_sub case_mul case_div + cat case_add case_sub case_mul case_div > case_all + +case_add: casegen + ./casegen "add" 100 > case_add + +case_sub: casegen + ./casegen "sub" 100 > case_sub + +case_mul: casegen + ./casegen "mul" 100 > case_mul + +case_div: casegen + ./casegen "div" 100 > case_div + +calc: calc.c + gcc -o calc calc.c + +casegen: casegen.c + gcc -o casegen casegen.c +clean: + rm -f out calc casegen case_* *.o diff --git a/casegen.c b/casegen.c new file mode 100644 index 0000000..6c736c8 --- /dev/null +++ b/casegen.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +const char *ops[] = {"add", "sub", "mul", "div"}; + +uint32_t xrand() { + /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ + static uint32_t x = 2024; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return x; +} + +void help() { + printf("Usage: ./casegen \n" + "Available s: add, sub, mul, div.\n" + ": The number of test cases generated (a positive integer).\n"); +} + +int is_op_legal(char *op) { + size_t sz = sizeof(ops) / sizeof(ops[0]); + for (int i = 0; i < sz; i++) { + if (strcmp(op, ops[i]) == 0) { + return 1; + } + } + return 0; +} + +int get_num(char *str_num, int *num) { + *num = strtol(str_num, NULL, 10); + return *num > 0; +} + +int main(int argc, char **argv) { + int num; + if (argc != 3 || !is_op_legal(argv[1]) || !get_num(argv[2], &num)) { + help(); + return 1; + } + while (num--) { + printf("%s %u %u\n", argv[1], xrand() % 10000 + 1, xrand() % 10000 + 1); + } + return 0; +} diff --git a/runbin b/runbin new file mode 100644 index 0000000..0482405 Binary files /dev/null and b/runbin differ