PageRenderTime 66ms CodeModel.GetById 36ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Perl | 275 lines | 140 code | 39 blank | 96 comment | 0 complexity | 56db6f7b59adb483e5d234329f376486 MD5 | raw file
  1use strict;
  2use warnings;
  3use Test::More tests => 51;
  4BEGIN { use_ok('primitive_types') }
  5require_ok('primitive_types');
  6
  7primitive_types::var_init();
  8
  9# assigning globals calls
 10$primitive_types::var_bool = $primitive_types::sct_bool;
 11$primitive_types::var_schar = $primitive_types::sct_schar;
 12$primitive_types::var_uchar = $primitive_types::sct_uchar;
 13$primitive_types::var_int = $primitive_types::sct_int;
 14$primitive_types::var_uint = $primitive_types::sct_uint;
 15$primitive_types::var_short = $primitive_types::sct_short;
 16$primitive_types::var_ushort = $primitive_types::sct_ushort;
 17$primitive_types::var_long = $primitive_types::sct_long;
 18$primitive_types::var_ulong = $primitive_types::sct_ulong;
 19$primitive_types::var_llong = $primitive_types::sct_llong;
 20$primitive_types::var_ullong = $primitive_types::sct_ullong;
 21$primitive_types::var_char = $primitive_types::sct_char;
 22$primitive_types::var_pchar = $primitive_types::sct_pchar;
 23$primitive_types::var_pcharc = $primitive_types::sct_pcharc;
 24$primitive_types::var_pint = $primitive_types::sct_pint;
 25$primitive_types::var_sizet = $primitive_types::sct_sizet;
 26$primitive_types::var_hello = $primitive_types::sct_hello;
 27$primitive_types::var_myint = $primitive_types::sct_myint;
 28$primitive_types::var_namet = $primitive_types::def_namet;
 29$primitive_types::var_parami = $primitive_types::sct_parami;
 30$primitive_types::var_paramd = $primitive_types::sct_paramd;
 31$primitive_types::var_paramc = $primitive_types::sct_paramc;
 32
 33ok(primitive_types::v_check(), "v_check");
 34
 35#def pyerror(name, val, cte):
 36#  print "bad val/cte", name, val, cte
 37#  raise RuntimeError
 38#  pass
 39
 40is($primitive_types::var_bool, $primitive_types::cct_bool, "bool");
 41is($primitive_types::var_schar, $primitive_types::cct_schar, "schar");
 42is($primitive_types::var_uchar, $primitive_types::cct_uchar, "uchar");
 43is($primitive_types::var_int, $primitive_types::cct_int, "int");
 44is($primitive_types::var_uint, $primitive_types::cct_uint, "uint");
 45is($primitive_types::var_short, $primitive_types::cct_short, "short");
 46is($primitive_types::var_ushort, $primitive_types::cct_ushort, "ushort");
 47is($primitive_types::var_long, $primitive_types::cct_long, "long");
 48is($primitive_types::var_ulong, $primitive_types::cct_ulong, "ulong");
 49is($primitive_types::var_llong, $primitive_types::cct_llong, "llong");
 50is($primitive_types::var_ullong, $primitive_types::cct_ullong, "ullong");
 51is($primitive_types::var_char, $primitive_types::cct_char, "char");
 52is($primitive_types::var_pchar, $primitive_types::cct_pchar, "pchar");
 53is($primitive_types::var_pcharc, $primitive_types::cct_pcharc, "pchar");
 54is($primitive_types::var_pint, $primitive_types::cct_pint, "pint");
 55is($primitive_types::var_sizet, $primitive_types::cct_sizet, "sizet");
 56is($primitive_types::var_hello, $primitive_types::cct_hello, "hello");
 57is($primitive_types::var_myint, $primitive_types::cct_myint, "myint");
 58is($primitive_types::var_namet, $primitive_types::def_namet, "name");
 59
 60#class PyTest (TestDirector):
 61#  def __init__(self):
 62#    TestDirector.__init__(self)
 63#    pass
 64#  def ident(self, x):
 65#    return x
 66#  
 67#  def vval_bool(self, x): return self.ident(x)
 68#  def vval_schar(self, x): return self.ident(x)
 69#  def vval_uchar(self, x): return self.ident(x)
 70#  def vval_int(self, x): return self.ident(x)
 71#  def vval_uint(self, x): return self.ident(x)
 72#  def vval_short(self, x): return self.ident(x)
 73#  def vval_ushort(self, x): return self.ident(x)
 74#  def vval_long(self, x): return self.ident(x)
 75#  def vval_ulong(self, x): return self.ident(x)
 76#  def vval_llong(self, x): return self.ident(x)
 77#  def vval_ullong(self, x): return self.ident(x)
 78#  def vval_float(self, x): return self.ident(x)
 79#  def vval_double(self, x): return self.ident(x)
 80#  def vval_char(self, x): return self.ident(x)
 81#  def vval_pchar(self, x): return self.ident(x)
 82#  def vval_pcharc(self, x): return self.ident(x)
 83#  def vval_pint(self, x): return self.ident(x)
 84#  def vval_sizet(self, x): return self.ident(x)
 85#  def vval_hello(self, x): return self.ident(x)
 86#  def vval_myint(self, x): return self.ident(x)
 87#
 88#  def vref_bool(self, x): return self.ident(x)
 89#  def vref_schar(self, x): return self.ident(x)
 90#  def vref_uchar(self, x): return self.ident(x)
 91#  def vref_int(self, x): return self.ident(x)
 92#  def vref_uint(self, x): return self.ident(x)
 93#  def vref_short(self, x): return self.ident(x)
 94#  def vref_ushort(self, x): return self.ident(x)
 95#  def vref_long(self, x): return self.ident(x)
 96#  def vref_ulong(self, x): return self.ident(x)
 97#  def vref_llong(self, x): return self.ident(x)
 98#  def vref_ullong(self, x): return self.ident(x)
 99#  def vref_float(self, x): return self.ident(x)
