1#!/usr/bin/perl 2use strict; 3use warnings; 4use Test::More tests => 415; 5BEGIN { use_ok('li_typemaps') } 6require_ok('li_typemaps'); 7 8sub batch { my($type, @values) = @_; 9 # this is a little ugly because I'm trying to be clever and save my 10 # wrists from hammering out all these tests. 11 for my $val (@values) { 12 for my $tst (qw( 13 in inr 14 out outr 15 inout inoutr 16 )) { 17 my $func = $tst . '_' . $type; 18 is(eval { li_typemaps->can($func)->($val) }, $val, "$func $val"); 19 if($@) { 20 my $err = $@; 21 $err =~ s/^/#\$\@# /mg; 22 print $err; 23 } 24 } 25 } 26} 27 28batch('bool', '', 1); 29# let's assume we're at least on a 32 bit machine 30batch('int', -0x80000000, -1, 0, 1, 12, 0x7fffffff); 31# long could be bigger, but it's at least this big 32batch('long', -0x80000000, -1, 0, 1, 12, 0x7fffffff); 33batch('short', -0x8000, -1, 0, 1, 12, 0x7fff); 34batch('uint', 0, 1, 12, 0xffffffff); 35batch('ushort', 0, 1, 12, 0xffff); 36batch('ulong', 0, 1, 12, 0xffffffff); 37batch('uchar', 0, 1, 12, 0xff); 38batch('schar', -0x80, 0, 1, 12, 0x7f); 39 40{ 41 use Math::BigInt qw(); 42 # the pack dance is to get plain old NVs out of the 43 # Math::BigInt objects. 44 my $inf = unpack 'd', pack 'd', Math::BigInt->new('Inf'); 45 my $nan = unpack 'd', pack 'd', Math::BigInt->new('NaN'); 46 batch('float', 47 -(2 - 2 ** -23) * 2 ** 127, 48 -1, -2 ** -149, 0, 2 ** -149, 1, 49 (2 - 2 ** -23) * 2 ** 127, 50 $nan); 51 { local $TODO = "float typemaps don't pass infinity"; 52 # it seems as though SWIG is unwilling to pass infinity around 53 # because that value always fails bounds checking. I think that 54 # is a bug. 55 batch('float', $inf); 56 } 57 batch('double', 58 -(2 - 2 ** -53) ** 1023, 59 -1, -2 ** -1074, 0, 2 ** 1074, 60 (2 - 2 ** -53) ** 1023, 61 $nan, $inf); 62} 63batch('longlong', -1, 0, 1, 12); 64batch('ulonglong', 0, 1, 12); 65SKIP: { 66 use Math::BigInt qw(); 67 skip "not a 64bit Perl", 18 unless eval { pack 'q', 1 }; 68 my $a = unpack 'q', pack 'q', 69 Math::BigInt->new('-9223372036854775808'); 70 my $b = unpack 'q', pack 'q', 71 Math::BigInt->new('9223372036854775807'); 72 my $c = unpack 'Q', pack 'Q', 73 Math::BigInt->new('18446744073709551615'); 74 batch('longlong', $a, $b); 75 batch('ulonglong', $c); 76} 77 78my($foo, $int) = li_typemaps::out_foo(10); 79isa_ok($foo, 'li_typemaps::Foo'); 80is($foo->{a}, 10); 81is($int, 20); 82 83my($a, $b) = li_typemaps::inoutr_int2(13, 31); 84is($a, 13); 85is($b, 31); 86 87