Compare commits

...

3 Commits

Author SHA1 Message Date
3eee3ea684 add tests.c 2023-09-09 14:14:53 +02:00
f885f08dcc add targets for building tests 2023-09-09 14:13:51 +02:00
8c49238e95 implement moving clients directionally 2023-09-09 14:11:27 +02:00
9 changed files with 193 additions and 6 deletions

View File

@ -13,6 +13,12 @@ OBJS=$(patsubst %,$(OBJDIR)/%.o, $(basename $(notdir $(SRC))))
$(shell mkdir -p $(dir $(OBJS)) > /dev/null)
test: cmocka $(OBJS)
$(CC) $(FLAGS) $(LIBS) -lcmocka $(OBJS) -o test
cmocka:
$(eval OBJS=$(filter-out obj/main.o, $(OBJS)))
$(EXEC_NAME): $(OBJS)
$(CC) $(FLAGS) $(LIBS) $^ -o $(EXEC_NAME)

View File

@ -201,6 +201,24 @@ void wm_client_focus_dir(Wm *wm, Client *c, int dir)
}
}
void wm_client_swap_dir(Wm* wm, Client *c, int dir)
{
RETURN_IF_NULL(c);
TreeNode *client_node = wm_treenode_ptr_find_client_node(c->ws->tree, c);
assert(client_node);
Client *client = wm_client_get_dir_rel_c(c, dir);
if (!client)
return;
TreeNode *client_node1 = wm_treenode_ptr_find_client_node(c->ws->tree, client);
wm_treenode_swap(c->ws->tree, client_node, client_node1);
wm_layout(wm, wm->smon);
}
void wm_client_free(Wm *wm, Client *c)
{
DEBUG_PRINT("%s\n", __func__);
@ -304,7 +322,7 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
if (!c)
return NULL;
Client *r = NULL;
Client *ret = NULL;
int x, y, dx, dy;
switch (dir) {
@ -351,15 +369,18 @@ Client* wm_client_get_dir_rel_c(Client *c, int dir)
DEBUG_PRINT("%s ", __func__)
DEBUG_PRINT("found window %d in direction ", (int)c->window)
DEBUG_PRINT("%d\n", dir)
return r;
ret = r;
goto ret;
}
}
// TODO: bar
} while((x > 0 && x < c->m->info.width) &&
(y > 0 && y < c->m->info.height));
DEBUG_PRINT("%s returning %p\n", __func__, r);
return r;
ret:
wm_ptrarray_free(&client_nodes);
DEBUG_PRINT("%s returning %p\n", __func__, ret);
return ret;
}
Client* wm_client_get_focused(Wm *wm)

View File

@ -55,6 +55,7 @@ void wm_client_hide(Wm *wm, Client *c);
void wm_client_show(Wm* wm, Client *c);
void wm_client_focus(Wm* wm, Client *c);
void wm_client_focus_dir(Wm* wm, Client *c, int dir);
void wm_client_swap_dir(Wm* wm, Client *c, int dir);
void wm_client_free(Wm *wm, Client *c);
void wm_client_kill(Wm *wm, Client *c);

View File

@ -24,7 +24,7 @@ void wm_cfg_init_def(Config *config)
void wm_keybinds_init_def(Config *config)
{
char *st[] = {"alacritty", NULL};
char *st[] = {"st", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
@ -85,6 +85,18 @@ void wm_keybinds_init_def(Config *config)
(Keybind) {Mod4Mask, XK_l, *wm_kb_focus_dir,
(Arg) {.i = RIGHT}},
(Keybind) {Mod4Mask | ShiftMask, XK_h, *wm_kb_move_dir,
(Arg) {.i = LEFT}},
(Keybind) {Mod4Mask | ShiftMask, XK_j, *wm_kb_move_dir,
(Arg) {.i = DOWN}},
(Keybind) {Mod4Mask | ShiftMask, XK_k, *wm_kb_move_dir,
(Arg) {.i = UP}},
(Keybind) {Mod4Mask | ShiftMask, XK_l, *wm_kb_move_dir,
(Arg) {.i = RIGHT}},
(Keybind) {Mod4Mask, XK_b, *wm_kb_switch_split_mode,
(Arg) {.i = SPLIT_VERTICAL}},
@ -159,7 +171,7 @@ void wm_configfile_read(ConfigFile *cfile, Config *config)
if (!fp) {
fprintf(stderr, "wm: could not open config file: %s\n", strerror(errno));
exit(1);
return;
}
const int max_line_len = 8192;

121
src/tests.c Normal file
View File

@ -0,0 +1,121 @@
#include "util.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
static void test_wm_treenode_new(void **state)
{
TreeNode *node = wm_treenode_new(NODE_VERTICAL, NULL);
assert_null(node->client);
assert_non_null(node->children.nodes);
assert_int_equal(node->children.size, 0);
assert_int_equal(node->type, NODE_VERTICAL);
wm_treenode_free(node);
}
static void test_wm_nodearray_push(void **state)
{
TreeNode *node = wm_treenode_new(NODE_VERTICAL, NULL);
TreeNode *node1 = wm_treenode_new(NODE_VERTICAL, node);
node1->id = 123;
assert_null(node->parent);
assert_ptr_equal(node1->parent, node);
wm_nodearray_push(&node->children, node1);
assert_int_equal(node->children.size, 1);
assert_ptr_equal(node->children.nodes[0], node1);
assert_int_equal(node1->id, 123);
assert_int_equal(node->children.nodes[0]->id, 123);
wm_treenode_free(node);
wm_treenode_free(node1);
}
static void test_wm_find_client_nodes_ptr(void **state)
{
TreeNode *root = wm_treenode_new(NODE_VERTICAL, NULL);
TreeNode *node1 = wm_treenode_new(NODE_CLIENT, NULL);
TreeNode *node2 = wm_treenode_new(NODE_CLIENT, NULL);
node1->client = (Client*)0x123;
node2->client = (Client*)0x456;
const unsigned long ids[] = {123, 456};
node1->id = ids[0];
node2->id = ids[1];
wm_nodearray_push(&root->children, node1);
wm_nodearray_push(&root->children, node2);
assert_int_equal(root->children.size, 2);
assert_ptr_equal(root->children.nodes[0], node1);
assert_ptr_equal(root->children.nodes[1], node2);
PtrArray client_ptrs = wm_treenode_find_client_nodes_ptr(root);
assert_int_equal(client_ptrs.size, 2);
assert_in_set(((TreeNode*)client_ptrs.ptrs[0])->id, ids, 2);
assert_in_set(((TreeNode*)client_ptrs.ptrs[1])->id, ids, 2);
wm_treenode_free(root);
wm_treenode_free(node1);
wm_treenode_free(node2);
wm_ptrarray_free(&client_ptrs);
}
static void test_wm_all_nodes_to_ptrarray(void **state)
{
TreeNode *root = wm_treenode_new(NODE_VERTICAL, NULL);
TreeNode *node1 = wm_treenode_new(NODE_CLIENT, NULL);
TreeNode *node2 = wm_treenode_new(NODE_CLIENT, NULL);
TreeNode *node3 = wm_treenode_new(NODE_CLIENT, NULL);
const unsigned long ids[] = {1, 2, 3, 4};
root->id = ids[0];
node1->id = ids[1];
node2->id = ids[2];
node3->id = ids[3];
wm_nodearray_push(&root->children, node1);
wm_nodearray_push(&root->children, node2);
wm_nodearray_push(&root->children, node3);
assert_int_equal(root->children.size, 3);
assert_ptr_equal(root->children.nodes[0], node1);
assert_ptr_equal(root->children.nodes[1], node2);
assert_ptr_equal(root->children.nodes[2], node3);
PtrArray all_nodes = wm_all_nodes_to_ptrarray(root);
assert_int_equal(all_nodes.size, 4);
assert_in_set(((TreeNode*)all_nodes.ptrs[0])->id, ids, 4);
assert_in_set(((TreeNode*)all_nodes.ptrs[1])->id, ids, 4);
assert_in_set(((TreeNode*)all_nodes.ptrs[2])->id, ids, 4);
assert_in_set(((TreeNode*)all_nodes.ptrs[3])->id, ids, 4);
wm_treenode_free(root);
wm_treenode_free(node1);
wm_treenode_free(node2);
wm_treenode_free(node3);
wm_ptrarray_free(&all_nodes);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_wm_treenode_new),
cmocka_unit_test(test_wm_nodearray_push),
cmocka_unit_test(test_wm_find_client_nodes_ptr),
cmocka_unit_test(test_wm_all_nodes_to_ptrarray),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

View File

@ -83,6 +83,20 @@ void wm_treenode_recalculate_space(TreeNode *node)
&node->children.nodes[1]->pos);
}
void wm_treenode_swap(TreeNode *root, TreeNode *node1, TreeNode* node2)
{
assert(root);
assert(node1);
assert(node2);
assert(node1->type == NODE_CLIENT);
assert(node2->type == NODE_CLIENT);
Client *tmp = node1->client;
node1->client = node2->client;
node2->client = tmp;
}
void wm_node_type_to_str(NodeType type, char *buf, size_t bufsize)
{
switch (type) {

View File

@ -67,6 +67,7 @@ void wm_treenode_free(TreeNode *node);
bool wm_treenode_is_empty(TreeNode *node);
void wm_treenode_split_space(TreeNode *node, Rect *ret1, Rect *ret2);
void wm_treenode_recalculate_space(TreeNode *node);
void wm_treenode_swap(TreeNode *root, TreeNode *node1, TreeNode* node2);
TreeNode* wm_treenode_remove_client(Wm *wm, TreeNode *root, Client *client);
void wm_treenode_remove_node(TreeNode *root, unsigned int node_id);
int wm_get_node_index(TreeNode *parent, unsigned int node_id);

View File

@ -664,6 +664,16 @@ void wm_kb_focus_dir(Wm *wm, Arg *args)
wm_client_focus_dir(wm, c, args->i);
}
void wm_kb_move_dir(Wm *wm, Arg *args)
{
Client *c;
RETURN_IF_NULL(args)
c = wm_client_get_focused(wm);
wm_client_swap_dir(wm, c, args->i);
}
void wm_kb_switch_split_mode(Wm *wm, Arg *args)
{
RETURN_IF_NULL(args)

View File

@ -147,6 +147,7 @@ void wm_kb_spawn(Wm *wm, Arg *args);
void wm_kb_kill(Wm *wm, Arg *args);
void wm_kb_switch_ws(Wm *wm, Arg *args);
void wm_kb_focus_dir(Wm *wm, Arg *args);
void wm_kb_move_dir(Wm *wm, Arg *args);
void wm_kb_exit(Wm* wm, Arg *args);
void wm_kb_switch_split_mode(Wm *wm, Arg *args);