Repository navigation
Expand file tree
/
Copy pathListTest.c
More file actions
93 lines (77 loc) · 2.55 KB
/
Copy pathListTest.c
File metadata and controls
93 lines (77 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*************************************************************
*
* $Id: ListTest.c 1972 2014-11-12 10:23:55Z phil $
*
*************************************************************
*/
/**
* Sample test program for generic Lists : application to strings.
*
*************************************************************/
static char rcsId[] = "@(#) $Id: ListTest.c 1972 2014-11-12 10:23:55Z phil $";
#include <stdio.h>
#include <string.h>
#include "List.h"
/*************************************************************
* Function to compare two elements (strings) : based on strcmp
* @param s1 the first string to compare
* @param s2 the second string to compare
* @return <0 if s1 is less than s2
* @return 0 if s1 equals s2
* @return >0 otherwise
*************************************************************/
static int compString (void * s1, void * s2) {
return strcmp((char*)s1, (char*)s2);
}
/*************************************************************
* Function to display an element of the list
* @param s the string to display
*************************************************************
*/
static void prString(void * s) {
printf("%s",(char*)s);
}
/*************************************************************
* another display function, just to test "forEach"
* @param s the string to display
*************************************************************/
static void prString2 (void * s) {
printf("%s\n",(char*)s);
}
/*************************************************************
* test program: creation of a list of strings then tests
* for main list functionalities.
*************************************************************/
int main() {
int i;
char * tab[] = {"belle marquise", "vos beaux yeux", "me font",
"mourir", "d'amour" };
/* list creation */
List* l = newList(compString,prString);
if (!l) return 1;
/* populating list */
for (i=0; i < sizeof(tab)/sizeof(char*); i++)
addList(l,tab[i]);
/* display list - should be sorted */
displayList(l);
putchar('\n');
/* test contain predicate */
if (isInList(l,"mourir"))
puts("mourir is in list");
else
puts("mourir is not in list");
if (isInList(l,"vivre"))
puts("vivre is in list");
else
puts("vivre is not in list");
/* test length (beware: sizeof is long int!) */
printf("length : %d (expected %ld)\n", lengthList(l),
sizeof(tab)/sizeof(char*));
/* test forEach */
puts("forEach:");
forEach(l,prString2);
/* final cleanup */
delList(l);
return 0;
}
/*************************************************************/