xref: /openbsd-src/gnu/usr.bin/perl/t/op/numify.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
1#! ./perl
2
3# Test string-to-number conversions.
4
5BEGIN {
6    chdir 't' if -d 't';
7    require './test.pl';
8    set_up_inc('../lib');
9}
10
11use strict;
12use warnings;
13
14# Quick test if NV supports infinities.
15# Note that this would be $Config{d_double_has_inf}, but this is only valid
16# if NV is configured as double.
17my $nv_has_inf = do { no warnings; 'inf' > 0 };
18
19foreach ([' +3', 3, 0],
20         ["10.\t", 10, 0],
21         ['abc', 0, 1],
22         ['- +3', 0, 1],        # GH 18584
23         ['++4', 0, 1],
24         ['0x123', 0, 1],
25         ['1x123', 1, 1],
26         ['+0x456', 0, 1],
27         ['- 0x789', 0, 1],
28         ['0b101', 0, 1],
29         ['-3.14', -3.14, 0],
30         ['- 3.14', 0, 1],
31         ($nv_has_inf ?
32          (['+infinity ', '+Inf', 0],
33           ['  -infin', '-Inf', 1],
34           ['+ inf', 0, 1],
35           ['+-inf', 0, 1]) :
36          ())
37    ) {
38    my ($str, $num, $warn) = @$_;
39
40    my $code = sub {
41        cmp_ok($str + 0, '==', $num, "numifying '$str'");
42    };
43
44    if ($warn) {
45        warning_like($code, qr/^Argument ".*" isn't numeric/,
46                     "numifying '$str' trigger a warning");
47    }
48    else {
49        warning_is($code, undef,
50                   "numifying '$str' does not trigger warnings");
51    }
52}
53
54done_testing();
55