aboutsummaryrefslogtreecommitdiffstats
path: root/unit/ck-test.c
blob: 36b23d854b55ff14eeffbd29490b8d5b7636cb41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "cklist.h"
#include "ckutil.h"

/* total number of unit tests */
#define TOTAL 2

#define CK_UNIT_TESTS                                         \
  X(ck_list_test, "Basic cklist test")                        \
  X(ck_str_utils_test, "Test string utility functions")       \

void ck_list_test() {
  /* init and add */
  cklist *ckl = list_make_new();
  cklist *ckl2 = list_make_and_add("first");
  list_add(ckl2, "second");
  list_add(ckl, "first");
  list_add(ckl, "second");
  assert(strcmp(list_get_at(ckl, 0),
		list_get_at(ckl2, 0)) == 0);
  assert(strcmp(list_get_at(ckl, 1),
		list_get_at(ckl2, 1)) == 0);
  list_free(ckl2);

  /* exists */
  assert(list_exists(ckl, "first"));
  assert(list_exists(ckl, "second"));
  assert(!list_exists(ckl, "thrid"));
  list_free(ckl);
}

void ck_str_utils_test() {
  /* make_ck_config_name */
  char ck_conf_path[STR_M];
  str_make_ck_config_name(ck_conf_path, "/test/path/.config", "emacs");
  assert(strcmp(ck_conf_path, "emacs/.config") == 0);

  /* str_is_empty */
  assert(str_is_empty(""));
  assert(str_is_empty("    "));
  assert(str_is_empty("\t"));

  /* swap ~ and home */
  char str[STR_M] = "";
  char str2[STR_M] = "";
  /* getenv("HOME") -> /home/user */
  char home[STR_S] = "/home/user";
  char testpath[STR_S] = "/home/user/file";
  assert(swap_home_with_tilde(str, testpath, home));
  assert(strcmp(str, "~/file") == 0);
  assert(swap_tilde_with_home(str2, str, home));
  assert(strcmp(str2, testpath) == 0);
  assert(swap_home_with_tilde(str, "/not/starting/with/home", home) == NULL);
  assert(swap_tilde_with_home(str2, "/not/starting/with/tilde", home) == NULL);
}

int main() {
  int count = 1;
#define X(TEST, DESC)				\
  printf("[%d/%d] %s\n", count++, TOTAL, DESC); \
  TEST();					\
  printf("=> Passed\n\n");
  CK_UNIT_TESTS
#undef X
}