From 9d10f61cbd680d0d28a26037a78ef5fbe9ca08ce Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Sat, 24 Jan 2004 00:21:02 +0000 Subject: [PATCH] add extensive tests for the primitive types, it will crash in almost every language now, see the file for instructions about some typemaps that need to be fixed git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5680 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/primitive_types.i | 372 ++++++++++++++++++ .../python/primitive_types_runme.py | 166 ++++++++ 2 files changed, 538 insertions(+) create mode 100644 SWIG/Examples/test-suite/primitive_types.i create mode 100644 SWIG/Examples/test-suite/python/primitive_types_runme.py diff --git a/SWIG/Examples/test-suite/primitive_types.i b/SWIG/Examples/test-suite/primitive_types.i new file mode 100644 index 000000000..c35346aab --- /dev/null +++ b/SWIG/Examples/test-suite/primitive_types.i @@ -0,0 +1,372 @@ +// Massive primitive datatype test. +%module(directors="1") primitive_types + + /* + + if your language has problems with MyInt* and/or Hello*, + you need to change the constant reference typemaps from something + like: + + %typemap(in) const char & (char temp), + const signed char & (signed char temp), + const unsigned char & (unsigned char temp), + const short & (short temp), + const unsigned short & (unsigned short temp), + const int & (int temp), + const unsigned int & (unsigned int temp), + const long & (long temp), + const unsigned long & (unsigned long temp), + const long long & ($*1_ltype temp), + const float & (float temp), + const double & (double temp) + %{ temp = ($*1_ltype)$input; $1 = &temp; %} + + to the following: + + %typemap(in) const char & ($basetype temp), + const signed char & ($basetype temp), + const unsigned char & ($basetype temp), + const short & ($basetype temp), + const unsigned short & ($basetype temp), + const int & ($basetype temp), + const unsigned int & ($basetype temp), + const long & ($basetype temp), + const unsigned long & ($basetype temp), + const long long & ($basetype temp), + const float & ($basetype temp), + const double & ($basetype temp) + %{ temp = ($basetype)$input; $1 = &temp; %} + + the other tipical change is to add the enum SWIGTYPE to the + integer throws typemaps: + + %typemap(throws) int, + long, + short, + unsigned int, + unsigned long, + unsigned short, + enum SWIGTYPE { + Tcl_SetObjResult(interp, Tcl_NewIntObj((long) $1)); + SWIG_fail; + } + + or just add the %apply directive after all the typemaps declaration + + %apply int { enum SWIGTYPE }; + + + */ + +// +// Try your language module with and without +// these nowarn flags. +// +#pragma SWIG nowarn=451 +#pragma SWIG nowarn=515 +#pragma SWIG nowarn=509 + +%{ +#include +#include +#include +%} + +%feature("director") TestDirector; + +// Fake integer class, only visible in C++ +%{ + struct MyInt + { + char name[5]; + int val; + + MyInt(int v = 0): val(v) { } + + operator int() const { return val; } + }; +%} +// +// adding applies for incomplete swig type MyInt +// +%apply int { MyInt }; +%apply const int& { const MyInt& }; + + + +// +// These applies shouldn't be needed ....!! +// +%apply const int& { const Hello& }; + +%apply long { pint }; + +// +// Some simple types +%inline %{ + enum Hello { + Hi, Hola + }; + + typedef char namet[5]; + typedef char* pchar; + typedef int* pint; + + char* const def_pchar = "hello"; + + const namet def_namet = "hola";//{'h','o','l','a',0}; + +%} + + +/* all the primitive types */ +#define def_bool 1 +#define def_schar 1 +#define def_uchar 1 +#define def_int 1 +#define def_uint 1 +#define def_short 1 +#define def_ushort 1 +#define def_long 1 +#define def_ulong 1 +#define def_llong 1 +#define def_ullong 1 +#define def_float 1 +#define def_double 1 +#define def_char 'H' +#define def_pint (int*)0 +#define def_sizet 1 +#define def_hello Hola +#define def_myint 1 + +%define %test_prim_types_int(macro, pfx) +/* all the primitive types */ +macro(bool, pfx, bool) +macro(signed char, pfx, schar) +macro(unsigned char, pfx, uchar) +macro(int, pfx, int) +macro(unsigned int, pfx, uint) +macro(short, pfx, short) +macro(unsigned short, pfx, ushort) +macro(long, pfx, long) +macro(unsigned long, pfx, ulong) +macro(long long, pfx, llong) +macro(unsigned long long, pfx, ullong) +macro(float, pfx, float) +macro(double, pfx, double) +macro(char, pfx, char) +%enddef + + +%define %test_prim_types(macro, pfx) +%test_prim_types_int(macro, pfx) +macro(pchar, pfx, pchar) +macro(pint, pfx, pint) +/* these ones should behave like primitive types too */ +macro(size_t, pfx, sizet) +macro(Hello, pfx, hello) +macro(MyInt, pfx, myint) +%enddef + + +/* function passing by value */ +%define val_decl(type, pfx, name) + type pfx##_##name(type x) throw (type) { return x; } +%enddef +/* function passing by ref */ +%define ref_decl(type, pfx, name) + const type& pfx##_##name(const type& x) throw (type) { return x; } +%enddef + +/* C++ constant declaration */ +%define cct_decl(type, pfx, name) + const type pfx##_##name = def##_##name; + const type* pfx##_##name##_p = &pfx##_##name; + const type& pfx##_##name##_r = pfx##_##name; +%enddef + +/* C++ static constant declaration */ +%define stc_decl(type, pfx, name) + static const type pfx##_##name = def##_##name; +%enddef + +/* Swig constant declaration */ +%define sct_decl(type, pfx, name) + %constant type pfx##_##name = def##_##name; +%enddef + +/* variable delaration */ +%define var_decl(type, pfx, name) + type pfx##_##name; +%enddef + +/* virtual function passing by value */ +%define vval_decl(type, pfx, name) + virtual val_decl(type, pfx, name) +%enddef +/* virtual function passing by ref */ +%define vref_decl(type, pfx, name) + virtual ref_decl(type, pfx, name) +%enddef + + +%test_prim_types(sct_decl, sct) + +%inline { + %test_prim_types(val_decl, val); + %test_prim_types(ref_decl, ref); + %test_prim_types(cct_decl, cct); + %test_prim_types(var_decl, var); + + var_decl(namet, var, namet); + + void var_init() + { + var_pchar = 0; + var_pint = 0; + var_namet[0] = 'h'; + } + +} + +/* check variables */ +%define var_check(type, pfx, name) + if (pfx##_##name != def_##name) { + std::ostringstream a; std::ostringstream b; + a << pfx##_##name; + b << def_##name; + if (a.str() != b.str()) { + std::cout << "failing in pfx""_""name : " + << a.str() << " : " << b.str() << std::endl; + // return 0; + } + } +%enddef + +/* check a function call */ +%define call_check(type, pfx, name) + if (pfx##_##name(def_##name) != def_##name) { + std::ostringstream a; std::ostringstream b; + a << pfx##_##name(def_##name); + b << def_##name; + if (a.str() != b.str()) { + std::cout << "failing in pfx""_""name : " + << a.str() << " : " << b.str() << std::endl; + // return 0; + } + } +%enddef + +%define wrp_decl(type, pfx, name) + type wrp##_##pfx##_##name(type x) { + return pfx##_##name(x); + } +%enddef + +/* function passing by value */ +%define ovr_decl(type, pfx, name) + int pfx##_##val(type x) { return 1; } + int pfx##_##ref(const type& x) { return 1; } +%enddef + +#ifdef SWIGPYTHON +%apply (char *STRING, int LENGTH) { (const char *str, size_t len) } +%apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) } +#endif + +%inline { + struct Test + { + Test() + : var_pchar(0), var_pint(0) + { + } + %test_prim_types_int(stc_decl, stc); + %test_prim_types(var_decl, var); + var_decl(namet, var, namet); + + %test_prim_types(val_decl, val); + %test_prim_types(ref_decl, ref); + + int c_check() + { + %test_prim_types(call_check, val); + %test_prim_types(call_check, ref); + return 1; + } + + int v_check() + { + %test_prim_types_int(var_check, stc); + %test_prim_types(var_check, var); + var_check(namet, var, namet); + return 1; + } + + %test_prim_types_int(ovr_decl, ovr); + + const char* stringl(const char *str, size_t len) + { + return str; + } + + int main(size_t argc, const char **argv) + { + return argc; + } + + + }; + + struct TestDirector + { + TestDirector() + : var_pchar(0), var_pint(0) + { + } + + virtual ~TestDirector() + { + } + + %test_prim_types_int(stc_decl, stc); + %test_prim_types(var_decl, var); + var_decl(namet, var, namet); + + %test_prim_types(val_decl, val); + %test_prim_types(ref_decl, ref); + + %test_prim_types(vval_decl, vval); + %test_prim_types(vref_decl, vref); + + %test_prim_types(wrp_decl, vref); + %test_prim_types(wrp_decl, vval); + + int c_check() + { + %test_prim_types(call_check, vval); + %test_prim_types(call_check, vref); + return 1; + } + + int v_check() + { + %test_prim_types_int(var_check, stc); + %test_prim_types(var_check, var); + return 1; + } + + + %test_prim_types_int(ovr_decl, ovr); + + }; + + int v_check() + { + %test_prim_types(var_check, cct); + %test_prim_types(var_check, var); + var_check(namet, var, namet); + return 1; + } + +} diff --git a/SWIG/Examples/test-suite/python/primitive_types_runme.py b/SWIG/Examples/test-suite/python/primitive_types_runme.py new file mode 100644 index 000000000..0976525a3 --- /dev/null +++ b/SWIG/Examples/test-suite/python/primitive_types_runme.py @@ -0,0 +1,166 @@ +from primitive_types import * + +var_init() + +# assigning globals calls +cvar.var_bool = sct_bool +cvar.var_schar = sct_schar +cvar.var_uchar = sct_uchar +cvar.var_int = sct_int +cvar.var_uint = sct_uint +cvar.var_short = sct_short +cvar.var_ushort = sct_ushort +cvar.var_long = sct_long +cvar.var_ulong = sct_ulong +cvar.var_llong = sct_llong +cvar.var_ullong = sct_ullong +cvar.var_float = sct_float +cvar.var_double = sct_double +cvar.var_char = sct_char +cvar.var_pchar = sct_pchar +cvar.var_pint = sct_pint +cvar.var_sizet = sct_sizet +cvar.var_hello = sct_hello +cvar.var_myint = sct_myint +cvar.var_namet = def_namet + +v_check() + +def pyerror(name, val, cte): + print "bad val/cte", name, val, cte + raise RuntimeError + pass + +if cvar.var_bool != cct_bool: pyerror("bool", var_bool, cct_bool) +if cvar.var_schar != cct_schar: pyerror("schar", var_schar, cct_schar) +if cvar.var_uchar != cct_uchar: pyerror("uchar", var_uchar, cct_uchar) +if cvar.var_int != cct_int: pyerror("int", var_int, cct_int) +if cvar.var_uint != cct_uint: pyerror("uint", var_uint, cct_uint) +if cvar.var_short != cct_short: pyerror("short", var_short, cct_short) +if cvar.var_ushort != cct_ushort: pyerror("ushort", var_ushort, cct_ushort) +if cvar.var_long != cct_long: pyerror("long", var_long, cct_long) +if cvar.var_ulong != cct_ulong: pyerror("ulong", var_ulong, cct_ulong) +if cvar.var_llong != cct_llong: pyerror("llong", var_llong, cct_llong) +if cvar.var_ullong != cct_ullong: pyerror("ullong", var_ullong, cct_ullong) +if cvar.var_float != cct_float: pyerror("float", var_float, cct_float) +if cvar.var_double != cct_double: pyerror("double", var_double, cct_double) +if cvar.var_char != cct_char: pyerror("char", var_char, cct_char) +if cvar.var_pchar != cct_pchar: pyerror("pchar", var_pchar, cct_pchar) +if cvar.var_pint != cct_pint: pyerror("pint", var_pint, cct_pint) +if cvar.var_sizet != cct_sizet: pyerror("sizet", var_sizet, cct_sizet) +if cvar.var_hello != cct_hello: pyerror("hello", var_hello, cct_hello) +if cvar.var_myint != cct_myint: pyerror("myint", var_myint, cct_myint) +if cvar.var_namet != def_namet: pyerror("name", var_namet, def_namet) + +class PyTest (TestDirector): + def __init__(self): + TestDirector.__init__(self) + pass + def ident(self, x): + return x + + def vval_bool(self, x): return self.ident(x) + def vval_schar(self, x): return self.ident(x) + def vval_uchar(self, x): return self.ident(x) + def vval_int(self, x): return self.ident(x) + def vval_uint(self, x): return self.ident(x) + def vval_short(self, x): return self.ident(x) + def vval_ushort(self, x): return self.ident(x) + def vval_long(self, x): return self.ident(x) + def vval_ulong(self, x): return self.ident(x) + def vval_llong(self, x): return self.ident(x) + def vval_ullong(self, x): return self.ident(x) + def vval_float(self, x): return self.ident(x) + def vval_double(self, x): return self.ident(x) + def vval_char(self, x): return self.ident(x) + def vval_pchar(self, x): return self.ident(x) + def vval_pint(self, x): return self.ident(x) + def vval_sizet(self, x): return self.ident(x) + def vval_hello(self, x): return self.ident(x) + def vval_myint(self, x): return self.ident(x) + + def vref_bool(self, x): return self.ident(x) + def vref_schar(self, x): return self.ident(x) + def vref_uchar(self, x): return self.ident(x) + def vref_int(self, x): return self.ident(x) + def vref_uint(self, x): return self.ident(x) + def vref_short(self, x): return self.ident(x) + def vref_ushort(self, x): return self.ident(x) + def vref_long(self, x): return self.ident(x) + def vref_ulong(self, x): return self.ident(x) + def vref_llong(self, x): return self.ident(x) + def vref_ullong(self, x): return self.ident(x) + def vref_float(self, x): return self.ident(x) + def vref_double(self, x): return self.ident(x) + def vref_char(self, x): return self.ident(x) + def vref_pchar(self, x): return self.ident(x) + def vref_pint(self, x): return self.ident(x) + def vref_sizet(self, x): return self.ident(x) + def vref_hello(self, x): return self.ident(x) + def vref_myint(self, x): return self.ident(x) + + pass + + +t = Test() +p = PyTest() + + +# internal call check +if t.c_check() != p.c_check(): + raise RuntimeError, "bad director" + +p.var_bool = p.stc_bool +p.var_schar = p.stc_schar +p.var_uchar = p.stc_uchar +p.var_int = p.stc_int +p.var_uint = p.stc_uint +p.var_short = p.stc_short +p.var_ushort = p.stc_ushort +p.var_long = p.stc_long +p.var_ulong = p.stc_ulong +p.var_llong = p.stc_llong +p.var_ullong = p.stc_ullong +p.var_float = p.stc_float +p.var_double = p.stc_double +p.var_char = p.stc_char +p.var_pchar = sct_pchar +p.var_pint = sct_pint +p.var_sizet = sct_sizet +p.var_hello = sct_hello +p.var_myint = sct_myint +p.var_namet = def_namet + +p.v_check() + + +t.var_bool = t.stc_bool +t.var_schar = t.stc_schar +t.var_uchar = t.stc_uchar +t.var_int = t.stc_int +t.var_uint = t.stc_uint +t.var_short = t.stc_short +t.var_ushort = t.stc_ushort +t.var_long = t.stc_long +t.var_ulong = t.stc_ulong +t.var_llong = t.stc_llong +t.var_ullong = t.stc_ullong +t.var_float = t.stc_float +t.var_double = t.stc_double +t.var_char = t.stc_char +t.var_pchar = sct_pchar +t.var_pint = sct_pint +t.var_sizet = sct_sizet +t.var_hello = sct_hello +t.var_myint = sct_myint +t.var_namet = def_namet + +t.v_check() + +if def_namet != 'hola': + raise RuntimeError, "bad namet" + +t.var_namet = 'holac' + +if t.var_namet != 'holac': + raise RuntimeError, "bad namet"