File: Reader-decoder.t

package info (click to toggle)
libmaxmind-db-reader-perl 1.000014-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,040 kB
  • sloc: perl: 1,668; makefile: 10
file content (100 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download | duplicates (3)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Note that this is not actually a test of the Decoder class, and as such
# applies to both the PP and XS databases.
use strict;
use warnings;
use utf8;

use Test::Requires {
    'Math::Int128' => 0,
};

use lib 't/lib';

# This must come before `use MaxMind::DB::Reader;` as otherwise the wrong
# reader may be loaded
use Test::MaxMind::DB::Reader;

use Math::Int128 qw( uint128 );
use MaxMind::DB::Reader;
use Test::More;
use Test::Number::Delta;

my $filename = 'MaxMind-DB-test-decoder.mmdb';
my $reader
    = MaxMind::DB::Reader->new( file => "maxmind-db/test-data/$filename" );

{
    my $mmdb_record = $reader->record_for_address('::1.1.1.0');
    ok( $mmdb_record, 'found record for ::1.1.1.0' );

    is(
        $mmdb_record->{utf8_string}, 'unicode! ☯ - ♫',
        'decoded utf8_string has expected value'
    );
    delta_ok(
        $mmdb_record->{double}, 42.123456,
        'decoded double has expected value'
    );
    is(
        $mmdb_record->{bytes}, pack( 'N', 42 ),
        'decoded bytes has expected value'
    );
    is( $mmdb_record->{uint16}, 100,   'decoded uint16 has expected value' );
    is( $mmdb_record->{uint32}, 2**28, 'decoded uint32 has expected value' );
    is(
        $mmdb_record->{int32}, -1 * ( 2**28 ),
        'decoded int32 has expected value'
    );
    is(
        $mmdb_record->{uint64}, uint128(1) << 60,
        'decoded uint64 has expected value'
    );
    is(
        $mmdb_record->{uint128}, uint128(1) << 120,
        'decoded uint128 has expected value'
    );
    is_deeply(
        $mmdb_record->{array}, [ 1, 2, 3 ],
        'decoded array has expected value'
    );

    is_deeply(
        $mmdb_record->{map},
        {
            mapX => {
                utf8_stringX => 'hello',
                arrayX       => [ 7, 8, 9 ],
            },
        },
        'decoded map has expected value'
    );

    ok( $mmdb_record->{boolean}, 'decoded bool is true' );
    delta_ok(
        $mmdb_record->{float}, 1.1,
        'decoded float has expected value'
    );
}

{
    my $mmdb_record = $reader->record_for_address('::0.0.0.0');
    ok( $mmdb_record, 'found record for ::0.0.0.0' );

    is(
        $mmdb_record->{utf8_string}, q{},
        'decoded utf8_string is empty string'
    );
    is( $mmdb_record->{double},  0,          'decoded double is 0' );
    is( $mmdb_record->{bytes},   q{},        'decoded bytes is empty' );
    is( $mmdb_record->{uint16},  0,          'decoded uint16 is 0' );
    is( $mmdb_record->{uint32},  0,          'decoded uint32 is 0' );
    is( $mmdb_record->{int32},   0,          'decoded int32 is 0' );
    is( $mmdb_record->{uint64},  uint128(0), 'decoded uint64 is 0' );
    is( $mmdb_record->{uint128}, uint128(0), 'decoded uint128 is 0' );
    is_deeply( $mmdb_record->{array}, [], 'decoded array is empty' );
    is_deeply( $mmdb_record->{map}, {},   'decoded map is empty' );
    ok( !$mmdb_record->{boolean}, 'decoded false bool' );
    is( $mmdb_record->{float}, 0, 'decoded float is 0' );
}

done_testing();