diff --git a/meson.build b/meson.build index 7b30f68f..2251f40b 100644 --- a/meson.build +++ b/meson.build @@ -54,6 +54,8 @@ foreach w : warns endif endforeach +test_h_dep = subproject('test.h').get_variable('test_h_dep') + subdir('src') subdir('man') diff --git a/meson_options.txt b/meson_options.txt index 95292d17..a999bf75 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -12,3 +12,5 @@ option('xrescheck', type: 'boolean', value: false, description: 'Enable X resour option('build_docs', type: 'boolean', value: false, description: 'Build documentation and man pages') option('modularize', type: 'boolean', value: false, description: 'Build with clang\'s module system') + +option('unittest', type: 'boolean', value: false, description: 'Enable unittests in the code') diff --git a/src/compton.c b/src/compton.c index 2263ffbd..16a7966c 100644 --- a/src/compton.c +++ b/src/compton.c @@ -24,6 +24,7 @@ #include #include +#include #include #include "common.h" diff --git a/src/meson.build b/src/meson.build index 2dec9dba..44b34173 100644 --- a/src/meson.build +++ b/src/meson.build @@ -62,8 +62,16 @@ if get_option('xrescheck') srcs += [ 'xrescheck.c' ] endif +if get_option('unittest') + cflags += ['-DUNIT_TEST'] +endif + subdir('backend') -executable('compton', srcs, c_args: cflags, - dependencies: [ base_deps, deps ], +compton = executable('compton', srcs, c_args: cflags, + dependencies: [ base_deps, deps, test_h_dep ], install: true, include_directories: compton_inc) + +if get_option('unittest') + test('compton unittest', compton, args: [ '--unittest' ]) +endif diff --git a/src/string_utils.c b/src/string_utils.c index 38eb92d8..65af0f20 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -3,6 +3,8 @@ #include +#include + #include "compiler.h" #include "string_utils.h" #include "utils.h" @@ -33,6 +35,20 @@ char *mstrjoin(const char *src1, const char *src2) { return str; } +TEST_CASE(mstrjoin) { + char *str = mstrjoin("asdf", "qwer"); + TEST_STREQUAL(str, "asdfqwer"); + free(str); + + str = mstrjoin("", "qwer"); + TEST_STREQUAL(str, "qwer"); + free(str); + + str = mstrjoin("asdf", ""); + TEST_STREQUAL(str, "asdf"); + free(str); +} + /** * Concatenate a string on heap with another string. */ @@ -51,6 +67,19 @@ void mstrextend(char **psrc1, const char *src2) { (*psrc1)[len - 1] = '\0'; } +TEST_CASE(mstrextend) { + char *str1 = NULL; + mstrextend(&str1, "asdf"); + TEST_STREQUAL(str1, "asdf"); + + mstrextend(&str1, "asd"); + TEST_STREQUAL(str1, "asdfasd"); + + mstrextend(&str1, ""); + TEST_STREQUAL(str1, "asdfasd"); + free(str1); +} + #pragma GCC diagnostic pop /// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*) @@ -83,3 +112,18 @@ double strtod_simple(const char *src, const char **end) { *end = src; return ret * neg; } + +TEST_CASE(strtod_simple) { + const char *end; + double result = strtod_simple("1.0", &end); + TEST_EQUAL(result, 1); + TEST_EQUAL(*end, '\0'); + + result = strtod_simple("-1.0", &end); + TEST_EQUAL(result, -1); + TEST_EQUAL(*end, '\0'); + + result = strtod_simple("+.5", &end); + TEST_EQUAL(result, 0.5); + TEST_EQUAL(*end, '\0'); +}