Prev Next check_swig_xam.py

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Swig Example / Test: Python Script

Load the Module

import py_swig_xam

Initialize Error Count

error_count = 0

factorial_by_value
if py_swig_xam.factorial_by_value(4) == 24 :
     print('py_swig_xam.factorial_by_value: OK')
else :
     print('py_swig_xam.factorial_by_value: Error')
     error_count = error_count + 1
see C++ factorial_by_value .

message_of_void
if py_swig_xam.message_of_void() == 'OK' :
     print('py_swig_xam.message_of_void: OK')
else :
     print('py_swig_xam.message_of_void: Error')
     error_count = error_count + 1
see C++ message_of_void .

int_class
obj = py_swig_xam.int_class()
py_swig_xam.add_by_ptr(3, 4, obj)
if obj.value() == 7 :
     print('py_swig_xam.add_by_ptr: OK')
else :
     print('py_swig_xam.add_by_ptr: Error')
     error_count = error_count + 1
see Swig int_class and C++ add_by_ptr .

int_array_ptr
n   = 10
array_ptr = py_swig_xam.new_int_array_ptr(n)
for i in range(n) :
     py_swig_xam.int_array_ptr_setitem(array_ptr, i, 2 * i)

if py_swig_xam.max_array_by_ptr(n, array_ptr) == 18 :
     print('py_swig_xam.max_array_by_ptr: pointer: OK')
else :
     print('py_swig_xam.max_array_by_ptr: pointer: Error')
     error_count = error_count + 1
py_swig_xam.delete_int_array_ptr(array_ptr)
see Swig int_array_ptr and C++ max_array_by_ptr .

int_array_class
n   = 10
array_obj = py_swig_xam.int_array_class(n)
for i in range(n) :
     array_obj[i] = 2 * i

if py_swig_xam.max_array_by_ptr(n, array_obj) == 18 :
     print('py_swig_xam.max_array_by_ptr: class: OK')
else :
     print('py_swig_xam.max_array_by_ptr: class: Error')
     error_count = error_count + 1
see Swig int_array_class and C++ max_array_by_ptr .

vector_double
n   = 10
vec = py_swig_xam.vector_double(n)
for i in range(n) :
     vec[i] = 2.0 * i;

if py_swig_xam.max_std_vector_double(vec) == 18.0 :
     print('py_swig_xam.max_std_vector_double: class: OK')
else :
     print('py_swig_xam.max_std_vector_double: class: Error')
     error_count = error_count + 1
see Swig vector_double and C++ max_std_vector_double .

raise_exception
try :
     py_swig_xam.raise_exception('test message')
     message = ''
except :
     message = py_swig_xam.raise_exception('')
if message == 'test message' :
     print('py_swig_xam.raise_exception: OK')
else :
     print('py_swig_xam.raise_exception.message_of_void: Error')
     error_count = error_count + 1
see C++ raise_exception .

normal_class
two   = py_swig_xam.normal_class(2)
three = py_swig_xam.normal_class(3)
five  = two + three
ok       = five == py_swig_xam.normal_class(5)
ok       = ok and 4 < five.value()  and five.value() < 6
if ok :
     print('py_swig_xam.normal_class: OK')
else :
     print('py_swig_xam.normal_class: Error')
     error_count = error_count + 1
see C++ swig_xam_normal_class .

double_class
two   = py_swig_xam.double_class(2.0)
three = py_swig_xam.double_class(3.0)
five  = two + three
ok       = five == py_swig_xam.double_class(5.0)
ok       = ok and 4.5 < five.value()  and five.value() < 5.5
if ok :
     print('py_swig_xam.double_class: OK')
else :
     print('py_swig_xam.double_class: Error')
     error_count = error_count + 1
see Swig double_class .

Set Exit Code

import sys
sys.exit(error_count)

Input File: swig_xam/python/check_swig_xam.py