Skip to content

Commit

Permalink
add cmocka framework, test head (#67)
Browse files Browse the repository at this point in the history
* add cmocka framework

* separate test makefile

* separate test makefile

* add some head tests, fix bug in head_matches_name_desc_partial

* test wrappers, more head tests

* test asserts, iwyu

* more head tests

* more head tests

* remove unnecessary head tests
  • Loading branch information
alex-courtis committed Feb 26, 2023
1 parent d21ef54 commit 52ec8a9
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 46 deletions.
1 change: 1 addition & 0 deletions .ccls
Expand Up @@ -2,6 +2,7 @@ clang

-Iinc
-Ipro
-Itst

-D_GNU_SOURCE
-DVERSION="DUMMY"
Expand Down
12 changes: 3 additions & 9 deletions .gitignore
Expand Up @@ -3,13 +3,7 @@
pro/*.h
pro/*.c

way-displays
example-client
test

tags

.copy

cfg.dev.yaml
/way-displays
/example-client
/tst-*

16 changes: 10 additions & 6 deletions GNUmakefile
Expand Up @@ -33,7 +33,7 @@ $(PRO_C): $(PRO_X)
wayland-scanner private-code $(@:.c=.xml) $@

clean:
rm -f way-displays example_client $(SRC_O) $(EXAMPLE_O) $(PRO_O) $(PRO_H) $(PRO_C) tags .copy
rm -f way-displays example_client $(SRC_O) $(EXAMPLE_O) $(PRO_O) $(PRO_H) $(PRO_C)

install: way-displays way-displays.1 cfg.yaml
mkdir -p $(DESTDIR)$(PREFIX)/bin
Expand All @@ -59,10 +59,14 @@ cppcheck: $(SRC_C) $(SRC_CXX) $(INC_H) $(EXAMPLE_C)
cppcheck $(^) --enable=warning,unusedFunction,performance,portability $(CPPFLAGS)

# make -k iwyu
iwyu: CC = $(IWYU) -Xiwyu --check_also="inc/*h"
iwyu: CXX = $(IWYU) -Xiwyu --check_also="inc/marshalling.h"
iwyu: clean $(SRC_O)
IWYU = /usr/bin/include-what-you-use -Xiwyu --no_fwd_decls -Xiwyu --no_comments -Xiwyu --verbose=2
iwyu:
$(MAKE) -f tst/GNUmakefile tst-iwyu

.PHONY: all clean install uninstall man cppcheck iwyu
test:
$(MAKE) -f tst/GNUmakefile tst-all

clean-test:
$(MAKE) -f tst/GNUmakefile tst-clean

.PHONY: all clean install uninstall man cppcheck iwyu test clean-test tst-iwyu tst-all tst-clean

2 changes: 2 additions & 0 deletions inc/mode.h
Expand Up @@ -37,5 +37,7 @@ void mode_free(void *mode);

void mode_res_refresh_free(void *mode);

struct Mode *mode_user_mode(struct SList *modes, struct SList *modes_failed, struct UserMode *user_mode);

#endif // MODE_H

1 change: 0 additions & 1 deletion src/fds.c
@@ -1,7 +1,6 @@
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/signalfd.h>
Expand Down
31 changes: 2 additions & 29 deletions src/head.c
@@ -1,10 +1,8 @@
#include <regex.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <wayland-util.h>

#include "head.h"
Expand Down Expand Up @@ -36,31 +34,6 @@ bool head_matches_user_mode(const void *user_mode, const void *head) {
return user_mode && head && head_matches_name_desc_partial(((struct UserMode*)user_mode)->name_desc, (struct Head*)head);
}

struct Mode *user_mode(struct Head *head, struct UserMode *user_mode) {
if (!head || !head->name || !user_mode)
return NULL;

struct SList *i, *j;

// highest mode matching the user mode
struct SList *mrrs = modes_res_refresh(head->modes);
for (i = mrrs; i; i = i->nex) {
struct ModesResRefresh *mrr = i->val;
if (mrr && mrr_satisfies_user_mode(mrr, user_mode)) {
for (j = mrr->modes; j; j = j->nex) {
struct Mode *mode = j->val;
if (!slist_find_equal(head->modes_failed, NULL, mode)) {
slist_free_vals(&mrrs, mode_res_refresh_free);
return mode;
}
}
}
}
slist_free_vals(&mrrs, mode_res_refresh_free);

return NULL;
}

struct Mode *preferred_mode(struct Head *head) {
if (!head)
return NULL;
Expand Down Expand Up @@ -185,7 +158,7 @@ bool head_matches_name_desc_partial(const void *a, const void *b) {
return false;

return (
(head->name && strcasecmp(name_desc, head->name) == 0) ||
(head->name && strcasestr(head->name, name_desc)) ||
(head->description && strcasestr(head->description, name_desc))
);
}
Expand Down Expand Up @@ -240,7 +213,7 @@ struct Mode *head_find_mode(struct Head *head) {
// maybe a user mode
struct UserMode *um = slist_find_equal_val(cfg->user_modes, head_matches_user_mode, head);
if (um) {
mode = user_mode(head, um);
mode = mode_user_mode(head->modes, head->modes_failed, um);
if (!mode && !um->warned_no_mode) {
um->warned_no_mode = true;
info_user_mode_string(um, buf, sizeof(buf));
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include "cfg.h"
#include "client.h"
Expand Down
25 changes: 25 additions & 0 deletions src/mode.c
Expand Up @@ -99,6 +99,31 @@ struct SList *modes_res_refresh(struct SList *modes) {
return mrrs;
}

struct Mode *mode_user_mode(struct SList *modes, struct SList *modes_failed, struct UserMode *user_mode) {
if (!modes || !user_mode)
return NULL;

struct SList *i, *j;

// highest mode matching the user mode
struct SList *mrrs = modes_res_refresh(modes);
for (i = mrrs; i; i = i->nex) {
struct ModesResRefresh *mrr = i->val;
if (mrr && mrr_satisfies_user_mode(mrr, user_mode)) {
for (j = mrr->modes; j; j = j->nex) {
struct Mode *mode = j->val;
if (!slist_find_equal(modes_failed, NULL, mode)) {
slist_free_vals(&mrrs, mode_res_refresh_free);
return mode;
}
}
}
}
slist_free_vals(&mrrs, mode_res_refresh_free);

return NULL;
}

void mode_free(void *data) {
struct Mode *mode = data;

Expand Down
1 change: 0 additions & 1 deletion src/server.c
@@ -1,7 +1,6 @@
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/signalfd.h>
#include <unistd.h>
Expand Down
33 changes: 33 additions & 0 deletions tst/GNUmakefile
@@ -0,0 +1,33 @@
include GNUmakefile

CPPFLAGS += -Itst

PKGS_TST = cmocka
CFLAGS += $(foreach p,$(PKGS_TST),$(shell pkg-config --cflags $(p)))
CXXFLAGS += $(foreach p,$(PKGS_TST),$(shell pkg-config --cflags $(p)))
LDLIBS += $(foreach p,$(PKGS_TST),$(shell pkg-config --libs $(p)))

TST_H = $(wildcard tst/*.h)
TST_C = $(wildcard tst/*.c)
TST_CXX = $(wildcard tst/*.cpp)
TST_O = $(TST_C:.c=.o) $(TST_CXX:.cpp=.o)
TST_E = $(TST_O:tst/%.o=%)

$(TST_O): $(TST_H) $(SRC_O) config.mk GNUmakefile tst/GNUmakefile

tst-head: tst/tst-head.o $(filter-out src/main.o,$(SRC_O)) $(PRO_O)
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS) -Wl,--wrap=mode_dpi,--wrap=mode_user_mode,--wrap=log_info,--wrap=log_warn

tst-layout: tst/tst-layout.o
$(CXX) -o $(@) $(^) $(LDFLAGS) $(LDLIBS)

tst-all: $(TST_E)
@for e in $(^); do echo; echo $$e; ./$$e; done

tst-clean:
rm -f $(TST_O) $(TST_E)

tst-iwyu: CC = $(IWYU) -Xiwyu --check_also="inc/*h"
tst-iwyu: CXX = $(IWYU) -Xiwyu --check_also="inc/marshalling.h"
tst-iwyu: clean $(TST_O)
IWYU = /usr/bin/include-what-you-use -Xiwyu --no_fwd_decls -Xiwyu --no_comments -Xiwyu --verbose=2
19 changes: 19 additions & 0 deletions tst/asserts.h
@@ -0,0 +1,19 @@
#ifndef ASSERTS_H
#define ASSERTS_H

#include <cmocka.h>

#include "wayland-util.h"

static void _assert_wl_fixed_t_equal_double(wl_fixed_t a, double b,
const char * const file, const int line) {

if (a != wl_fixed_from_double(b)) {
cmocka_print_error("%g != %g\n", wl_fixed_to_double(a), b);
_fail(file, line);
}
}
#define assert_wl_fixed_t_equal_double(a, b) _assert_wl_fixed_t_equal_double(a, b, __FILE__, __LINE__)

#endif // ASSERTS_H

0 comments on commit 52ec8a9

Please sign in to comment.