1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * check-list.c
4 * Copyright (C) 2011-2012 Akira TAGOH
5 *
6 * Authors:
7 * Akira TAGOH <akira@tagoh.org>
8 *
9 * You may distribute under the terms of either the GNU
10 * Lesser General Public License or the Mozilla Public
11 * License, as specified in the README file.
12 */
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #include <liblangtag/langtag.h>
19 #include "liblangtag/lt-mem.h"
20 #include "main.h"
21
22 /************************************************************/
23 /* common functions */
24 /************************************************************/
25 void
setup(void)26 setup(void)
27 {
28 }
29
30 void
teardown(void)31 teardown(void)
32 {
33 }
34
35 /************************************************************/
36 /* Test cases */
37 /************************************************************/
TDEF(lt_list_new)38 TDEF (lt_list_new) {
39 lt_list_t *l;
40
41 l = lt_list_new();
42 fail_unless(l != NULL, "Allocation failed");
43 lt_list_unref(l);
44 } TEND
45
TDEF(lt_list_unref)46 TDEF (lt_list_unref) {
47 lt_list_t *l;
48
49 l = lt_list_new();
50 lt_mem_add_weak_pointer((lt_mem_t *)l, (lt_pointer_t *)&l);
51 lt_list_unref(l);
52 fail_unless(l == NULL, "Not registered as a weak pointer properly");
53 } TEND
54
TDEF(lt_list_append)55 TDEF (lt_list_append) {
56 lt_list_t *l = lt_list_append(NULL, strdup("foo"), free);
57 lt_list_t *t;
58
59 fail_unless(l != NULL, "Allocation failed");
60 fail_unless(lt_list_value(l) != NULL, "failed to obtain value");
61 fail_unless(strcmp(lt_list_value(l), "foo") == 0, "Failed to compare value");
62 fail_unless(lt_list_next(l) == NULL, "only one list element should be available");
63
64 l = lt_list_append(l, strdup("bar"), free);
65 fail_unless(l != NULL, "Allocation failed");
66 fail_unless(lt_list_next(l) != NULL, "Allocation failed");
67 fail_unless(lt_list_value(lt_list_next(l)) != NULL, "failed to obtain value");
68 fail_unless(strcmp(lt_list_value(lt_list_next(l)), "bar") == 0, "Failed to compare value");
69 fail_unless(lt_list_next(lt_list_next(l)) == NULL, "only one list element should be available");
70
71 t = lt_list_next(l);
72 lt_list_unref(l);
73 fail_unless(lt_list_previous(t) == NULL, "Failed to update the link");
74 fail_unless(lt_list_next(t) == NULL, "Failed to update the link");
75 fail_unless(lt_list_value(t) != NULL, "Something goes wrong");
76 fail_unless(strcmp(lt_list_value(t), "bar") == 0, "Failed to compare value");
77 lt_list_free(t);
78 } TEND
79
80 /************************************************************/
81 Suite *
tester_suite(void)82 tester_suite(void)
83 {
84 Suite *s = suite_create("lt_list_t");
85 TCase *tc = tcase_create("Basic functionality");
86
87 tcase_add_checked_fixture(tc, setup, teardown);
88
89 T (lt_list_new);
90 T (lt_list_unref);
91 T (lt_list_append);
92
93 suite_add_tcase(s, tc);
94
95 return s;
96 }
97