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