summaryrefslogtreecommitdiffstats
path: root/src/raptor_permute_test.c
blob: ad504fb7f0e124d7f6d160f0f2ffab9ee14feba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * raptor_permute.c - Test of permutations of ints in a sequence
 *
 * Copyright (C) 2011, David Beckett http://www.dajobe.org/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif

#include <stdio.h>
#include <string.h>

/* Raptor includes */
#include "raptor2.h"
#include "raptor_internal.h"


#define PERMUTE_DEBUG 0


typedef struct 
{
  raptor_sequence* seq;
  int* contents;
} intseq;


static int
print_handler(void *object, FILE *fh)
{
  int* pi = (int*)object;
  fprintf(fh, "%d", *pi);
  return 0;
}


static intseq*
new_intseq(int size) 
{
  intseq* iseq;
  int i;
  
  iseq = RAPTOR_CALLOC(intseq*, 1, sizeof(*iseq));
  iseq->contents = RAPTOR_CALLOC(int*, size, sizeof(int));
  /* will be a sequence of int* pointing into iseq->contents */
  iseq->seq = raptor_new_sequence(NULL, print_handler);

  for(i = 0; i < (int)size; i++) {
    iseq->contents[i] = i + 1;
    raptor_sequence_set_at(iseq->seq, i, &iseq->contents[i]);
  }

  return iseq;
}

static void
free_intseq(intseq* iseq)
{
  if(iseq->contents)
    RAPTOR_FREE(int*, iseq->contents);

  if(iseq->seq)
    raptor_free_sequence(iseq->seq);

  RAPTOR_FREE(intseq*, iseq);
}

static void
intseq_print(intseq *iseq, FILE* stream)
{
  raptor_sequence_print(iseq->seq, stream);
}

static int
intseq_reverse(intseq *iseq, int start_index, int length) 
{
  return raptor_sequence_reverse(iseq->seq, start_index, length);
}

static int
intseq_compare_at(const void* a, const void* b)
{
  int* ia = (int*)a;
  int* ib = (int*)b;
  return *ia - *ib;
}

static int
intseq_next_permutation(intseq *iseq)
{
  return raptor_sequence_next_permutation(iseq->seq, intseq_compare_at);
}

static int
intseq_get_at(intseq *iseq, int idx)
{       
  return *(int*)(raptor_sequence_get_at(iseq->seq, idx));
}
       
#define MAX_SIZE 5

int expected_results_size5[120][5] = {
  {1, 2, 3, 4, 5},
  {1, 2, 3, 5, 4},
  {1, 2, 4, 3, 5},
  {1, 2, 4, 5, 3},
  {1, 2, 5, 3, 4},
  {1, 2, 5, 4, 3},
  {1, 3, 2, 4, 5},
  {1, 3, 2, 5, 4},
  {1, 3, 4, 2, 5},
  {1, 3, 4, 5, 2},
  {1, 3, 5, 2, 4},
  {1, 3, 5, 4, 2},
  {1, 4, 2, 3, 5},
  {1, 4, 2, 5, 3},
  {1, 4, 3, 2, 5},
  {1, 4, 3, 5, 2},
  {1, 4, 5, 2, 3},
  {1, 4, 5, 3, 2},
  {1, 5, 2, 3, 4},
  {1, 5, 2, 4, 3},
  {1, 5, 3, 2, 4},
  {1, 5, 3, 4, 2},
  {1, 5, 4, 2, 3},
  {1, 5, 4, 3, 2},
  {2, 1, 3, 4, 5},
  {2, 1, 3, 5, 4},
  {2, 1, 4, 3, 5},
  {2, 1, 4, 5, 3},
  {2, 1, 5, 3, 4},
  {2, 1, 5, 4, 3},
  {2, 3, 1, 4, 5},
  {2, 3, 1, 5, 4},
  {2, 3, 4, 1, 5},
  {2, 3, 4, 5, 1},
  {2, 3, 5, 1, 4},
  {2, 3, 5, 4, 1},
  {2, 4, 1, 3, 5},
  {2, 4, 1, 5, 3},
  {2, 4, 3, 1, 5},
  {2, 4, 3, 5, 1},
  {2, 4, 5, 1, 3},
  {2, 4, 5, 3, 1},
  {2, 5, 1, 3, 4},
  {2, 5, 1, 4, 3},
  {2, 5, 3, 1, 4},
  {2, 5, 3, 4, 1},
  {2, 5, 4, 1, 3},
  {2, 5, 4, 3, 1},
  {3, 1, 2, 4, 5},
  {3, 1, 2, 5, 4},
  {3, 1, 4, 2, 5},
  {3, 1, 4, 5, 2},
  {3, 1, 5, 2, 4},
  {3, 1, 5, 4, 2},
  {3, 2, 1, 4, 5},
  {3, 2, 1, 5, 4},
  {3, 2, 4, 1, 5},
  {3, 2, 4, 5, 1},
  {3, 2, 5, 1, 4},
  {3, 2, 5, 4, 1},
  {3, 4, 1, 2, 5},
  {3, 4, 1, 5, 2},
  {3, 4, 2, 1, 5},
  {3, 4, 2, 5, 1},
  {3, 4, 5, 1, 2},
  {3, 4, 5, 2, 1},
  {3, 5, 1, 2, 4},
  {3, 5, 1, 4, 2},
  {3, 5, 2, 1, 4},
  {3, 5, 2, 4, 1},
  {3, 5, 4, 1, 2},
  {3, 5, 4, 2, 1},
  {4, 1, 2, 3, 5},
  {4, 1, 2, 5, 3},
  {4, 1, 3, 2, 5},
  {4, 1, 3, 5, 2},
  {4, 1, 5, 2, 3},
  {4, 1, 5, 3, 2},
  {4, 2, 1, 3, 5},
  {4, 2, 1, 5, 3},
  {4, 2, 3, 1, 5},
  {4, 2, 3, 5, 1},
  {4, 2, 5, 1, 3},
  {4, 2, 5, 3, 1},
  {4, 3, 1, 2, 5},
  {4, 3, 1, 5, 2},
  {4, 3, 2, 1, 5},
  {4, 3, 2, 5, 1},
  {4, 3, 5, 1, 2},
  {4, 3, 5, 2, 1},
  {4, 5, 1, 2, 3},
  {4, 5, 1, 3, 2},
  {4, 5, 2, 1, 3},
  {4, 5, 2, 3, 1},
  {4, 5, 3, 1, 2},
  {4, 5, 3, 2, 1},
  {5, 1, 2, 3, 4},
  {5, 1, 2, 4, 3},
  {5, 1, 3, 2, 4},
  {5, 1, 3, 4, 2},
  {5, 1, 4, 2, 3},
  {5, 1, 4, 3, 2},
  {5, 2, 1, 3, 4},
  {5, 2, 1, 4, 3},
  {5, 2, 3, 1, 4},
  {5, 2, 3, 4, 1},
  {5, 2, 4, 1, 3},
  {5, 2, 4, 3, 1},
  {5, 3, 1, 2, 4},
  {5, 3, 1, 4, 2},
  {5, 3, 2, 1, 4},
  {5, 3, 2, 4, 1},
  {5, 3, 4, 1, 2},
  {5, 3, 4, 2, 1},
  {5, 4, 1, 2, 3},
  {5, 4, 1, 3, 2},
  {5, 4, 2, 1, 3},
  {5, 4, 2, 3, 1},
  {5, 4, 3, 1, 2},
  {5, 4, 3, 2, 1}
};

