-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
6368 lines (5374 loc) · 188 KB
/
test.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
/*
* Stranger
* Copyright (C) 2013-2014 University of California Santa Barbara.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335,
* USA.
*
* Authors: Muath Alkhalaf, Fang Yu
*/
#include <stranger/stranger.h>
#include <stranger/stranger_lib_internal.h>
/*********************************************
TEST FUNCTION
*********************************************/
void dfa_init_indices_map_coeffs(int* indices, int* map, int* coeffs,
int numVars) {
int i;
for (i = 0; i < numVars; i++) {
indices[i] = i;
indices[i + numVars] = i + numVars;
map[2 * i] = 2 * i + 1;
map[2 * i + 1] = 2 * i;
coeffs[2 * i] = 0;
coeffs[2 * i + 1] = 0;
}
}
//a is the automaton
//t is the transition relation
DFA* dfa_forward_image(DFA* a, DFA* t, int* map, int numVars) {
int i;
DFA * temp;
temp = dfaProduct(a, t, dfaAND);
//dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(t);
a = dfaMinimize(temp);
dfaFree(temp);
//indices: even is the current state variable, odd is the next statevariable
for (i = 0; i < numVars - 1; i++) {
temp = dfaProject(a, 2 * i);
printf("\n project away the track %d\n", i);
//dfaPrintVerbose(temp);
dfaFree(a);
a = dfaMinimize(temp);
dfaFree(temp);
}
//dfaPrintVerbose(a);
temp = dfaProject(a, 2 * numVars - 2);
dfaFree(a);
//dfaPrintVerbose(temp);
dfaReplaceIndices(temp, map);
//dfaPrintVerbose(temp);
a = dfaMinimize(temp);
dfaFree(temp);
temp = dfaPrefixClose2(a);
//dfaPrintVerbose(temp);
dfaFree(a);
a = dfaMinimize(temp);
dfaFree(temp);
return a;
}
void reset_coeffs(int* coeffs, int var) {
int i;
for (i = 0; i < 2 * var; i++)
coeffs[i] = 0;
}
//Implement Construct(a, stmt)
//a is the automaton
//stmt is specified as lhscoeff*lhs = right is the transition relation
DFA* dfa_forward_image_stmt(DFA* input, int lhs, int lhscoeff,
int* current_coeffs, int constant, int* map, int* indices, int var) {
int i;
int* coeffs;
DFA* a = dfaCopy(input);
DFA* temp;
DFA* tran;
coeffs = (int *) malloc(sizeof(int) * 2 * var);
reset_coeffs(coeffs, var);
coeffs[lhs + 1] = lhscoeff;
for (i = 0; i < var; i++)
coeffs[2 * i] = current_coeffs[2 * i];
tran = build_DFA_eq_2sc(2 * var, coeffs, constant, indices);
temp = dfaProduct(a, tran, dfaAND);
// dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(tran);
a = dfaMinimize(temp);
dfaFree(temp);
reset_coeffs(coeffs, var);
for (i = 0; i < var; i++)
if (i != lhs) {
coeffs[2 * i] = -1;
coeffs[2 * i + 1] = 1;
}
tran = build_DFA_eq_2sc(2 * var, coeffs, 0, indices);
temp = dfaProduct(a, tran, dfaAND);
// dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(tran);
a = dfaMinimize(temp);
dfaFree(temp);
//indices: even is the current state variable, odd is the next statevariable
for (i = 0; i < var - 1; i++) {
temp = dfaProject(a, 2 * i);
// printf("\n project away the track %d\n", i);
// dfaPrintVerbose(temp);
dfaFree(a);
a = dfaMinimize(temp);
dfaFree(temp);
}
//dfaPrintVerbose(a);
temp = dfaProject(a, 2 * var - 2);
dfaFree(a);
// dfaPrintVerbose(temp);
dfaReplaceIndices(temp, map);
// dfaPrintVerbose(temp);
a = dfaMinimize(temp);
dfaFree(temp);
temp = dfaPrefixClose2(a);
// dfaPrintVerbose(temp);
a = dfaMinimize(temp);
dfaFree(temp);
return a;
}
//Implement Construct(a, stmt)
//a is the automaton
//Two stmts are specified as lhscoeff*lhs + right +const =0 and lhscoeff2*lhs2 + current_coeffs2 +const2 =0
// is the transition relation
DFA* dfa_forward_image_two_stmt(DFA* input, int lhs, int lhscoeff,
int* current_coeffs, int constant, int lhs2, int lhscoeff2,
int* current_coeffs2, int constant2, int* map, int* indices, int var) {
int i;
int* coeffs;
DFA* a = dfaCopy(input);
DFA* temp;
DFA* tran;
coeffs = (int *) malloc(sizeof(int) * 2 * var);
reset_coeffs(coeffs, var);
coeffs[lhs + 1] = lhscoeff;
for (i = 0; i < var; i++)
coeffs[2 * i] = current_coeffs[2 * i];
tran = build_DFA_eq_2sc(2 * var, coeffs, constant, indices);
temp = dfaProduct(a, tran, dfaAND);
// dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(tran);
a = dfaMinimize(temp);
dfaFree(temp);
reset_coeffs(coeffs, var);
coeffs[lhs2 + 1] = lhscoeff2;
for (i = 0; i < var; i++)
coeffs[2 * i] = current_coeffs2[2 * i];
tran = build_DFA_eq_2sc(2 * var, coeffs, constant2, indices);
temp = dfaProduct(a, tran, dfaAND);
// dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(tran);
a = dfaMinimize(temp);
dfaFree(temp);
reset_coeffs(coeffs, var);
for (i = 0; i < var; i++)
if (i != lhs || i != lhs2) {
coeffs[2 * i] = -1;
coeffs[2 * i + 1] = 1;
}
tran = build_DFA_eq_2sc(2 * var, coeffs, 0, indices);
temp = dfaProduct(a, tran, dfaAND);
// dfaPrintVerbose(temp);
dfaFree(a);
dfaFree(tran);
a = dfaMinimize(temp);
dfaFree(temp);
//indices: even is the current state variable, odd is the next statevariable
for (i = 0; i < var - 1; i++) {
temp = dfaProject(a, 2 * i);
// printf("\n project away the track %d\n", i);
// dfaPrintVerbose(temp);
dfaFree(a);
a = dfaMinimize(temp);
dfaFree(temp);
}
//dfaPrintVerbose(a);
temp = dfaProject(a, 2 * var - 2);
dfaFree(a);
// dfaPrintVerbose(temp);
dfaReplaceIndices(temp, map);
// dfaPrintVerbose(temp);
a = dfaMinimize(temp);
dfaFree(temp);
temp = dfaPrefixClose2(a);
// dfaPrintVerbose(temp);
a = dfaMinimize(temp);
dfaFree(temp);
return a;
}
//var is equal to the number of string variables + the number of integer variables
void dfa_test_arith(int var) {
int* indices;
int* map;
int* coeffs;
int constant = 4;
DFA *a1 = NULL;
indices = (int *) malloc(sizeof(int) * 2 * var);
map = (int *) malloc(sizeof(int) * 2 * var);
coeffs = (int *) malloc(sizeof(int) * 2 * var);
dfa_init_indices_map_coeffs(indices, map, coeffs, var);
coeffs[0] = 1;
coeffs[2] = 2;
a1 = build_DFA_eq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
//dfaPrintVerbose(a1);
reset_coeffs(coeffs, var);
// coeffs[1]=1;
coeffs[2] = -1;
constant = 0;
// a2 = build_DFA_eq_2sc(2*var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
// dfaPrintVerbose(a2);
// a1 = dfa_forward_image(a1, a2, map, var);
//DFA* dfa_forward_image_stmt(DFA* a, int lhs, int lhscoeff, int* rightcoeffs, int constant, int* map, int* indices, int var)
a1 = dfa_forward_image_stmt(a1, 0, 1, coeffs, 0, map, indices, var);
//dfaPrintVerbose(a1);
}
void dfa_test_length(int var, int *indices) {
DFA *tmp1 = NULL;
DFA *tmp2 = NULL;
DFA *tmp3 = NULL;
DFA *tmp4 = NULL;
DFA *tmp5 = NULL;
DFA *tmp6 = NULL;
DFA *tmp7 = NULL;
DFA *tmp8 = NULL;
DFA *tmp9 = NULL;
DFA *tmp10 = NULL;
DFA *tmp11 = NULL;
DFA *uL = NULL;
DFA *bL = NULL;
char *c1 = "";
char *c2 = "k";
char *c3 = "<ls>\n";
int i = -1;
struct semilinear_type* s;
printf("\n\nTEST WIDEN...............\n");
tmp1 = dfa_construct_string("ab", var, indices);
tmp2 = dfa_construct_string("abab", var, indices);
tmp3 = dfaWiden(tmp1, tmp2);
//dfaPrintVerbose(tmp3);
printf("\n\nSTART TESTING...............\n");
// printf("\n 1. CONSTRUCT baaab as M1\n");
tmp1 = dfa_construct_string("baaab", var, indices);
/* dfaPrintVerbose(tmp1);
printf("\n CONSTRUCT unary DFA and semilinear set of baaab as M1\n");
uL = dfa_string_to_unaryDFA(tmp1, var, indices);
dfaPrintVerbose(uL);
s=getSemilinerSetCoefficients(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
dfaPrintVerbose(bL);
dfaFree(uL);
dfaFree(bL);
free(s);
*/
printf("\n 1. (baaab)+\n");
tmp2 = dfa_closure_extrabit(tmp1, var, indices);
//dfaPrintVerbose(tmp2);
uL = dfa_string_to_unaryDFA(tmp2, var, indices);
printf("\n Unary Length Automaton:\n");
//dfaPrintVerbose(uL);
s = getSemilinerSetCoefficients(uL);
print_semilinear_coefficients(s);
printf("\n Binary Length Automaton:\n");
bL = dfa_semiliner_to_binaryDFA(s);
//dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 2. (baaab)+ab\n");
tmp3 = dfa_construct_string("ab", var, indices);
tmp2 = dfa_concat_extrabit(tmp2, tmp3, var, indices);
uL = dfa_string_to_unaryDFA(tmp2, var, indices);
printf("\n Unary Length Automaton:\n");
s = getSemilinerSetCoefficients(uL);
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
printf("\n Binary Length Automaton:\n");
bL = dfa_semiliner_to_binaryDFA(s);
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 3. CLOSURE of (baaab)+ab\n");
tmp4 = dfa_closure_extrabit(tmp2, var, indices);
uL = dfa_string_to_unaryDFA(tmp4, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
printf("\n Binary Length Automaton:\n");
bL = dfa_semiliner_to_binaryDFA(s);
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 4. (abb)+\n");
tmp3 = dfa_construct_string("abb", var, indices);
tmp3 = dfa_closure_extrabit(tmp3, var, indices);
// dfaPrintVerbose(tmp3);
uL = dfa_string_to_unaryDFA(tmp3, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 5. Union (baaab)+ab and (abb)+\n");
//tmp3=dfa_construct_string("ab", var, indices);
tmp4 = dfa_union(tmp2, tmp3);
uL = dfa_string_to_unaryDFA(tmp4, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 6 CLOSURE of (baaab)+ab | (abb)+\n");
//tmp3=dfa_construct_string("ab", var, indices);
tmp4 = dfa_closure_extrabit(tmp4, var, indices);
uL = dfa_string_to_unaryDFA(tmp4, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 7. (abcd)+\n");
tmp3 = dfa_construct_string("abcd", var, indices);
tmp3 = dfa_closure_extrabit(tmp3, var, indices);
// dfaPrintVerbose(tmp3);
uL = dfa_string_to_unaryDFA(tmp3, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 8. CONCAT (baaab)+ab (abcd)+\n");
tmp4 = dfa_concat_extrabit(tmp2, tmp3, var, indices);
// dfaPrintVerbose(tmp4);
uL = dfa_string_to_unaryDFA(tmp4, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 9. UNION (baaab)+ab(abcd)+ and (baaab)+ab\n");
tmp5 = dfa_union(tmp4, tmp2);
// dfaPrintVerbose(tmp5);
uL = dfa_string_to_unaryDFA(tmp5, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 10. CLOSURE of ((baaab)+ab(abcd)+ | (baaab)+ab)\n");
tmp6 = dfa_closure_extrabit(tmp5, var, indices);
// dfaPrintVerbose(tmp6);
uL = dfa_string_to_unaryDFA(tmp6, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf(
"\n 11. CONCAT ((baaab)+ab(abcd)+ | (baaab)+ab)+ and ((baaab)+ab(abcd)+ | (baaab)+ab)\n");
tmp7 = dfa_concat_extrabit(tmp6, tmp5, var, indices);
// dfaPrintVerbose(tmp7);
uL = dfa_string_to_unaryDFA(tmp7, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 12. Emptiness Checking of 11 \n");
i = check_emptiness(tmp7, var, indices);
printf("\n\t Result[-1:unknown, 0:false, 1:true]: %d \n", i);
printf("\n 13. Equivalence Checking of 10(M+.M) and 9(M+) \n");
i = -1;
i = check_equivalence(tmp7, tmp6, var, indices);
printf("\n\t Result[-1:unknown, 0:false, 1:true]: %d \n", i);
printf("\n 14. UNION M and (M)+.M\n");
tmp8 = dfa_union(tmp7, tmp5);
// dfaPrintVerbose(tmp8);
uL = dfa_string_to_unaryDFA(tmp8, var, indices);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
s = getSemilinerSetCoefficients(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 15. Equivalence Checking on (M u M+.M) and (M+) \n");
i = -1;
i = check_equivalence(tmp8, tmp6, var, indices);
printf("\n\t Result[-1:unknown, 0:false, 1:true]: %d \n", i);
printf("\n 16. Replace test: deletion \n");
tmp9 = dfa_replace_extrabit(tmp2, tmp3, c1, var, indices);
// dfaPrintVerbose(tmp9);
uL = dfa_string_to_unaryDFA(tmp9, var, indices);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
s = getSemilinerSetCoefficients(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 17. Replace test: char \n");
tmp10 = dfa_replace_extrabit(tmp2, tmp3, c2, var, indices);
// dfaPrintVerbose(tmp10);
uL = dfa_string_to_unaryDFA(tmp10, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
printf("\n 18. Replace test: string \n");
tmp11 = dfa_replace_extrabit(tmp2, tmp3, c3, var, indices);
// dfaPrintVerbose(tmp11);
uL = dfa_string_to_unaryDFA(tmp11, var, indices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
bL = dfa_semiliner_to_binaryDFA(s);
printf("\n Binary Length Automaton:\n");
dfaPrintVerbose(bL);
dfaFree(bL);
dfaFree(uL);
free(s);
//printf("\n\nEquivalence: %d\n\n", dfaEquivalence(tmp1, resultDFA));
//dfaPrintVerbose(resultDFA);
printf("free tmp1-8\n");
dfaFree(tmp1);
dfaFree(tmp2);
dfaFree(tmp3);
dfaFree(tmp4);
dfaFree(tmp5);
dfaFree(tmp6);
dfaFree(tmp7);
dfaFree(tmp8);
printf("free tmp9\n");
if (tmp9 != tmp2)
dfaFree(tmp9);
printf("free tmp10\n");
if (tmp10 != tmp2)
dfaFree(tmp10);
printf("free tmp11\n");
if (tmp11 != tmp2)
dfaFree(tmp11);
}
void dfa_test_basic(int var, int *indices) {
DFA *tmp1 = NULL;
DFA *tmp2 = NULL;
DFA *tmp3 = NULL;
DFA *tmp4 = NULL;
DFA *tmp9 = NULL;
DFA *tmp10 = NULL;
DFA *tmp11 = NULL;
char *c1 = "";
char *c2 = "k";
char *c3 = "ddd";
printf("\n\nSTART TESTING...............\n");
printf("\n 1. ac\n");
tmp1 = dfa_construct_string("ac", var, indices);
tmp1 = dfa_construct_range('a', 'z', var, indices);
dfaPrintVerbose(tmp1);
printf("\n 2. (ac)+\n");
tmp2 = dfa_closure_extrabit(tmp1, var, indices);
dfaPrintVerbose(tmp2);
printf("\n 3. b(ac)+b\n");
tmp3 = dfa_construct_string("b", var, indices);
tmp4 = dfa_concat_extrabit(tmp3, tmp2, var, indices);
tmp4 = dfa_concat_extrabit(tmp4, tmp3, var, indices);
dfaPrintVerbose(tmp4);
printf("\n 12. Replace(b(ac)+b, ac, '') test: deletion \n");
tmp9 = dfa_replace_extrabit(tmp4, tmp1, c1, var, indices);
dfaPrintVerbose(tmp9);
printf("\n 12.1 Replace(bac+b, ac+, '') test: deletion closure \n");
tmp9 = dfa_replace_extrabit(tmp4, tmp2, c1, var, indices);
dfaPrintVerbose(tmp9);
printf("\n 13. Replace(bac+b, ac, k) test: char \n");
tmp10 = dfa_replace_extrabit(tmp4, tmp1, c2, var, indices);
dfaPrintVerbose(tmp10);
printf("\n 13.1 Replace(bac+b, ac+, k) test: char closure \n");
tmp10 = dfa_replace_extrabit(tmp4, tmp2, c2, var, indices);
dfaPrintVerbose(tmp10);
printf("\n 14. Replace(bac+b, ac, ddd): string \n");
tmp11 = dfa_replace_extrabit(tmp4, tmp1, c3, var, indices);
dfaPrintVerbose(tmp11);
printf("\n 14.1 Replace(bac+b, ac+, ddd): string closure \n");
tmp11 = dfa_replace_extrabit(tmp4, tmp2, c3, var, indices);
dfaPrintVerbose(tmp11);
//printf("\n\nEquivalence: %d\n\n", dfaEquivalence(tmp1, resultDFA));
//dfaPrintVerbose(resultDFA);
printf("free tmp1-8\n");
dfaFree(tmp1);
dfaFree(tmp2);
dfaFree(tmp3);
dfaFree(tmp4);
/* dfaFree(tmp5);
dfaFree(tmp6);
dfaFree(tmp7);
dfaFree(tmp8);
*/
printf("free tmp9\n");
if (tmp9 != tmp4)
dfaFree(tmp9);
printf("free tmp10\n");
if (tmp10 != tmp4)
dfaFree(tmp10);
printf("free tmp11\n");
if (tmp11 != tmp4)
dfaFree(tmp11);
}
/***********************************************************************************************
Manual Testing Programs
For real C programs (used in TACAS09)
********************************************************************************************************/
/***********************************************************************************************
//This is the test prorgram for the following c codes
// simplified version of strlen() in wu_ftp
unsigned int strlen(char *s){
char *ptr = s;
unsigned int cnt =0;
//a[0], ptr[0]
while(*ptr ! = '\0'){
++ptr;
++cnt;
//fixpoint: ptr = \Sigma^* \wedge ptr.length +cnt = s.length \wedge ptr.length >=0 (\{1+k|k>=0\})
//a[1], ptr[1]
}
assert(cnt==s.length);
//fixpoint: ptr = {} \wedge ptr.length +cnt = s.length \wedge ptr.length ==0
//a[2], ptr[2]
return cnt;
}
********************************************************************************************************/
//var is equal to the number of string variables + the number of integer variables
void dfa_test_strlen() {
int var = 3;
DFA* ptr[3]; //string automaton
DFA* a[3]; //an array of arithmetic automaton
int i, itr;
//arithmetic automaton
int* indices;
int* map;
int* coeffs;
int* coeffs2;
int constant = 0;
DFA* atmp = NULL;
DFA* assert = NULL;
//string automaton
int svar = NUM_ASCII_TRACKS;
int* sindices = allocateAscIIIndexWithExtraBit(NUM_ASCII_TRACKS);
DFA* stmp = NULL; //for fixed point computation
DFA* stmpb = NULL; //for branch condition
int afixflag = 0;
int sfixflag = 0;
//semiliner
DFA* uL = NULL;
struct semilinear_type* s;
for (i = 0; i < 3; i++)
a[i] = NULL;
indices = (int *) malloc(sizeof(int) * 2 * var);
map = (int *) malloc(sizeof(int) * 2 * var);
coeffs = (int *) malloc(sizeof(int) * 2 * var);
coeffs2 = (int *) malloc(sizeof(int) * 2 * var);
dfa_init_indices_map_coeffs(indices, map, coeffs, var);
// 0: ptr.length, 2:s.length, 4:cnt
reset_coeffs(coeffs, var);
coeffs[2] = 1;
coeffs[4] = -1;
constant = 0;
assert = build_DFA_eq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
dfaPrintVerbose(assert);
// char *ptr = s;
reset_coeffs(coeffs, var);
coeffs[0] = 1;
coeffs[2] = -1;
//coeffs[4]=-1;
constant = 0;
a[0] = build_DFA_eq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
dfaPrintVerbose(a[0]);
reset_coeffs(coeffs, var);
coeffs[4] = 1;
constant = 0;
atmp = build_DFA_eq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
a[0] = dfaMinimize(dfaProduct(a[0], atmp, dfaAND));
dfaFree(atmp);
ptr[0] = dfaAllStringASCIIExceptReserveWords(svar, sindices);
//cnt =0;
//reset_coeffs(coeffs, var);
//constant = 0;
//DFA* dfa_forward_image_stmt(DFA* a, int lhs, int lhscoeff, int* rightcoeffs, int constant, int* map, int* indices, int var)
//a[0] = dfaMinimize(dfa_forward_image_stmt(a[0], 4, 1, coeffs, 0, map, indices, var));
dfaPrintVerbose(a[0]);
//while(*ptr!='\0')
stmpb = dfaASCIINotNullString(svar, sindices);
//dfaPrintVerbose(ptr[1]);
//++ptr
atmp = dfaCopy(a[0]);
stmp = dfaCopy(ptr[0]);
itr = 0;
while (1) {
printf("Iteration %d\n", itr);
if (!sfixflag) {
//string analysis
stmp = dfaMinimize(dfaProduct(stmp, stmpb, dfaAND));
//DFA *dfa_Suffix(DFA *M, int c1, int c2, int var, int *oldindices)
ptr[1] = dfaMinimize(dfa_Suffix(stmp, 1, 1, svar, sindices));
ptr[1] = dfaMinimize(dfaProduct(ptr[1], stmp, dfaOR));
printf("DFA STIRNG OR %d:\n", itr);
dfaPrintVerbose(ptr[1]);
stmp = dfaMinimize(dfaWiden(stmp, ptr[1]));
dfaFree(stmp);
stmp = ptr[1];
if (check_inclusion(stmp, ptr[1], 2 * var, indices)) {
sfixflag = 1;
dfaFree(ptr[1]);
ptr[1] = stmp;
printf("String Reaching a fixed point at %d iteration!\n", itr);
} else
dfaFree(ptr[1]);
}
if (!afixflag) {
//arithmetic analysis
//-ptr'+ptr - 1 = 0; -cnt'+cnt+1 =0;
reset_coeffs(coeffs, var);
coeffs[0] = 1;
constant = -1;
reset_coeffs(coeffs2, var);
coeffs2[4] = 1;
a[1] = dfaMinimize(
dfa_forward_image_two_stmt(atmp, 0, -1, coeffs, constant, 4,
-1, coeffs2, 1, map, indices, var));
printf("DFA Post %d:\n", itr);
dfaPrintVerbose(a[1]);
/*
//-ptr'+ptr - 1 = 0;
reset_coeffs(coeffs, var);
coeffs[0] = 1;
constant = -1;
//DFA* dfa_forward_image_stmt(DFA* a, int lhs, int lhscoeff, int* othercoeffs, int constant, int* map, int* indices, int var)
a[1] = dfaMinimize(dfa_forward_image_stmt(atmp, 0, -1, coeffs, constant, map, indices, var));
//dfaPrintVerbose(a[1]);
//++cnt;
//-cnt'+cnt + 1 =0;
reset_coeffs(coeffs, var);
coeffs[4] = 1;
constant = 1;
//DFA* dfa_forward_image_stmt(DFA* a, int lhs, int lhscoeff, int* rightcoeffs, int constant, int* map, int* indices, int var)
a[1] = dfaMinimize(dfa_forward_image_stmt(a[1], 4, -1, coeffs, constant, map, indices, var));
//a[1] = dfaMinimize(dfa_forward_image_two_stmt(atmp, 0, -1, coeffs, constant, 4, -1, coeffs2, 1, map, indices, var));
printf("DFA Post %d:\n", itr);
dfaPrintVerbose(a[1]);
*/
// alternative for forward_image
//alternative for widening
//atmp = dfaProduct(a[1], atmp, dfaOR);
a[1] = dfaMinimize(dfaProduct(a[1], atmp, dfaOR));
printf("DFA OR %d:\n", itr);
dfaPrintVerbose(a[1]);
atmp = dfaMinimize(dfaWiden(atmp, a[1]));
//atmp = dfaMinimize(dfaWiden(a[1], atmp));
printf("DFA Widen %d:\n", itr);
dfaPrintVerbose(atmp);
if (check_inclusion(atmp, a[1], 2 * var, indices)) {
afixflag = 1;
dfaFree(a[1]);
a[1] = atmp;
printf("Arith Reaching a fixed point at %d iteration!\n", itr);
} else {
dfaFree(a[1]);
}
}
if (afixflag && sfixflag)
break;
else {
itr++;
if (itr >= 5) {
if (!afixflag)
a[1] = atmp;
if (!sfixflag)
ptr[1] = stmp;
//printf("Out of bound %d, under approximation a fixed point!\n", 5);
break;
}
}
}
ptr[2] = dfaMinimize(
dfaProduct(ptr[0], dfaASCIIOnlyNullString(svar, sindices), dfaAND));
dfaPrintVerbose(ptr[2]);
uL = dfa_string_to_unaryDFA(ptr[2], svar, sindices);
s = getSemilinerSetCoefficients(uL);
printf("\n Unary Length Automaton:\n");
dfaPrintVerbose(uL);
print_semilinear_coefficients(s);
reset_coeffs(coeffs, var);
coeffs[0] = 1;
constant = 0;
atmp = build_DFA_eq_2sc(2 * var, coeffs, constant, indices); //Constructs a DFA for the equation coeffs*variables+constant=0
a[2] = dfaMinimize(dfaProduct(a[1], atmp, dfaAND));
dfaPrintVerbose(a[2]);
if (check_inclusion(a[2], assert, 2 * var, indices)) {
if (afixflag)
printf("Assertion Proven!\n");
else
printf("Assertion not violated within %d iteration", itr);
} else {
printf("Assertion Violated!\n");
}
printf("Memory Allocated: %d\n", mem_allocated());
for (i = 0; i < 3; i++)
dfaFree(a[i]);
dfaFree(assert);
}
/***********************************************************************************************
//This is the test prorgram for the following c codes
// strrchr returns the substring from the last occurence of c in s
//assertion(rlt \in c\Sigma* \cup {\epsilon})
char* strrchr(char *s, char c){
char *rlt = NULL;
//r[0]
while(*s ! = '\0'){
if(*s=c)
rlt = s; //r[1]
s++;
//fixpoint: ptr = \Sigma^* \wedge ptr.length +cnt = s.length \wedge ptr.length >=0 (\{1+k|k>=0\})
// r[2]
}
assert(*rlt \in c(\Sigma-c)*);
//fixpoint: ptr = {} \wedge ptr.length +cnt = s.length \wedge ptr.length ==0
//r[3]
return rlt;
}
********************************************************************************************************/
//var is equal to the number of string variables + the number of integer variables
void dfa_test_strrchr(int n) {
DFA* rlt = NULL; //string automaton
DFA* s = NULL; //an array of arithmetic automaton
//string automaton
int svar = NUM_ASCII_TRACKS;
int* sindices = allocateAscIIIndexWithExtraBit(NUM_ASCII_TRACKS);
DFA* stmp1 = NULL; //for fixed point computation
DFA* stmpb = NULL; //for while branch condition
DFA* stmpc = NULL; //for if branch condition
DFA* tmp = NULL;
int sfixflag = 0;
DFA* assert = NULL;
int itr = 0;
rlt = dfaOnlyNullString(svar, sindices);
//while(*s!='\0')
stmpb = dfaASCIINotNullString(svar, sindices);
stmpc = dfa_concat_extrabit(dfa_construct_string("c", svar, sindices),
dfaAllStringASCIIExceptReserveWords(svar, sindices), svar,
sindices);
assert = dfaMinimize(dfaProduct(rlt, stmpc, dfaOR));
//arbitrary input
s = dfaAllStringASCIIExceptReserveWords(svar, sindices);
while (itr < n) {
printf("Iteration %d:\n", itr);
stmp1 = dfaMinimize(dfaProduct(s, stmpb, dfaAND));
stmp1 = dfaMinimize(dfaProduct(stmp1, stmpc, dfaAND));
tmp = dfaMinimize(dfa_Prefix(stmp1, 1, 1, svar, sindices));
if (check_inclusion(tmp, dfa_construct_string("c", svar, sindices),
svar, sindices))
printf("Prefix CORRECT!\n");
else