Add some unittests

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-03-18 22:34:59 +00:00
parent 2cefefb531
commit aa37c4f4ca
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
5 changed files with 59 additions and 2 deletions

View File

@ -54,6 +54,8 @@ foreach w : warns
endif
endforeach
test_h_dep = subproject('test.h').get_variable('test_h_dep')
subdir('src')
subdir('man')

View File

@ -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')

View File

@ -24,6 +24,7 @@
#include <xcb/sync.h>
#include <xcb/xfixes.h>
#include <test.h>
#include <ev.h>
#include "common.h"

View File

@ -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

View File

@ -3,6 +3,8 @@
#include <string.h>
#include <test.h>
#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');
}