Skip to content

Commit

Permalink
Minor bug fix, documentation and more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaabp committed Feb 13, 2024
1 parent a65f2f5 commit 1b4c7bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions macros/contexts/contextBaseN.pl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ =head1 DESCRIPTION
Compute('9TE');
A few strings can be passed with preset meanings:
A few strings can be passed to the C<setBase> method with preset meanings:
C<binary> for [0,1]
C<octal> for [0 .. 7]
Expand Down Expand Up @@ -209,7 +209,7 @@ sub setBase {
$base = [ 0, 1 ] if ($base eq 'binary');
$base = [ 0 .. 7 ] if ($base eq 'octal');
$base = [ 0 .. 9 ] if ($base eq 'decimal');
$base = [ 0 .. 9, '2', 'B' ] if ($base eq 'duodecimal');
$base = [ 0 .. 9, 'A', 'B' ] if ($base eq 'duodecimal');
$base = [ 0 .. 9, 'A' .. 'F' ] if ($base eq 'hexadecimal');
$base = [ 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '_', '?' ] if ($base eq 'base64');

Expand Down
36 changes: 36 additions & 0 deletions t/contexts/nondecimal_base.t → t/contexts/baseN.t
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,40 @@ subtest 'Test with different set of digits' => sub {

};

subtest 'Test for using named bases' => sub {
Context('BaseN')->setBase('binary');
my $a1 = Compute('100101');
is $a1->value, 37, "check base => 'binary'";

Context()->setBase('octal');
my $a2 = Compute('367');
is $a2->value, 247, "check base => 'octal'";

Context()->setBase('decimal');
my $a3 = Compute('459');
is $a3->value, 459, "check base => 'decimal'";

Context()->setBase('duodecimal');
my $a4 = Compute('A91');
is $a4->value, 1549, "check base => 'duodecimal'";

Context()->setBase('hexadecimal');
my $a5 = Compute('3CB');
is $a5->value, 971, "check base => 'hexadecimal'";

Context()->setBase('base64');
my $a6 = Compute('Z_');
is $a6->value, 1662, "check base => 'decimal'";
};

subtest 'Test modulo operator' => sub {
Context('BaseN')->setBase('binary');
my $a1 = Compute('100101 % 11');
is $a1->value, 1, 'check binary modulo 100101 % 11';

Context()->setBase('octal');
my $a2 = Compute('347 % 14');
is $a2->value, 3, 'check octal modulo 347 % 14';
};

done_testing();

0 comments on commit 1b4c7bb

Please sign in to comment.