This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patht_chkrat.pas
985 lines (904 loc) · 21.3 KB
/
t_chkrat.pas
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
{Test program for MPArith/mp_rat, (c) W.Ehrhardt 2004-2012}
{Many test cases derived from M.J. Fromberger's IMath }
{Some test files are calculated with ARIBAS or Pari/GP}
program t_chkrat;
{$i STD.INC}
{$i mp_conf.inc}
{$x+} {pchar I/O}
{$i+} {RTE on I/O error}
{$ifndef FPC}
{$N+}
{$endif}
{$ifdef APPCONS}
{$apptype console}
{$endif}
uses
BTypes,mp_types, mp_base,
{$ifdef WINCRT}
WinCRT,
{$endif}
{$ifdef MPC_Diagnostic}
mp_supp,
{$endif}
mp_ratio;
var
a,b,c,t: mp_rat;
x,y: mp_int;
const
LMAX=32000;
var
tf: text;
totalfailed: longint;
line: array[0..LMAX] of char8;
const
Prefix = 'm#';
const
fn_add = Prefix+'radd.dat';
fn_div = Prefix+'rdiv.dat';
fn_expt = Prefix+'rexpt.dat';
fn_inv = Prefix+'rdiv.dat';
fn_mul = Prefix+'rmul.dat';
fn_sub = Prefix+'rsub.dat';
fn_flt = Prefix+'rflt.dat';
{---------------------------------------------------------------------------}
function open_file(const name: mp_string): boolean;
{-Open data file (with error message), true if error}
begin
{$i-}
assign(tf,string(name));
reset(tf);
{$i+}
open_file := false;
if IOResult<>0 then begin
writeln('File reset error: ', name);
open_file := true;
end;
end;
{---------------------------------------------------------------------------}
procedure readline;
{-Read a line into buffer}
var
c: char8;
i: integer;
begin
readln(tf,line);
for i:=0 to sizeof(line)-1 do begin
c := line[i];
if (c=#10) or (c=#13) or (c=#0) then begin
line[i] := #0;
break;
end;
end;
end;
{---------------------------------------------------------------------------}
procedure add_test;
var
i,f: integer;
begin
writeln('mpr_add test: ', fn_add);
if open_file(fn_add) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
case random(3) of
0: begin
mpr_copy(a,t);
mpr_add(t,b,t);
end;
1: begin
mpr_copy(b,t);
mpr_add(a,t,t);
end;
2: mpr_add(a,b,t);
end;
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i);
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure sub_test;
var
i,f: integer;
begin
writeln('mpr_sub test: ', fn_sub);
if open_file(fn_sub) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
case random(3) of
0: begin
mpr_copy(a,t);
mpr_sub(t,b,t);
end;
1: begin
mpr_copy(b,t);
mpr_sub(a,t,t);
end;
2: mpr_sub(a,b,t);
end;
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
end;
inc(i)
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure mul_test;
var
i,f: integer;
begin
writeln('mpr_mul test: ', fn_mul);
if open_file(fn_mul) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
case random(3) of
0: begin
mpr_copy(a,t);
mpr_mul(t,b,t);
end;
1: begin
mpr_copy(b,t);
mpr_mul(a,t,t);
end;
2: mpr_mul(a,b,t);
end;
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
inc(f);
end;
inc(i)
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure div_test;
var
i,f: integer;
begin
writeln('mpr_div test: ', fn_div);
if open_file(fn_div) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
case random(3) of
0: begin
mpr_copy(a,t);
mpr_div(t,b,t);
end;
1: begin
mpr_copy(b,t);
mpr_div(a,t,t);
end;
2: mpr_div(a,b,t);
end;
if not mpr_is_eq(c,t) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i)
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure add1_test;
var
i,f: integer;
begin
writeln('mpr_add1 test: ', fn_add);
if open_file(fn_add) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if mp_is1(b.den) then begin
mpr_add_mpi(a,b.num,t);
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i);
end;
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure sub1_test;
var
i,f: integer;
begin
writeln('mpr_sub1 test: ', fn_sub);
if open_file(fn_sub) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if mp_is1(b.den) then begin
mpr_sub_mpi(a, b.num,t);
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
end;
inc(i)
end
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure mul1_test;
var
i,f: integer;
begin
writeln('mpr_mul1 test: ', fn_mul);
if open_file(fn_mul) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if mp_is1(b.den) then begin
mpr_mul_mpi(a,b.num,t);
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
inc(f);
end;
inc(i)
end;
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure div1_test;
var
i,f: integer;
begin
writeln('mpr_div1 test: ', fn_div);
if open_file(fn_div) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if mp_is1(b.den) then begin
mpr_div_mpi(a,b.num,t);
if not mpr_is_eq(c,t) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i)
end;
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure divint_test;
var
i,f: integer;
k: longint;
begin
writeln('mpr_div_int test: ', fn_div);
if open_file(fn_div) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if (mp_is1(b.den)) and mp_is_longint(b.num,k) then begin
mpr_div_int(a,k,t);
if not mpr_is_eq(c,t) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' k: ', k);
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i)
end;
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure mulint_test;
var
i,f: integer;
k: longint;
begin
writeln('mpr_mul_int test: ', fn_mul);
if open_file(fn_mul) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
if (mp_is1(b.den)) and mp_is_longint(b.num,k) then begin
mpr_mul_int(a,k,t);
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' k: ', k);
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
inc(f);
end;
inc(i)
end;
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure expt_test;
var
i,f: integer;
x: longint;
begin
writeln('mpr_expt test: ', fn_expt);
if open_file(fn_expt) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
{write(i,#13);}
readln(tf);
readline;
mpr_read_radix(a,line,10);
readln(tf, x);
readline;
mpr_read_radix(c,line,10);
case random(2) of
0: begin
mpr_copy(a,t);
mpr_expt(t,x,t);
end;
1: mpr_expt(a,x,t);
end;
if not mpr_is_eq(t,c) then begin
writeln('Failed! #',i);
inc(f);
end;
inc(i)
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure inv_test;
var
i,f: integer;
begin
{use div data for inv test: a/b = a*t = c with t=inv(b)}
writeln('mpr_inv test: ', fn_inv);
if open_file(fn_inv) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
case random(2) of
0: begin
mpr_copy(b,t);
mpr_inv(t,t);
end;
1: begin
mpr_inv(b,t);
end;
end;
mpr_mul(a,t,t);
if not mpr_is_eq(c,t) then begin
writeln('Failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' t: ', mpr_decimal(t));
end;
inc(i)
end;
close(tf);
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure tofloat_test;
var
i,f: integer;
radix, prec: word;
t1,t2,r1,r2,tt: mp_string;
begin
writeln('mpr_tofloat test: ', fn_flt);
if open_file(fn_flt) then exit;
i:=1;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readln(tf, radix);
readln(tf, prec);
readln(tf, t1);
readln(tf, r1);
mp_roundfloat := false;
t2 := mpr_tofloat_str(a, radix, prec);
tt := mpr_tofloat_str(a, radix, prec+10);
mp_roundfloat := true;
r2 := mpr_tofloat_str(a, radix, prec);
if (t1<>t2) or (r1<>r2) then begin
writeln('Failed! #',i);
writeln(' a: ', mpr_decimal(a));
writeln(' r: ', radix);
writeln(' p: ', prec);
writeln('t1: ', t1);
writeln('t2: ', t2);
writeln('r1: ', r1);
writeln('r2: ', r2);
writeln('tt: ', tt);
writeln('CM:', mp_ucrmap[radix-1], ' CH:', mp_ucrmap[radix div 2]);
inc(f);
end;
if i and 63 = 0 then write('.');
inc(i)
end;
close(tf);
writeln;
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure testradixconv;
{-Test input/output radix conversion}
var
cnt,f, radix,digs: word;
x,y: mp_int;
{$ifndef BIT16}
const dmax=1000;
var astr: ansistring;
{$else}
{$ifdef IN_IDE}
const dmax=100;
{$else}
const dmax=200;
{$endif}
{$endif}
begin
mp_init2(x,y);
writeln('Test radix conversion (MaxRadix=',MaxRadix,'): ');
f := 0;
cnt := 0;
for radix:=2 to MaxRadix do begin
digs := 2;
while digs<=dmax do begin
mp_rand_radix(x,radix,1+random(digs));
if random(2)=0 then mp_chs(x,x);
mp_rand_radix(y,radix,1+random(digs));
mpr_set(a,x,y);
mp_show_plus := random(2)=0;
mp_uppercase := random(2)=0;
mpr_toradix_n(a, line, radix, LMAX);
{$ifndef BIT16}
astr := mpr_radix_astr(a,radix);
mpr_read_radix(b, pchar8(astr),radix);
inc(cnt);
if mpr_is_ne(a,b) then begin
writeln('Diff for mpr_radix_astr: Radix=',radix, ' digits=',digs);
write('a='); mpr_output_radix(a,radix); writeln;
write('b='); mpr_output_radix(b,radix); writeln;
inc(f);
end;
{$endif}
inc(cnt);
mpr_toradix_n(a, line, radix, LMAX);
mpr_read_radix(b,line,radix);
if mpr_is_ne(a,b) then begin
writeln('Diff mpr_toradix_n: Radix=',radix, ' digits=',digs);
write('a='); mpr_output_radix(a,radix); writeln;
write('b='); mpr_output_radix(b,radix); writeln;
inc(f);
end;
digs := 2 + 14*digs div 10;
end;
write('.');
end;
mp_clear2(x,y);
writeln;
writeln('No. of checks: ',cnt,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure testfloatconv;
{-Test input/output radix conversion}
var
cnt,f, radix,digs,prec: word;
x,y: mp_int;
{$ifndef BIT16}
const dmax=2000;
{$else}
{$ifdef IN_IDE}
const dmax=200;
{$else}
const dmax=400;
{$endif}
{$endif}
begin
mp_init2(x,y);
writeln('Test float conversion: ');
f := 0;
cnt := 0;
for radix:=2 to MaxRadix do begin
digs := 2;
while digs<=dmax do begin
inc(cnt);
mp_roundfloat := random(2)=1;
mp_rand_radix(x,radix,1+random(digs));
if random(2)=0 then mp_chs(x,x);
prec := 1+random(digs);
mp_rand_radix(y,radix,prec);
mpr_set(a,x,y);
mp_show_plus := random(2)=0;
mp_uppercase := random(2)=0;
mpr_tofloat_n(a, line, radix, prec, LMAX);
mpr_read_float_radix(b,line,radix);
mpr_sub(a,b,c);
mpr_abs(c,c);
mp_set(t.num,1);
mp_set_pow(t.den, radix, prec);
if mp_roundfloat then mp_shl(t.den, 1, t.den);
if mpr_is_gt(c,t) then begin
writeln('Diff Radix=',radix, ' digits=',digs);
write('a='); mpr_output_radix(a,radix); writeln;
write('b='); mpr_output_radix(b,radix); writeln;
write('c='); writeln(mpr_tofloat_str(c,radix,prec+3));
write('t='); writeln(mpr_tofloat_str(t,radix,prec+3));
writeln('float=',line);
inc(f);
end;
digs := 2 + 14*digs div 10;
end;
write('.');
end;
mp_clear2(x,y);
writeln;
writeln('No. of checks: ',cnt,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure t_read_double;
const
EpsDbl = 2.220446049250313E-16; { 2^(-52) }
MaxDbl = 1.797693134862315E+308; { 2^1024 }
MinDbl = 2.225073858507202E-308; { 2^(-1022) }
var
i,e,f,cnt: integer;
h: double;
procedure onetest(d: double);
var
g: double;
begin
inc(cnt);
mpr_read_double(a,d);
g := mpr_todouble(a);
if abs(d-g)>EpsDbl*abs(d) then begin
writeln('d=',d, ' g=',g);
inc(f);
end;
end;
begin
writeln('Test read_double: ');
f := 0;
cnt := 0;
onetest(0.0);
onetest(EpsDbl);
onetest(MaxDbl);
onetest(MinDbl);
onetest(-EpsDbl);
onetest(-MaxDbl);
onetest(-MinDbl);
for i:=1 to 1000 do begin
h := random-0.5;
e := random(2040);
h := ldexpd(h,e-1020);
if (abs(h)>=MinDbl) and (h<=MaxDbl) then onetest(h);
end;
writeln('No. of checks: ',cnt,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
{---------------------------------------------------------------------------}
procedure test_checksum;
{-adler self test and checksums for i/2, i=-2..2}
const
vec: array[1..3] of byte = ($11,$22,$33);
{$ifdef MP_32BIT}
TV: array[-2..2] of longint = ($540006,$580007,$1E0003,$480006,$440005); {4 byte mp_digits}
{$else}
TV: array[-2..2] of longint = ($400006,$420007,$180003,$360006,$340005); {2 byte mp_digits}
{$endif}
var
i,f: integer;
A1, AF: longint;
begin
AF := 1;
A1 := 1;
f := 0;
s_mp_checksum(AF, @vec, sizeof(vec));
for i:=1 to sizeof(vec) do s_mp_checksum(A1, @vec[i], 1);
if (A1<>$00AD0067) or (AF<>$00AD0067) then begin
writeln('Check of s_mp_checksum failed!');
inc(f);
end;
for i:=-2 to 2 do begin
mpr_set_int(a,i,2);
AF := mpr_checksum(a);
if AF<>TV[i] then inc(f);
end;
if f<>0 then begin
writeln('No. of mp_checksum test failed: ',f);
inc(totalfailed,f);
end;
end;
{---------------------------------------------------------------------------}
procedure divmul2k_test;
var
i,f: integer;
k: longint;
{$ifdef BIT16}
const
KR=15000;
{$else}
const
KR=60000;
{$endif}
begin
writeln('mpr_div/mul_2k test: ', fn_add);
if open_file(fn_add) then exit;
i:=0;
f:=0;
while not eof(tf) do begin
readln(tf);
readline;
mpr_read_radix(a,line,10);
readline;
mpr_read_radix(b,line,10);
readline;
mpr_read_radix(c,line,10);
k := random(KR);
k := k - (KR div 2);
mp_2expt(x,abs(k));
if i and 7 = 0 then begin
if k<0 then write('-') else write('+');
end;
inc(i);
mpr_mul_2k(c,k,a);
if k<0 then mpr_div_mpi(c,x,b)
else mpr_mul_mpi(c,x,b);
if not mpr_is_eq(a,b) then begin
writeln('mpr_mul_2k failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' k: ', k);
end;
inc(i);
mpr_div_2k(c,k,a);
if k<0 then mpr_mul_mpi(c,x,b)
else mpr_div_mpi(c,x,b);
if not mpr_is_eq(a,b) then begin
writeln('mpr_div_2k failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
writeln(' k: ', k);
end;
inc(i);
mpr_mul_2(c,a);
mpr_add(c,c,b);
if not mpr_is_eq(a,b) then begin
writeln('mpr_mul_2 failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
end;
inc(i);
mpr_div_2(c,a);
mpr_div_int(c,2,b);
if not mpr_is_eq(a,b) then begin
writeln('mpr_div_2 failed! #',i);
inc(f);
writeln(' a: ', mpr_decimal(a));
writeln(' b: ', mpr_decimal(b));
writeln(' c: ', mpr_decimal(c));
end;
end;
close(tf);
writeln;
writeln('No. of checks: ',i,', failed: ',f);
inc(totalfailed,f);
writeln;
end;
label
done;
begin
writeln('Test of MPArith V', MP_VERSION, ' [mp_ratio] (c) 2007-2012 W.Ehrhardt ');
writeln('MAXDigits = ',MAXDigits, ', MP_MAXBIT = ',MP_MAXBIT, ', MAXRadix = ', MAXRadix);
writeln('Karatsuba cutoffs: mul/sqr = ',mp_mul_cutoff,'/',mp_sqr_cutoff);
writeln('Toom-3, BZ cutoffs: mul/sqr = ',mp_t3m_cutoff,'/',mp_t3s_cutoff, ', div = ',mp_bz_cutoff);
writeln;
mpr_init4(a,b,c,t);
mp_init2(x,y);
totalfailed := 0;
testradixconv;
if totalfailed<>0 then begin
writeln('Failures in testradixconv! Program aborted!');
goto done;
end;
test_checksum;
testfloatconv;
mp_show_plus := false;
mp_uppercase := true;
mp_roundfloat := true;
tofloat_test;
mp_roundfloat := false;
mp_uppercase := false;
t_read_double;
add_test;
sub_test;
mul_test;
div_test;
add1_test;
sub1_test;
mul1_test;
div1_test;
mulint_test;
divint_test;
divmul2k_test;
inv_test;
expt_test;
done:
mp_clear2(x,y);
mpr_clear4(a,b,c,t);
if totalfailed<>0 then begin
writeln('************************');
writeln('****** Total failed: '#7#7#7, totalfailed);
writeln('************************');
end
else writeln('All test OK: no failures');
writeln;
{$ifdef MPC_Diagnostic}
mp_dump_meminfo;
mp_dump_diagctr;
{$endif}
end.