/* * test_items.c -- the tests of items.c (make check) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include "items.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static Item Make(const char *label, const char *command) { Item it; memset(&it, 0, sizeof it); snprintf(it.label, sizeof it.label, "%s", label); snprintf(it.command, sizeof it.command, "%s", command); return it; } int main(void) { char dir[] = "/tmp/test_itemsXXXXXX", file[64]; Item items[ITEMS_MAX], a, b, c; FILE *fp; int n; if (mkdtemp(dir) == NULL) return 1; snprintf(file, sizeof file, "%s/favourites", dir); CHECK(ItemsRead(file, items, ITEMS_MAX) == 0); /* no file */ fp = fopen(file, "w"); fputs("# my programs\n\nTerminal\tdtterm\n Files \t dtfile -dir ~ \n" "xclock\n\tno label\nno command\t\n", fp); fclose(fp); n = ItemsRead(file, items, ITEMS_MAX); CHECK(n == 3); CHECK(strcmp(items[0].label, "Terminal") == 0 && strcmp(items[0].command, "dtterm") == 0); CHECK(strcmp(items[1].label, "Files") == 0 && strcmp(items[1].command, "dtfile -dir ~") == 0); CHECK(strcmp(items[2].label, "xclock") == 0 && strcmp(items[2].command, "xclock") == 0); CHECK(ItemsRead(file, items, 2) == 2); /* the recent list: newest first, no doubles, cut at max */ a = Make("A", "a"); b = Make("B", "b"); c = Make("C", "c"); n = 0; n = ItemsPush(items, n, 2, &a); n = ItemsPush(items, n, 2, &b); CHECK(n == 2 && strcmp(items[0].command, "b") == 0 && strcmp(items[1].command, "a") == 0); n = ItemsPush(items, n, 2, &a); /* moves to the front */ CHECK(n == 2 && strcmp(items[0].command, "a") == 0 && strcmp(items[1].command, "b") == 0); n = ItemsPush(items, n, 2, &c); /* b falls off */ CHECK(n == 2 && strcmp(items[0].command, "c") == 0 && strcmp(items[1].command, "a") == 0); CHECK(ItemsWrite(file, items, n) == 0); n = ItemsRead(file, items, ITEMS_MAX); CHECK(n == 2 && strcmp(items[0].label, "C") == 0 && strcmp(items[1].command, "a") == 0); unlink(file); rmdir(dir); if (failures == 0) printf("PASS test_items\n"); return failures != 0; }