Test and stuff.

Need to fix testing functions so that every assert is run in each
function of the registry.
This commit is contained in:
Jacob Signorovitch 2024-09-30 20:55:46 -04:00
parent 32386ccf58
commit d95c134a54
10 changed files with 151 additions and 19 deletions

View File

@ -21,35 +21,40 @@ UNITY_C = $(TEST_DIR)/unity/unity.c
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
# Stupid things.
RESETCOLOR = \x1b[0m
WHITE = $(RESETCOLOR)\x1b[37m
WHITE_BOLD = $(RESETCOLOR)\x1b[37;1m
all: $(TARGET)
# Link to final binary.
$(TARGET): $(OBJ_FILES)
@ echo -e "\x1b[37;1mLinking \x1b[0m\x1b[37m$(TARGET)\x1b[37;1m...\x1b[0m\x1b[37m $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)\x1b[0m"
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
# Compile project sources.
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/include/%.h
@ mkdir -p $(OBJ_DIR)
@ echo -e "\x1b[37;1mCompiling \x1b[0m\x1b[37m$<\x1b[37;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -c $< -o $@\x1b[0m"
@ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
@ $(CC) $(CFLAGS) -c $< -o $@
# Compile test sources.
$(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c
@ mkdir -p $(TEST_OBJ_DIR)
@ echo -e "\x1b[37;1mCompiling Test \x1b[0m\x1b[37m$<\x1b[37;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@\x1b[0m"
@ echo -e "$(WHITE_BOLD)Compiling Test $(WHITE)$<$(WHITE_BOLD)... $(WHITE)$(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@$(RESETCOLOR)"
@ $(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@
# Link the test executable.
test: $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C)
@ echo -e "\x1b[37;1mLinking test binary\x1b[0m\x1b[37m...\x1b[0m"
@ $(LINK) $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C) -o $(TEST_BUILD_DIR)/test.out
@ echo -e "\x1b[37;1mRunning tests\x1b[0m\x1b[37m...\x1b[0m"
@ echo -e "$(WHITE_BOLD)Linking test binary$(WHITE)...$(RESETCOLOR)"
@ $(LINK) -DUNITY_OUTPUT_COLOR $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C) -o $(TEST_BUILD_DIR)/test.out
@ echo -e "$(WHITE_BOLD)Running tests$(WHITE)...$(RESETCOLOR)"
@ ./$(TEST_BUILD_DIR)/test.out
clean:
@ echo -e "\x1b[37;1mCleaning up...\x1b[0m\x1b[37m $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)\x1b[0m"
@ echo -e "$(WHITE_BOLD)Cleaning up...$(WHITE) $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)$(RESETCOLOR)"
@ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)
.PHONY: all clean test
.PHONY: all clean test nocolor

12
src/include/lexer.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef LEXER_H
#define LEXER_H
#include "token.h"
// Lexer: converts text to tokens.
typedef struct Lexer {
char* src;
} lexer_t;
#endif

18
src/include/token.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef TOKEN_H
#define TOKEN_H
typedef enum TokenType {
TOKEN_TYPE_CALL,
TOKEN_TYPE_NUMBER,
} TokenType;
// Token.
typedef struct Token {
TokenType type; // The type of the Token.
char* val; // The text of the Token.
} Token;
Token* token_init(TokenType type, char* val);
void token_destroy(Token* token);
#endif

17
src/token.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdlib.h>
#include "include/token.h"
Token* token_init(TokenType type, char* val) {
Token* t = malloc(sizeof(Token));
t->type = type;
t->val = val;
return t;
}
void token_destroy(Token* t) {
free(t->val);
free(t);
}

12
test/main.c Normal file
View File

@ -0,0 +1,12 @@
#include "unity/unity.h"
#include "registry.h"
void (*tests[30])();
void setUp() {}
void tearDown() {}
int main() {
run_all_tests();
return 0;
}

19
test/registry.c Normal file
View File

@ -0,0 +1,19 @@
#include "unity/unity_internals.h"
#include "registry.h"
#define TESTS_MAX 128
static Test tests[TESTS_MAX];
static int test_count = 0;
void register_test(Test t) {
if (test_count < TESTS_MAX) {
tests[test_count] = t;
test_count++;
} else
printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX);
}
void run_all_tests() {
for (int i = 0; i < test_count; i++) tests[i]();
}

15
test/registry.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef REGISTRY_H
#define REGISTRY_H
#include <stdio.h>
// Test functions neither consume nor return anything.
typedef void (*Test)(void);
// Register a new test function.
void register_test(Test t);
// Run all registered tests.
void run_all_tests();
#endif

32
test/token.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdlib.h>
#include "unity/unity.h"
#include "registry.h"
#include "unity/unity_internals.h"
#include "../src/include/token.h"
void test_token_init() {
char* s = malloc(sizeof("Hello, world!"));
s = "Hello, world!";
Token* t = token_init(TOKEN_TYPE_CALL, s);
TEST_ASSERT_EQUAL(TOKEN_TYPE_NUMBER, t->type);
TEST_ASSERT_EQUAL_STRING("Hellso, world!", t->val);
}
void test_token_destroy() {
char* s = malloc(1 * sizeof(char));
*s = 'h';
Token* t = token_init(TOKEN_TYPE_CALL, s);
token_destroy(t);
}
void token_test() {
UNITY_BEGIN();
RUN_TEST(test_token_init);
RUN_TEST(test_token_destroy);
UNITY_END();
}
__attribute__((constructor)) void register_tests_token() {
register_test(token_test);
}

View File

@ -24,10 +24,10 @@ void UNITY_OUTPUT_CHAR(int);
struct UNITY_STORAGE_T Unity;
#ifdef UNITY_OUTPUT_COLOR
const char UNITY_PROGMEM UnityStrOk[] = "\033[42mOK\033[0m";
const char UNITY_PROGMEM UnityStrPass[] = "\033[42mPASS\033[0m";
const char UNITY_PROGMEM UnityStrFail[] = "\033[41mFAIL\033[0m";
const char UNITY_PROGMEM UnityStrIgnore[] = "\033[43mIGNORE\033[0m";
const char UNITY_PROGMEM UnityStrOk[] = "\033[32;1mOK\033[0m";
const char UNITY_PROGMEM UnityStrPass[] = "\033[32;1mPASS\033[0m";
const char UNITY_PROGMEM UnityStrFail[] = "\033[31;1mFAIL\033[0m";
const char UNITY_PROGMEM UnityStrIgnore[] = "\033[30;1mIGNORE\033[0m";
#else
const char UNITY_PROGMEM UnityStrOk[] = "OK";
const char UNITY_PROGMEM UnityStrPass[] = "PASS";

View File

@ -1,17 +1,19 @@
#include "unity/unity.h"
#include "../src/include//util.h"
void setUp() {}
void tearDown() {}
#include "registry.h"
#include "unity/unity_internals.h"
#include "../src/include/util.h"
void test_is_even() {
TEST_ASSERT_EQUAL(0, is_even(1));
TEST_ASSERT_EQUAL(1, is_even(2));
}
int main() {
void util_test() {
UNITY_BEGIN();
RUN_TEST(test_is_even);
return UNITY_END();
UNITY_END();
}
__attribute__((constructor)) void register_tests_util() {
register_test(util_test);
}