Blame t/author-lib-arithmetic-binary-_div.t

Packit Service 3bf9d6
#!perl
Packit Service 3bf9d6
Packit Service 3bf9d6
BEGIN {
Packit Service 3bf9d6
    unless ($ENV{AUTHOR_TESTING}) {
Packit Service 3bf9d6
        require Test::More;
Packit Service 3bf9d6
        Test::More::plan(skip_all =>
Packit Service 3bf9d6
                         'these tests are for testing by the author');
Packit Service 3bf9d6
    }
Packit Service 3bf9d6
}
Packit Service 3bf9d6
Packit Service 3bf9d6
use strict;
Packit Service 3bf9d6
use warnings;
Packit Service 3bf9d6
Packit Service 3bf9d6
use Test::More tests => 3997;
Packit Service 3bf9d6
Packit Service 3bf9d6
###############################################################################
Packit Service 3bf9d6
# Read and load configuration file and backend library.
Packit Service 3bf9d6
Packit Service 3bf9d6
use Config::Tiny ();
Packit Service 3bf9d6
Packit Service 3bf9d6
my $config_file = 't/author-lib.ini';
Packit Service 3bf9d6
my $config = Config::Tiny -> read('t/author-lib.ini')
Packit Service 3bf9d6
  or die Config::Tiny -> errstr();
Packit Service 3bf9d6
Packit Service 3bf9d6
# Read the library to test.
Packit Service 3bf9d6
Packit Service 3bf9d6
our $LIB = $config->{_}->{lib};
Packit Service 3bf9d6
Packit Service 3bf9d6
die "No library defined in file '$config_file'"
Packit Service 3bf9d6
  unless defined $LIB;
Packit Service 3bf9d6
die "Invalid library name '$LIB' in file '$config_file'"
Packit Service 3bf9d6
  unless $LIB =~ /^[A-Za-z]\w*(::\w+)*\z/;
Packit Service 3bf9d6
Packit Service 3bf9d6
# Read the reference type(s) the library uses.
Packit Service 3bf9d6
Packit Service 3bf9d6
our $REF = $config->{_}->{ref};
Packit Service 3bf9d6
Packit Service 3bf9d6
die "No reference type defined in file '$config_file'"
Packit Service 3bf9d6
  unless defined $REF;
Packit Service 3bf9d6
die "Invalid reference type '$REF' in file '$config_file'"
Packit Service 3bf9d6
  unless $REF =~ /^[A-Za-z]\w*(::\w+)*\z/;
Packit Service 3bf9d6
Packit Service 3bf9d6
# Load the library.
Packit Service 3bf9d6
Packit Service 3bf9d6
eval "require $LIB";
Packit Service 3bf9d6
die $@ if $@;
Packit Service 3bf9d6
Packit Service 3bf9d6
###############################################################################
Packit Service 3bf9d6
Packit Service 3bf9d6
can_ok($LIB, '_div');
Packit Service 3bf9d6
Packit Service 3bf9d6
my $scalar_util_ok = eval { require Scalar::Util; };
Packit Service 3bf9d6
Scalar::Util -> import('refaddr') if $scalar_util_ok;
Packit Service 3bf9d6
Packit Service 3bf9d6
diag "Skipping some tests since Scalar::Util is not installed."
Packit Service 3bf9d6
  unless $scalar_util_ok;
Packit Service 3bf9d6
Packit Service 3bf9d6
my @data;
Packit Service 3bf9d6
Packit Service 3bf9d6
# Small numbers.
Packit Service 3bf9d6
Packit Service 3bf9d6
for (my $x = 0; $x <= 24 ; ++ $x) {
Packit Service 3bf9d6
    for (my $y = 1; $y <= 24 ; ++ $y) {
Packit Service 3bf9d6
        push @data, [ $x, $y, int($x / $y), $x % $y ];
Packit Service 3bf9d6
    }
Packit Service 3bf9d6
}
Packit Service 3bf9d6
Packit Service 3bf9d6
# Add data in data file.
Packit Service 3bf9d6
Packit Service 3bf9d6
(my $datafile = $0) =~ s/\.t/.dat/;
Packit Service 3bf9d6
open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
Packit Service 3bf9d6
while (<DATAFILE>) {
Packit Service 3bf9d6
    s/\s+\z//;
Packit Service 3bf9d6
    next if /^#/ || ! /\S/;
Packit Service 3bf9d6
    push @data, [ split /:/ ];
Packit Service 3bf9d6
}
Packit Service 3bf9d6
close DATAFILE or die "$datafile: can't close file after reading: $!";
Packit Service 3bf9d6
Packit Service 3bf9d6
# List context.
Packit Service 3bf9d6
Packit Service 3bf9d6
for (my $i = 0 ; $i <= $#data ; ++ $i) {
Packit Service 3bf9d6
    my ($in0, $in1, $out0, $out1) = @{ $data[$i] };
Packit Service 3bf9d6
Packit Service 3bf9d6
    my ($x, $y, @got);
Packit Service 3bf9d6
Packit Service 3bf9d6
    my $test = qq|\$x = $LIB->_new("$in0"); |
Packit Service 3bf9d6
             . qq|\$y = $LIB->_new("$in1"); |
Packit Service 3bf9d6
             . qq|\@got = $LIB->_div(\$x, \$y);|;
Packit Service 3bf9d6
Packit Service 3bf9d6
    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
Packit Service 3bf9d6
Packit Service 3bf9d6
    eval $test;
Packit Service 3bf9d6
    is($@, "", "'$test' gives emtpy \$\@");
Packit Service 3bf9d6
Packit Service 3bf9d6
    subtest "_div() in list context: $test", sub {
Packit Service 3bf9d6
        plan tests => $scalar_util_ok ? 13 : 11;
Packit Service 3bf9d6
Packit Service 3bf9d6
        cmp_ok(scalar @got, '==', 2,
Packit Service 3bf9d6
               "'$test' gives two output args");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($got[0]), $REF,
Packit Service 3bf9d6
           "'$test' first output arg is a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_check($got[0]), 0,
Packit Service 3bf9d6
           "'$test' first output arg is valid");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_str($got[0]), $out0,
Packit Service 3bf9d6
           "'$test' first output arg has the right value");
