diff --git a/SWIG/Examples/test-suite/common.mk b/SWIG/Examples/test-suite/common.mk index 8876c0eaa..0c11d4b3a 100644 --- a/SWIG/Examples/test-suite/common.mk +++ b/SWIG/Examples/test-suite/common.mk @@ -91,6 +91,7 @@ CPP_TEST_CASES += \ constructor_exception \ constructor_explicit \ constructor_value \ + contract \ conversion \ conversion_namespace \ conversion_ns_template \ diff --git a/SWIG/Examples/test-suite/contract.i b/SWIG/Examples/test-suite/contract.i new file mode 100644 index 000000000..10203c668 --- /dev/null +++ b/SWIG/Examples/test-suite/contract.i @@ -0,0 +1,177 @@ +%module contract + + +%contract test_preassert(int a, int b) { +require: + a > 0; + b > 0; +} + +%contract test_postassert(int a) { +ensure: + test_postassert > 0; +} + +%contract test_prepost(int a, int b) { +require: + a > 0; +ensure: + test_prepost > 0; +} + +%inline %{ + +int test_preassert(int x, int y) { + if ((x > 0) && (y > 0)) return 1; + return 0; +} + +int test_postassert(int x) { + return x; +} + +int test_prepost(int x, int y) { + return x+y; +} +%} + +/* Class tests */ + +%contract Foo::test_preassert(int x, int y) { + require: + x > 0; + y > 0; +} + +%contract Foo::test_postassert(int a) { + ensure: + test_postassert > 0; +} + +%contract Foo::test_prepost(int a, int b) { + require: + a > 0; + ensure: + test_prepost > 0; +} + +%contract Foo::stest_prepost(int a, int b) { + require: + a > 0; + ensure: + stest_prepost > 0; +} + +%contract Bar::test_prepost(int c, int d) { + require: + d > 0; +} + +%inline %{ +class Foo { +public: + virtual int test_preassert(int x, int y) { + if ((x > 0) && (y > 0)) return 1; + return 0; + } + virtual int test_postassert(int x) { + return x; + } + virtual int test_prepost(int x, int y) { + return x+y; + } + static int stest_prepost(int x, int y) { + return x+y; + } + }; + +class Bar : public Foo { +public: + virtual int test_prepost(int x, int y) { + return x+y; + } +}; + +%} + +/* Multiple inheritance test */ + +%contract A::foo(int i, int j, int k, int l, int m) { + require: + i > 0; + j > 0; + ensure: + foo > 0; +} + +%contract B::bar(int x, int y, int z, int w, int v) { + require: + w > 0; + v > 0; + ensure: + bar > 0; +} + +%contract C::foo(int a, int b, int c, int d, int e) { + require: + c > 0; + d > 0; + ensure: + foo > 0; +} + +%contract D::foo(int, int, int, int, int x) { + require: + x > 0; +} + +%contract D::bar(int a, int b, int c, int, int) { + require: + a > 0; + b > 0; + c > 0; +} + +%inline %{ + class A { + public: + virtual int foo(int a, int b, int c, int d, int e) { + if ((a > 0) && (b > 0) && (c > 0) && (d > 0) && (e > 0)) { + return 1; + } + return 0; + } + }; + + class B { + public: + virtual int bar(int a, int b, int c, int d, int e) { + if ((a > 0) && (b > 0) && (c > 0) && (d > 0) && (e > 0)) { + return 1; + } + return 0; + } + }; + + class C : public A, public B { + public: + virtual int foo(int a, int b, int c, int d, int e) { + return A::foo(a,b,c,d,e); + } + virtual int bar(int a, int b, int c, int d, int e) { + return B::bar(a,b,c,d,e); + } + }; + + class D : public C { + public: + public: + virtual int foo(int a, int b, int c, int d, int e) { + return C::foo(a,b,c,d,e); + } + virtual int bar(int a, int b, int c, int d, int e) { + return C::bar(a,b,c,d,e); + } + }; + %} + diff --git a/SWIG/Examples/test-suite/python/contract_runme.py b/SWIG/Examples/test-suite/python/contract_runme.py new file mode 100644 index 000000000..9ded5bb5b --- /dev/null +++ b/SWIG/Examples/test-suite/python/contract_runme.py @@ -0,0 +1,135 @@ +import contract + +contract.test_preassert(1,2) +try: + contract.test_preassert(-1) + print "Failed! Preassertions are broken" +except: + pass + +contract.test_postassert(3) +try: + contract.test_postassert(-3) + print "Failed! Postassertions are broken" +except: + pass + +contract.test_prepost(2,3) +contract.test_prepost(5,-4) +try: + contract.test_prepost(-3,4) + print "Failed! Preassertions are broken" +except: + pass + +try: + contract.test_prepost(4,-10) + print "Failed! Postassertions are broken" + +except: + pass + +f = contract.Foo() +f.test_preassert(4,5) +try: + f.test_preassert(-2,3) + print "Failed! Method preassertion." +except: + pass + +f.test_postassert(4) +try: + f.test_postassert(-4) + print "Failed! Method postassertion" +except: + pass + +f.test_prepost(3,4) +f.test_prepost(4,-3) +try: + f.test_prepost(-4,2) + print "Failed! Method preassertion." +except: + pass + +try: + f.test_prepost(4,-10) + print "Failed! Method postassertion." +except: + pass + +contract.Foo_stest_prepost(4,0) +try: + contract.Foo_stest_prepost(-4,2) + print "Failed! Static method preassertion" +except: + pass + +try: + contract.Foo_stest_prepost(4,-10) + print "Failed! Static method posteassertion" +except: + pass + +b = contract.Bar() +try: + b.test_prepost(2,-4) + print "Failed! Inherited preassertion." +except: + pass + + +d = contract.D() +try: + d.foo(-1,1,1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.foo(1,-1,1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.foo(1,1,-1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.foo(1,1,1,-1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.foo(1,1,1,1,-1) + print "Failed! Inherited preassertion (D)." +except: + pass + + +try: + d.bar(-1,1,1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.bar(1,-1,1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.bar(1,1,-1,1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.bar(1,1,1,-1,1) + print "Failed! Inherited preassertion (D)." +except: + pass +try: + d.bar(1,1,1,1,-1) + print "Failed! Inherited preassertion (D)." +except: + pass +