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
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 use strict;
10
11 use Config;
12
13 my $PInf = "Inf"  + 0;
14 my $NInf = "-Inf" + 0;
15 my $NaN  = "NaN"  + 0;
16
17 my @PInf = ("Inf", "inf", "INF", "+Inf",
18             "Infinity", "INFINITE",
19             "1.#INF", "1#INF");
20 my @NInf = map { "-$_" } grep { ! /^\+/ } @PInf;
21
22 my @NaN = ("NAN", "nan", "qnan", "SNAN", "NanQ", "NANS",
23            "1.#QNAN", "+1#SNAN", "-1.#NAN", "1#IND",
24            "NaN123", "NAN(123)", "nan%",
25            "nanonano"); # RIP, Robin Williams.
26
27 my @num_fmt = qw(e f g a d u o b x p);
28
29 my $inf_tests = 13 + @num_fmt + 8 + 3 * @PInf + 3 * @NInf + 5 + 3;
30 my $nan_tests =  8 + @num_fmt + 4 + 2 * @NaN + 3;
31
32 my $infnan_tests = 4;
33
34 plan tests => $inf_tests + $nan_tests + $infnan_tests;
35
36 my $has_inf;
37 my $has_nan;
38
39 SKIP: {
40   if ($PInf == 1 && $NInf == 1) {
41     skip $inf_tests, "no infinity found";
42   }
43
44   $has_inf = 1;
45
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");
52
53   is($PInf,  "Inf", "$PInf value stringifies as Inf");
54   is($NInf, "-Inf", "$NInf value stringifies as -Inf");
55
56   cmp_ok($PInf * 2, '==', $PInf, "twice Inf is Inf");
57   cmp_ok($PInf / 2, '==', $PInf, "half of Inf is Inf");
58
59   cmp_ok($PInf + 1, '==', $PInf, "Inf + one is Inf");
60   cmp_ok($NInf + 1, '==', $NInf, "-Inf + one is -Inf");
61
62   is(sprintf("%g", $PInf), "Inf", "$PInf sprintf %g is Inf");
63   is(sprintf("%a", $PInf), "Inf", "$PInf sprintf %a is Inf");
64
65   for my $f (@num_fmt) {
66       is(sprintf("%$f", $PInf), "Inf", "$PInf sprintf %$f is Inf");
67   }
68
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");
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");
83   }
84
85   for my $i (@PInf) {
86     cmp_ok($i + 0 , '==', $PInf, "$i is +Inf");
87     cmp_ok($i, '>', 0, "$i is positive");
88     is("@{[$i+0]}", "Inf", "$i value stringifies as Inf");
89   }
90
91   for my $i (@NInf) {
92     cmp_ok($i + 0, '==', $NInf, "$i is -Inf");
93     cmp_ok($i, '<', 0, "$i is negative");
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");
102
103  SKIP: {
104      my $here = "$^O $Config{osvers}";
105      if ($here =~ /^hpux 10/) {
106          skip "$here: pow doesn't generate Inf", 1;
107      }
108      is(9**9**9, $PInf, "9**9**9 is Inf");
109   }
110 }
111
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
120 SKIP: {
121   if ($NaN == 1) {
122     skip $nan_tests, "no nan found";
123   }
124
125   $has_nan = 1;
126
127   cmp_ok($NaN, '!=', $NaN, "NaN is NaN numerically (by not being NaN)");
128   ok($NaN eq $NaN, "NaN is NaN stringifically");
129
130   is("$NaN", "NaN", "$NaN value stringifies as NaN");
131
132   is("+NaN" + 0, "NaN", "+NaN is NaN");
133   is("-NaN" + 0, "NaN", "-NaN is NaN");
134
135   is($NaN * 2, $NaN, "twice NaN is NaN");
136   is($NaN / 2, $NaN, "half of NaN is NaN");
137
138   is($NaN + 1, $NaN, "NaN + one is NaN");
139
140   for my $f (@num_fmt) {
141       is(sprintf("%$f", $NaN), "NaN", "$NaN sprintf %$f is NaN");
142   }
143
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");
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");
152   }
153
154   for my $i (@NaN) {
155     cmp_ok($i + 0, '!=', $i + 0, "$i is NaN numerically (by not being NaN)");
156     is("@{[$i+0]}", "NaN", "$i value stringifies as NaN");
157   }
158
159   # is() okay with $NaN because it uses eq.
160   is($NaN * 0, $NaN, "NaN times zero is NaN");
161   is($NaN * 2, $NaN, "NaN times two is NaN");
162
163  SKIP: {
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;
167      }
168      is(sin(9**9**9), $NaN, "sin(9**9**9) is NaN");
169   }
170 }
171
172 SKIP: {
173   unless ($has_inf && $has_nan) {
174     skip $infnan_tests, "no both Inf and Nan";
175   }
176
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");
182 }