Skip to content

Commit

Permalink
One the way to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrflor committed Feb 4, 2023
1 parent 5155f06 commit 54aa8e1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ BIN=$(BUILD)/openai
OBJS=$(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(SRCS))

SRCS=$(wildcard $(SRC)/*.c)
TESTS=$(wildcard $(TEST)/*.c))
TESTS=$(wildcard $(TEST)/*.c)

LIBRCS=$(filter-out $(SRC)/main.c, $(SRCS))

INITBUILD=mkdir -p $(BUILD)
COMPILE=$(INITBUILD) && $(TCC) $(CFLAGS) $(SRCS)
COMPILE=$(INITBUILD) && $(TCC) $(CFLAGS)

all: $(BIN) $(BUILD)/libopenai-c.a $(BUILD)/libopenai-c.so

Expand All @@ -33,10 +35,13 @@ $(BUILD)/libopenai-c.so: $(SRCS)
$(CC) -fPIC -shared $(SRCS) -o $@ $(CFLAGS)

run:
$(COMPILE) -g -o $(BIN) && ./$(BIN)
$(COMPILE) $(SRCS) -g -o $(BIN) && ./$(BIN)

test:
$(COMPILE) $(LIBRCS) $(TESTS) -DDEBUG -g -o $(BIN)_test && ./$(BIN)_test

debug:
$(COMPILE) -g -o $(BIN) && $(DEBUGGER) -q -ex run $(BIN)
$(COMPILE) $(SRCS) -g -o $(BIN) && $(DEBUGGER) -q -ex run $(BIN)

clean:
rm -r $(BUILD)
47 changes: 47 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "../src/util.h"
#include <assert.h>
#include <stdio.h>

#define TEST(name, func) ;

#define expect(actual, expected, evaluator) \
TOTAL_TESTS++; \
if (!(evaluator(actual, expected))) { \
FAILED_TESTS++; \
printf("Assertion failed\n, expected: , got: "); \
}

#define equals(a, b) a == b

int TOTAL_TESTS = 0;
int FAILED_TESTS = 0;

struct Test {
char *name;
int (*func)(void);
};

int trol(void) { return 0; }

int firstTest() {
expect(1, 1, equals);
return 0;
}

int main(int argc, char *argv[]) {

printf("\n[TESTS] started...\n");

struct Test test = {
.name = "SuperTest",
.func = trol,
};

log("Super login\n");
printf("[TESTS] RAN: %d, PASSED: %d, FAILED: %d, PERCENT: %.2f%%\n",
TOTAL_TESTS, TOTAL_TESTS - FAILED_TESTS, FAILED_TESTS,
TOTAL_TESTS == 0 ? 0
: 100 * (((float)TOTAL_TESTS - (float)FAILED_TESTS)) /
(float)TOTAL_TESTS);
return 0;
}

0 comments on commit 54aa8e1

Please sign in to comment.