### ----------------------------------------------------
### -- Arithmetic::PaperAndPencil
### -- Licenses: Artistic-2.0
### -- Authors: Jean Forget
### -- File: xt/04-number-fatal.rakutest
### ----------------------------------------------------
# -*- encoding: utf-8; indent-tabs-mode: nil -*-
#
# Checking the checks at build time
#
use v6.c;
use Test;
use Arithmetic::PaperAndPencil::Number;

plan 44;

dies-ok(  { max-unit( 1) }, "radix is 2 or more");
lives-ok( { max-unit( 2) }, "radix is 2 or more");
lives-ok( { max-unit(36) }, "radix is 36 or less");
dies-ok(  { max-unit(37) }, "radix is 36 or less");

dies-ok(  { Arithmetic::PaperAndPencil::Number.new(value => "101", radix =>  1) }, "radix is 2 or more");
lives-ok( { Arithmetic::PaperAndPencil::Number.new(value => "101", radix =>  2) }, "radix is 2 or more");
lives-ok( { Arithmetic::PaperAndPencil::Number.new(value => "XYZ", radix => 36) }, "radix is 36 or less");
dies-ok(  { Arithmetic::PaperAndPencil::Number.new(value => "XYZ", radix => 37) }, "radix is 36 or less");
dies-ok(  { Arithmetic::PaperAndPencil::Number.new(value => "210", radix =>  2) }, "wrong digit in radix 2");
dies-ok(  { Arithmetic::PaperAndPencil::Number.new(value => "XYZ", radix => 35) }, "wrong digit in radix 35");

my Arithmetic::PaperAndPencil::Number $x .= new(value => "123");
my Arithmetic::PaperAndPencil::Number $y .= new(value => "123", radix => 16);
my Arithmetic::PaperAndPencil::Number $z .= new(value => "4"  , radix => 16);
my Arithmetic::PaperAndPencil::Number $t .= new(value => "4F" , radix => 16);
my Arithmetic::PaperAndPencil::Number $zero .= new(value => "0" , radix => 16);

dies-ok(  { $x ☈+ $y }, "cannot add numbers with different bases");
dies-ok(  { $x ☈× $y }, "cannot multiply number with different bases");
dies-ok(  { $x ☈+ $x }, "cannot add multi-digit numbers");
dies-ok(  { $y ☈+ $y }, "cannot add multi-digit numbers");
lives-ok( { $y ☈+ $z }, "can add a multi-digit number to a single-digit number");
dies-ok(  { $y ☈× $y }, "cannot multiply a multi-digit number with a single-digit number");
dies-ok(  { $x    ☈- $y }, "plain sub: cannot subtract number with different bases");
dies-ok(  { $y    ☈- $y }, "plain sub: cannot subtract a single-digit number from a multi-digit number");
dies-ok(  { $zero ☈- $z }, "plain sub: cannot subtract a high number from a low number");
dies-ok(  { adjust-sub($x, $y) }, "adjust-sub: cannot subtract numbers with different bases");
dies-ok(  { adjust-sub($y, $z) }, "adjust-sub: the high number must be a single-digit number");
dies-ok(  { adjust-sub($z, $y) }, "adjust-sub: the low number must be a single-digit number or a 2-digit number");
lives-ok( { adjust-sub($z, $z) }, "adjust-sub: the low number must be a single-digit number or a 2-digit number");
lives-ok( { adjust-sub($z, $t) }, "adjust-sub: the low number must be a single-digit number or a 2-digit number");
dies-ok(  { $x ☈÷ $y    }, "cannot divide numbers with different bases");
dies-ok(  { $y ☈÷ $z    }, "cannot divide a 3-digit number");
lives-ok( { $t ☈÷ $z    }, "can divide a 2-digit number");
dies-ok(  { $z ☈÷ $t    }, "cannot divide by a 2-digit number");
dies-ok(  { $z ☈÷ $zero }, "cannot divide by zero");
lives-ok( { $z.square-root }, "square root of a single digit number is allowed");
lives-ok( { $t.square-root }, "square root of a double digit number is allowed");
dies-ok(  { $y.square-root }, "square root of a 3-digit number is not allowed");
dies-ok(  { $y.complement(2) }, "Cannot compute the 2-digit complement of a 3-digit number");
lives-ok( { $y.complement(3) }, "Can compute the 3-digit complement of a 3-digit number");

dies-ok(  { $x ☈leg $y }, "cannot compare numbers with different bases");
dies-ok(  { $x ☈lt  $y }, "cannot compare numbers with different bases");
dies-ok(  { $x ☈<=> $y }, "cannot compare numbers with different bases");
dies-ok(  { $x ☈<   $y }, "cannot compare numbers with different bases");
dies-ok(  { $x ☈≤   $y }, "cannot compare numbers with different bases");
lives-ok( { $z ☈leg $y }, "can compare numbers with same radix");
lives-ok( { $z ☈lt  $y }, "can compare numbers with same radix");
lives-ok( { $z ☈<=> $y }, "can compare numbers with same radix");
lives-ok( { $z ☈<   $y }, "can compare numbers with same radix");
lives-ok( { $z ☈≤   $y }, "can compare numbers with same radix");
done-testing;


