• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check: a unit test framework for C
3  * Copyright (C) 2001, 2002 Arien Malec
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  */
20 
21 #include "libcompat/libcompat.h"
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "check_list.h"
27 #include "check_error.h"
28 
29 
30 enum
31 {
32   LINIT = 1,
33   LGROW = 2
34 };
35 
36 struct List
37 {
38   unsigned int n_elts;
39   unsigned int max_elts;
40   int current;                  /* pointer to the current node */
41   int last;                     /* pointer to the node before END */
42   void **data;
43 };
44 
45 static void
maybe_grow(List * lp)46 maybe_grow (List * lp)
47 {
48   if (lp->n_elts >= lp->max_elts) {
49     lp->max_elts *= LGROW;
50     lp->data =
51         (void **) erealloc (lp->data, lp->max_elts * sizeof (lp->data[0]));
52   }
53 }
54 
55 List *
check_list_create(void)56 check_list_create (void)
57 {
58   List *lp;
59 
60   lp = (List *) emalloc (sizeof (List));
61   lp->n_elts = 0;
62   lp->max_elts = LINIT;
63   lp->data = (void **) emalloc (sizeof (lp->data[0]) * LINIT);
64   lp->current = lp->last = -1;
65   return lp;
66 }
67 
68 void
check_list_add_front(List * lp,void * val)69 check_list_add_front (List * lp, void *val)
70 {
71   if (lp == NULL)
72     return;
73   maybe_grow (lp);
74   memmove (lp->data + 1, lp->data, lp->n_elts * sizeof lp->data[0]);
75   lp->last++;
76   lp->n_elts++;
77   lp->current = 0;
78   lp->data[lp->current] = val;
79 }
80 
81 void
check_list_add_end(List * lp,void * val)82 check_list_add_end (List * lp, void *val)
83 {
84   if (lp == NULL)
85     return;
86   maybe_grow (lp);
87   lp->last++;
88   lp->n_elts++;
89   lp->current = lp->last;
90   lp->data[lp->current] = val;
91 }
92 
93 int
check_list_at_end(List * lp)94 check_list_at_end (List * lp)
95 {
96   if (lp->current == -1)
97     return 1;
98   else
99     return (lp->current > lp->last);
100 }
101 
102 void
check_list_front(List * lp)103 check_list_front (List * lp)
104 {
105   if (lp->current == -1)
106     return;
107   lp->current = 0;
108 }
109 
110 
111 void
check_list_free(List * lp)112 check_list_free (List * lp)
113 {
114   if (lp == NULL)
115     return;
116 
117   free (lp->data);
118   free (lp);
119 }
120 
121 void *
check_list_val(List * lp)122 check_list_val (List * lp)
123 {
124   if (lp == NULL)
125     return NULL;
126   if (lp->current == -1 || lp->current > lp->last)
127     return NULL;
128 
129   return lp->data[lp->current];
130 }
131 
132 void
check_list_advance(List * lp)133 check_list_advance (List * lp)
134 {
135   if (lp == NULL)
136     return;
137   if (check_list_at_end (lp))
138     return;
139   lp->current++;
140 }
141 
142 
143 void
check_list_apply(List * lp,void (* fp)(void *))144 check_list_apply (List * lp, void (*fp) (void *))
145 {
146   if (lp == NULL || fp == NULL)
147     return;
148 
149   for (check_list_front (lp); !check_list_at_end (lp); check_list_advance (lp))
150     fp (check_list_val (lp));
151 
152 }
153 
154 bool
check_list_contains(List * lp,void * val)155 check_list_contains (List * lp, void *val)
156 {
157   for (check_list_front (lp); !check_list_at_end (lp); check_list_advance (lp)) {
158     if (check_list_val (lp) == val) {
159       return true;
160     }
161   }
162 
163   return false;
164 }
165