File: decimal128.t

package info (click to toggle)
libbson-perl 1.12.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,168 kB
  • sloc: perl: 3,983; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,765 bytes parent folder | download | duplicates (2)
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
use 5.008001;
use strict;
use warnings;

use Test::More 0.96;

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

use BSON qw/encode decode/;
use BSON::Types ':all';

use JSON::MaybeXS;

my ($hash);

# test constructor
is( bson_decimal128(), "0", "empty bson_decimal128() is 0" );

eval { BSON::Decimal128->new };
ok( $@, "BSON::Decimal128->new throws" );

# test overloading
is( bson_decimal128("3.14159"), "3.14159", "overloading correct" );

# BSON::Decimal128 -> BSON::Decimal128
$hash = decode( encode( { A => bson_decimal128("3.14159") } ) );
is( ref( $hash->{A} ), 'BSON::Decimal128', "BSON::Decimal128->BSON::Decimal128" );
is( $hash->{A}->value, "3.14159", "value correct" );

# test special decimal128s
for my $s ( qw/Infinity -Infinity NaN/ ) {
    $hash = decode( encode( { A => bson_decimal128($s) } ) );
    is( $hash->{A}->value, $s, "$s value correct" );
}

# to JSON
is( to_myjson( { a => bson_decimal128("0.0") } ),
    q[{"a":"0.0"}], 'bson_decimal128(0.0)' );
is( to_myjson( { a => bson_decimal128("42") } ),
    q[{"a":"42"}], 'bson_decimal128(42)' );
is( to_myjson( { a => bson_decimal128("0.1") } ),
    q[{"a":"0.1"}], 'bson_decimal128(0.1)' );

# to extended JSON
is(
    to_extjson( { a => bson_decimal128("0.0") } ),
    q[{"a":{"$numberDecimal":"0.0"}}],
    'bson_decimal128(0.0)'
);

# normalizes representation
is(
    to_extjson( { a => bson_decimal128("12345678E678") } ),
    q[{"a":{"$numberDecimal":"1.2345678E+685"}}],
    'bson_decimal128(12345678E+678)'
);

done_testing;

#
# This file is part of BSON
#
# This software is Copyright (c) 2020 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: