File: numify.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (54 lines) | stat: -rw-r--r-- 1,273 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
#! ./perl

# Test string-to-number conversions.

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}

use strict;
use warnings;

# Quick test if NV supports infinities.
# Note that this would be $Config{d_double_has_inf}, but this is only valid
# if NV is configured as double.
my $nv_has_inf = do { no warnings; 'inf' > 0 };

foreach ([' +3', 3, 0],
         ["10.\t", 10, 0],
         ['abc', 0, 1],
         ['- +3', 0, 1],        # GH 18584
         ['++4', 0, 1],
         ['0x123', 0, 1],
         ['1x123', 1, 1],
         ['+0x456', 0, 1],
         ['- 0x789', 0, 1],
         ['0b101', 0, 1],
         ['-3.14', -3.14, 0],
         ['- 3.14', 0, 1],
         ($nv_has_inf ?
          (['+infinity ', '+Inf', 0],
           ['  -infin', '-Inf', 1],
           ['+ inf', 0, 1],
           ['+-inf', 0, 1]) :
          ())
    ) {
    my ($str, $num, $warn) = @$_;

    my $code = sub {
        cmp_ok($str + 0, '==', $num, "numifying '$str'");
    };

    if ($warn) {
        warning_like($code, qr/^Argument ".*" isn't numeric/,
                     "numifying '$str' trigger a warning");
    }
    else {
        warning_is($code, undef,
                   "numifying '$str' does not trigger warnings");
    }
}

done_testing();