Add to git.
[c2lib.git] / test_vector.c
1 /* Test the vector class.
2  * By Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_vector.c,v 1.2 2001/02/16 12:55:54 rich Exp $
19  *
20  * XXX This test is very incomplete at present.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31
32 #ifdef HAVE_ASSERT_H
33 #include <assert.h>
34 #endif
35
36 #include <pool.h>
37 #include <vector.h>
38 #include <pstring.h>
39
40 /* These are the numbers we'll be inserting into the array. */
41 static int numbers1to9[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
42
43 static void sqr_ptr (int *a, int *r) { *r = *a * *a; }
44 static int gt20_ptr (int *a) { return *a > 20; }
45
46 int
47 main ()
48 {
49   pool pool = new_pool ();
50   vector numbers, squares, squaresgt20;
51
52   /* Create initial vector. */
53   numbers = new_vector (pool, int);
54   vector_insert_array (numbers, 0, numbers1to9, 9);
55   assert (vector_size (numbers) == 9);
56
57   /* Print numbers. */
58   printf ("numbers = [ %s ]\n",
59           pjoin (pool, pvitostr (pool, numbers), ", "));
60
61   /* Square each number. */
62   squares = vector_map (pool, numbers,
63                         (void (*) (const void *, void *)) sqr_ptr,
64                         int);
65   assert (vector_size (squares) == 9);
66   printf ("squares = [ %s ]\n",
67           pjoin (pool, pvitostr (pool, squares), ", "));
68
69   /* Get squares > 20. */
70   squaresgt20 = vector_grep (pool, squares,
71                              (int (*) (const void *)) gt20_ptr);
72   assert (vector_size (squaresgt20) == 5);
73   printf ("squares > 20 = [ %s ]\n",
74           pjoin (pool, pvitostr (pool, squaresgt20), ", "));
75
76   delete_pool (pool);
77   exit (0);
78 }