forked from duckduckgo/zeroclickinfo-goodies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitsum.t
50 lines (44 loc) · 1.71 KB
/
Bitsum.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Deep;
use DDG::Test::Goodie;
zci answer_type => "bitsum";
zci is_cached => 1;
sub build_structured_answer {
my ($input_number, $result) = @_;
return $result,
structured_answer => {
data => {
title => $result,
subtitle => "Hamming Weight Calculation: $input_number"
},
templates => {
group => 'text'
}
};
}
sub build_test { test_zci(build_structured_answer(@_)) }
ddg_goodie_test(
[qw( DDG::Goodie::Bitsum )],
'bitsum 127' => build_test('127 (Base 10, Decimal)', '7'),
'bitsum of 127' => build_test('127 (Base 10, Decimal)', '7'),
'bitsum 0b1111111' => build_test('0b1111111 (Base 2, Binary)', '7'),
'bitsum for 0x1234' => build_test('0x1234 (Base 16, Hexadecimal)', '5'),
'hammingweight 1024' => build_test('1024 (Base 10, Decimal)', '1'),
'hammingweight 0b10101' => build_test('0b10101 (Base 2, Binary)', '3'),
'hw 0xff' => build_test('0xff (Base 16, Hexadecimal)', '8'),
'hw for 0xaa' => build_test('0xaa (Base 16, Hexadecimal)', '4'),
'hw for 0b11' => build_test('0b11 (Base 2, Binary)', '2'),
# Long number tests
'hw 123456789123456789123456789' => build_test('123456789123456789123456789 (Base 10, Decimal)', '50'),
'bitsum 0x123456789ABCDEF123456789ABCDEF' => build_test('0x123456789ABCDEF123456789ABCDEF (Base 16, Hexadecimal)', '64'),
# Tests which should fail
'bitsum 213f3a', undef,
'hw 0d23238', undef,
'bitsum 0x' => undef,
'bitsum test' => undef,
'bitsum 0b' => undef,
);
done_testing;