Skip to content

Commit

Permalink
#164 tidy asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Dec 23, 2024
1 parent d215303 commit 4580191
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 167 deletions.
72 changes: 34 additions & 38 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,43 +341,6 @@ void report_adaptive_sync_fail(struct Head *head) {
log_info(" - '%s'", head->model ? head->model : "monitor description");
}

void handle_success(void) {
switch(displ->delta.element) {
case MODE:
// successful mode change is not always reported
displ->delta.head->current.mode = displ->delta.head->desired.mode;
break;

case VRR_OFF:
// sway reports adaptive sync failure as success
if (head_current_adaptive_sync_not_desired(displ->delta.head)) {
report_adaptive_sync_fail(displ->delta.head);
displ->delta.head->adaptive_sync_failed = true;

displ_delta_destroy();
return;
}
break;

default:
break;
}

if (cfg->change_success_cmd) {
log_info("\nExecuting CHANGE_SUCCESS_CMD:");
log_info(" %s", cfg->change_success_cmd);

const struct STable *env = stable_init(1, 1, false);
stable_put(env, "WD_CHANGE_SUCCESS_MSG", displ->delta.human);
spawn_sh_cmd(cfg->change_success_cmd, env);
stable_free(env);
}

displ_delta_destroy();

log_info("\nChanges successful");
}

