FORS Pipeline Reference Manual  5.0.9
list-test.c
1 /* $Id: list-test.c,v 1.5 2007-09-26 13:31:58 jmlarsen Exp $
2  *
3  * This file is part of the FORS Library
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author: jmlarsen $
23  * $Date: 2007-09-26 13:31:58 $
24  * $Revision: 1.5 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #define LIST_ELEM int
33 #include <list.h>
34 
35 #include <test.h>
36 
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #define LIST_DEFINE
42 #define LIST_ELEM int
43 #include <list.h>
44 
51 static int *
52 int_duplicate(const int *i)
53 {
54  int *j = cpl_malloc(sizeof *j);
55  *j = *i;
56  return j;
57 }
58 
59 static void
60 int_delete(int **i)
61 {
62  if (i && *i) {
63  cpl_free(*i); *i = NULL;
64  }
65  return;
66 }
67 
68 static bool
69 int_less_than(const int *i, const int *j, void *data)
70 {
71  data = data;
72  return (*i < *j);
73 }
74 
75 static double
76 int_evaluate(const int *i, void *data)
77 {
78  data = data;
79  return *i;
80 }
81 
82 static void
83 test_list(void)
84 {
85  int_list *l = int_list_new();
86  int x = 8;
87 
88  test( l != NULL );
89  test( int_list_size(l) == 0 );
90 
91  int_list_insert(l, int_duplicate(&x));
92  int_list_insert(l, int_duplicate(&x));
93  x = 0;
94  int_list_insert(l, int_duplicate(&x));
95 
96  test( int_list_size(l) == 3 );
97 
98  x = 7;
99 
100  test_eq( *int_list_min (l, int_less_than, NULL), 0 );
101  test_eq( *int_list_max (l, int_less_than, NULL), 8 );
102  test_eq( *int_list_min_val(l, int_evaluate, NULL), 0 );
103  test_eq( *int_list_max_val(l, int_evaluate, NULL), 8 );
104 
105  {
106  const int_list *l2 = int_list_duplicate(l, int_duplicate);
107 
108  test( *int_list_kth_const(l2, 1, int_less_than, NULL) == 0 );
109  test( *int_list_kth_const(l2, 3, int_less_than, NULL) == 8 );
110  test( *int_list_kth_const(l2, 3, int_less_than, NULL) == 8 );
111  test( *int_list_kth_const(l2, 2, int_less_than, NULL) == 8 );
112 
113  test( *int_list_max_const(l2, int_less_than, NULL) == 8 );
114 
115  {
116  int num_pairs = 0;
117  const int *p1, *p2;
118  for (int_list_first_pair_const(l2, &p1, &p2);
119  p1 != NULL;
120  int_list_next_pair_const(l2, &p1, &p2)) {
121 
122  test( p1 != p2 );
123  num_pairs += 1;
124  }
125  test( p2 == NULL );
126  test_eq( num_pairs, (3*2/2));
127  }
128 
129  int_list_delete_const(&l2, int_delete);
130  test( l2 == NULL );
131  }
132 
133  {
134  int *ip;
135  ip = int_list_remove(l, int_list_first(l));
136  int_delete(&ip);
137 
138  test( int_list_size(l) == 2 );
139 
140  int_list_first(l);
141  ip = int_list_remove(l, int_list_next(l));
142  int_delete(&ip);
143  test( int_list_size(l) == 1 );
144 
145  ip = int_list_remove(l, int_list_first(l));
146  int_delete(&ip);
147  test( int_list_size(l) == 0 );
148  }
149 
150  int_list_delete(&l, int_delete);
151 
152  test( l == NULL );
153 
154  return;
155 }
156 
157 
161 int main(void)
162 {
163  TEST_INIT;
164 
165  test_list();
166 
167  TEST_END;
168 }
169 
int main(void)
main
Definition: list-test.c:161