list-test.c

00001 /* $Id: list-test.c,v 1.5 2007/09/26 13:31:58 jmlarsen Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: jmlarsen $
00023  * $Date: 2007/09/26 13:31:58 $
00024  * $Revision: 1.5 $
00025  * $Name: fors-4_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #define LIST_ELEM int
00033 #include <list.h>
00034 
00035 #include <test.h>
00036 
00037 #include <stdbool.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 
00041 #define LIST_DEFINE
00042 #define LIST_ELEM int
00043 #include <list.h>
00044 
00051 static int *
00052 int_duplicate(const int *i)
00053 {
00054     int *j = cpl_malloc(sizeof *j);
00055     *j = *i;
00056     return j;
00057 }
00058 
00059 static void
00060 int_delete(int **i)
00061 {
00062     if (i && *i) {
00063         cpl_free(*i); *i = NULL;
00064     }
00065     return;
00066 }
00067 
00068 static bool
00069 int_less_than(const int *i, const int *j, void *data)
00070 {
00071     data = data;
00072     return (*i < *j);
00073 }
00074 
00075 static double
00076 int_evaluate(const int *i, void *data)
00077 {
00078     data = data;
00079     return *i;
00080 }
00081 
00082 static void
00083 test_list(void)
00084 {
00085     int_list *l = int_list_new();
00086     int x = 8;
00087     
00088     test( l != NULL );
00089     test( int_list_size(l) == 0 );
00090 
00091     int_list_insert(l, int_duplicate(&x));
00092     int_list_insert(l, int_duplicate(&x));
00093     x = 0;
00094     int_list_insert(l, int_duplicate(&x));
00095 
00096     test( int_list_size(l) == 3 );
00097 
00098     x = 7;
00099 
00100     test_eq( *int_list_min    (l, int_less_than, NULL), 0 );
00101     test_eq( *int_list_max    (l, int_less_than, NULL), 8 );
00102     test_eq( *int_list_min_val(l, int_evaluate, NULL), 0 );
00103     test_eq( *int_list_max_val(l, int_evaluate, NULL), 8 );
00104 
00105     {
00106         const int_list *l2 = int_list_duplicate(l, int_duplicate);
00107         
00108         test( *int_list_kth_const(l2, 1, int_less_than, NULL) == 0 );
00109         test( *int_list_kth_const(l2, 3, int_less_than, NULL) == 8 );
00110         test( *int_list_kth_const(l2, 3, int_less_than, NULL) == 8 );
00111         test( *int_list_kth_const(l2, 2, int_less_than, NULL) == 8 );
00112         
00113         test( *int_list_max_const(l2, int_less_than, NULL) == 8 );
00114 
00115         {
00116             int num_pairs = 0;
00117             const int *p1, *p2;
00118             for (int_list_first_pair_const(l2, &p1, &p2);
00119                  p1 != NULL;
00120                  int_list_next_pair_const(l2, &p1, &p2)) {
00121 
00122                 test( p1 != p2 );
00123                 num_pairs += 1;
00124             }
00125             test( p2 == NULL );
00126             test_eq( num_pairs, (3*2/2));
00127         }
00128 
00129         int_list_delete_const(&l2, int_delete);
00130         test( l2 == NULL );
00131     }
00132 
00133     {
00134         int *ip;
00135         ip = int_list_remove(l, int_list_first(l));
00136         int_delete(&ip);
00137         
00138         test( int_list_size(l) == 2 );
00139         
00140         int_list_first(l);
00141         ip = int_list_remove(l, int_list_next(l));
00142         int_delete(&ip);
00143         test( int_list_size(l) == 1 );
00144         
00145         ip = int_list_remove(l, int_list_first(l));
00146         int_delete(&ip);
00147         test( int_list_size(l) == 0 );
00148     }
00149 
00150     int_list_delete(&l, int_delete);
00151     
00152     test( l == NULL );
00153 
00154     return;
00155 }
00156 
00157 
00161 int main(void)
00162 {
00163     TEST_INIT;
00164 
00165     test_list();
00166 
00167     TEST_END;
00168 }
00169 

Generated on Fri Mar 4 09:46:01 2011 for FORS Pipeline Reference Manual by  doxygen 1.4.7