BinaryLogic: Harmonising the test format

master
Rob Emery 2014-08-30 22:22:40 +01:00
parent ac5d4acb83
commit e6034d147b
2 changed files with 91 additions and 44 deletions

33
lib/DDG/Goodie/BinaryLogic.pm Normal file → Executable file
View File

@ -11,7 +11,7 @@ triggers query_raw => qr/.*\s+(and|or|xor|⊕|∧|)\s+.*/;
triggers query_raw => qr/not\s+.*/;
triggers query_raw => qr/¬.*/;
# zci is_cached => 1;
zci is_cached => 1;
zci answer_type => "binary_logic";
attribution
@ -31,10 +31,6 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD
category 'calculations';
topics 'math';
# TODO: - add binary numbers.
# the parser callback is already available but the
# lexer grammar did not work.
# (BinaryLogic_Actions::bin_number)
my $rules = <<'END_OF_GRAMMAR';
:default ::= action => ::first
:start ::= Expression
@ -55,7 +51,7 @@ Number ::=
| DecimalDigits
DecimalDigits ~ [\d]+
HexDigits ~ [\dA-Fa-f]+
HexDigits ~ [0-9A-Fa-f]+
:discard ~ whitespace
whitespace ~ [\s]+
@ -92,13 +88,14 @@ sub BinaryLogic_Actions::do_not {
}
handle query_raw => sub {
my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } );
my $recce = Marpa::R2::Scanless::R->new(
{ grammar => $grammar,
semantics_package => 'BinaryLogic_Actions' } );
my $grammar = Marpa::R2::Scanless::G->new({ source => \$rules });
my $recce = Marpa::R2::Scanless::R->new({
grammar => $grammar,
semantics_package => 'BinaryLogic_Actions'
});
my $input = $_;
# Substitute the unicode characters. The parser does not seem to
# like unicode.
$input =~ s/⊕/ xor /;
@ -109,18 +106,16 @@ handle query_raw => sub {
# using eval to catch possible errors with $@
eval { $recce->read( \$input ) };
if ( $@ ) {
return;
}
return if ( $@ );
my $value_ref = $recce->value();
return if not defined $value_ref;
my $text_output = ${$value_ref};
my $html_output = "<div>Result: <b>" . ${$value_ref} . "</b></div>";
my $heading = "Binary Logic: '" . $_ . "'";
my $heading = "Binary Logic";
return answer => $text_output, html => $html_output, heading => $heading;
};

102
t/BinaryLogic.t Normal file → Executable file
View File

@ -7,41 +7,93 @@ use DDG::Test::Goodie;
use utf8;
zci answer_type => 'binary_logic';
# zci is_cached => 1;
zci is_cached => 1;
sub bl_test {
my $head = $_[0];
my $text = $_[1];
test_zci($text,
html => "<div>Result: <b>" . $text . "</b></div>",
heading => "Binary Logic: '" . $head . "'");
}
ddg_goodie_test(
[qw(
DDG::Goodie::BinaryLogic
)],
'4 xor 5' => bl_test('4 xor 5', '1'),
'4 ⊕ 5' => bl_test('4 ⊕ 5', '1'),
DDG::Goodie::BinaryLogic
)],
'4 xor 5' => test_zci('1',
html => "<div>Result: <b>1</b></div>",
heading => "Binary Logic"
),
'4 ⊕ 5' => test_zci('1',
html => "<div>Result: <b>1</b></div>",
heading => "Binary Logic"
),
'9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985' => test_zci('7378',
html => "<div>Result: <b>7378</b></div>",
heading => "Binary Logic"
),
'9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985' =>
bl_test('9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985', '7378'),
'10 and 12' => test_zci('8',
html => "<div>Result: <b>8</b></div>",
heading => "Binary Logic"
),
'10 ∧ 12' => test_zci('8',
html => "<div>Result: <b>8</b></div>",
heading => "Binary Logic"
),
'10 and 12' => bl_test('10 and 12', '8'),
'10 ∧ 12' => bl_test('10 ∧ 12', '8'),
'52 or 100' => test_zci('116',
html => "<div>Result: <b>116</b></div>",
heading => "Binary Logic"
),
'52 100' => test_zci('116',
html => "<div>Result: <b>116</b></div>",
heading => "Binary Logic"
),
'52 or 100' => bl_test('52 or 100', '116'),
'52 100' => bl_test('52 100', '116'),
'23 and (30 or 128)' => test_zci('22',
html => "<div>Result: <b>22</b></div>",
heading => "Binary Logic"
),
'23 ∧ (30 128)' => test_zci('22',
html => "<div>Result: <b>22</b></div>",
heading => "Binary Logic"
),
'23 and (30 or 128)' => bl_test('23 and (30 or 128)', '22'),
'23 ∧ (30 128)' => bl_test('23 ∧ (30 128)', '22'),
'0x999 xor 0x589' => test_zci('3088',
html => "<div>Result: <b>3088</b></div>",
heading => "Binary Logic"
),
'0x999 ⊕ 0x589' => test_zci('3088',
html => "<div>Result: <b>3088</b></div>",
heading => "Binary Logic"
),
'0x999 xor 0x589' => bl_test('0x999 xor 0x589', '3088'),
'0x999 ⊕ 0x589' => bl_test('0x999 ⊕ 0x589', '3088'),
'not 1' => test_zci('18446744073709551614',
html => "<div>Result: <b>18446744073709551614</b></div>",
heading => "Binary Logic"
),
'¬1' => test_zci('18446744073709551614',
html => "<div>Result: <b>18446744073709551614</b></div>",
heading => "Binary Logic"
),
'3 and 2' => test_zci('2',
html => "<div>Result: <b>2</b></div>",
heading => "Binary Logic"
),
'1 or 1234' => test_zci('1235',
html => "<div>Result: <b>1235</b></div>",
heading => "Binary Logic"
),
'34 or 100' => test_zci('102',
html => "<div>Result: <b>102</b></div>",
heading => "Binary Logic"
),
'10 and (30 or 128)' => test_zci('10',
html => "<div>Result: <b>10</b></div>",
heading => "Binary Logic"
),
'0x01 or not 0X100' => test_zci('1',
html => "<div>Result: <b>1</b></div>",
heading => "Binary Logic"
),
'not 1' => bl_test('not 1', '18446744073709551614'),
'¬1' => bl_test('¬1', '18446744073709551614'),
);
done_testing;