//~ kolan@nickolay-842 ~/dev/c/xalloc(default) $ time ./xalloc_test 50000 100000
//~ Success, iterations = 50000, nobj = 100000
//~
//~ real	0m0.302s
//~ user	0m0.100s
//~ sys	0m0.190s

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xalloc.h"

int main(int argc, char *argv[])
{
	if (argc != 3) {
		fputs("Usage: realloc_speed_test_cpp iterations nobj\n", stderr);
		exit(-1);
	}

	size_t iterations = (size_t)atoi(argv[1]);
	size_t nobj = (size_t)atoi(argv[2]);

	void **p = xmalloc(sizeof(void *) * nobj);
	memset(p, 0, sizeof(void *) * nobj);

	size_t i, idx, sz;
	for (i = 0; i < iterations; i++) {
		idx = (size_t)(rand() % (int)nobj);
		sz = i;
		p[idx] = xrealloc(p[idx], sz);

		if (sz && !p[idx]) {
			fprintf(stderr, "Alloc error at i = %lu\n", i);
			exit(-1);
		}
	}

	for (i = 0; i < nobj; i++) {
		xfree(p[i]);
		p[i] = NULL;
	}

	xclear(&p);

	printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);

	return EXIT_SUCCESS;
}
