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
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-07-16 17:39:16 +00:00
|
|
|
*/
|
|
|
|
|
2009-12-18 21:28:35 +00:00
|
|
|
#include "config.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
|
|
|
|
{
|
2009-07-16 17:39:16 +00:00
|
|
|
int n_elts;
|
|
|
|
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 */
|
2009-07-16 17:39:16 +00:00
|
|
|
const void **data;
|
|
|
|
};
|
|
|
|
|
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;
|
2009-10-12 11:49:35 +00:00
|
|
|
lp->data = 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;
|
2009-10-12 11:49:35 +00:00
|
|
|
lp = emalloc (sizeof (List));
|
2009-07-16 17:39:16 +00:00
|
|
|
lp->n_elts = 0;
|
|
|
|
lp->max_elts = LINIT;
|
2009-10-12 11:49:35 +00:00
|
|
|
lp->data = 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
|
|
|
|
list_add_front (List * lp, const 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
|
|
|
|
list_add_end (List * lp, const 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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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 *
|
|
|
|
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
|
|
|
|
|
|
|
return (void *) lp->data[lp->current];
|
2009-07-16 17:39:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
list_advance (List * lp)
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL)
|
|
|
|
return;
|
2009-10-12 11:49:35 +00:00
|
|
|
if (list_at_end (lp))
|
2009-07-16 17:39:16 +00:00
|
|
|
return;
|
|
|
|
lp->current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
void
|
|
|
|
list_apply (List * lp, void (*fp) (void *))
|
2009-07-16 17:39:16 +00:00
|
|
|
{
|
|
|
|
if (lp == NULL || fp == NULL)
|
|
|
|
return;
|
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
for (list_front (lp); !list_at_end (lp); list_advance (lp))
|
|
|
|
fp (list_val (lp));
|
2009-07-16 17:39:16 +00:00
|
|
|
|
2009-10-12 11:49:35 +00:00
|
|
|
}
|