1 /* Test the vector class.
2 * By Richard W.M. Jones <rich@annexia.org>
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.
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.
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.
18 * $Id: test_vector.c,v 1.2 2001/02/16 12:55:54 rich Exp $
20 * XXX This test is very incomplete at present.
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 };
43 static void sqr_ptr (int *a, int *r) { *r = *a * *a; }
44 static int gt20_ptr (int *a) { return *a > 20; }
49 pool pool = new_pool ();
50 vector numbers, squares, squaresgt20;
52 /* Create initial vector. */
53 numbers = new_vector (pool, int);
54 vector_insert_array (numbers, 0, numbers1to9, 9);
55 assert (vector_size (numbers) == 9);
58 printf ("numbers = [ %s ]\n",
59 pjoin (pool, pvitostr (pool, numbers), ", "));
61 /* Square each number. */
62 squares = vector_map (pool, numbers,
63 (void (*) (const void *, void *)) sqr_ptr,
65 assert (vector_size (squares) == 9);
66 printf ("squares = [ %s ]\n",
67 pjoin (pool, pvitostr (pool, squares), ", "));
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), ", "));