void handle_failure(void) {
switch(displ->delta.element) {
case MODE:
Expand All @@ -391,7 +354,6 @@ void handle_failure(void) {
// current mode may be misreported
displ->delta.head->current.mode = NULL;

displ->delta.head = NULL;
break;

case VRR_OFF:
Expand All @@ -414,6 +376,40 @@ void handle_failure(void) {
displ_delta_destroy();
}

void handle_success(void) {
switch(displ->delta.element) {
case MODE:
// successful mode change is not always reported
displ->delta.head->current.mode = displ->delta.head->desired.mode;
break;

case VRR_OFF:
// sway reports adaptive sync failure as success
if (head_current_adaptive_sync_not_desired(displ->delta.head)) {
handle_failure();
return;
}
break;

default:
break;
}

if (cfg->change_success_cmd) {
log_info("\nExecuting CHANGE_SUCCESS_CMD:");
log_info(" %s", cfg->change_success_cmd);

const struct STable *env = stable_init(1, 1, false);
stable_put(env, "WD_CHANGE_SUCCESS_MSG", displ->delta.human);
spawn_sh_cmd(cfg->change_success_cmd, env);
stable_free(env);
}

displ_delta_destroy();

log_info("\nChanges successful");
}

void layout(void) {

print_heads(INFO, ARRIVED, heads_arrived);
Expand Down
42 changes: 38 additions & 4 deletions tst/asserts.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cmocka.h>
#include <stdio.h>
#include <string.h>
#include <wayland-util.h>

#include "cfg.h"
Expand All @@ -12,10 +13,43 @@
#include "stable.h"
#include "util.h"

#define assert_string_equal_nn(a, b) \
assert_non_null(a); \
assert_non_null(b); \
_assert_string_equal((a), (b), __FILE__, __LINE__)
void _assert_nul(const void *a, const char * const ae, const char * const file, const int line) {
if (a) {
cm_print_error("%s is not NULL\n", ae);
_fail(file, line);
}
}
#define assert_nul(a) _assert_nul(a, #a, __FILE__, __LINE__)

void _assert_non_nul(const void *a, const char * const ae, const char * const file, const int line) {
if (!a) {
cm_print_error("%s is NULL\n", ae);
_fail(file, line);
}
}
#define assert_non_nul(a) _assert_non_nul(a, #a, __FILE__, __LINE__)

void _assert_str_equal(const char * const a, const char * const ae, const char * const b, const char * const be, const char * const file, const int line) {
if (!a && !b)
return;
_assert_non_nul(a, ae, file, line);
_assert_non_nul(b, be, file, line);
_assert_string_equal(a, b, file, line);
}
#define assert_str_equal(a, b) _assert_str_equal(a, #a, b, #b, __FILE__, __LINE__)

void _assert_str_equal_n(const char * const a, const char * const ae, const char * const b, const char * const be, const size_t n, const char * const file, const int line) {
if (!a && !b)
return;
_assert_non_nul(a, ae, file, line);
_assert_non_nul(b, be, file, line);
if (strncmp(a, b, n) != 0) {
cm_print_error("\"%.*s\" != \"%.*s\"\n", (int)n, a, (int)n, b);
_fail(file, line);
}
}

#define assert_str_equal_n(a, b, n) _assert_str_equal_n(a, #a, b, #b, n, __FILE__, __LINE__)

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

Expand Down
74 changes: 37 additions & 37 deletions tst/tst-cfg-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ void cfg_file_write__none(void **state) {

assert_log(INFO, "\nWrote configuration file: /path/to/zero\n");

assert_string_equal_nn(cfg->file_path, "/path/to/zero");
assert_string_equal_nn(cfg->dir_path, "/path/to");
assert_string_equal_nn(cfg->file_name, "zero");
assert_string_equal_nn(cfg->resolved_from, "/path/to/zero");
assert_str_equal(cfg->file_path, "/path/to/zero");
assert_str_equal(cfg->dir_path, "/path/to");
assert_str_equal(cfg->file_name, "zero");
assert_str_equal(cfg->resolved_from, "/path/to/zero");
assert_ptr_equal(cfg->resolved_from, slist_at(cfg_file_paths, 0));
assert_int_equal(cfg->updated, false);
}
Expand Down Expand Up @@ -206,10 +206,10 @@ void cfg_file_write__cannot_write_use_alternative(void **state) {

assert_log(INFO, "\nWrote configuration file: /path/to/three\n");

assert_string_equal_nn(cfg->file_path, "/path/to/three");
assert_string_equal_nn(cfg->dir_path, "/path/to");
assert_string_equal_nn(cfg->file_name, "three");
assert_string_equal_nn(cfg->resolved_from, "/path/to/three");
assert_str_equal(cfg->file_path, "/path/to/three");
assert_str_equal(cfg->dir_path, "/path/to");
assert_str_equal(cfg->file_name, "three");
assert_str_equal(cfg->resolved_from, "/path/to/three");
assert_ptr_equal(cfg->resolved_from, slist_at(cfg_file_paths, 3));
assert_int_equal(cfg->updated, false);

Expand Down Expand Up @@ -246,10 +246,10 @@ void cfg_file_write__cannot_write_no_alternative(void **state) {

cfg_file_write();

assert_null(cfg->file_path);
assert_null(cfg->dir_path);
assert_null(cfg->file_name);
assert_null(cfg->resolved_from);
assert_nul(cfg->file_path);
assert_nul(cfg->dir_path);
assert_nul(cfg->file_name);
assert_nul(cfg->resolved_from);
assert_int_equal(cfg->updated, false);

free(expected);
Expand All @@ -259,7 +259,7 @@ void cfg_file_write__existing(void **state) {
cfg->file_path = strdup("tst/tmp/write-existing-cfg.yaml");

FILE *f = fopen(cfg->file_path, "w");
assert_non_null(f);
assert_non_nul(f);
fclose(f);

char *expected = strdup("XXXX");
Expand All @@ -286,9 +286,9 @@ void cfg_file_paths_init__min(void **state) {

cfg_file_paths_init("inexistent");

assert_string_equal_nn(slist_at(cfg_file_paths, 0), "/usr/local/etc/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 0), "/usr/local/etc/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 1), ROOT_ETC"/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 1), ROOT_ETC"/way-displays/cfg.yaml");

assert_int_equal(slist_length(cfg_file_paths), 2);
}
Expand All @@ -299,11 +299,11 @@ void cfg_file_paths_init__xch(void **state) {

cfg_file_paths_init(NULL);

assert_string_equal_nn(slist_at(cfg_file_paths, 0), "xch/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 0), "xch/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 1), "/usr/local/etc/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 1), "/usr/local/etc/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 2), ROOT_ETC"/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 2), ROOT_ETC"/way-displays/cfg.yaml");

assert_int_equal(slist_length(cfg_file_paths), 3);
}
Expand All @@ -314,11 +314,11 @@ void cfg_file_paths_init__home(void **state) {

cfg_file_paths_init(NULL);

assert_string_equal_nn(slist_at(cfg_file_paths, 0), "hom/.config/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 0), "hom/.config/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 1), "/usr/local/etc/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 1), "/usr/local/etc/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 2), ROOT_ETC"/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 2), ROOT_ETC"/way-displays/cfg.yaml");

assert_int_equal(slist_length(cfg_file_paths), 3);
}
Expand All @@ -329,13 +329,13 @@ void cfg_file_paths_init__user(void **state) {

cfg_file_paths_init(".");

assert_string_equal_nn(slist_at(cfg_file_paths, 0), ".");
assert_str_equal(slist_at(cfg_file_paths, 0), ".");

assert_string_equal_nn(slist_at(cfg_file_paths, 1), "xch/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 1), "xch/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 2), "/usr/local/etc/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 2), "/usr/local/etc/way-displays/cfg.yaml");

assert_string_equal_nn(slist_at(cfg_file_paths, 3), ROOT_ETC"/way-displays/cfg.yaml");
assert_str_equal(slist_at(cfg_file_paths, 3), ROOT_ETC"/way-displays/cfg.yaml");

assert_int_equal(slist_length(cfg_file_paths), 4);
}
Expand All @@ -352,10 +352,10 @@ void resolve_cfg_file__not_found(void **state) {

assert_false(resolve_cfg_file(cfg));

assert_null(cfg->file_path);
assert_null(cfg->dir_path);
assert_null(cfg->file_name);
assert_null(cfg->resolved_from);
assert_nul(cfg->file_path);
assert_nul(cfg->dir_path);
assert_nul(cfg->file_name);
assert_nul(cfg->resolved_from);
}

void resolve_cfg_file__direct(void **state) {
Expand All @@ -374,10 +374,10 @@ void resolve_cfg_file__direct(void **state) {

assert_true(resolve_cfg_file(cfg));

assert_string_equal_nn(cfg->file_path, file_path);
assert_string_equal_nn(cfg->dir_path, dir_path);
assert_string_equal_nn(cfg->file_name, "resolved.yaml");
assert_string_equal_nn(cfg->resolved_from, file_path);
assert_str_equal(cfg->file_path, file_path);
assert_str_equal(cfg->dir_path, dir_path);
assert_str_equal(cfg->file_name, "resolved.yaml");
assert_str_equal(cfg->resolved_from, file_path);
assert_ptr_equal(cfg->resolved_from, slist_at(cfg_file_paths, 0));
}

Expand All @@ -402,10 +402,10 @@ void resolve_cfg_file__linked(void **state) {

assert_true(resolve_cfg_file(cfg));

assert_string_equal_nn(cfg->file_path, file_path);
assert_string_equal_nn(cfg->dir_path, dir_path);
assert_string_equal_nn(cfg->file_name, "resolved.yaml");
assert_string_equal_nn(cfg->resolved_from, linked_path);
assert_str_equal(cfg->file_path, file_path);
assert_str_equal(cfg->dir_path, dir_path);
assert_str_equal(cfg->file_name, "resolved.yaml");
assert_str_equal(cfg->resolved_from, linked_path);
assert_ptr_equal(cfg->resolved_from, slist_at(cfg_file_paths, 0));
}

Expand Down
Loading

0 comments on commit 4580191

Please sign in to comment.