xref: /openbsd/gnu/usr.bin/perl/ext/XS-APItest/t/grok.t (revision 898184e3)
1#!perl -w
2use strict;
3
4use Test::More;
5use Config;
6use XS::APItest;
7use feature 'switch';
8use constant TRUTH => '0 but true';
9
10# Tests for grok_number. Not yet comprehensive.
11foreach my $leader ('', ' ', '  ') {
12    foreach my $trailer ('', ' ', '  ') {
13	foreach ((map {"0" x $_} 1 .. 12),
14		 (map {("0" x $_) . "1"} 0 .. 12),
15		 (map {"1" . ("0" x $_)} 1 .. 9),
16		 (map {1 << $_} 0 .. 31),
17		 (map {1 << $_} 0 .. 31),
18		 (map {0xFFFFFFFF >> $_} reverse (0 .. 31)),
19		) {
20	    foreach my $sign ('', '-', '+') {
21		my $string = $leader . $sign . $_ . $trailer;
22		my ($flags, $value) = grok_number($string);
23		is($flags & IS_NUMBER_IN_UV, IS_NUMBER_IN_UV,
24		   "'$string' is a UV");
25		is($flags & IS_NUMBER_NEG, $sign eq '-' ? IS_NUMBER_NEG : 0,
26		   "'$string' sign");
27		is($value, abs $string, "value is correct");
28	    }
29	}
30
31	{
32	    my (@UV, @NV);
33	    given ($Config{ivsize}) {
34		when (4) {
35		    @UV = qw(429496729  4294967290 4294967294 4294967295);
36		    @NV = qw(4294967296 4294967297 4294967300 4294967304);
37		}
38		when (8) {
39		    @UV = qw(1844674407370955161  18446744073709551610
40			     18446744073709551614 18446744073709551615);
41		    @NV = qw(18446744073709551616 18446744073709551617
42			     18446744073709551620 18446744073709551624);
43		}
44		default {
45		    die "Unknown IV size $_";
46		}
47	    }
48	    foreach (@UV) {
49		my $string = $leader . $_ . $trailer;
50		my ($flags, $value) = grok_number($string);
51		is($flags & IS_NUMBER_IN_UV, IS_NUMBER_IN_UV,
52		   "'$string' is a UV");
53		is($value, abs $string, "value is correct");
54	    }
55	    foreach (@NV) {
56		my $string = $leader . $_ . $trailer;
57		my ($flags, $value) = grok_number($string);
58		is($flags & IS_NUMBER_IN_UV, 0, "'$string' is an NV");
59		is($value, undef, "value is correct");
60	    }
61	}
62
63	my $string = $leader . TRUTH . $trailer;
64	my ($flags, $value) = grok_number($string);
65
66	if ($string eq TRUTH) {
67	    is($flags & IS_NUMBER_IN_UV, IS_NUMBER_IN_UV, "'$string' is a UV");
68	    is($value, 0);
69	} else {
70	    is($flags, 0, "'$string' is not a number");
71	    is($value, undef);
72	}
73    }
74}
75
76done_testing();
77