Added stack, queue ADTs and related tests.
[platform/upstream/glib.git] / tests / queue-test.c
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4 #include <glib.h>
5
6 int main()
7 {
8   GQueue *q;
9
10   q = g_queue_new ();
11
12   g_assert (g_queue_empty (q) == TRUE);
13
14   g_queue_push (q, GINT_TO_POINTER (1));
15   g_assert (g_list_length (q->list) == 1);
16   g_queue_push (q, GINT_TO_POINTER (2));
17   g_assert (g_list_length (q->list) == 2);
18   g_queue_push (q, GINT_TO_POINTER (3));
19   g_assert (g_list_length (q->list) == 3);
20   g_queue_push (q, GINT_TO_POINTER (4));
21   g_assert (g_list_length (q->list) == 4);
22   g_queue_push (q, GINT_TO_POINTER (5));
23   g_assert (g_list_length (q->list) == 5);
24
25   g_assert (g_queue_empty (q) == FALSE);
26
27   g_assert (g_queue_index (q, GINT_TO_POINTER (2)) == 1);
28   g_assert (g_queue_index (q, GINT_TO_POINTER (142)) == -1);
29
30   g_assert (g_queue_peek (q) == GINT_TO_POINTER (1));
31   g_assert (g_queue_peek_front (q) == GINT_TO_POINTER (1));
32   g_assert (g_queue_peek_back (q) == GINT_TO_POINTER (5));
33
34   g_assert (g_queue_pop (q) == GINT_TO_POINTER (1));
35   g_assert (g_list_length (q->list) == 4);
36   g_assert (g_queue_pop (q) == GINT_TO_POINTER (2));
37   g_assert (g_list_length (q->list) == 3);
38   g_assert (g_queue_pop (q) == GINT_TO_POINTER (3));
39   g_assert (g_list_length (q->list) == 2);
40   g_assert (g_queue_pop (q) == GINT_TO_POINTER (4));
41   g_assert (g_list_length (q->list) == 1);
42   g_assert (g_queue_pop (q) == GINT_TO_POINTER (5));
43   g_assert (g_list_length (q->list) == 0);
44   g_assert (g_queue_pop (q) == NULL);
45   g_assert (g_list_length (q->list) == 0);
46   g_assert (g_queue_pop (q) == NULL);
47   g_assert (g_list_length (q->list) == 0);
48
49   g_assert (g_queue_empty (q) == TRUE);
50
51   /************************/
52
53   g_queue_push_front (q, GINT_TO_POINTER (1));
54   g_assert (g_list_length (q->list) == 1);
55   g_queue_push_front (q, GINT_TO_POINTER (2));
56   g_assert (g_list_length (q->list) == 2);
57   g_queue_push_front (q, GINT_TO_POINTER (3));
58   g_assert (g_list_length (q->list) == 3);
59   g_queue_push_front (q, GINT_TO_POINTER (4));
60   g_assert (g_list_length (q->list) == 4);
61   g_queue_push_front (q, GINT_TO_POINTER (5));
62   g_assert (g_list_length (q->list) == 5);
63
64   g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (5));
65   g_assert (g_list_length (q->list) == 4);
66   g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (4));
67   g_assert (g_list_length (q->list) == 3);
68   g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (3));
69   g_assert (g_list_length (q->list) == 2);
70   g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (2));
71   g_assert (g_list_length (q->list) == 1);
72   g_assert (g_queue_pop_front (q) == GINT_TO_POINTER (1));
73   g_assert (g_list_length (q->list) == 0);
74   g_assert (g_queue_pop_front (q) == NULL);
75   g_assert (g_list_length (q->list) == 0);
76   g_assert (g_queue_pop_front (q) == NULL);
77   g_assert (g_list_length (q->list) == 0);
78
79   g_queue_free (q);
80
81   return 0;
82 }
83