Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / combination / test.c
1 /* combination/test.c
2  * based on permutation/test.c by Brian Gough
3  * 
4  * Copyright (C) 2001 Szymon Jaroszewicz
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 3 of the License, or (at
9  * your option) any later version.
10  * 
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.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <gsl/gsl_combination.h>
26 #include <gsl/gsl_test.h>
27 #include <gsl/gsl_ieee_utils.h>
28
29 size_t c63[20][3] = {
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 }
35 } ;
36
37 void my_error_handler (const char *reason, const char *file, int line, int err);
38
39
40 int 
41 main (void)
42 {
43   size_t i, j;
44   int status = 0, s;
45   gsl_combination * c ;
46
47   gsl_ieee_env_setup ();
48
49   c = gsl_combination_alloc (6,3);
50
51   /* Test combinations in forward order */
52
53   gsl_combination_init_first (c);
54   
55   i = 0;
56
57   do 
58     {
59       if ( i >= 20 )
60         {
61           status = 1;
62           break;
63         }
64       for (j = 0; j < 3; j++)
65         {
66           status |= (c->data[j] != c63[i][j]);
67         }
68
69       {
70         int s1 = gsl_combination_valid (c);
71         gsl_test (s1, "gsl_combination_valid (%u)", i);
72       }
73
74       i++;
75     }
76   while (gsl_combination_next(c) == GSL_SUCCESS);
77
78   gsl_test(status, "gsl_combination_next, 6 choose 3 combination, 20 steps");
79
80   gsl_combination_next(c);
81   gsl_combination_next(c);
82   gsl_combination_next(c);
83   for (j = 0; j < 3; j++)
84     {
85       status |= (c->data[j] != c63[19][j]);
86     }
87   gsl_test(status, "gsl_combination_next on the last combination");
88
89   {
90     int s1 = gsl_combination_valid (c);
91     gsl_test (s1, "gsl_combination_valid on the last combination");
92   }
93
94   {
95     gsl_combination * d = gsl_combination_alloc (6,3);
96     gsl_combination_memcpy (d, c);
97
98     status = 0;
99
100     for (j = 0; j < 3; j++)
101       {
102         status |= (d->data[j] != c->data[j]);
103       }
104
105     gsl_test (status, "gsl_combination_memcpy, 6 choose 3 combination");
106     gsl_combination_free(d);
107   }
108
109
110   /* Now test combinations in reverse order */
111
112   gsl_combination_init_last (c);
113
114   i = 20;
115   do 
116     {
117       if ( i == 0 )
118         {
119           status = 1;
120           break;
121         }
122
123       i--;
124
125       for (j = 0; j < 3; j++)
126         {
127           status |= (c->data[j] != c63[i][j]);
128         }
129
130       {
131         int s1 = gsl_combination_valid (c);
132         gsl_test (s1, "gsl_combination_valid (%u)", i);
133       }
134     }
135   while (gsl_combination_prev(c) == GSL_SUCCESS);
136
137   gsl_test(status, "gsl_combination_prev, 6 choose 3 combination, 20 steps");
138
139   gsl_combination_prev(c);
140   gsl_combination_prev(c);
141   gsl_combination_prev(c);
142   for (j = 0; j < 3; j++)
143     {
144       status |= (c->data[j] != c63[0][j]);
145     }
146   gsl_test(status, "gsl_combination_prev on the first combination");
147
148   {
149     int s1 = gsl_combination_valid (c);
150     gsl_test (s1, "gsl_combination_valid on the first combination");
151   }
152
153   {
154     gsl_combination * d = gsl_combination_alloc (6,3);
155     gsl_combination_memcpy (d, c);
156
157     status = 0;
158
159     for (j = 0; j < 3; j++)
160       {
161         status |= (d->data[j] != c->data[j]);
162       }
163
164     gsl_test (status, "gsl_combination_memcpy, 6 choose 3 combination");
165     gsl_combination_free(d);
166   }
167
168   gsl_combination_free (c);
169
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);
178
179   c = gsl_combination_calloc(7, 7);
180   /* should return GSL_FAILURE every time */
181   for(j = 0; j < 7; j++)
182   {
183     status |= (gsl_combination_get(c, j) != j);
184   }
185   status |= (gsl_combination_next(c) != GSL_FAILURE);
186   for(j = 0; j < 7; j++)
187   {
188     status |= (gsl_combination_get(c, j) != j);
189   }
190   status |= (gsl_combination_next(c) != GSL_FAILURE);
191   for(j = 0; j < 7; j++)
192   {
193     status |= (gsl_combination_get(c, j) != j);
194   }
195   status |= (gsl_combination_prev(c) != GSL_FAILURE);
196   for(j = 0; j < 7; j++)
197   {
198     status |= (gsl_combination_get(c, j) != j);
199   }
200   status |= (gsl_combination_prev(c) != GSL_FAILURE);
201   for(j = 0; j < 7; j++)
202   {
203     status |= (gsl_combination_get(c, j) != j);
204   }
205   gsl_test(status, "gsl_combination 7 choose 7");
206   gsl_combination_free (c);
207
208   c = gsl_combination_calloc(6, 3);
209
210   gsl_set_error_handler (&my_error_handler);
211
212   c->data[0] = 1;
213   c->data[1] = 1;
214   c->data[2] = 2;
215   s = gsl_combination_valid (c);
216   gsl_test (!s, "gsl_combination_valid on an invalid combination (1,1,2)");
217
218   c->data[0] = 2;
219   c->data[1] = 1;
220   c->data[2] = 0;
221   s = gsl_combination_valid (c);
222   gsl_test (!s, "gsl_combination_valid on an invalid combination (2,1,0)");
223
224   c->data[0] = 1;
225   c->data[1] = 2;
226   c->data[2] = 0;
227   s = gsl_combination_valid (c);
228   gsl_test (!s, "gsl_combination_valid on an invalid combination (1,2,0)");
229
230   {
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);
235   }
236
237   {
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);
242   }
243
244   {
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);
249   }
250
251
252   gsl_combination_free (c);
253
254   exit (gsl_test_summary());
255 }
256
257 void
258 my_error_handler (const char *reason, const char *file, int line, int err)
259 {
260   if (0) printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err) ;
261 }