2 * based on permutation/test.c by Brian Gough
4 * Copyright (C) 2001 Szymon Jaroszewicz
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 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <gsl/gsl_combination.h>
26 #include <gsl/gsl_test.h>
27 #include <gsl/gsl_ieee_utils.h>
30 { 0, 1, 2 }, { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 },
31 { 0, 2, 3 }, { 0, 2, 4 }, { 0, 2, 5 }, { 0, 3, 4 },
32 { 0, 3, 5 }, { 0, 4, 5 }, { 1, 2, 3 }, { 1, 2, 4 },
33 { 1, 2, 5 }, { 1, 3, 4 }, { 1, 3, 5 }, { 1, 4, 5 },
34 { 2, 3, 4 }, { 2, 3, 5 }, { 2, 4, 5 }, { 3, 4, 5 }
37 void my_error_handler (const char *reason, const char *file, int line, int err);
47 gsl_ieee_env_setup ();
49 c = gsl_combination_alloc (6,3);
51 /* Test combinations in forward order */
53 gsl_combination_init_first (c);
64 for (j = 0; j < 3; j++)
66 status |= (c->data[j] != c63[i][j]);
70 int s1 = gsl_combination_valid (c);
71 gsl_test (s1, "gsl_combination_valid (%u)", i);
76 while (gsl_combination_next(c) == GSL_SUCCESS);
78 gsl_test(status, "gsl_combination_next, 6 choose 3 combination, 20 steps");
80 gsl_combination_next(c);
81 gsl_combination_next(c);
82 gsl_combination_next(c);
83 for (j = 0; j < 3; j++)
85 status |= (c->data[j] != c63[19][j]);
87 gsl_test(status, "gsl_combination_next on the last combination");
90 int s1 = gsl_combination_valid (c);
91 gsl_test (s1, "gsl_combination_valid on the last combination");
95 gsl_combination * d = gsl_combination_alloc (6,3);
96 gsl_combination_memcpy (d, c);
100 for (j = 0; j < 3; j++)
102 status |= (d->data[j] != c->data[j]);
105 gsl_test (status, "gsl_combination_memcpy, 6 choose 3 combination");
106 gsl_combination_free(d);
110 /* Now test combinations in reverse order */
112 gsl_combination_init_last (c);
125 for (j = 0; j < 3; j++)
127 status |= (c->data[j] != c63[i][j]);
131 int s1 = gsl_combination_valid (c);
132 gsl_test (s1, "gsl_combination_valid (%u)", i);
135 while (gsl_combination_prev(c) == GSL_SUCCESS);
137 gsl_test(status, "gsl_combination_prev, 6 choose 3 combination, 20 steps");
139 gsl_combination_prev(c);
140 gsl_combination_prev(c);
141 gsl_combination_prev(c);
142 for (j = 0; j < 3; j++)
144 status |= (c->data[j] != c63[0][j]);
146 gsl_test(status, "gsl_combination_prev on the first combination");
149 int s1 = gsl_combination_valid (c);
150 gsl_test (s1, "gsl_combination_valid on the first combination");
154 gsl_combination * d = gsl_combination_alloc (6,3);
155 gsl_combination_memcpy (d, c);
159 for (j = 0; j < 3; j++)
161 status |= (d->data[j] != c->data[j]);
164 gsl_test (status, "gsl_combination_memcpy, 6 choose 3 combination");
165 gsl_combination_free(d);
168 gsl_combination_free (c);
170 c = gsl_combination_calloc(7, 0);
171 /* should return GSL_FAILURE every time */
172 status |= (gsl_combination_next(c) != GSL_FAILURE);
173 status |= (gsl_combination_next(c) != GSL_FAILURE);
174 status |= (gsl_combination_prev(c) != GSL_FAILURE);
175 status |= (gsl_combination_prev(c) != GSL_FAILURE);
176 gsl_test(status, "gsl_combination 7 choose 0");
177 gsl_combination_free (c);
179 c = gsl_combination_calloc(7, 7);
180 /* should return GSL_FAILURE every time */
181 for(j = 0; j < 7; j++)
183 status |= (gsl_combination_get(c, j) != j);
185 status |= (gsl_combination_next(c) != GSL_FAILURE);
186 for(j = 0; j < 7; j++)
188 status |= (gsl_combination_get(c, j) != j);
190 status |= (gsl_combination_next(c) != GSL_FAILURE);
191 for(j = 0; j < 7; j++)
193 status |= (gsl_combination_get(c, j) != j);
195 status |= (gsl_combination_prev(c) != GSL_FAILURE);
196 for(j = 0; j < 7; j++)
198 status |= (gsl_combination_get(c, j) != j);
200 status |= (gsl_combination_prev(c) != GSL_FAILURE);
201 for(j = 0; j < 7; j++)
203 status |= (gsl_combination_get(c, j) != j);
205 gsl_test(status, "gsl_combination 7 choose 7");
206 gsl_combination_free (c);
208 c = gsl_combination_calloc(6, 3);
210 gsl_set_error_handler (&my_error_handler);
215 s = gsl_combination_valid (c);
216 gsl_test (!s, "gsl_combination_valid on an invalid combination (1,1,2)");
221 s = gsl_combination_valid (c);
222 gsl_test (!s, "gsl_combination_valid on an invalid combination (2,1,0)");
227 s = gsl_combination_valid (c);
228 gsl_test (!s, "gsl_combination_valid on an invalid combination (1,2,0)");
231 gsl_combination * d = gsl_combination_alloc (6,4);
232 int s = gsl_combination_memcpy (d, c);
233 gsl_test (!s, "gsl_combination_memcpy, (6,4) vs (6,3)");
234 gsl_combination_free(d);
238 gsl_combination * d = gsl_combination_alloc (7,3);
239 int s = gsl_combination_memcpy (d, c);
240 gsl_test (!s, "gsl_combination_memcpy, (7,3) vs (6,3)");
241 gsl_combination_free(d);
245 gsl_combination * d = gsl_combination_alloc (7,2);
246 int s = gsl_combination_memcpy (d, c);
247 gsl_test (!s, "gsl_combination_memcpy, (7,2) vs (6,3)");
248 gsl_combination_free(d);
252 gsl_combination_free (c);
254 exit (gsl_test_summary());
258 my_error_handler (const char *reason, const char *file, int line, int err)
260 if (0) printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err) ;