Skip to content
This repository was archived by the owner on Aug 19, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
calc
case_*
casegen
out
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions casegen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 <op> <num>\n"
"Available <op>s: add, sub, mul, div.\n"
"<num>: 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;
}
Binary file added runbin
Binary file not shown.