BinaryLogic: Harmonising the test format
parent
ac5d4acb83
commit
e6034d147b
|
@ -11,7 +11,7 @@ triggers query_raw => qr/.*\s+(and|or|xor|⊕|∧|∨)\s+.*/;
|
||||||
triggers query_raw => qr/not\s+.*/;
|
triggers query_raw => qr/not\s+.*/;
|
||||||
triggers query_raw => qr/¬.*/;
|
triggers query_raw => qr/¬.*/;
|
||||||
|
|
||||||
# zci is_cached => 1;
|
zci is_cached => 1;
|
||||||
zci answer_type => "binary_logic";
|
zci answer_type => "binary_logic";
|
||||||
|
|
||||||
attribution
|
attribution
|
||||||
|
@ -31,10 +31,6 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD
|
||||||
category 'calculations';
|
category 'calculations';
|
||||||
topics 'math';
|
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';
|
my $rules = <<'END_OF_GRAMMAR';
|
||||||
:default ::= action => ::first
|
:default ::= action => ::first
|
||||||
:start ::= Expression
|
:start ::= Expression
|
||||||
|
@ -55,7 +51,7 @@ Number ::=
|
||||||
| DecimalDigits
|
| DecimalDigits
|
||||||
|
|
||||||
DecimalDigits ~ [\d]+
|
DecimalDigits ~ [\d]+
|
||||||
HexDigits ~ [\dA-Fa-f]+
|
HexDigits ~ [0-9A-Fa-f]+
|
||||||
|
|
||||||
:discard ~ whitespace
|
:discard ~ whitespace
|
||||||
whitespace ~ [\s]+
|
whitespace ~ [\s]+
|
||||||
|
@ -92,13 +88,14 @@ sub BinaryLogic_Actions::do_not {
|
||||||
}
|
}
|
||||||
|
|
||||||
handle query_raw => sub {
|
handle query_raw => sub {
|
||||||
my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } );
|
my $grammar = Marpa::R2::Scanless::G->new({ source => \$rules });
|
||||||
my $recce = Marpa::R2::Scanless::R->new(
|
my $recce = Marpa::R2::Scanless::R->new({
|
||||||
{ grammar => $grammar,
|
grammar => $grammar,
|
||||||
semantics_package => 'BinaryLogic_Actions' } );
|
semantics_package => 'BinaryLogic_Actions'
|
||||||
|
});
|
||||||
|
|
||||||
my $input = $_;
|
my $input = $_;
|
||||||
|
|
||||||
# Substitute the unicode characters. The parser does not seem to
|
# Substitute the unicode characters. The parser does not seem to
|
||||||
# like unicode.
|
# like unicode.
|
||||||
$input =~ s/⊕/ xor /;
|
$input =~ s/⊕/ xor /;
|
||||||
|
@ -109,18 +106,16 @@ handle query_raw => sub {
|
||||||
# using eval to catch possible errors with $@
|
# using eval to catch possible errors with $@
|
||||||
eval { $recce->read( \$input ) };
|
eval { $recce->read( \$input ) };
|
||||||
|
|
||||||
if ( $@ ) {
|
return if ( $@ );
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $value_ref = $recce->value();
|
my $value_ref = $recce->value();
|
||||||
|
|
||||||
return if not defined $value_ref;
|
return if not defined $value_ref;
|
||||||
|
|
||||||
my $text_output = ${$value_ref};
|
my $text_output = ${$value_ref};
|
||||||
my $html_output = "<div>Result: <b>" . ${$value_ref} . "</b></div>";
|
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;
|
return answer => $text_output, html => $html_output, heading => $heading;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,41 +7,93 @@ use DDG::Test::Goodie;
|
||||||
use utf8;
|
use utf8;
|
||||||
|
|
||||||
zci answer_type => 'binary_logic';
|
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(
|
ddg_goodie_test(
|
||||||
[qw(
|
[qw(
|
||||||
DDG::Goodie::BinaryLogic
|
DDG::Goodie::BinaryLogic
|
||||||
)],
|
)],
|
||||||
'4 xor 5' => bl_test('4 xor 5', '1'),
|
'4 xor 5' => test_zci('1',
|
||||||
'4 ⊕ 5' => bl_test('4 ⊕ 5', '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' =>
|
'10 and 12' => test_zci('8',
|
||||||
bl_test('9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985', '7378'),
|
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'),
|
'52 or 100' => test_zci('116',
|
||||||
'10 ∧ 12' => bl_test('10 ∧ 12', '8'),
|
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'),
|
'23 and (30 or 128)' => test_zci('22',
|
||||||
'52 ∨ 100' => bl_test('52 ∨ 100', '116'),
|
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'),
|
'0x999 xor 0x589' => test_zci('3088',
|
||||||
'23 ∧ (30 ∨ 128)' => bl_test('23 ∧ (30 ∨ 128)', '22'),
|
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'),
|
'not 1' => test_zci('18446744073709551614',
|
||||||
'0x999 ⊕ 0x589' => bl_test('0x999 ⊕ 0x589', '3088'),
|
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;
|
done_testing;
|
Loading…
Reference in New Issue