Separa código em diretórios.
[cascardo/declara.git] / test / listtest.c
diff --git a/test/listtest.c b/test/listtest.c
new file mode 100644 (file)
index 0000000..d40b78b
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ *  Copyright (C) 2012-2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <list.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+static char *names[] = {
+       "backwards",
+       "end to start",
+       "finish",
+       "english",
+};
+
+static char *sorting[] = {
+       "1",
+       "5",
+       "0",
+       "3",
+       "9",
+};
+
+static char *sorted[] = {
+       "0",
+       "1",
+       "3",
+       "5",
+       "9",
+};
+
+#define ALEN(x) (sizeof(x)/sizeof(x[0]))
+
+int main(int argc, char **argv)
+{
+       struct list *l;
+       int i;
+       int arraylen;
+
+       arraylen = sizeof(names)/sizeof(names[0]);
+
+       l = list_new();
+       list_add(&l, "GNU");
+       list_free(l, NULL);
+       printf("Single static list test pass\n");
+       fflush(stdout);
+
+       l = list_new();
+       for (i = 0; i < arraylen; i++) {
+               list_add(&l, strdup(names[i]));
+       }
+       for (i = 0; i < arraylen; i++) {
+               if (strcmp(list_get(l, i), names[i]))
+                       return 1;
+       }
+       list_free(l, free);
+       printf("Multiple add ordering list test pass\n");
+       fflush(stdout);
+
+       l = list_new();
+       for (i = 0; i < arraylen; i++) {
+               list_insert(&l, 0, strdup(names[i]));
+       }
+       for (i = 0; i < arraylen; i++) {
+               if (strcmp(list_get(l, i), names[arraylen - i - 1]))
+                       return 1;
+       }
+       list_free(l, free);
+       printf("Multiple insertion at start ordering list test pass\n");
+       fflush(stdout);
+
+       l = list_new();
+       for (i = 0; i < arraylen; i++) {
+               list_insert(&l, i, strdup(names[i]));
+       }
+       for (i = 0; i < arraylen; i++) {
+               if (strcmp(list_get(l, i), names[i]))
+                       return 1;
+       }
+       list_free(l, free);
+       printf("Multiple insertion at end ordering list test pass\n");
+       fflush(stdout);
+
+       l = list_new();
+       for (i = 0; i < ALEN(sorting); i++) {
+               list_insert_ordered(&l, strdup(sorting[i]),
+                                   (sort_function_t *) strcmp);
+       }
+       for (i = 0; i < ALEN(sorted); i++) {
+               if (strcmp(list_get(l, i), sorted[i]))
+                       return 1;
+       }
+       list_free(l, free);
+       printf("Ordered insertion list test pass\n");
+       fflush(stdout);
+
+       return 0;
+}