29b449fec94cc37777ea74d0bdfe93c51c099aee
[openssl.git] / test / recipes / 10-test_bn.t
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Math::BigInt;
7
8 use OpenSSL::Test qw/:DEFAULT top_file/;
9
10 setup("test_bn");
11
12 plan tests => 3;
13
14 require_ok(top_file("test","recipes","bc.pl"));
15
16 my $testresults = "tmp.bntest";
17 my $init = ok(run(test(["bntest"], stdout => $testresults)), 'initialize');
18
19  SKIP: {
20      skip "Initializing failed, skipping", 1 if !$init;
21
22      subtest 'Checking the bn results' => sub {
23          my @lines = ();
24          if (open DATA, $testresults) {
25              @lines = <DATA>;
26              close DATA;
27          }
28          chomp(@lines);
29
30          plan tests => scalar grep(/^print /, @lines);
31
32          my $l = "";
33
34          while (scalar @lines) {
35              $l = shift @lines;
36
37              last if $l =~ /^print /;
38          }
39
40          while (1) {
41              $l =~ s/^print "//;
42              $l =~ s/\\n"//;
43              my $t = $l;
44              my @operations = ();
45
46              $l = undef;
47              while (scalar @lines) {
48                  $l = shift @lines;
49
50                  last if $l =~ /^print /;
51                  push @operations, $l;
52                  $l = undef;
53              }
54
55              ok(check_operations(@operations), "verify $t");
56
57              last unless $l;
58          }
59      };
60 }
61
62 sub check_operations {
63     my $failcount = 0;
64
65     foreach my $line (@_) {
66         my $result = calc(split /\s+/, $line);
67
68         if ($result ne "0" && $result ne "0x0") {
69             $failcount++;
70             print STDERR "Failed! $line => $result\n";
71         }
72     }
73
74     return $failcount == 0;
75 }