PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/perl5/li_typemaps_runme.pl

#
Perl | 106 lines | 96 code | 6 blank | 4 comment | 4 complexity | 7470a1465c151ec56727aacdd0e9f6b7 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More tests => 631;
  5. BEGIN { use_ok('li_typemaps') }
  6. require_ok('li_typemaps');
  7. my @tests = qw(
  8. in inr
  9. out outr
  10. inout inoutr
  11. );
  12. sub should_pass { my($type, @values) = @_;
  13. # verify that each value passes cleanly
  14. for my $test (@tests) {
  15. my $name = "${test}_${type}";
  16. my $func = li_typemaps->can($name);
  17. for my $val (@values) {
  18. my $rv = eval { $func->($val) };
  19. is($rv, $val, "$name $val");
  20. }
  21. }
  22. }
  23. sub should_fail { my($type, @values) = @_;
  24. # verify that all values trigger runtime errors
  25. for my $test (@tests) {
  26. my $name = "${test}_${type}";
  27. my $func = li_typemaps->can($name);
  28. for my $val (@values) {
  29. my $rv = eval { $func->($val) };
  30. like($@, qr/\b(?:Overflow|Type)Error\b/, "overflow $name $val");
  31. }
  32. }
  33. }
  34. sub pad { my($t, $s, $f) = @_;
  35. my $nbytes = length pack $t, 0;
  36. return unpack $t, $s . ($f x ($nbytes - 1));
  37. }
  38. # some edge case values:
  39. my $nan = unpack 'f>', "\x7f\xc0\x00\x00";
  40. my $inf = unpack 'f>', "\x7f\x80\x00\x00";
  41. my $char_min = pad 'c', "\x80";
  42. my $char_max = pad 'c', "\x7f";
  43. my $char_umax = pad 'C', "\xff";
  44. my $short_min = pad 's!>', "\x80", "\x00";
  45. my $short_max = pad 's!>', "\x7f", "\xff";
  46. my $short_umax = pad 'S!>', "\xff", "\xff";
  47. my $int_min = pad 'i!>', "\x80", "\x00";
  48. my $int_max = pad 'i!>', "\x7f", "\xff";
  49. my $int_umax = pad 'I!>', "\xff", "\xff";
  50. my $long_min = pad 'l!>', "\x80", "\x00";
  51. my $long_max = pad 'l!>', "\x7f", "\xff";
  52. my $long_umax = pad 'L!>', "\xff", "\xff";
  53. should_pass('bool', '', 1);
  54. should_pass('int', $int_min, -1, 0, 1, 12, $int_max);
  55. should_fail('int', $int_min - 1000, $int_max + 1000, $inf, $nan);
  56. should_pass('long', $long_min, -1, 0, 1, 12, $long_max);
  57. should_fail('long', $long_min - 8000, $long_max + 8000, $inf, $nan);
  58. should_pass('short', $short_min, -1, 0, 1, 12, $short_max);
  59. should_fail('short', $short_min - 1, $short_max + 1, $inf, $nan);
  60. should_pass('uint', 0, 1, 12, $int_umax);
  61. should_fail('uint', -1, $int_umax + 1000, $inf, $nan);
  62. should_pass('ushort', 0, 1, 12, $short_umax);
  63. should_fail('ushort', -1, $short_umax + 1, $inf, $nan);
  64. should_pass('ulong', 0, 1, 12, $long_umax);
  65. should_fail('ulong', -1, $long_umax + 8000, $inf, $nan);
  66. should_pass('uchar', 0, 1, 12, $char_umax);
  67. should_fail('uchar', -1, $char_umax + 1, $inf, $nan);
  68. should_pass('schar', $char_min, -1, 0, 1, 12, $char_max);
  69. should_fail('schar', $char_min - 1, $char_max + 1, $inf, $nan);
  70. should_pass('float', -1, 0, 1, $nan);
  71. TODO: {
  72. local $TODO = "typemaps don't allow float infinity";
  73. should_pass('float', -$inf, $inf);
  74. }
  75. should_pass('double', -$inf, -1, 0, 1, $inf, $nan);
  76. should_pass('longlong', -1, 0, 1, 12);
  77. should_fail('longlong', $inf, $nan);
  78. should_pass('ulonglong', 0, 1, 12);
  79. should_fail('ulonglong', -1, $inf, $nan);
  80. SKIP: {
  81. my $llong_min = eval { pad 'q>', "\x80", "\x00" };
  82. my $llong_max = eval { pad 'q>', "\x7f", "\xff" };
  83. my $llong_umax = eval { pad 'Q>', "\xff", "\xff" };
  84. skip 'not a 64 bit perl', 6 * 6 unless defined $llong_min;
  85. should_pass('longlong', $llong_min, $llong_max);
  86. should_fail('longlong', $llong_min - 8000, $llong_max + 8000);
  87. should_pass('ulonglong', $llong_umax);
  88. should_fail('ulonglong', $llong_umax + 8000);
  89. }
  90. my($foo, $int) = li_typemaps::out_foo(10);
  91. isa_ok($foo, 'li_typemaps::Foo');
  92. is($foo->{a}, 10);
  93. is($int, 20);
  94. my($a, $b) = li_typemaps::inoutr_int2(13, 31);
  95. is($a, 13);
  96. is($b, 31);