This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add tests for IVdf, UVuf, IV_MAX and UV_MAX
[perl5.git] / dist / Devel-PPPort / parts / inc / format
CommitLineData
adfe19db
MHM
1################################################################################
2##
b2049988 3## Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
adfe19db
MHM
4## Version 2.x, Copyright (C) 2001, Paul Marquess.
5## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
6##
7## This program is free software; you can redistribute it and/or
8## modify it under the same terms as Perl itself.
9##
10################################################################################
11
12=provides
13
14/^#\s*define\s+(\w+)/
15
16=implementation
17
18#ifndef IVdf
19# if IVSIZE == LONGSIZE
b2049988
MHM
20# define IVdf "ld"
21# define UVuf "lu"
22# define UVof "lo"
23# define UVxf "lx"
24# define UVXf "lX"
25# elif IVSIZE == INTSIZE
26# define IVdf "d"
27# define UVuf "u"
28# define UVof "o"
29# define UVxf "x"
30# define UVXf "X"
adfe19db 31# else
b2049988 32# error "cannot define IV/UV formats"
adfe19db
MHM
33# endif
34#endif
35
36#ifndef NVef
37# if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) && \
1d175cda
MHM
38 defined(PERL_PRIfldbl) && { VERSION != 5.6.0 }
39 /* Not very likely, but let's try anyway. */
adfe19db
MHM
40# define NVef PERL_PRIeldbl
41# define NVff PERL_PRIfldbl
42# define NVgf PERL_PRIgldbl
43# else
44# define NVef "e"
45# define NVff "f"
46# define NVgf "g"
47# endif
48#endif
49
1d175cda
MHM
50=xsubs
51
52void
53croak_NVgf(num)
b2049988
MHM
54 NV num
55 PPCODE:
56 Perl_croak(aTHX_ "%.20" NVgf "\n", num);
1d175cda 57
bc4230ed
P
58SV *
59sprintf_iv(iv)
60 IV iv
61CODE:
62 RETVAL = newSVpvf("XX_%" IVdf "_XX", iv);
63OUTPUT:
64 RETVAL
65
66SV *
67sprintf_uv(uv)
68 UV uv
69CODE:
70 RETVAL = newSVpvf("XX_%" UVuf "_XX", uv);
71OUTPUT:
72 RETVAL
73
74SV *
75sprintf_ivmax()
76CODE:
77 RETVAL = newSVpvf("%" IVdf, IV_MAX);
78OUTPUT:
79 RETVAL
80
81SV *
82sprintf_uvmax()
83CODE:
84 RETVAL = newSVpvf("%" UVuf, UV_MAX);
85OUTPUT:
86 RETVAL
87
88=tests plan => 5
89
90use Config;
1d175cda
MHM
91
92my $num = 1.12345678901234567890;
93
94eval { Devel::PPPort::croak_NVgf($num) };
95ok($@ =~ /^1.1234567890/);
bc4230ed
P
96
97ok(Devel::PPPort::sprintf_iv(-8), 'XX_-8_XX');
98ok(Devel::PPPort::sprintf_uv(15), 'XX_15_XX');
99
100my $ivsize = $Config::Config{ivsize};
101my $ivmax = ($ivsize == 4) ? '2147483647' : ($ivsize == 8) ? '9223372036854775807' : 0;
102my $uvmax = ($ivsize == 4) ? '4294967295' : ($ivsize == 8) ? '18446744073709551615' : 0;
103if ($ivmax == 0) {
104 skip 'skip: unknown ivsize', 0 for 1..2;
105} else {
106 ok(Devel::PPPort::sprintf_ivmax(), $ivmax);
107 ok(Devel::PPPort::sprintf_uvmax(), $uvmax);
108}