forked from ligurio/jenny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenny.c
1802 lines (1588 loc) · 49.8 KB
/
jenny.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-------------------------------------------------------------------------------
By Bob Jenkins, March 2003. Public domain.
jenny.c -- jennyrate tests from m dimensions of features that cover all
n-tuples of features, n <= m, with each feature chosen from a different
dimension. For example, given 10 dimensions (m=10) with 2 to 8 features
apiece, cover all feature triplets (n=3). A lower bound on the number
of tests required is the product of the sizes of the largest n dimensions.
Already-written tests can be piped in to be reused.
Arguments
Arguments without a leading '-' : an integer in 2..52. Represents a
dimension. Dimensions are implicitly numbered 1..65535, in the
order they appear. Features in dimensions are always implicitly
given 1-character names, which are in order a..z, A..Z . It's a
good idea to pass the output of jenny through a postprocessor that
expands these names into something intelligible.
-o : old. -ofoo.txt reads existing tests from the file foo.txt, includes
those tests in the output, and adds whatever other tests are needed to
complete coverage. An error is reported if the input tests are of the
wrong shape or contain disallowed feature interactions. If you have
added new dimensions since those tests were written, be sure to include
a do-nothing feature in each new dimension, then pad the existing tests
with do-nothing out to the correct number of dimensions.
-h : help. Print out instructions for using jenny.
-n : an integer. Cover all n-tuples of features, one from each dimension.
Default is 2 (pairs). 3 (triplets) may be reasonable. 4 (quadruplets)
is definitely overkill. n > 4 is highly discouraged.
-s : seed. An integer. Seed the random number generator.
-w : without this combination of features. A feature is given by a dimension
number followed by a one-character feature name. A single -w can
disallow multiple features in a dimension. For example, -w1a2cd4ac
disallows the combinations (1a,2c,4a),(1a,2c,4c),(1a,2d,4a),(1a,2d,4c)
where 1a represents the first dimension's first feature, 2c is the
second dimension's third feature, and 4a is the fourth dimension's
first feature.
Example: 10 dimensions of 2, 3, 8, 3, 2, 2, 5, 3, 2, 2 features apiece,
with some restrictions, asking for all triplets of features to be covered.
This will produce at least 8*5*3=120 tests. Splitting the 8 features in the
third dimension into three dimensions each of length 2 would reduce the
number of testcases required to at least 5*3*3=45.
jenny -n3 2 3 8 -w1a2bc3b -w1b3a 3 -w1a4b 2 2 5 3 2 2 -w9a10b -w3a4b -s3
-------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*
-------------------------------------------------------------------------------
Implementation:
Internally, there can be 64K dimensions with 64K features apiece. Externally,
the number of features per dimensions is limited to just 52, and are implicitly
named a..z, A..Z. Other printable characters, like |, caused trouble in the
shell when I tried to give them during a without.
The program first finds tests for all features, then adds tests to cover
all pairs of features, then all triples of features, and so forth up to
the tuples size the user asked for.
-------------------------------------------------------------------------------
*/
/*
-------------------------------------------------------------------------------
Structures
-------------------------------------------------------------------------------
*/
typedef unsigned char ub1;
typedef char sb1;
typedef unsigned short ub2;
typedef signed short sb2;
typedef unsigned long ub4;
typedef signed long sb4;
typedef unsigned long long ub8;
typedef signed long long sb8;
#define TRUE 1
#define FALSE 0
#define UB4MAXVAL 0xffffffff
#define UB2MAXVAL 0xffff
/*
-------------------------------------------------------------------------------
Random number stuff
-------------------------------------------------------------------------------
*/
#define FLEARAND_SIZE 256
typedef struct flearandctx {
ub4 b,c,d,z; /* special memory */
ub4 m[FLEARAND_SIZE]; /* big random pool of memory */
ub4 r[FLEARAND_SIZE]; /* results */
ub4 q; /* counter, which result of r was last reported */
} flearandctx;
/* Pseudorandom numbers, courtesy of FLEA */
void flearand_batch( flearandctx *x) {
ub4 a, b=x->b, c=x->c+(++x->z), d=x->d, i, *m=x->m, *r=x->r;
for (i=0; i<FLEARAND_SIZE; ++i) {
a = m[b % FLEARAND_SIZE];
m[b % FLEARAND_SIZE] = d;
d = (c<<19) + (c>>13) + b;
c = b ^ m[i];
b = a + d;
r[i] = c;
}
x->b=b; x->c=c; x->d=d;
}
ub4 flearand( flearandctx *x) {
if (!x->q--) {
x->q = FLEARAND_SIZE-1;
flearand_batch(x);
}
return x->r[x->q];
}
void flearand_init( flearandctx *x, ub4 seed) {
ub4 i;
x->b = x->c = x->d = x->z = seed;
for (i = 0; i<FLEARAND_SIZE; ++i) {
x->m[i] = seed;
}
for (i=0; i<10; ++i) {
flearand_batch(x);
}
x->q = 0;
}
/*
------------------------------------------------------------------------------
Other helper routines
------------------------------------------------------------------------------
*/
#define TUPLE_ARRAY 5040 /* tuple array size, multiple of 1,2,3,4,5,6 */
/* An arbitrary feature, prefix fe */
typedef struct feature {
ub2 d; /* Dimension name */
ub2 f; /* Feature name */
} feature;
/* a tuple array, prefix tu */
typedef struct tu_arr {
struct tu_arr *next; /* next tuple array */
ub2 len; /* length of this array */
feature fe[TUPLE_ARRAY]; /* array of tuples */
} tu_arr;
/* an iterator over a tuple array, prefix tu */
typedef struct tu_iter {
struct tu_arr **tu; /* current tuple array */
ub2 offset; /* offset of current tuple */
ub2 n; /* number of features per tuple */
ub4 *count; /* number of tuples in list */
feature *fe; /* current tuple */
} tu_iter;
/* names of features, for output */
static const char feature_name[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/*
------------------------------------------------------------------------------
Stuff specific to jenny
------------------------------------------------------------------------------
*/
#define MAX_FEATURES 52 /* can't name more than 52 features */
#define MAX_TESTS 65534 /* arbitrary limit on number of testcases */
#define MAX_N 32 /* never can do complete coverage of 33-tuples */
#define MAX_WITHOUT (MAX_FEATURES*MAX_N) /* max features in a without */
#define MAX_DIMENSIONS (((ub2)~0)-1) /* More than 64K dimensions needs a ub4 */
/* A "test", which is a combination of features. Prefix t. */
typedef struct test {
ub2 *f; /* features in this test */
} test;
/* representation of a restriction, prefix w */
typedef struct without {
ub2 len; /* length of feature array */
struct feature *fe; /* feature array */
} without;
/* without chain */
typedef struct wchain {
without *w;
struct wchain *next;
} wchain;
/* Return a count of how many withouts are disobeyed. */
/* Also set a pointer to a randomly chosen violated without */
int count_withouts(
test *t, /* test to check */
wchain *wc) /* restrictions */
{
ub4 count; /* count of disobeyed withouts */
for (count = 0; wc; wc = wc->next) {
without *w = wc->w;
ub4 i = 0;
int match = TRUE; /* match the entire restriction */
while (i<w->len) {
int dimension_match = FALSE; /* match in this dimension */
do {
if (t->f[w->fe[i].d] == w->fe[i].f) {
dimension_match = TRUE;
}
++i;
} while (i<w->len && (w->fe[i].d == w->fe[i-1].d));
if (!dimension_match) {
match = FALSE;
break;
}
}
if (match) {
++count;
}
}
return count;
}
/* token definitions */
typedef enum token_type {
TOKEN_ERROR = 0, /* TOKEN_ERROR has to be 0 */
TOKEN_END,
TOKEN_NUMBER,
TOKEN_FEATURE,
TOKEN_SPACE
} token_type;
/* whole current state, prefix s */
typedef struct state {
ub1 n_final; /* The n in the user's n-tuples, default is 2 */
ub2 ndim; /* number of dimensions */
ub2 ntests; /* number of testcases in t */
ub1 **n; /* n[d][f] is current n-tuple size for dimension d feature f */
test **t; /* all the tests generated so far */
ub2 *dim; /* number of features in each dimension */
wchain **wc; /* s->wc[d] lists withouts for dimension d */
wchain *wc2; /* a list of all the original withouts */
wchain *wc3; /* additional, deduced withouts */
tu_arr ***tu; /* tu[d][f] lists untested tuples for dimension d feature f */
tu_arr ***one;
/* one[testcase][d] lists tuples with d covered only by this testcase */
ub4 **onec; /* onec[testcase][d] is count of one[testcase][d] */
ub4 **used;
/* used[testcase][d] = pass# if this pass has already explored test[t][d] */
ub4 **tc; /* tc[d][f] is # untested tulpes for dimension d feature f */
test *tuple_tester; /* an all -1 test used to test individual tuples */
ub2 *dimord; /* order in which to choose dimensions */
ub2 *featord; /* order in which to choose features */
flearandctx r; /* random number context */
} state;
void my_free( char *x)
{
free(x);
}
/* zero out a list of tuples */
void truncate_tuple( tu_arr **tu, ub4 *count)
{
while (*tu) {
tu_arr *tu2 = *tu;
*tu = ((*tu)->next);
my_free((char *)tu2);
}
*count = 0;
}
/* delete the i-th test */
void delete_test( state *s, ub4 i)
{
test *t = s->t[i];
s->t[i] = s->t[--s->ntests];
my_free((char *)t->f);
my_free((char *)t);
if (s->one[s->ntests]) {
ub2 d;
for (d=0; d<s->ndim; ++d) {
truncate_tuple(&s->one[s->ntests][d], &s->onec[s->ntests][d]);
}
my_free((char *)s->one[s->ntests]);
my_free((char *)s->onec[s->ntests]);
s->one[s->ntests] = (tu_arr **)0;
s->onec[s->ntests] = (ub4 *)0;
}
}
void cleanup(state *s)
{
if (s->tu) {
ub2 d,f;
for (d=0; d<s->ndim; ++d) {
if (s->tu[d]) {
for (f=0; f<s->dim[d]; ++f) {
truncate_tuple(&s->tu[d][f], &s->tc[d][f]);
}
my_free((char *)s->tu[d]);
}
}
my_free((char *)s->tu);
}
/* free n, the tuple lengths */
if (s->n) {
ub2 d;
for (d=0; d<s->ndim; ++d) {
if (s->n[d]) {
my_free((char *)s->n[d]);
}
}
my_free((char *)s->n);
}
/* free tc, count of uncovered tuples */
if (s->tc) {
ub2 d;
for (d=0; d<s->ndim; ++d) {
if (s->tc[d]) {
my_free((char *)s->tc[d]);
}
}
my_free((char *)s->tc);
}
/* free the secondary chains of restrictions */
if (s->wc) {
ub2 i;
for (i=0; i<s->ndim; ++i) {
while (s->wc[i]) {
wchain *wc = s->wc[i];
s->wc[i] = s->wc[i]->next;
my_free((char *)wc);
}
}
my_free((char *)s->wc);
}
/* free all the actual restrictions */
while (s->wc2) {
wchain *wc = s->wc2;
without *w = wc->w;
s->wc2 = s->wc2->next;
if (w->fe) my_free((char *)w->fe);
my_free((char *)w);
my_free((char *)wc);
}
while (s->wc3) {
wchain *wc = s->wc3;
without *w = wc->w;
s->wc3 = s->wc3->next;
if (w->fe) my_free((char *)w->fe);
my_free((char *)w);
my_free((char *)wc);
}
if (s->t) {
while (s->ntests) {
delete_test(s, 0);
}
my_free((char *)s->t);
}
if (s->one) {
my_free((char *)s->one);
}
if (s->onec) {
my_free((char *)s->onec);
}
if (s->tuple_tester) {
if (s->tuple_tester->f) {
my_free((char *)s->tuple_tester->f);
}
my_free((char *)s->tuple_tester);
}
if (s->dimord) {
my_free((char *)s->dimord);
}
if (s->featord) {
my_free((char *)s->featord);
}
/* free the array of dimension lengths */
if (s->dim) my_free((char *)s->dim);
}
char *my_alloc( state *s, size_t len)
{
char *rsl;
if (!(rsl = (char *)malloc(len+sizeof(size_t)))) {
printf("jenny: could not allocate space\n");
cleanup(s);
exit(0);
}
memset(rsl, 0x00, len);
return rsl;
}
/* insert a tuple into a tuple array */
int insert_tuple( state *s, tu_iter *ctx, feature *tuple)
{
ub4 i;
feature *fe;
ub1 n = ctx->n;
ub4 lim = TUPLE_ARRAY / n;
while (*ctx->tu && (*ctx->tu)->len == lim) {
ctx->tu = &((*ctx->tu)->next);
}
if (!*ctx->tu) {
if (!((*ctx->tu) = (tu_arr *)my_alloc(s, sizeof(tu_arr)))) {
return FALSE;
}
(*ctx->tu)->len = 0;
(*ctx->tu)->next = (tu_arr *)0;
}
fe = &(*ctx->tu)->fe[(*ctx->tu)->len*n];
for (i=0; i<n; ++i) {
fe[i].d = tuple[i].d;
fe[i].f = tuple[i].f;
}
++(*ctx->tu)->len;
++*ctx->count;
return TRUE;
}
/* print out a single tuple */
void show_tuple( feature *fe, ub2 len)
{
ub4 i;
for (i=0; i<len; ++i) {
printf(" %d%c", fe[i].d+1, feature_name[fe[i].f]);
}
printf(" \n");
}
/* delete a tuple from a tuple array */
feature *delete_tuple( tu_iter *ctx)
{
feature *fe;
ub4 i;
feature *tuple = ctx->fe;
ub1 n = ctx->n;
--(*ctx->tu)->len;
--*ctx->count;
fe = &(*ctx->tu)->fe[(*ctx->tu)->len * n];
for (i=0; i<n; ++i) {
tuple[i].d = fe[i].d;
tuple[i].f = fe[i].f;
}
if (!(*ctx->tu)->len) {
tu_arr *tu2 = *ctx->tu;
*ctx->tu = ((*ctx->tu)->next);
my_free((char *)tu2);
}
if (!*ctx->tu) { /* freed the last block */
ctx->offset = 0;
ctx->fe = (feature *)0;
return ctx->fe;
} else if (tuple == fe) {
ctx->offset = 0;
ctx->fe = &(*ctx->tu)->fe[0]; /* freed this block, move to next */
return ctx->fe;
} else {
return tuple; /* moved a new tuple into the old location */
}
}
/* start a tuple iterator */
feature *start_tuple( tu_iter *ctx, tu_arr **tu, ub4 n, ub4 *count)
{
ctx->tu = tu;
ctx->offset = 0;
ctx->n = n;
ctx->count = count;
if (*tu) {
ctx->fe = (*tu)->fe;
} else {
ctx->fe = (feature *)0;
}
return ctx->fe;
}
/* get the next tuple from a tuple iterator (0 if no more tuples) */
static feature *next_tuple( tu_iter *ctx)
{
if (++ctx->offset < (*ctx->tu)->len) {
ctx->fe += ctx->n;
} else {
ctx->tu = &(*ctx->tu)->next;
ctx->offset = 0;
if (*ctx->tu && (*ctx->tu)->len) {
ctx->fe = (*ctx->tu)->fe;
} else {
ctx->fe = (feature *)0;
}
}
return ctx->fe;
}
/* test if this test covers this tuple */
static int test_tuple( ub2 *test, feature *tuple, ub2 n)
{
sb4 i;
for (i=0; i<n; ++i) {
if (tuple[i].f != test[tuple[i].d]) {
return FALSE;
}
}
return TRUE;
}
/* test if first tuple (t1, n1) is a subset of second tuple (t2, n2) */
int subset_tuple( feature *t1, ub1 n1, feature *t2, ub1 n2)
{
sb4 i, j;
if (n2 < n1)
return FALSE;
for (i=0, j=0; i<n1; ++i) {
while (t1[i].d > t2[j].d) {
if (++j == n2)
return FALSE;
}
if (t1[i].d != t2[j].d || t1[i].f != t2[j].f) {
return FALSE;
}
}
return TRUE;
}
void initialize( state *s)
{
/* make all freeable pointers start out zero */
s->dim = (ub2 *)0;
s->wc = (wchain **)0;
s->wc2 = (wchain *)0;
s->wc3 = (wchain *)0;
s->tu = (tu_arr ***)0;
s->one = (tu_arr ***)0;
s->onec = (ub4 **)0;
s->n = (ub1 **)0;
s->tc = (ub4 **)0;
s->t = (test **)0;
s->tuple_tester = (test *)0;
s->dimord = (ub2 *)0;
s->featord = (ub2 *)0;
/* fill in default values */
s->ndim = (ub2)0;
s->n_final = 2; /* guarantees that all pairs of dimensions are covered */
s->ntests = 0;
flearand_init(&s->r, 0); /* initialize random number generator */
}
/* add one test to the list of tests */
int add_test( state *s, test *t)
{
ub4 i;
if (s->ntests == MAX_TESTS) {
return FALSE;
}
s->one[s->ntests] = (tu_arr **)my_alloc(s, sizeof(tu_arr *)*s->ndim);
s->onec[s->ntests] = (ub4 *)my_alloc(s, sizeof(ub4)*s->ndim);
for (i=0; i<s->ndim; ++i) {
s->one[s->ntests][i] = (tu_arr *)0;
s->onec[s->ntests][i] = 0;
}
s->t[s->ntests++] = t;
return TRUE;
}
/*
* parse a token
* Start at *curr in string *inp of length inl
* Adjust *curr to be after the just-parsed token
* Place the token value in *rsl
* Return the token type
*/
token_type parse_token(char *inp, ub4 inl, ub4 *curr, ub4 *rsl)
{
char mychar;
ub4 i;
if (*curr == inl)
return TOKEN_END;
mychar = inp[*curr];
if (mychar == '\0') {
return TOKEN_END;
} else if (mychar == ' ' || mychar == '\t' || mychar == '\n') {
/*--------------------------------------------------------- parse spaces */
for (i=*curr+1; i < inl; ++i) {
mychar = inp[i];
if (!(mychar == ' ' || mychar == '\t' || mychar == '\n'))
break;
}
*curr = i;
return TOKEN_SPACE;
} else if (mychar >= '0' && mychar <= '9') {
/*------------------------------------------------------- parse a number */
ub4 i, number = 0;
for (i=*curr; i < inl && inp[i] >= '0' && inp[i] <= '9'; ++i) {
number = number*10 + (inp[i] - '0');
}
*curr = i;
*rsl = number;
return TOKEN_NUMBER;
} else if ((mychar >= 'a' && mychar <= 'z') ||
(mychar >= 'A' && mychar <= 'Z')) {
/*------------------------------------------------- parse a feature name */
ub4 i;
for (i=0; i<MAX_FEATURES; ++i)
if (feature_name[i] == mychar)
break;
if (i == MAX_FEATURES) {
printf("jenny: the name '%c' is not used for any feature\n",
mychar);
return TOKEN_ERROR;
}
*rsl = i;
++*curr;
return TOKEN_FEATURE;
} else {
return TOKEN_ERROR;
}
}
#define BUFSIZE (MAX_DIMENSIONS*7+2)
/* load old tests before generating new ones */
int load( state *s, char *testfile)
{
char buf[BUFSIZE]; /* buffer holding a line read from the file */
FILE *f;
if (testfile[0] == '\0') {
f = stdin;
} else {
f = fopen(testfile, "r");
}
if (!f) {
printf("jenny: file %s could not be opened\n", testfile);
return FALSE;
}
while (fgets(buf, BUFSIZE, f) && (buf[0] != '.')) {
ub4 curr = 0; /* current offset into buf */
ub4 value; /* token value */
ub4 i;
test *t;
t = (test *)my_alloc( s, sizeof(test));
t->f = (ub2 *)my_alloc( s, sizeof(ub2)*s->ndim);
if (!add_test(s, t)) {
goto failure;
}
for (i=0; i<s->ndim; ++i) {
if (parse_token(buf, UB4MAXVAL, &curr, &value) != TOKEN_SPACE) {
printf("jenny: -o, non-space found where space expected\n");
goto failure;
}
if (parse_token(buf, UB4MAXVAL, &curr, &value) != TOKEN_NUMBER) {
printf("jenny: -o, non-number found where number expected\n");
goto failure;
}
if (value-1 != i) {
printf("jenny: -o, number %lu found out-of-place\n", value);
goto failure;
}
if (parse_token(buf, UB4MAXVAL, &curr, &value) != TOKEN_FEATURE) {
printf("jenny: -o, non-feature found where feature expected\n");
goto failure;
}
if (value >= s->dim[i]) {
printf("jenny: -o, feature %c does not exist in dimension %lu\n",
feature_name[value], i+1);
goto failure;
}
t->f[i] = value;
}
if (parse_token(buf, UB4MAXVAL, &curr, &value) != TOKEN_SPACE) {
printf("jenny: -o, non-space found where trailing space expected\n");
goto failure;
}
if (parse_token(buf, UB4MAXVAL, &curr, &value) != TOKEN_END) {
printf("jenny: -o, testcase not properly terminated\n");
goto failure;
}
/* make sure the testcase obeys all the withouts */
if (count_withouts(t, s->wc2)) {
printf("jenny: -o, old testcase contains some without\n");
goto failure;
}
}
(void)fclose(f);
return TRUE;
failure:
while (fgets(buf, BUFSIZE, f) && (buf[0] != '.'))
; /* finish reading the input */
(void)fclose(f); /* close the file */
return FALSE;
}
static const sb1 *jenny_doc[] = {
"jenny:\n",
" Given a set of feature dimensions and withouts, produce tests\n",
" covering all n-tuples of features where all features come from\n",
" different dimensions. For example (=, <, >, <=, >=, !=) is a\n",
" dimension with 6 features. The type of the left-hand argument is\n",
" another dimension. Dimensions are numbered 1..65535, in the order\n",
" they are listed. Features are implicitly named a..z, A..Z.\n",
" 3 Dimensions are given by the number of features in that dimension.\n",
" -h prints out these instructions.\n",
" -n specifies the n in n-tuple. The default is 2 (meaning pairs).\n",
" -w gives withouts. -w1b4ab says that combining the second feature\n",
" of the first dimension with the first or second feature of the\n",
" fourth dimension is disallowed.\n",
" -ofoo.txt reads old jenny testcases from file foo.txt and extends them.",
"\n\n",
" The output is a testcase per line, one feature per dimension per\n",
" testcase, followed by the list of all allowed tuples that jenny could\n",
" not reach.\n",
"\n",
" Example: jenny -n3 3 2 2 -w2b3b 5 3 -w1c3b4ace5ac 8 2 2 3 2\n",
" This gives ten dimensions, asks that for any three dimensions all\n",
" combinations of features (one feature per dimension) be covered,\n",
" plus it asks that certain combinations of features\n",
" (like (1c,3b,4c,5c)) not be covered.\n",
"\n"
};
/* parse -n, the tuple size */
int parse_n( state *s, char *myarg)
{
ub4 curr = 0;
ub4 temp = UB4MAXVAL;
token_type token;
ub4 dummy;
if ((token=parse_token(myarg, UB4MAXVAL, &curr, &temp)) != TOKEN_NUMBER) {
printf("jenny: -n should give an integer in 1..32, for example, -n2.\n");
return FALSE;
}
if ((token=parse_token(myarg, UB4MAXVAL, &curr, &dummy)) != TOKEN_END) {
printf("jenny: -n should be followed by just an integer\n");
return FALSE;
}
if ((temp < 1) || (temp > 32)) {
printf("jenny: -n says all n-tuples should be covered.\n");
return FALSE;
}
if (temp > s->ndim) {
printf("jenny: -n, %ld-tuples are impossible with only %d dimensions\n",
temp, s->ndim);
return FALSE;
}
s->n_final = (ub2)temp;
return TRUE;
}
/* parse -w, a without */
int parse_w( state *s, sb1 *myarg)
{
without *w;
wchain *wc;
feature fe[MAX_WITHOUT];
ub1 used[MAX_DIMENSIONS];
ub4 dimension_number;
ub4 curr = 0;
ub4 fe_len, value;
ub4 i, j;
size_t len = strlen(myarg);
token_type t = parse_token(myarg, len, &curr, &value);
for (i=0; i<s->ndim; ++i)
used[i] = FALSE;
if (t != TOKEN_NUMBER) {
printf("jenny: -w is <number><features><number><features>...\n");
printf("jenny: -w must start with an integer (1 to #dimensions)\n");
return FALSE;
}
fe_len=0;
number:
dimension_number = --value;
if (dimension_number >= s->ndim) {
printf("jenny: -w, dimension %ld does not exist, ", dimension_number+1);
printf("you gave only %d dimensions\n", s->ndim);
return FALSE;
}
if (used[dimension_number]) {
printf("jenny: -w, dimension %lu was given twice in a single without\n",
dimension_number+1);
return FALSE;
}
used[dimension_number] = TRUE;
switch (parse_token(myarg, len, &curr, &value)) {
case TOKEN_FEATURE: goto feature;
case TOKEN_END:
printf("jenny: -w, withouts must follow numbers with features\n");
return FALSE;
default:
printf("jenny: -w, unexpected without syntax\n");
printf("jenny: proper withouts look like -w2a1bc99a\n");
return FALSE;
}
feature:
if (value >= s->dim[dimension_number]) {
printf("jenny: -w, there is no feature '%c' in dimension %lu\n",
feature_name[value], dimension_number+1);
return FALSE;
}
fe[fe_len].d = dimension_number;
fe[fe_len].f = value;
if (++fe_len >= MAX_WITHOUT) {
printf("jenny: -w, at most %d features in a single without\n",
MAX_WITHOUT);
return FALSE;
}
switch (parse_token(myarg, len, &curr, &value)) {
case TOKEN_FEATURE: goto feature;
case TOKEN_NUMBER: goto number;
case TOKEN_END: goto end;
default:
printf("jenny: -w, unexpected without syntax\n");
printf("jenny: proper withouts look like -w2a1bc99a\n");
return FALSE;
}
end:
/* sort the dimensions and features in this restriction */
for (i=0; i<fe_len; ++i) {
for (j=i+1; j<fe_len; ++j) {
if ((fe[i].d > fe[j].d) ||
((fe[i].d == fe[j].d) && (fe[i].f > fe[j].f))) {
ub2 fe_temp;
fe_temp = fe[i].d;
fe[i].d = fe[j].d;
fe[j].d = fe_temp;
fe_temp = fe[i].f;
fe[i].f = fe[j].f;
fe[j].f = fe_temp;
}
}
}
/* allocate a without */
w = (without *)my_alloc( s, sizeof(without));
wc = (wchain *)my_alloc( s, sizeof(wchain));
wc->next = s->wc2;
wc->w = w;
w->len = fe_len;
w->fe = (feature *)my_alloc( s, sizeof(feature)*fe_len);
for (i=0; i<fe_len; ++i) {
w->fe[i].d = fe[i].d;
w->fe[i].f = fe[i].f;
}
s->wc2 = wc;
return TRUE;
}
/* parse -s, a seed for the random number generator */
int parse_s( state *s, sb1 *myarg)
{
ub4 seed = 0;
ub4 dummy = 0;
ub4 curr = 0;
if (parse_token( myarg, UB4MAXVAL, &curr, &seed) != TOKEN_NUMBER) {
printf("jenny: -s must be followed by a positive integer\n");
return FALSE;
}
if (parse_token( myarg, UB4MAXVAL, &curr, &dummy) != TOKEN_END) {
printf("jenny: -s should give just an integer, example -s123\n");
return FALSE;
}
flearand_init(&s->r, seed); /* initialize random number generator */
return TRUE;
}
void preliminary( state *s)
{
wchain *wc;
ub4 d;
s->tuple_tester = (test *)my_alloc( s, sizeof(test));
s->tuple_tester->f = (ub2 *)my_alloc( s, sizeof(ub2)*s->ndim);
s->dimord = (ub2 *)my_alloc( s, sizeof(ub2)*s->ndim);
s->wc = (wchain **)my_alloc( s, sizeof(wchain *)*s->ndim);
s->tu = (tu_arr ***)my_alloc( s, sizeof(tu_arr **)*s->ndim);
s->one = (tu_arr ***)my_alloc( s, sizeof(tu_arr **)*MAX_TESTS);
s->n = (ub1 **)my_alloc( s, sizeof(ub1 *)*s->ndim);
s->onec = (ub4 **)my_alloc( s, sizeof(ub4 *)*MAX_TESTS);
s->tc = (ub4 **)my_alloc( s, sizeof(ub4 *)*s->ndim);
s->t = (test **)my_alloc( s, sizeof(test *)*MAX_TESTS);
/* initialize to safe values before doing further allocations */
for (d=0; d<s->ndim; ++d) {
s->tuple_tester->f[d] = (ub2)~0;
s->dimord[d] = (ub2)d;
s->wc[d] = (wchain *)0;
s->tu[d] = (tu_arr **)0;
s->n[d] = (ub1 *)0;
s->tc[d] = (ub4 *)0;
}
s->featord = (ub2 *)my_alloc( s, sizeof(ub2)*MAX_FEATURES);
/* allocate roots for feature-specific lists of uncovered tuples */
for (d=0; d<s->ndim; ++d) {
ub2 f;
s->tu[d] = (tu_arr **)my_alloc( s, sizeof(tu_arr *)*s->dim[d]);
s->n[d] = (ub1 *)my_alloc(s, sizeof(ub1)*s->dim[d]);
s->tc[d] = (ub4 *)my_alloc(s, sizeof(ub4)*s->dim[d]);
for (f=0; f<s->dim[d]; ++f) {
s->tu[d][f] = (tu_arr *)0;
s->n[d][f] = 0;
s->tc[d][f] = 0;
}
}
/* make dimension-specific lists of withouts */
for (wc=s->wc2; wc; wc=wc->next) {
without *w = wc->w;
int old = -1;
int i;
for (i=0; i<w->len; ++i) {
if (w->fe[i].d != old) {
wchain *wcx = (wchain *)my_alloc( s, sizeof(wchain));
wcx->w = w;
wcx->next = s->wc[w->fe[i].d];
s->wc[w->fe[i].d] = wcx;
old = w->fe[i].d;
}
}
}
}
/* parse the inputs */
int parse( int argc, char *argv[], state *s)
{
int i, j;
ub4 temp;
char *testfile = (char *)0;
/* internal check: we have MAX_FEATURES names for features */
if (strlen(feature_name) != MAX_FEATURES) {
printf("feature_name length is wrong, %lu\n", strlen(feature_name));
return FALSE;