Integrate change #9639 from maintperl into mainline:
[p5sagit/p5-mst-13.2.git] / t / lib / xs-typemap.t
CommitLineData
cf12903c 1BEGIN {
16421035 2 chdir 't' if -d 't';
3 @INC = '../lib';
4 require Config; import Config;
5 if ($Config{'extensions'} !~ /\bXS\/Typemap\b/) {
6 print "1..0 # Skip: XS::Typemap was not built\n";
7 exit 0;
8 }
cf12903c 9}
10
ea035a69 11use Test;
12BEGIN { plan tests => 78 }
13
14use strict;
15use warnings;
16use XS::Typemap;
17
18ok(1);
19
20# Some inheritance trees to check ISA relationships
21BEGIN {
22 package intObjPtr::SubClass;
23 use base qw/ intObjPtr /;
24 sub xxx { 1; }
25}
26
27BEGIN {
28 package intRefIvPtr::SubClass;
29 use base qw/ intRefIvPtr /;
30 sub xxx { 1 }
31}
32
33# T_SV - standard perl scalar value
34print "# T_SV\n";
35
36my $sv = "Testing T_SV";
37ok( T_SV($sv), $sv);
38
39# T_SVREF - reference to Scalar
40print "# T_SVREF\n";
41
42$sv .= "REF";
43my $svref = \$sv;
44ok( T_SVREF($svref), $svref );
45
46# Now test that a non reference is rejected
47# the typemaps croak
48eval { T_SVREF( "fail - not ref" ) };
49ok( $@ );
50
51# T_AVREF - reference to a perl Array
52print "# T_AVREF\n";
53
54my @array;
55ok( T_AVREF(\@array), \@array);
56
57# Now test that a non array ref is rejected
58eval { T_AVREF( \$sv ) };
59ok( $@ );
60
61# T_HVREF - reference to a perl Hash
62print "# T_HVREF\n";
63
64my %hash;
65ok( T_HVREF(\%hash), \%hash);
66
67# Now test that a non hash ref is rejected
68eval { T_HVREF( \@array ) };
69ok( $@ );
70
71
72# T_CVREF - reference to perl subroutine
73print "# T_CVREF\n";
74my $sub = sub { 1 };
75ok( T_CVREF($sub), $sub );
76
77# Now test that a non code ref is rejected
78eval { T_CVREF( \@array ) };
79ok( $@ );
80
81# T_SYSRET - system return values
82print "# T_SYSRET\n";
83
84# first check success
85ok( T_SYSRET_pass );
86
87# ... now failure
88ok( T_SYSRET_fail, undef);
89
90# T_UV - unsigned integer
91print "# T_UV\n";
92
93ok( T_UV(5), 5 ); # pass
94ok( T_UV(-4) != -4); # fail
95
96# T_IV - signed integer
97print "# T_IV\n";
98
99ok( T_IV(5), 5);
100ok( T_IV(-4), -4);
101ok( T_IV(4.1), int(4.1));
102ok( T_IV("52"), "52");
103ok( T_IV(4.5) != 4.5); # failure
104
105
106# Skip T_INT
107
108# T_ENUM - enum list
109print "# T_ENUM\n";
110
111ok( T_ENUM() ); # just hope for a true value
112
113# T_BOOL - boolean
114print "# T_BOOL\n";
115
116ok( T_BOOL(52) );
117ok( ! T_BOOL(0) );
118ok( ! T_BOOL('') );
119ok( ! T_BOOL(undef) );
120
121# Skip T_U_INT
122
123# Skip T_SHORT
124
125# T_U_SHORT aka U16
126
127print "# T_U_SHORT\n";
128
129ok( T_U_SHORT(32000), 32000);
130ok( T_U_SHORT(65536) != 65536); # probably dont want to test edge cases
131
132# T_U_LONG aka U32
133
134print "# T_U_LONG\n";
135
136ok( T_U_LONG(65536), 65536);
137ok( T_U_LONG(-1) != -1);
138
139# T_CHAR
140
141print "# T_CHAR\n";
142
143ok( T_CHAR("a"), "a");
144ok( T_CHAR("-"), "-");
145ok( T_CHAR(chr(128)),chr(128));
146ok( T_CHAR(chr(256)) ne chr(256));
147
148# T_U_CHAR
149
150print "# T_U_CHAR\n";
151
152ok( T_U_CHAR(127), 127);
153ok( T_U_CHAR(128), 128);
154ok( T_U_CHAR(-1) != -1);
155ok( T_U_CHAR(300) != 300);
156
157# T_FLOAT
158print "# T_FLOAT\n";
159
160# limited precision
161ok( sprintf("%6.3f",T_FLOAT(52.345)), 52.345);
162
163# T_NV
164print "# T_NV\n";
165
166ok( T_NV(52.345), 52.345);
167
168# T_DOUBLE
169print "# T_DOUBLE\n";
170
171ok( T_DOUBLE(52.345), 52.345);
172
173# T_PV
174print "# T_PV\n";
175
176ok( T_PV("a string"), "a string");
177ok( T_PV(52), 52);
178
179# T_PTR
180print "# T_PTR\n";
181
182my $t = 5;
183my $ptr = T_PTR_OUT($t);
184ok( T_PTR_IN( $ptr ), $t );
185
186# T_PTRREF
187print "# T_PTRREF\n";
188
189$t = -52;
190$ptr = T_PTRREF_OUT( $t );
191ok( ref($ptr), "SCALAR");
192ok( T_PTRREF_IN( $ptr ), $t );
193
194# test that a non-scalar ref is rejected
195eval { T_PTRREF_IN( $t ); };
196ok( $@ );
197
198# T_PTROBJ
199print "# T_PTROBJ\n";
200
201$t = 256;
202$ptr = T_PTROBJ_OUT( $t );
203ok( ref($ptr), "intObjPtr");
204ok( $ptr->T_PTROBJ_IN, $t );
205
206# check that normal scalar refs fail
207eval {intObjPtr::T_PTROBJ_IN( \$t );};
208ok( $@ );
209
210# check that inheritance works
211bless $ptr, "intObjPtr::SubClass";
212ok( ref($ptr), "intObjPtr::SubClass");
213ok( $ptr->T_PTROBJ_IN, $t );
214
215# Skip T_REF_IV_REF
216
217# T_REF_IV_PTR
218print "# T_REF_IV_PTR\n";
219
220$t = -365;
221$ptr = T_REF_IV_PTR_OUT( $t );
222ok( ref($ptr), "intRefIvPtr");
223ok( $ptr->T_REF_IV_PTR_IN(), $t);
224
225# inheritance should not work
226bless $ptr, "intRefIvPtr::SubClass";
227eval { $ptr->T_REF_IV_PTR_IN };
228ok( $@ );
229
230# Skip T_PTRDESC
231
232# Skip T_REFREF
233
234# Skip T_REFOBJ
235
236# T_OPAQUEPTR
237print "# T_OPAQUEPTR\n";
238
239$t = 22;
240$ptr = T_OPAQUEPTR_IN( $t );
241ok( T_OPAQUEPTR_OUT($ptr), $t);
242
243# T_OPAQUE
244print "# T_OPAQUE\n";
245
246$t = 48;
247$ptr = T_OPAQUE_IN( $t );
aa921f48 248ok(T_OPAQUEPTR_OUT_short( $ptr ), $t);
ea035a69 249
250# T_OPAQUE_array
251my @opq = (2,4,8);
252my $packed = T_OPAQUE_array(@opq);
253my @uopq = unpack("i*",$packed);
254for (0..$#opq) {
255 ok( $uopq[$_], $opq[$_]);
256}
257
258# Skip T_PACKED
259
260# Skip T_PACKEDARRAY
261
262# Skip T_DATAUNIT
263
264# Skip T_CALLBACK
265
266# T_ARRAY
267print "# T_ARRAY\n";
268my @inarr = (1,2,3,4,5,6,7,8,9,10);
269my @outarr = T_ARRAY( 5, @inarr );
270ok(scalar(@outarr), scalar(@inarr));
271
272for (0..$#inarr) {
273 ok($outarr[$_], $inarr[$_]);
274}
275
276
277
278# T_STDIO
279print "# T_STDIO\n";
280
281# open a file in XS for write
282my $testfile= "stdio.tmp";
283my $fh = T_STDIO_open( $testfile );
284ok( $fh );
285
286# write to it using perl
287if (defined $fh) {
288
289 my @lines = ("NormalSTDIO\n", "PerlIO\n");
290
291 # print to it using FILE* through XS
292 ok( T_STDIO_print($fh, $lines[0]), length($lines[0]));
293
294 # print to it using normal perl
295 ok(print $fh "$lines[1]");
296
297 # close it using XS
298 # This works fine but causes a segmentation fault during global
299 # destruction when the glob associated with this filehandle is
300 # tidied up.
301# ok( T_STDIO_close( $fh ) );
302 ok(close($fh)); # using perlio to close the glob works fine
303
304 # open from perl, and check contents
305 open($fh, "< $testfile");
306 ok($fh);
307 my $line = <$fh>;
308 ok($line,$lines[0]);
309 $line = <$fh>;
310 ok($line,$lines[1]);
311
312 ok(close($fh));
313 ok(unlink($testfile));
314
315} else {
316 for (1..8) {
317 skip("Skip Test not relevant since file was not opened correctly",0);
318 }
319}
320