File: number_heuristics.t

package info (click to toggle)
libbson-perl 1.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,152 kB
  • sloc: perl: 3,970; makefile: 2
file content (94 lines) | stat: -rw-r--r-- 2,559 bytes parent folder | download
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
use 5.008001;
use strict;
use warnings;
use utf8;

use Test::More 0.96;

binmode( Test::More->builder->$_, ":utf8" )
  for qw/output failure_output todo_output/;

use lib 't/lib';
use lib 't/pvtlib';
use CleanEnv;
use TestUtils;

use B;
use BSON;
use BSON::Types ':all';
use Devel::Peek;
use Scalar::Util qw/dualvar/;

my $pn0 = BSON->new( prefer_numeric => 0 );
my $pn1 = BSON->new( prefer_numeric => 1 );
my $dec = BSON->new( wrap_numbers   => 1, wrap_strings => 1 );

sub _flags {
    my $value = shift;
    return B::svref_2object( \$value )->FLAGS;
}

sub w_iv  { _flags(shift) & B::SVf_IOK }
sub wo_iv { _flags(shift) & ~B::SVf_IOK }
sub w_nv  { _flags(shift) & B::SVf_NOK }
sub wo_nv { _flags(shift) & ~B::SVf_NOK }
sub w_pv  { _flags(shift) & B::SVf_POK }

sub w_pviv   { w_pv( $_[0] ) && w_iv( $_[0] ) }
sub w_pvnv   { w_pv( $_[0] ) && w_nv( $_[0] ) }
sub w_pvonly { w_pv( $_[0] ) && !( w_iv( $_[0] ) || w_nv( $_[0] ) ) }

sub _dump {
    my $x = shift;
    my $dump;
    open my $fh, ">", \$dump;
    local *STDERR = $fh;
    Dump($x);
    return $dump;
}

sub _rt {
    my ( $encoder, $x ) = @_;
    return $dec->decode_one( $encoder->encode_one($x) );
}

# LABEL, INPUT, FLAGS, OUTPUT W/O PREFER_NUMERIC, OUTPUT W/ PREFER_NUMERIC
#
# Uses 'dualvar()' to construct duals because other forms of NV->PVNV
# conversion don't consistently set POK on all Perl versions.
my @cases = (
    [ 'Pure int',   42,   \&w_iv,     'BSON::Int32',  'BSON::Int32' ],
    [ 'String int', "42", \&w_pvonly, 'BSON::String', 'BSON::Int32' ],
    [ 'Dual int', dualvar( 42, "42" ), \&w_pviv, 'BSON::Int32', 'BSON::Int32' ],

    [ 'Pure double',   3.14,   \&w_nv,     'BSON::Double', 'BSON::Double' ],
    [ 'String double', "3.14", \&w_pvonly, 'BSON::String', 'BSON::Double' ],
    [ 'Dual double', dualvar( 3.14, "3.14" ), \&w_pvnv, 'BSON::Double', 'BSON::Double' ],
);

for my $c (@cases) {
    my ( $label, $x, $type_chk, $y0, $y1 ) = @$c;
    ok( $type_chk->($x), "$label: SvTYPE(s)" ) or diag _dump($x);
    my $doc = { x => $x };

    for my $enc ( [ "prefer_numeric=0", $pn0, $y0 ], [ "prefer_numeric=1", $pn1, $y1 ] )
    {
        my $rt_x = _rt( $enc->[1], $doc )->{x};
        is( ref($rt_x), $enc->[2], "$label: $enc->[0]" );
        like( $rt_x->value, qr/\Q$x\E/, "$label: value matches $x" );
    }
}

done_testing;

#
# This file is part of BSON
#
# This software is Copyright (c) 2018 by Stefan G. and MongoDB, Inc.
#
# This is free software, licensed under:
#
#   The Apache License, Version 2.0, January 2004
#
#
# vim: set ts=4 sts=4 sw=4 et tw=75: