Base: use structured answer.
parent
0eb5ad7275
commit
5555ef2da9
|
@ -14,6 +14,7 @@ my %base_map = (
|
|||
octal => 8,
|
||||
binary => 2,
|
||||
);
|
||||
my $map_keys = join '|', keys %base_map;
|
||||
|
||||
triggers any => 'base', keys %base_map;
|
||||
|
||||
|
@ -32,12 +33,17 @@ attribution web => [ 'http://perlgeek.de/blog-en', 'Moritz Lenz' ],
|
|||
|
||||
|
||||
handle query_clean => sub {
|
||||
return unless /^([0-9]+)\s*(?:(?:in|as|to)\s+)?(hex|hexadecimal|octal|oct|binary|base\s*([0-9]+))$/;
|
||||
my $number = $1;
|
||||
my $base = $3 // $base_map{$2};
|
||||
return unless /^(?<num>[0-9]+)\s*(?:(?:in|as|to)\s+)?(?:(?<bt>$map_keys)|(?:base\s*(?<bn>[0-9]+)))$/;
|
||||
my $number = $+{'num'};
|
||||
my $base = $+{'bn'} // $base_map{$+{bt}};
|
||||
return if $base < 2 || $base > 36;
|
||||
my $based = int2base($number, $base);
|
||||
return "$number in base $base is $based";
|
||||
return "$number in base $base is $based",
|
||||
structured_answer => {
|
||||
input => ["$number"],
|
||||
operation => 'decimal to base ' . $base,
|
||||
result => $based
|
||||
};
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
99
t/Base.t
99
t/Base.t
|
@ -6,22 +6,93 @@ use Test::More;
|
|||
use DDG::Test::Goodie;
|
||||
|
||||
zci answer_type => 'conversion';
|
||||
zci is_cached => 1;
|
||||
zci is_cached => 1;
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(
|
||||
DDG::Goodie::Base
|
||||
)],
|
||||
|
||||
'255 in hex' => test_zci('255 in base 16 is FF'),
|
||||
'255 in base 16' => test_zci('255 in base 16 is FF'),
|
||||
'42 in binary' => test_zci('42 in base 2 is 101010'),
|
||||
'42 in base 2' => test_zci('42 in base 2 is 101010'),
|
||||
'42 to hex', => test_zci('42 in base 16 is 2A'),
|
||||
'10 in base 3' => test_zci('10 in base 3 is 101'),
|
||||
'18442240474082181119 to hex' => test_zci('18442240474082181119 in base 16 is FFEFFFFFFFFFFFFF'),
|
||||
'999999999999999999999999 to hex' => test_zci('999999999999999999999999 in base 16 is D3C21BCECCEDA0FFFFFF')
|
||||
ddg_goodie_test([qw(
|
||||
DDG::Goodie::Base
|
||||
)
|
||||
],
|
||||
|
||||
'255 in hex' => test_zci(
|
||||
'255 in base 16 is FF',
|
||||
structured_answer => {
|
||||
input => [255],
|
||||
operation => 'decimal to base 16',
|
||||
result => 'FF'
|
||||
}
|
||||
),
|
||||
'255 in base 16' => test_zci(
|
||||
'255 in base 16 is FF',
|
||||
structured_answer => {
|
||||
input => [255],
|
||||
operation => 'decimal to base 16',
|
||||
result => 'FF'
|
||||
}
|
||||
),
|
||||
'255 in base 16' => test_zci(
|
||||
'255 in base 16 is FF',
|
||||
structured_answer => {
|
||||
input => [255],
|
||||
operation => 'decimal to base 16',
|
||||
result => 'FF'
|
||||
}
|
||||
),
|
||||
'42 in binary' => test_zci(
|
||||
'42 in base 2 is 101010',
|
||||
structured_answer => {
|
||||
input => [42],
|
||||
operation => 'decimal to base 2',
|
||||
result => '101010'
|
||||
}
|
||||
),
|
||||
'42 in base 2' => test_zci(
|
||||
'42 in base 2 is 101010',
|
||||
structured_answer => {
|
||||
input => [42],
|
||||
operation => 'decimal to base 2',
|
||||
result => "101010",
|
||||
}
|
||||
),
|
||||
'42 to hex' => test_zci(
|
||||
'42 in base 16 is 2A',
|
||||
structured_answer => {
|
||||
input => [42],
|
||||
operation => 'decimal to base 16',
|
||||
result => '2A'
|
||||
}
|
||||
),
|
||||
'42 to octal' => test_zci(
|
||||
'42 in base 8 is 52',
|
||||
structured_answer => {
|
||||
input => [42],
|
||||
operation => 'decimal to base 8',
|
||||
result => '52'
|
||||
}
|
||||
),
|
||||
'10 in base 3' => test_zci(
|
||||
'10 in base 3 is 101',
|
||||
structured_answer => {
|
||||
input => [10],
|
||||
operation => 'decimal to base 3',
|
||||
result => '101'
|
||||
}
|
||||
),
|
||||
'18442240474082181119 to hex' => test_zci(
|
||||
'18442240474082181119 in base 16 is FFEFFFFFFFFFFFFF',
|
||||
structured_answer => {
|
||||
input => [18442240474082181119],
|
||||
operation => 'decimal to base 16',
|
||||
result => 'FFEFFFFFFFFFFFFF'
|
||||
}
|
||||
),
|
||||
'999999999999999999999999 to hex' => test_zci(
|
||||
'999999999999999999999999 in base 16 is D3C21BCECCEDA0FFFFFF',
|
||||
structured_answer => {
|
||||
input => ["999999999999999999999999"],
|
||||
operation => 'decimal to base 16',
|
||||
result => 'D3C21BCECCEDA0FFFFFF'
|
||||
}
|
||||
),
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue