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