From 1b4c7bb04b2f86aef4301bdd4804fc6be287710d Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Tue, 13 Feb 2024 08:35:33 -0500 Subject: [PATCH] Minor bug fix, documentation and more tests. --- macros/contexts/contextBaseN.pl | 4 +-- t/contexts/{nondecimal_base.t => baseN.t} | 36 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) rename t/contexts/{nondecimal_base.t => baseN.t} (87%) diff --git a/macros/contexts/contextBaseN.pl b/macros/contexts/contextBaseN.pl index 2c69c77db3..50636de52b 100644 --- a/macros/contexts/contextBaseN.pl +++ b/macros/contexts/contextBaseN.pl @@ -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 method with preset meanings: C for [0,1] C for [0 .. 7] @@ -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'); diff --git a/t/contexts/nondecimal_base.t b/t/contexts/baseN.t similarity index 87% rename from t/contexts/nondecimal_base.t rename to t/contexts/baseN.t index d621944343..9f38146a5f 100644 --- a/t/contexts/nondecimal_base.t +++ b/t/contexts/baseN.t @@ -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();