int main (int argc, char *argv[]) 
{
  const char *program = raptor_basename(argv[0]);
  int failures = 0;
  raptor_world *world;
  int size;
  int expected_counts[MAX_SIZE + 1] = { 1, 1, 2, 6, 24, 120 };
   
  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);

  for(size = 0; size <= MAX_SIZE; size++) {
    intseq* iseq;
    int count;

    iseq = new_intseq(size);

#if PERMUTE_DEBUG > 0
    fprintf(stderr, "%s: Permutation test %d.  Initial state: ", program, size);
    intseq_print(iseq, stderr);
    fputc('\n', stderr);
#endif

    for(count = 1; 1; count++) {
#if PERMUTE_DEBUG > 1
      fprintf(stderr, "Permutation %3d: ", count);
      intseq_print(iseq, stderr);
      fputc('\n', stderr);
#endif
      if(size == 5) {
        int* expected_result = expected_results_size5[count - 1];
        int j;
        int ok = 1;
        
        for(j = 0; j < size; j++) {
          int actual = intseq_get_at(iseq, j);
          int expected = expected_result[j];
          if(actual != expected) {
            ok = 0;
            break;
          }
        }
        if(!ok) {
          fprintf(stderr, "%s: FAILED test %d result %d - returned ",
                  program, size, count);
          intseq_print(iseq, stderr);
          fputs(" expected [", stderr);
          for(j = 0; j < size; j++) {
            fprintf(stderr, "%d, ", expected_result[j]);
          }
          fputs("]\n", stderr);
          
          failures++;
        }
      }
      
      if(intseq_next_permutation(iseq))
        break;
    }

#if PERMUTE_DEBUG > 0
    fprintf(stderr, "%s: Returned %d results.  Final state: ", program, count);
    intseq_print(iseq, stderr);
    fputc('\n', stderr);
#endif

    free_intseq(iseq);

    if(count != expected_counts[size]) {
      fprintf(stderr, "%s: FAILED test %d - returned %d items expected %d\n",
              program, size, count, expected_counts[size]);
      failures++;
      break;
    }
  }


  /* This is mainly a test for crashes */
  for(size = 0; size <= MAX_SIZE; size++) {
    intseq* iseq;
    int st;

    iseq = new_intseq(size);

    for(st = 0; st < size + 1; st++) {
      intseq_reverse(iseq, 0, st) ;
      intseq_reverse(iseq, st, st) ;
      intseq_reverse(iseq, st, 0) ;
    }

    free_intseq(iseq);

  }
  
  raptor_free_world(world);
  
  return failures;
}