2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 18:12:43 +00:00

- Unit tests for the examples

[SVN r17987]
This commit is contained in:
Bruno da Silva de Oliveira
2003-03-19 02:47:29 +00:00
parent 39f243f76b
commit 29d537571b
31 changed files with 277 additions and 44 deletions

1
pyste/example/.cvsignore Normal file
View File

@@ -0,0 +1 @@
*.cpp

View File

@@ -1,3 +1,5 @@
namespace basic {
struct C
{
virtual int f(int x = 10)
@@ -19,3 +21,5 @@ int call_f(C& c, int x)
{
return c.f(x);
}
}

View File

@@ -1,2 +1,2 @@
Class('C', 'basic.h')
Function('call_f', 'basic.h')
Class('basic::C', 'basic.h')
Function('basic::call_f', 'basic.h')

View File

@@ -1,4 +1,5 @@
namespace test {
namespace enums {
enum color { red, blue };
struct X

View File

@@ -1,7 +1,7 @@
color = Enum('test::color', 'enums.h')
color = Enum('enums::color', 'enums.h')
rename(color.red, 'Red')
rename(color.blue, 'Blue')
X = Class('test::X', 'enums.h')
X = Class('enums::X', 'enums.h')
rename(X.choices.bad, 'Bad')
rename(X.choices.good, 'Good')
rename(X.choices, 'Choices')

View File

@@ -1,23 +1,26 @@
#include <iostream>
#include <map>
#include <string>
namespace header_test {
enum choice { red, blue };
void print_choice(choice c)
std::string choice_str(choice c)
{
std::map<choice, std::string> choice_map;
choice_map[red] = "red";
choice_map[blue] = "blue";
std::cout << "You chose: " << choice_map[c] << std::endl;
return choice_map[c];
}
struct C
{
choice c;
void print_()
std::string get()
{
print_choice(c);
return choice_str(c);
}
};
}

View File

@@ -1,4 +1,5 @@
namespace nested {
struct X
{
struct Y
@@ -19,3 +20,5 @@ int X::staticXValue = 10;
int X::Y::staticYValue = 20;
typedef X Root;
}

View File

@@ -1 +1 @@
Class('Root', 'nested.h')
Class('nested::Root', 'nested.h')

View File

@@ -1,12 +0,0 @@
Include('iostream')
test = Wrapper('sum',
'''
const C sum(const C&, const C&)
{
std::cout << "sum!" << std::endl;
return C();
}
'''
)
C = Class('C', 'operator.h')
set_wrapper(C.operator['+'], test)

View File

@@ -1,5 +1,6 @@
#include <iostream>
namespace operators {
struct C
{
@@ -16,14 +17,15 @@ struct C
{
return value;
}
double operator()()
{
return x;
return C::x;
}
double operator()(double other)
{
return x + other;
return C::x + other;
}
@@ -38,10 +40,4 @@ const C operator*(const C& lhs, const C& rhs)
return c;
}
std::ostream& operator <<( std::ostream& s, const C& c)
{
std::cout << "here";
s << "C instance: ";
return s;
}
}

View File

@@ -0,0 +1 @@
Class('operators::C', 'operators.h')

View File

@@ -1,4 +1,5 @@
namespace templates {
template <class X, class Y>
struct Point
{
@@ -6,3 +7,4 @@ struct Point
Y y;
};
}

View File

@@ -1,8 +1,9 @@
Point = Template('Point', 'templates.h')
Point = Template('templates::Point', 'templates.h')
rename(Point.x, 'i')
rename(Point.y, 'j')
IPoint = Point('int double')
FPoint = Point('double int')
FPoint = Point('double int', 'FPoint')
rename(IPoint, 'IPoint')
rename(IPoint.x, '_x_')
rename(IPoint.x, 'x')
rename(IPoint.y, 'y')

16
pyste/example/unions.h Normal file
View File

@@ -0,0 +1,16 @@
namespace unions {
class UnionTest
{
public:
union // unions are not supported for now
{
int i;
short s1;
short s2;
} mBad;
int mGood;
};
}

View File

@@ -0,0 +1,2 @@
UnionTest = Class('unions::UnionTest', 'unions.h')
exclude(UnionTest.mBad)

View File

@@ -1,4 +1,5 @@
namespace virtual_ {
struct C
{
public:
@@ -21,3 +22,4 @@ private:
int call_f(C& c) { return c.f(); }
}

View File

@@ -1 +1,2 @@
Class('C', 'virtual.h')
Class('virtual_::C', 'virtual.h')
Function('virtual_::call_f', 'virtual.h')

View File

@@ -4,6 +4,8 @@
#include <vector>
namespace wrappertest {
std::vector<int> Range(int count)
{
std::vector<int> v;
@@ -32,4 +34,6 @@ struct C
}
};
}
#endif

View File

@@ -1,15 +1,15 @@
Include('wrappertest_wrappers.h')
f = Function('Range', 'wrappertest.h')
f = Function('wrappertest::Range', 'wrappertest.h')
set_wrapper(f, 'RangeWrapper')
mul = Wrapper('MulWrapper',
'''
list MulWrapper(C& c, int value){
list MulWrapper(wrappertest::C& c, int value){
return VectorToList(c.Mul(value));
}
'''
)
C = Class('C', 'wrappertest.h')
C = Class('wrappertest::C', 'wrappertest.h')
set_wrapper(C.Mul, mul)

View File

@@ -20,7 +20,7 @@ list VectorToList(const std::vector<T> & v)
}
list RangeWrapper(int count){
return VectorToList(Range(count));
return VectorToList(wrappertest::Range(count));
}
#endif

View File

@@ -0,0 +1,19 @@
@echo off
setlocal
set MODULE_NAME=%1
set PYSTE_FILE=%2
set BOOST_ROOT=d:/programming/libraries/boost-cvs
set PYTHON_ROOT=c:/python
set STLPORT_ROOT=d:/programming/libraries/stlport-4.5.3
set PYSTE_FILE_DIR=%@PATH[%PYSTE_FILE]
python ../src/pyste.py -I%PYSTE_FILE_DIR --out=%MODULE_NAME.cpp --module=%MODULE_NAME %PYSTE_FILE
icl /nologo /LD /GR /GX -I%PYSTE_FILE_DIR -I%STLPORT_ROOT/stlport -I%BOOST_ROOT/boost -I%PYTHON_ROOT/include %MODULE_NAME.cpp /link /libpath:%PYTHON_ROOT/libs /libpath:%BOOST_ROOT/lib /libpath:%STLPORT_ROOT/lib boost_python.lib
rm %MODULE_NAME.cpp
rm %MODULE_NAME.exp
rm %MODULE_NAME.lib
rm %MODULE_NAME.obj
endlocal

View File

@@ -0,0 +1,27 @@
import unittest
import os
class BasicExampleTest(unittest.TestCase):
def testIt(self):
from basic import C, call_f
class D(C):
def f(self, x=10):
return x+1
d = D()
c = C()
self.assertEqual(c.f(), 20)
self.assertEqual(c.f(3), 6)
self.assertEqual(d.f(), 11)
self.assertEqual(d.f(3), 4)
self.assertEqual(call_f(c), 20)
self.assertEqual(call_f(c, 4), 8)
self.assertEqual(call_f(d), 11)
self.assertEqual(call_f(d, 3), 4)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,18 @@
import unittest
from enums import *
class EnumsTest(unittest.TestCase):
def testIt(self):
self.assertEqual(int(color.Red), 0)
self.assertEqual(int(color.Blue), 1)
self.assertEqual(int(X.Choices.Good), 1)
self.assertEqual(int(X.Choices.Bad), 2)
x = X()
self.assertEqual(x.set(x.Choices.Good), 1)
self.assertEqual(x.set(x.Choices.Bad), 2)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,15 @@
import unittest
from header_test import *
class HeaderTest(unittest.TestCase):
def testIt(self):
self.assertEqual(choice.red, 0)
self.assertEqual(choice.blue, 1)
self.assertEqual(choice_str(choice.blue), 'blue')
self.assertEqual(choice_str(choice.red), 'red')
c = C()
c.c = choice.blue
self.assertEqual(c.get(), 'blue')
c.c = choice.red
self.assertEqual(c.get(), 'red')

View File

@@ -0,0 +1,15 @@
import unittest
from nested import *
class NestedTest(unittest.TestCase):
def testIt(self):
self.assertEqual(Root.staticXValue, 10)
self.assertEqual(Root.Y.staticYValue, 20)
z = Root.Y.Z()
z.valueZ = 3
self.assertEqual(z.valueZ, 3)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,25 @@
import unittest
from operators import *
class OperatorTest(unittest.TestCase):
def testIt(self):
c = C()
c.value = 3.0
d = C()
d.value = 2.0
self.assertEqual(c.x, 10)
self.assertEqual(C.x, 10)
self.assertEqual(C.x, 10)
self.assertEqual((c * d).value, 6.0)
self.assertEqual((c + d).value, 5.0)
self.assertEqual(int(c), 3)
self.assertEqual(int(d), 2)
self.assertEqual(c(), 10)
self.assertEqual(d(), 10)
self.assertEqual(c(3.0), 13.0)
self.assertEqual(d(6.0), 16.0)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,26 @@
import unittest
from templates import *
class TemplatesTest(unittest.TestCase):
def testIt(self):
fp = FPoint()
fp.i = 3.0
fp.j = 4
ip = IPoint()
ip.x = 10
ip.y = 3.0
self.assertEqual(fp.i, 3.0)
self.assertEqual(fp.j, 4)
self.assertEqual(ip.x, 10)
self.assertEqual(ip.y, 3.0)
self.assertEqual(type(fp.i), float)
self.assertEqual(type(fp.j), int)
self.assertEqual(type(ip.x), int)
self.assertEqual(type(ip.y), float)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,31 @@
import unittest
from virtual import *
class VirtualTest(unittest.TestCase):
def testIt(self):
class D(C):
def f_abs(self):
return 3
class E(C):
def f(self):
return 10
def name(self):
return 'E'
d = D()
e = E()
self.assertEqual(d.f(), 3)
self.assertEqual(call_f(d), 3)
self.assertEqual(e.f(), 10)
self.assertEqual(call_f(e), 10)
self.assertEqual(d.get_name(), 'C')
self.assertEqual(e.get_name(), 'E')
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,11 @@
import unittest
from wrappertest import *
class WrapperTest(unittest.TestCase):
def testIt(self):
self.assertEqual(Range(10), range(10))
self.assertEqual(C().Mul(10), [x*10 for x in range(10)])
if __name__ == '__main__':
unittest.main()

View File

@@ -11,4 +11,5 @@ if __name__ == '__main__':
module = __import__(os.path.splitext(name)[0])
tests.append(loader.loadTestsFromModule(module))
runner = unittest.TextTestRunner()
runner.run(unittest.TestSuite(tests))
result = runner.run(unittest.TestSuite(tests))
sys.exit(not result.wasSuccessful())

20
pyste/tests/test_all.bat Normal file
View File

@@ -0,0 +1,20 @@
@echo off
call build_pyste_nt basic ../example/basic.pyste
call build_pyste_nt enums ../example/enums.pyste
call build_pyste_nt header_test ../example/header_test.pyste
call build_pyste_nt nested ../example/nested.pyste
call build_pyste_nt operators ../example/operators.pyste
call build_pyste_nt templates ../example/templates.pyste
call build_pyste_nt virtual ../example/virtual.pyste
call build_pyste_nt wrappertest ../example/wrappertest.pyste
call build_pyste_nt unions ../example/unions.pyste
runtests.py
if errorlevel != 0 goto end
rm *.dll
rm *.pyc
:end