This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / infnan.t
CommitLineData
8c12dc63 1#!./perl -w
313d3d89
JH
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
8c12dc63
JH
9use strict;
10
636a970b
JH
11use Config;
12
313d3d89
JH
13my $PInf = "Inf" + 0;
14my $NInf = "-Inf" + 0;
15my $NaN = "NaN" + 0;
16
8c12dc63
JH
17my @PInf = ("Inf", "inf", "INF", "+Inf",
18 "Infinity", "INFINITE",
313d3d89 19 "1.#INF", "1#INF");
8c12dc63 20my @NInf = map { "-$_" } grep { ! /^\+/ } @PInf;
313d3d89
JH
21
22my @NaN = ("NAN", "nan", "qnan", "SNAN", "NanQ", "NANS",
8c12dc63 23 "1.#QNAN", "+1#SNAN", "-1.#NAN", "1#IND",
313d3d89
JH
24 "NaN123", "NAN(123)", "nan%",
25 "nanonano"); # RIP, Robin Williams.
26
1cd88304 27my @num_fmt = qw(e f g a d u o b x p);
540a63d6 28
d1877901
JH
29my $inf_tests = 13 + @num_fmt + 8 + 3 * @PInf + 3 * @NInf + 5 + 3;
30my $nan_tests = 8 + @num_fmt + 4 + 2 * @NaN + 3;
8c12dc63 31
313d3d89
JH
32my $infnan_tests = 4;
33
34plan tests => $inf_tests + $nan_tests + $infnan_tests;
35
36my $has_inf;
37my $has_nan;
38
39SKIP: {
8c12dc63 40 if ($PInf == 1 && $NInf == 1) {
313d3d89
JH
41 skip $inf_tests, "no infinity found";
42 }
43
44 $has_inf = 1;
45
8c12dc63
JH
46 cmp_ok($PInf, '>', 0, "positive infinity");
47 cmp_ok($NInf, '<', 0, "negative infinity");
48
49 cmp_ok($PInf, '>', $NInf, "positive > negative");
50 cmp_ok($NInf, '==', -$PInf, "negative == -positive");
51 cmp_ok(-$NInf, '==', $PInf, "--negative == positive");
313d3d89
JH
52
53 is($PInf, "Inf", "$PInf value stringifies as Inf");
8c12dc63 54 is($NInf, "-Inf", "$NInf value stringifies as -Inf");
313d3d89 55
d06c1c80
JH
56 cmp_ok($PInf * 2, '==', $PInf, "twice Inf is Inf");
57 cmp_ok($PInf / 2, '==', $PInf, "half of Inf is Inf");
58
d1877901
JH
59 cmp_ok($PInf + 1, '==', $PInf, "Inf + one is Inf");
60 cmp_ok($NInf + 1, '==', $NInf, "-Inf + one is -Inf");
61
313d3d89
JH
62 is(sprintf("%g", $PInf), "Inf", "$PInf sprintf %g is Inf");
63 is(sprintf("%a", $PInf), "Inf", "$PInf sprintf %a is Inf");
64
1cd88304 65 for my $f (@num_fmt) {
540a63d6
JH
66 is(sprintf("%$f", $PInf), "Inf", "$PInf sprintf %$f is Inf");
67 }
68
1cd88304
JH
69 {
70 local $^W = 0;
71
72 is(sprintf("%c", $PInf), chr(0xFFFD), "$PInf sprintf %c is Inf");
73 is(chr($PInf), chr(0xFFFD), "$PInf chr() is U+FFFD");
74
75 is(sprintf("%c", $NInf), chr(0xFFFD), "$NInf sprintf %c is Inf");
76 is(chr($NInf), chr(0xFFFD), "$NInf chr() is U+FFFD");
1f4ef0f1
JH
77
78 is(pack('C', $PInf), chr(0xFF), "$PInf pack C is 0xFF byte");
79 is(pack('c', $PInf), chr(0xFF), "$PInf pack c is 0xFF byte");
80
81 is(pack('C', $NInf), chr(0xFF), "$NInf pack C is 0xFF byte");
82 is(pack('c', $NInf), chr(0xFF), "$NInf pack c is 0xFF byte");
1cd88304
JH
83 }
84
313d3d89 85 for my $i (@PInf) {
8c12dc63
JH
86 cmp_ok($i + 0 , '==', $PInf, "$i is +Inf");
87 cmp_ok($i, '>', 0, "$i is positive");
313d3d89
JH
88 is("@{[$i+0]}", "Inf", "$i value stringifies as Inf");
89 }
90
91 for my $i (@NInf) {
8c12dc63
JH
92 cmp_ok($i + 0, '==', $NInf, "$i is -Inf");
93 cmp_ok($i, '<', 0, "$i is negative");
313d3d89
JH
94 is("@{[$i+0]}", "-Inf", "$i value stringifies as -Inf");
95 }
96
97 is($PInf + $PInf, $PInf, "+inf plus +inf is +inf");
98 is($NInf + $NInf, $NInf, "-inf plus -inf is -inf");
99
100 is(1/$PInf, 0, "one per +Inf is zero");
101 is(1/$NInf, 0, "one per -Inf is zero");
87821667 102
636a970b 103 SKIP: {
0f1abed3
JH
104 my $here = "$^O $Config{osvers}";
105 if ($here =~ /^hpux 10/) {
106 skip "$here: pow doesn't generate Inf", 1;
636a970b
JH
107 }
108 is(9**9**9, $PInf, "9**9**9 is Inf");
109 }
313d3d89
JH
110}
111
0ec38c0a
JH
112{
113 # Silence "isn't numeric in addition", that's kind of the point.
114 local $^W = 0;
115 for my $i (qw(Info Infiniti Infinityz)) {
116 cmp_ok("$i" + 0, '==', 0, "false infinity $i");
117 }
118}
119
313d3d89
JH
120SKIP: {
121 if ($NaN == 1) {
122 skip $nan_tests, "no nan found";
123 }
124
125 $has_nan = 1;
126
8c12dc63
JH
127 cmp_ok($NaN, '!=', $NaN, "NaN is NaN numerically (by not being NaN)");
128 ok($NaN eq $NaN, "NaN is NaN stringifically");
313d3d89 129
d06c1c80 130 is("$NaN", "NaN", "$NaN value stringifies as NaN");
313d3d89 131
8c12dc63
JH
132 is("+NaN" + 0, "NaN", "+NaN is NaN");
133 is("-NaN" + 0, "NaN", "-NaN is NaN");
134
d06c1c80
JH
135 is($NaN * 2, $NaN, "twice NaN is NaN");
136 is($NaN / 2, $NaN, "half of NaN is NaN");
137
d1877901
JH
138 is($NaN + 1, $NaN, "NaN + one is NaN");
139
1cd88304 140 for my $f (@num_fmt) {
540a63d6
JH
141 is(sprintf("%$f", $NaN), "NaN", "$NaN sprintf %$f is NaN");
142 }
313d3d89 143
1cd88304
JH
144 {
145 local $^W = 0;
146
147 is(sprintf("%c", $NaN), chr(0xFFFD), "$NaN sprintf %c is Inf");
148 is(chr($NaN), chr(0xFFFD), "$NaN chr() is U+FFFD");
1f4ef0f1
JH
149
150 is(pack('C', $NaN), chr(0xFF), "$NaN pack C is 0xFF byte");
151 is(pack('c', $NaN), chr(0xFF), "$NaN pack c is 0xFF");
1cd88304
JH
152 }
153
313d3d89 154 for my $i (@NaN) {
8c12dc63 155 cmp_ok($i + 0, '!=', $i + 0, "$i is NaN numerically (by not being NaN)");
313d3d89
JH
156 is("@{[$i+0]}", "NaN", "$i value stringifies as NaN");
157 }
158
8c12dc63 159 # is() okay with $NaN because it uses eq.
313d3d89
JH
160 is($NaN * 0, $NaN, "NaN times zero is NaN");
161 is($NaN * 2, $NaN, "NaN times two is NaN");
87821667 162
636a970b 163 SKIP: {
0f1abed3
JH
164 my $here = "$^O $Config{osvers}";
165 if ($here =~ /^hpux 10/) {
166 skip "$here: pow doesn't generate Inf, so sin(Inf) won't happen", 1;
636a970b
JH
167 }
168 is(sin(9**9**9), $NaN, "sin(9**9**9) is NaN");
169 }
313d3d89
JH
170}
171
172SKIP: {
173 unless ($has_inf && $has_nan) {
8c12dc63 174 skip $infnan_tests, "no both Inf and Nan";
313d3d89
JH
175 }
176
8c12dc63
JH
177 # is() okay with $NaN because it uses eq.
178 is($PInf * 0, $NaN, "Inf times zero is NaN");
179 is($PInf * $NaN, $NaN, "Inf times NaN is NaN");
180 is($PInf + $NaN, $NaN, "Inf plus NaN is NaN");
181 is($PInf - $PInf, $NaN, "Inf minus inf is NaN");
313d3d89 182}