--- /dev/null
+/* $Id$ */
+
+/* dynar_int: A test case for the dynar using integers as content */
+
+/* Authors: Martin Quinson */
+/* Copyright (C) 2003 the OURAGAN project. */
+
+/* This program is free software; you can redistribute it and/or modify it
+ under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include <stdio.h>
+#include <gras.h>
+
+#define NB_ELEM 5000
+
+int main(int argc,char *argv[]) {
+ gras_dynar_t *d;
+ gras_error_t errcode;
+ int i,cpt,cursor;
+
+ // TRYFAIL(gras_log_control_set("root.thresh=debug dynar.thresh=info"));
+ TRYFAIL(gras_log_control_set("root.thresh=info"));
+ fprintf(stderr,"==== Push %d int, set them again 3 times, traverse them, shift them\n",NB_ELEM);
+ TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_push(d,&cpt));
+ // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_set(d,cpt,&cpt));
+ }
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_set(d,cpt,&cpt));
+ }
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_set(d,cpt,&cpt));
+ }
+ cpt=0;
+ gras_dynar_foreach(d,cursor,i){
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ cpt++;
+ }
+ if (cpt !=NB_ELEM) {
+ fprintf(stderr,
+ "Cannot retrieve my %d values. Last got one is %d\n",NB_ELEM,
+ cpt);
+ abort();
+ }
+
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ gras_dynar_shift(d,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ gras_dynar_free(d);
+
+
+ fprintf(stderr,"==== Unshift/pop %d int\n",NB_ELEM);
+ TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_unshift(d,&cpt));
+ // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ gras_dynar_pop(d,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ gras_dynar_free(d);
+
+
+
+ fprintf(stderr,"==== Push %d int, insert 1000 int in the middle, shift everything\n",NB_ELEM);
+ TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_push(d,&cpt));
+ // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ for (cpt=0; cpt< 1000; cpt++) {
+ TRYFAIL(gras_dynar_insert_at(d,2500,&cpt));
+ // fprintf (stderr,"Push %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+
+ for (cpt=0; cpt< 2500; cpt++) {
+ gras_dynar_shift(d,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one at the begining (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ // fprintf (stderr,"Pop %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ for (cpt=999; cpt>=0; cpt--) {
+ gras_dynar_shift(d,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one in the middle (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ }
+ for (cpt=2500; cpt< NB_ELEM; cpt++) {
+ gras_dynar_shift(d,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "The retrieved value is not the same than the injected one at the end (%d!=%d)\n",
+ i,cpt);
+ abort();
+ }
+ }
+ gras_dynar_free(d);
+
+
+ fprintf(stderr,"==== Push %d int, remove 2000-4000. free the rest\n",NB_ELEM);
+ TRYFAIL(gras_dynar_new(&d,sizeof(int),NULL));
+ for (cpt=0; cpt< NB_ELEM; cpt++) {
+ TRYFAIL(gras_dynar_push(d,&cpt));
+ }
+ for (cpt=2000; cpt< 4000; cpt++) {
+ gras_dynar_remove_at(d,2000,&i);
+ if (i != cpt) {
+ fprintf(stderr,
+ "Remove a bad value. Got %d, expected %d\n",
+ i,cpt);
+ abort();
+ }
+ // fprintf (stderr,"remove %d, length=%d \n",cpt, gras_dynar_length(d));
+ }
+ gras_dynar_free(d);
+
+ return 0;
+}