100#  def vref_double(self, x): return self.ident(x)
101#  def vref_char(self, x): return self.ident(x)
102#  def vref_pchar(self, x): return self.ident(x)
103#  def vref_pcharc(self, x): return self.ident(x)
104#  def vref_pint(self, x): return self.ident(x)
105#  def vref_sizet(self, x): return self.ident(x)
106#  def vref_hello(self, x): return self.ident(x)
107#  def vref_myint(self, x): return self.ident(x)
108#
109#  pass
110
111
112my $t = primitive_types::Test->new();
113#p = PyTest()
114#
115#
116# internal call check
117#if t.c_check() != p.c_check():
118#  raise RuntimeError, "bad director"
119#
120#p.var_bool = p.stc_bool
121#p.var_schar = p.stc_schar
122#p.var_uchar = p.stc_uchar
123#p.var_int = p.stc_int
124#p.var_uint = p.stc_uint
125#p.var_short = p.stc_short
126#p.var_ushort = p.stc_ushort
127#p.var_long = p.stc_long
128#p.var_ulong = p.stc_ulong
129#p.var_llong = p.stc_llong
130#p.var_ullong = p.stc_ullong
131#p.var_char = p.stc_char
132#p.var_pchar = sct_pchar
133#p.var_pcharc = sct_pcharc
134#p.var_pint = sct_pint
135#p.var_sizet = sct_sizet
136#p.var_hello = sct_hello
137#p.var_myint = sct_myint
138#p.var_namet = def_namet
139#p.var_parami = sct_parami
140#p.var_paramd = sct_paramd
141#p.var_paramc = sct_paramc
142#
143#p.v_check()
144
145$t->{var_bool} = $primitive_types::Test::stc_bool;
146$t->{var_schar} = $primitive_types::Test::stc_schar;
147$t->{var_uchar} = $primitive_types::Test::stc_uchar;
148$t->{var_int} = $primitive_types::Test::stc_int;
149$t->{var_uint} = $primitive_types::Test::stc_uint;
150$t->{var_short} = $primitive_types::Test::stc_short;
151$t->{var_ushort} = $primitive_types::Test::stc_ushort;
152$t->{var_long} = $primitive_types::Test::stc_long;
153$t->{var_ulong} = $primitive_types::Test::stc_ulong;
154$t->{var_llong} = $primitive_types::Test::stc_llong;
155$t->{var_ullong} = $primitive_types::Test::stc_ullong;
156$t->{var_char} = $primitive_types::Test::stc_char;
157$t->{var_pchar} = $primitive_types::sct_pchar;
158$t->{var_pcharc} = $primitive_types::sct_pcharc;
159$t->{var_pint} = $primitive_types::sct_pint;
160$t->{var_sizet} = $primitive_types::sct_sizet;
161$t->{var_hello} = $primitive_types::sct_hello;
162$t->{var_myint} = $primitive_types::sct_myint;
163$t->{var_namet} = $primitive_types::def_namet;
164$t->{var_parami} = $primitive_types::sct_parami;
165$t->{var_paramd} = $primitive_types::sct_paramd;
166$t->{var_paramc} = $primitive_types::sct_paramc;
167ok($t->v_check(), 'v_check');
168
169is($primitive_types::def_namet, "ho\0la", "namet");
170$t->{var_namet} = $primitive_types::def_namet;
171is($t->{var_namet}, $primitive_types::def_namet, "namet");
172
173$t->{var_namet} = 'holac';
174
175is($t->{var_namet}, 'holac', "namet");
176
177$t->{var_namet} = 'hol';
178
179is($t->{var_namet}, 'hol', "namet");
180
181
182$primitive_types::var_char = "\0";
183is($primitive_types::var_char, "\0", "char '0' case");
184  
185$primitive_types::var_char = 0;
186is($primitive_types::var_char, "\0", "char '0' case");
187
188$primitive_types::var_namet = "\0";
189is($primitive_types::var_namet, '', "char '\\0' case");
190
191$primitive_types::var_namet = '';
192is($primitive_types::var_namet, '', "char empty case");
193
194$primitive_types::var_pchar = undef;
195is($primitive_types::var_pchar, undef, "undef case");
196
197$primitive_types::var_pchar = '';
198is($primitive_types::var_pchar, '', "char empty case");
199
200$primitive_types::var_pcharc = undef;
201is($primitive_types::var_pcharc, undef, "undef case");
202
203$primitive_types::var_pcharc = '';
204is($primitive_types::var_pcharc, '', "char empty case");
205
206
207#
208# creating a raw char*
209#
210my $pc = primitive_types::new_pchar(5);
211primitive_types::pchar_setitem($pc, 0, 'h');
212primitive_types::pchar_setitem($pc, 1, 'o');
213primitive_types::pchar_setitem($pc, 2, 'l');
214primitive_types::pchar_setitem($pc, 3, 'a');
215primitive_types::pchar_setitem($pc, 4, 0);
216
217
218$primitive_types::var_pchar = $pc;
219is($primitive_types::var_pchar, "hola", "pointer case");
220
221$primitive_types::var_namet = $pc;
222is($primitive_types::var_namet, "hola", "pointer case");
223
224primitive_types::delete_pchar($pc);
225
226#
227# Now when things should fail
228#
229
230{
231	my $orig = $t->{var_uchar};
232	eval { $t->{var_uchar} = 10000 };
233	like($@, qr/\bOverflowError\b/, "uchar typemap");
234	is($orig, $t->{var_uchar}, "uchar typemap");
235}
236{
237	my $orig = $t->{var_char};
238	#eval { $t->{var_char} = "23" }; Perl will gladly make a number out of that
239	eval { $t->{var_char} = "twenty-three" };
240	like($@, qr/\bTypeError\b/, "char typemap");
241	is($orig, $t->{var_char}, "char typemap");
242}
243{
244	my $orig = $t->{var_uint};
245	eval { $t->{var_uint} = -1 };
246	like($@, qr/\bOverflowError\b/, "uint typemap");
247	is($orig, $t->{var_uint}, "uint typemap");
248}
249{
250	my $orig = $t->{var_namet};
251	eval { $t->{var_namet} = '123456' };
252	like($@, qr/\bTypeError\b/, "namet typemap");
253	is($orig, $t->{var_namet}, "namet typemap");
254}
255#t2 = p.vtest(t)
256#if t.var_namet !=  t2.var_namet:
257#  raise RuntimeError, "bad SWIGTYPE* typemap"
258
259is($primitive_types::fixsize, "ho\0la\0\0\0", "FIXSIZE typemap");
260
261$primitive_types::fixsize = 'ho';
262is($primitive_types::fixsize, "ho\0\0\0\0\0\0", "FIXSIZE typemap");
263
264
265my $f = primitive_types::Foo->new(3);
266my $f1 = primitive_types::fptr_val($f);
267my $f2 = primitive_types::fptr_ref($f);
268is($f1->{_a}, $f2->{_a}, "const ptr& typemap");
269  
270
271is(primitive_types::char_foo(1,3), 3, "int typemap");
272
273is(primitive_types::char_foo(1,"hello"), "hello", "char* typemap");
274  
275is(primitive_types::SetPos(1,3), 4, "int typemap");