2009-07-16 17:39:16 +00:00
|
|
|
/*
|
|
|
|
* Check: a unit test framework for C
|
|
|
|
* Copyright (C) 2001, 2002 Arien Malec
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
2016-12-09 09:48:11 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2009-07-16 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-09 09:48:11 +00:00
|
|
|
#include "libcompat/libcompat.h"
|
2009-07-16 17:39:16 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "check_list.h"
|
|
|
|
#include "check_error.h"
|
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
enum
|
|
|
|
{
|
2009-07-16 17:39:16 +00:00
|
|
|
LINIT = 1,
|
|
|
|
LGROW = 2
|
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
struct List
|
|
|
|
{
|
2014-11-15 11:53:32 +00:00
|
|
|
unsigned int n_elts;
|
|
|
|
unsigned int max_elts;
|
2009-10-12 11:49:35 +00:00
|
|
|
int current; /* pointer to the current node */
|
|
|
|
int last; /* pointer to the node before END */
|
2014-11-15 11:53:32 +00:00
|
|
|
void **data;
|
2009-07-16 17:39:16 +00:00
|
|
|
};
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
static void
|
|
|
|
maybe_grow (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp->n_elts >= lp->max_elts) {
|
|
|
|
lp->max_elts *= LGROW;
|
2014-11-15 11:53:32 +00:00
|
|
|
lp->data =
|
|
|
|
(void **) erealloc (lp->data, lp->max_elts * sizeof (lp->data[0]));
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
List *
|
|
|
|
check_list_create (void)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
List *lp;
|
2014-11-15 11:53:32 +00:00
|
|
|
|
|
|
|
lp = (List *) emalloc (sizeof (List));
|
2009-07-16 17:39:16 +00:00
|
|
|
lp->n_elts = 0;
|
|
|
|
lp->max_elts = LINIT;
|
2014-11-15 11:53:32 +00:00
|
|
|
lp->data = (void **) emalloc (sizeof (lp->data[0]) * LINIT);
|
2009-07-16 17:39:16 +00:00
|
|
|
lp->current = lp->last = -1;
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_add_front (List * lp, void *val)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2009-10-12 11:49:35 +00:00
|
|
|
maybe_grow (lp);
|
|
|
|
memmove (lp->data + 1, lp->data, lp->n_elts * sizeof lp->data[0]);
|
2009-07-16 17:39:16 +00:00
|
|
|
lp->last++;
|
|
|
|
lp->n_elts++;
|
|
|
|
lp->current = 0;
|
|
|
|
lp->data[lp->current] = val;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_add_end (List * lp, void *val)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2009-10-12 11:49:35 +00:00
|
|
|
maybe_grow (lp);
|
2009-07-16 17:39:16 +00:00
|
|
|
lp->last++;
|
|
|
|
lp->n_elts++;
|
|
|
|
lp->current = lp->last;
|
|
|
|
lp->data[lp->current] = val;
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
int
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_at_end (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp->current == -1)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return (lp->current > lp->last);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_front (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp->current == -1)
|
|
|
|
return;
|
|
|
|
lp->current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_free (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
|
|
|
free (lp->data);
|
2009-07-16 17:39:16 +00:00
|
|
|
free (lp);
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void *
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_val (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (lp->current == -1 || lp->current > lp->last)
|
|
|
|
return NULL;
|
2009-10-12 11:49:35 +00:00
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
return lp->data[lp->current];
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_advance (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2014-11-15 11:53:32 +00:00
|
|
|
if (check_list_at_end (lp))
|
2009-07-16 17:39:16 +00:00
|
|
|
return;
|
|
|
|
lp->current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
2014-11-15 11:53:32 +00:00
|
|
|
check_list_apply (List * lp, void (*fp) (void *))
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL || fp == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-11-15 11:53:32 +00:00
|
|
|
for (check_list_front (lp); !check_list_at_end (lp); check_list_advance (lp))
|
|
|
|
fp (check_list_val (lp));
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
}
|
2016-12-09 09:48:11 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
check_list_contains (List * lp, void *val)
|
|
|
|
{
|
|
|
|
for (check_list_front (lp); !check_list_at_end (lp); check_list_advance (lp)) {
|
|
|
|
if (check_list_val (lp) == val) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|