Compare commits
No commits in common. "ffcf2fb013cf748173d4734b18b35278e650ea2f" and "e19fc8820a46c12c54a1ced04226e9e55648313a" have entirely different histories.
ffcf2fb013
...
e19fc8820a
59
Makefile
59
Makefile
@ -20,13 +20,9 @@ SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
|
||||
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC_FILES))
|
||||
OBJ_FILES_NOMAIN = $(filter-out $(OBJ_DIR)/main.o, $(OBJ_FILES)) # Object files without main.c.
|
||||
GRAM_FILES = $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h
|
||||
UNITY_DIR = $(TEST_DIR)/Unity
|
||||
UNITY_C = $(UNITY_DIR)/src/unity.c
|
||||
UNITY_H = $(UNITY_DIR)/src/unity.h
|
||||
UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
|
||||
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))
|
||||
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
|
||||
|
||||
RESETCOLOR = \033[0m
|
||||
WHITE = $(RESETCOLOR)\033[37m
|
||||
@ -38,60 +34,51 @@ release: clean
|
||||
release: CFLAGS = -Wall -O2
|
||||
release: $(TARGET)
|
||||
|
||||
# Run the target.
|
||||
run: $(TARGET)
|
||||
./$(TARGET)
|
||||
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
|
||||
@ ./$(TARGET)
|
||||
|
||||
# Generate grammars with bison.
|
||||
$(GRAM_FILES): $(SRC_DIR)/grammar.y
|
||||
@ mkdir -p $(GRAM_DIR)
|
||||
@ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR)"
|
||||
bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
|
||||
@ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR) bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h"
|
||||
@ bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
|
||||
|
||||
# Compile grammars.
|
||||
$(OBJ_DIR)/grammar.o: $(GRAM_DIR)/grammar.tab.c $(GRAM_DIR)/grammar.tab.h $(OBJ_DIR)/lexer.o
|
||||
@ echo -e "$(WHITE_BOLD)Compiling grammars...$(RESETCOLOR)"
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Lexer depends on grammars.
|
||||
$(OBJ_DIR)/lexer.o: $(SRC_DIR)/lexer.c $(GRAM_FILES)
|
||||
@ mkdir -p $(OBJ_DIR)
|
||||
@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Compile project source objects.
|
||||
# Compile project sources.
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
|
||||
@ mkdir -p $(OBJ_DIR)
|
||||
@ echo -e "$(WHITE_BOLD)Compiling source object $(WHITE)$@$(WHITE_BOLD)... $(RESETCOLOR)"
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
@ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
|
||||
@ $(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Link to final binary.
|
||||
$(TARGET): $(OBJ_DIR)/grammar.o $(OBJ_FILES)
|
||||
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||
$(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
|
||||
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
|
||||
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(OBJ_DIR)/grammar.o $(LDFLAGS)
|
||||
|
||||
# Compile Unity object.
|
||||
$(UNITY_OBJ): $(UNITY_C) $(UNITY_H)
|
||||
@ echo -e "$(WHITE_BOLD)Compiling Unity...$(RESETCOLOR)"
|
||||
$(CC) $(CFLAGS) -D UNITY_OUTPUT_COLOR -c $< -o $@
|
||||
# Compile test sources.
|
||||
$(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c
|
||||
@ mkdir -p $(TEST_OBJ_DIR)
|
||||
@ 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 $@
|
||||
|
||||
# Compile test object.
|
||||
$(TEST_OBJ_DIR)/test_%.o: $(TEST_DIR)/test_%.c
|
||||
@ echo -e "$(WHITE_BOLD)Compiling test object $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Link final test binary.
|
||||
$(TEST_BUILD_DIR)/test_%.out: $(TEST_OBJ_DIR)/test_%.o $(OBJ_DIR)/%.o $(UNITY_OBJ)
|
||||
@ echo -e "$(WHITE_BOLD)Linking test binary $(WHITE)$@$(WHITE_BOLD)...$(RESETCOLOR)"
|
||||
$(LINK) -o $@ $? $(LDFLAGS)
|
||||
|
||||
# Run the test files.
|
||||
test: $(TEST_BIN_FILES)
|
||||
@ echo -e "$(WHITE_BOLD)Running tests...$(RESETCOLOR)"
|
||||
for test in $< do ./$${test}; done
|
||||
# Link the test executable.
|
||||
test: $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C)
|
||||
@ 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 "$(WHITE_BOLD)Cleaning up...$(RESETCOLOR)"
|
||||
rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(UNITY_OBJ)
|
||||
@ echo -e "$(WHITE_BOLD)Cleaning up...$(WHITE) $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/* $(RESETCOLOR)"
|
||||
@ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) $(GRAM_DIR)/*
|
||||
|
||||
.PHONY: all clean test nocolor release run
|
||||
|
28
src/dstr.c
28
src/dstr.c
@ -1,7 +1,6 @@
|
||||
#include "include/dstr.h"
|
||||
#include "include/util.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -21,21 +20,16 @@ void dstr_destroy(Dstr* dstr) {
|
||||
free(dstr);
|
||||
}
|
||||
|
||||
// Check whether the buffer is overflowing and resize it if necessary.
|
||||
void check_resz(Dstr* dstr, size_t ln) {
|
||||
while (dstr->ln + ln + 1 > dstr->bufsz) {
|
||||
void dstr_append(Dstr* dest, char* src, size_t ln) {
|
||||
while (dest->ln + ln + 1 > dest->bufsz) {
|
||||
// Double the buffer size when overflown.
|
||||
dstr->bufsz *= 2;
|
||||
dstr->buf = realloc(dstr->buf, dstr->bufsz);
|
||||
dest->bufsz *= 2;
|
||||
dest->buf = realloc(dest->buf, dest->bufsz);
|
||||
log_dbgf(
|
||||
"dstr @ %p doubled from %ld to %ld", dstr, dstr->bufsz / 2,
|
||||
dstr->bufsz
|
||||
"dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2,
|
||||
dest->bufsz
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void dstr_append(Dstr* dest, char* src, size_t ln) {
|
||||
check_resz(dest, ln);
|
||||
|
||||
// Overwrites the \0 at the end of the string, keeps the null from the given
|
||||
// string.
|
||||
@ -44,7 +38,15 @@ void dstr_append(Dstr* dest, char* src, size_t ln) {
|
||||
}
|
||||
|
||||
void dstr_appendch(Dstr* dest, char ch) {
|
||||
check_resz(dest, 1);
|
||||
if (dest->ln + 1 + 1 > dest->bufsz) {
|
||||
// Double the buffer size when overflown.
|
||||
dest->bufsz *= 2;
|
||||
dest->buf = realloc(dest->buf, dest->bufsz);
|
||||
log_dbgf(
|
||||
"dstr @ %p doubled from %ld to %ld", dest, dest->bufsz / 2,
|
||||
dest->bufsz
|
||||
);
|
||||
}
|
||||
|
||||
// Overwrites the preexisting null terminator, and adds one of its own.
|
||||
dest->buf[dest->ln] = ch;
|
||||
|
@ -1,2 +1,4 @@
|
||||
// This file serves no purpose but because I've written my makefile this way it
|
||||
// has to exist for things to compile :P. TODO: Fix this.
|
||||
|
||||
int four() { return 4; }
|
||||
|
0
test/main.c
Normal file
0
test/main.c
Normal file
@ -1,79 +0,0 @@
|
||||
#include "../src/include/dstr.h"
|
||||
#include "Unity/src/unity.h"
|
||||
#include <string.h>
|
||||
|
||||
void setUp() {};
|
||||
void tearDown() {};
|
||||
|
||||
void test_dstr_init() {
|
||||
Dstr* dstr = dstr_init();
|
||||
TEST_ASSERT_EQUAL(0, strlen(dstr->buf));
|
||||
TEST_ASSERT_EQUAL(0, dstr->ln);
|
||||
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||
}
|
||||
|
||||
void test_dstr_append() {
|
||||
Dstr* dstr;
|
||||
|
||||
// Test simple appending.
|
||||
dstr = dstr_init();
|
||||
char* hello_world = "Hello, world!";
|
||||
dstr_append(dstr, hello_world, strlen(hello_world));
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(hello_world, dstr->buf);
|
||||
TEST_ASSERT_EQUAL(strlen(hello_world), dstr->ln);
|
||||
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||
|
||||
dstr_destroy(dstr);
|
||||
|
||||
// Test buffer doubling.
|
||||
dstr = dstr_init();
|
||||
|
||||
char h[DSTR_INITSZ + 20];
|
||||
memset(h, 'h', DSTR_INITSZ + 19);
|
||||
h[DSTR_INITSZ + 19] = '\0';
|
||||
dstr_append(dstr, h, strlen(h));
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
|
||||
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
|
||||
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
|
||||
}
|
||||
|
||||
void test_dstr_appendch() {
|
||||
Dstr* dstr;
|
||||
|
||||
// Test simple character appending.
|
||||
dstr = dstr_init();
|
||||
char c = 'c';
|
||||
char* c_str = "c";
|
||||
dstr_appendch(dstr, c);
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(c_str, dstr->buf);
|
||||
TEST_ASSERT_EQUAL(strlen(c_str), dstr->ln);
|
||||
TEST_ASSERT_EQUAL(DSTR_INITSZ, dstr->bufsz);
|
||||
|
||||
dstr_destroy(dstr);
|
||||
|
||||
// Test buffer doubling.
|
||||
dstr = dstr_init();
|
||||
|
||||
// Test against this string.
|
||||
char h[DSTR_INITSZ + 20];
|
||||
memset(h, 'h', DSTR_INITSZ + 19);
|
||||
h[DSTR_INITSZ + 19] = '\0';
|
||||
|
||||
for (int i = 0; i < DSTR_INITSZ + 19; i++) dstr_appendch(dstr, 'h');
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING(h, dstr->buf);
|
||||
TEST_ASSERT_EQUAL(strlen(h), dstr->ln);
|
||||
TEST_ASSERT_EQUAL(DSTR_INITSZ * 2, dstr->bufsz);
|
||||
}
|
||||
|
||||
// not needed when using generate_test_runner.rb
|
||||
int main(void) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_dstr_init);
|
||||
RUN_TEST(test_dstr_append);
|
||||
RUN_TEST(test_dstr_appendch);
|
||||
return UNITY_END();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user