Packit Service 3bf9d6
Packit Service 3bf9d6
        isnt(refaddr($got[0]), refaddr($y),
Packit Service 3bf9d6
             "'$test' first output arg is not the second input arg")
Packit Service 3bf9d6
          if $scalar_util_ok;
Packit Service 3bf9d6
Packit Service 3bf9d6
      SKIP: {
Packit Service 3bf9d6
            skip "$LIB doesn't use real objects", 1
Packit Service 3bf9d6
              if $LIB eq 'Math::BigInt::FastCalc';
Packit Service 3bf9d6
Packit Service 3bf9d6
            is(ref($got[1]), $REF,
Packit Service 3bf9d6
               "'$test' second output arg is a $REF");
Packit Service 3bf9d6
        }
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_check($got[1]), 0,
Packit Service 3bf9d6
           "'$test' second output arg is valid");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_str($got[1]), $out1,
Packit Service 3bf9d6
           "'$test' second output arg has the right value");
Packit Service 3bf9d6
Packit Service 3bf9d6
        isnt(refaddr($got[1]), refaddr($y),
Packit Service 3bf9d6
             "'$test' second output arg is not the second input arg");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($x), $REF,
Packit Service 3bf9d6
           "'$test' first input arg is still a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
Packit Service 3bf9d6
           "'$test' first input arg has the correct value");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($y), $REF,
Packit Service 3bf9d6
           "'$test' second input arg is still a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_str($y), $in1,
Packit Service 3bf9d6
           "'$test' second input arg is unmodified");
Packit Service 3bf9d6
    };
Packit Service 3bf9d6
}
Packit Service 3bf9d6
Packit Service 3bf9d6
# Scalar context.
Packit Service 3bf9d6
Packit Service 3bf9d6
for (my $i = 0 ; $i <= $#data ; ++ $i) {
Packit Service 3bf9d6
    my ($in0, $in1, $out0, $out1) = @{ $data[$i] };
Packit Service 3bf9d6
Packit Service 3bf9d6
    my ($x, $y, $got);
Packit Service 3bf9d6
Packit Service 3bf9d6
    my $test = qq|\$x = $LIB->_new("$in0"); |
Packit Service 3bf9d6
             . qq|\$y = $LIB->_new("$in1"); |
Packit Service 3bf9d6
             . qq|\$got = $LIB->_div(\$x, \$y);|;
Packit Service 3bf9d6
Packit Service 3bf9d6
    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
Packit Service 3bf9d6
Packit Service 3bf9d6
    eval $test;
Packit Service 3bf9d6
    is($@, "", "'$test' gives emtpy \$\@");
Packit Service 3bf9d6
Packit Service 3bf9d6
    subtest "_div() in scalar context: $test", sub {
Packit Service 3bf9d6
        plan tests => $scalar_util_ok ? 8 : 7;
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($got), $REF,
Packit Service 3bf9d6
           "'$test' output arg is a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_check($got), 0,
Packit Service 3bf9d6
           "'$test' output is valid");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_str($got), $out0,
Packit Service 3bf9d6
           "'$test' output arg has the right value");
Packit Service 3bf9d6
Packit Service 3bf9d6
        isnt(refaddr($got), refaddr($y),
Packit Service 3bf9d6
             "'$test' output arg is not the second input arg")
Packit Service 3bf9d6
          if $scalar_util_ok;
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($x), $REF,
Packit Service 3bf9d6
           "'$test' first input arg is still a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
Packit Service 3bf9d6
           "'$test' first input arg has the correct value");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is(ref($y), $REF,
Packit Service 3bf9d6
           "'$test' second input arg is still a $REF");
Packit Service 3bf9d6
Packit Service 3bf9d6
        is($LIB->_str($y), $in1,
Packit Service 3bf9d6
           "'$test' second input arg is unmodified");
Packit Service 3bf9d6
    };
Packit Service 3bf9d6
}