You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB
C

/*
* 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.
*
* 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 Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* (c) Copyright 2014 Alexander Vdolainen <avdolainen@zoho.com>
*
* Index allocator tests
*
*/
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <tdata/bitwise.h>
#include <tdata/idx_allocator.h>
#define IDXMAX 131072
int main(int argc, char **argv)
{
idx_allocator_t idxa;
ulong_t r = 0, i = 0;
memset(&idxa, 0, sizeof(idxa));
if((r = idx_allocator_init(&idxa, IDXMAX, 0))) {
fprintf(stderr, "%d:(%s): IDX init failed with %lu\n", __LINE__,
__FUNCTION__, r);
goto __finiv;
}
/* one thread */
/* (1) use the half of pool and free it */
for(i = 0; i < IDXMAX/2; i++) {
r = idx_allocate(&idxa);
if(r != i) {
fprintf(stderr, "%d:(%s):error: expected %lu instead of %lu\n",
__LINE__, __FUNCTION__, i, r);
r = -1;
goto __finiv1;
}
}
/* (2) use all pool and free the second part */
for(i = IDXMAX/2; i < IDXMAX; i++) {
r = idx_allocate(&idxa);
if(r != i) {
fprintf(stderr, "%d:(%s):error: expected %lu instead of %lu\n",
__LINE__, __FUNCTION__, i, r);
r = -1;
goto __finiv1;
}
}
/* (3) fill second part and free all and try to allocate 1/4 */
/* (4) free all */
__finiv1:
idx_allocator_destroy(&idxa);
__finiv:
return r;
}