diff options
author | Oxore <oxore@protonmail.com> | 2018-07-12 11:51:00 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2018-07-12 11:51:00 +0300 |
commit | d3fddd7d28f314a6738f2a0ba4d9f58024b01984 (patch) | |
tree | 7345455bdc6f051289ffa9854fcad2001a55a223 | |
parent | 9a9711945c2add826e5887aabe2330bee9042b4b (diff) |
Introduce testing with munit, refactor
Split unicode routines from text.
Testing: add munit submodule, move translation units with main functions
to separate folder, make corresponding changes in Makefile. Make simple
test for unicode handling routine.
Remove _vimrc_local.
test.c: optimize includes.
Add test run to .travis.yml.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | Makefile | 48 | ||||
-rw-r--r-- | _vimrc_local.vim | 9 | ||||
m--------- | deps/munit | 0 | ||||
-rw-r--r-- | include/text.h | 2 | ||||
-rw-r--r-- | include/unicode.h | 2 | ||||
-rw-r--r-- | src/engine.c | 1 | ||||
-rw-r--r-- | src/target/test.c | 57 | ||||
-rw-r--r-- | src/target/tetris.c (renamed from src/main.c) | 8 | ||||
-rw-r--r-- | src/text.c | 53 | ||||
-rw-r--r-- | src/unicode.c | 52 |
13 files changed, 163 insertions, 79 deletions
@@ -1,4 +1,5 @@ tetris +test *.o *.swp *.out diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d493dd7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deps/munit"] + path = deps/munit + url = https://github.com/nemequ/munit.git diff --git a/.travis.yml b/.travis.yml index 8376b6c..eb30128 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,12 @@ language: c os: osx before_script: + - git submodule update --init --recursive - brew install csfml -script: +script: - make all + - ./test -compiler: +compiler: - gcc @@ -1,15 +1,23 @@ Q=@ QQ=@ -TARGET:=tetris +TARGET_TETRIS:=tetris +TARGET_TEST:=test +MUNIT:=deps/munit + +ifeq ($(wildcard $(MUNIT)/*),) +NOTEST=1 +endif -BUILD:=build SRC:=src +BUILD:=build +TARGET:=$(BUILD)/target SOURCES:=$(wildcard $(SRC)/*.c) OBJECTS:=$(SOURCES:$(SRC)/%.c=$(BUILD)/%.c.o) DEPENDS:=$(OBJECTS:.o=.d) INCLUDE+=include +INCLUDE+=$(MUNIT) INCLUDE:=$(INCLUDE:%=-I%) #COMMON+=-fsanitize=address @@ -27,25 +35,44 @@ CFLAGS+=-O0 CFLAGS+=-MD LDFLAGS+=$(COMMON) -LDFLAGS+=-lcsfml-graphics -LDFLAGS+=-lcsfml-window -LDFLAGS+=-lcsfml-system -LDFLAGS+=-lyaml + +LDFLAGS_TETRIS+=$(LDFLAGS) +LDFLAGS_TETRIS+=-lcsfml-graphics +LDFLAGS_TETRIS+=-lcsfml-window +LDFLAGS_TETRIS+=-lcsfml-system +LDFLAGS_TETRIS+=-lyaml + +LDFLAGS_TEST+=$(LDFLAGS) #====================================================================== all: all: $(TARGET) +all: $(TARGET_TETRIS) +ifndef NOTEST +all: $(TARGET_TEST) +endif -$(TARGET): $(OBJECTS) +$(TARGET_TETRIS): $(OBJECTS) $(TARGET)/$(TARGET_TETRIS).c.o $(QQ) echo " LD $@" - $(Q) $(CC) -o $@ $^ $(LDFLAGS) + $(Q) $(CC) -o $@ $^ $(LDFLAGS_TETRIS) + +$(TARGET_TEST): $(TARGET)/$(TARGET_TEST).c.o $(MUNIT)/munit.c.o $(BUILD)/unicode.c.o + $(QQ) echo " LD $@" + $(Q) $(CC) -o $@ $^ $(LDFLAGS_TEST) $(DEPENDS): | $(BUILD) $(OBJECTS): | $(BUILD) $(BUILD): - $(Q) mkdir -p $(BUILD) + $(Q) mkdir -p $@ + +$(TARGET): + $(Q) mkdir -p $@ + +$(MUNIT)/munit.c.o: $(MUNIT)/munit.c + $(QQ) echo " CC $@" + $(Q) $(CC) -c $(CFLAGS) -o $@ $< $(BUILD)/%.c.o: $(SRC)/%.c $(QQ) echo " CC $@" @@ -54,6 +81,7 @@ $(BUILD)/%.c.o: $(SRC)/%.c -include $(DEPENDS) clean: - $(Q) $(RM) -rfv $(TARGET) $(BUILD) + $(Q) $(RM) -rfv $(TARGET_TETRIS) $(TARGET_TEST) $(BUILD) + $(Q) $(RM) -rfv $(MUNIT)/*.d $(MUNIT)/*.o .PHONY: all clean diff --git a/_vimrc_local.vim b/_vimrc_local.vim deleted file mode 100644 index 78f02f7..0000000 --- a/_vimrc_local.vim +++ /dev/null @@ -1,9 +0,0 @@ -if (&ft=='c' || &ft=='h' || &ft=='cpp') - setlocal shiftwidth=4 - setlocal tabstop=4 - setlocal expandtab -endif - -" Neomake c include -let g:neomake_c_enabled_makers = ['gcc'] -let g:neomake_c_gcc_maker = {'args': ['-fsyntax-only', '-Wall', '-Wextra', '-I../include/', '-I./include/']} diff --git a/deps/munit b/deps/munit new file mode 160000 +Subproject 439de4a9b136bc3b5163e73d4caf37c590bef87 diff --git a/include/text.h b/include/text.h index 8a939c2..5c2dc5d 100644 --- a/include/text.h +++ b/include/text.h @@ -11,7 +11,5 @@ typedef struct text { unsigned long id; } Text; -unsigned long utf8_strlen(void *string); -void utf8to32_strcpy(wchar_t *dest, char *src); struct idlist *load_texts(char *filename); void text_destroy(void *text); diff --git a/include/unicode.h b/include/unicode.h new file mode 100644 index 0000000..f950f12 --- /dev/null +++ b/include/unicode.h @@ -0,0 +1,2 @@ +unsigned long utf8_strlen(void *string); +void utf8to32_strcpy(wchar_t *dest, char *src); diff --git a/src/engine.c b/src/engine.c index d9cfe98..482c6df 100644 --- a/src/engine.c +++ b/src/engine.c @@ -11,6 +11,7 @@ #include "vector.h" #include "text.h" #include "field.h" +#include "unicode.h" #include "painter.h" #include "engine.h" diff --git a/src/target/test.c b/src/target/test.c new file mode 100644 index 0000000..60439a5 --- /dev/null +++ b/src/target/test.c @@ -0,0 +1,57 @@ +#include "munit.h" + +#include "unicode.h" + +struct u_pair { + char *utf8; + wchar_t *utf32; +}; + +struct u_pair *text_fixture; + +static void *test_text_setup(const MunitParameter params[], void *user_data) { + (void) params; + (void) user_data; + + return text_fixture; +} + +static MunitResult test_text(const MunitParameter params[], void *fixture) { + (void) params; + struct u_pair *f = fixture; + + munit_assert_ulong(utf8_strlen(f[0].utf8), ==, strlen(f[0].utf8)); + + return MUNIT_OK; +} + +static MunitTest test_suite_tests[] = { + { + "/example/parameters", + test_text, + test_text_setup, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, + {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL} +}; + +static const MunitSuite test_suite = { + "unit", + test_suite_tests, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE +}; + +int main(int argc, char **argv) { + text_fixture = (struct u_pair []){ + { + .utf8 = "Single byte ascii symbols string", + .utf32 = L"Single byte ascii symbols string" + } + }; + + return munit_suite_main(&test_suite, NULL, argc, argv); +} diff --git a/src/main.c b/src/target/tetris.c index bf9014a..2419c8d 100644 --- a/src/main.c +++ b/src/target/tetris.c @@ -1,11 +1,11 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> #include <SFML/System/Clock.h> #include <SFML/Window/Keyboard.h> #include <SFML/Graphics/RenderWindow.h> #include <SFML/Graphics/Font.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> #include "common.h" #include "idlist.h" @@ -1,63 +1,12 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <SFML/Graphics/Font.h> -#include <SFML/Graphics/Text.h> -#include <SFML/Graphics.h> #include <yaml.h> -#include "common.h" #include "idlist.h" #include "vector.h" #include "text.h" - -static inline unsigned int utf8_char_len(unsigned char c) -{ - if (c > 0x00 && c < 0xC0) - return 1; - else if (c >= 0xC2 && c < 0xE0) - return 2; - else if (c >= 0xE0 && c < 0xF0) - return 3; - else if (c >= 0xF0 && c < 0xF5) - return 4; - else - return 0; -} - -unsigned long utf8_strlen(void *string) -{ - unsigned long len = 0, keep = 0; - for (unsigned char *c = string; *c; (keep ? --keep : ++len), ++c) - if (!keep) - keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep; - return len; -} - -void utf8to32_strcpy(wchar_t *dest, char *src) -{ - wchar_t *dc = dest; - char *c = src; - unsigned long len = 0; - while (*c) { - int clen = utf8_char_len(*c); - if (clen == 1) { - dc[len] = c[0] & 0x7f; - } else if (clen == 2) { - dc[len] = ((c[0] & 0x1f) << 6) | ((c[1] & 0x3f) << 0); - } else if (clen == 3) { - dc[len] = ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) | ((c[2] & 0x3f) << 0); - } else if (clen == 4) { - dc[len] = ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) | ((c[2] & 0x3f) << 6) | ((c[3] & 0x3f) << 0); - } else { - dc[len] = 0; - return; - } - c += clen; - ++len; - } - dc[len] = 0; -} +#include "unicode.h" static FILE *openFile(char *filename) { diff --git a/src/unicode.c b/src/unicode.c new file mode 100644 index 0000000..dea541a --- /dev/null +++ b/src/unicode.c @@ -0,0 +1,52 @@ +#include <stdlib.h> +#include <string.h> + +#include "unicode.h" + +static inline unsigned int utf8_char_len(unsigned char c) +{ + if (c > 0x00 && c < 0xC0) + return 1; + else if (c >= 0xC2 && c < 0xE0) + return 2; + else if (c >= 0xE0 && c < 0xF0) + return 3; + else if (c >= 0xF0 && c < 0xF5) + return 4; + else + return 0; +} + +unsigned long utf8_strlen(void *string) +{ + unsigned long len = 0, keep = 0; + for (unsigned char *c = string; *c; (keep ? --keep : ++len), ++c) + if (!keep) + keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep; + return len; +} + +void utf8to32_strcpy(wchar_t *dest, char *src) +{ + wchar_t *dc = dest; + char *c = src; + unsigned long len = 0; + while (*c) { + int clen = utf8_char_len(*c); + if (clen == 1) { + dc[len] = c[0] & 0x7f; + } else if (clen == 2) { + dc[len] = ((c[0] & 0x1f) << 6) | ((c[1] & 0x3f) << 0); + } else if (clen == 3) { + dc[len] = ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) | ((c[2] & 0x3f) << 0); + } else if (clen == 4) { + dc[len] = ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) | ((c[2] & 0x3f) << 6) | ((c[3] & 0x3f) << 0); + } else { + dc[len] = 0; + return; + } + c += clen; + ++len; + } + dc[len] = 0; +} |