From 223f7ef967be294bbee61eb6cbfc397b92e07853 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 14 Nov 2001 15:17:11 +0000 Subject: [PATCH] Added tests for void returns [SVN r11676] --- bind_test.cpp | 133 +++++++++++++++++++++++++++++++++ mem_fn_void_test.cpp | 173 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 306 insertions(+) create mode 100644 mem_fn_void_test.cpp diff --git a/bind_test.cpp b/bind_test.cpp index 8e9d276..63493ed 100644 --- a/bind_test.cpp +++ b/bind_test.cpp @@ -261,6 +261,8 @@ void member_function_test() BOOST_TEST( x.hash == 23558 ); } +// + void nested_bind_test() { using namespace boost; @@ -275,6 +277,136 @@ void nested_bind_test() BOOST_TEST( bind(f_1, bind(f_0))() == 17041L ); } +// + +// + +struct V +{ + mutable unsigned int hash; + + V(): hash(0) {} + + void f0() { f1(17); } + void g0() const { g1(17); } + + void f1(int a1) { hash = (hash * 17041 + a1) % 32768; } + void g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; } + + void f2(int a1, int a2) { f1(a1); f1(a2); } + void g2(int a1, int a2) const { g1(a1); g1(a2); } + + void f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); } + void g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); } + + void f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); } + void g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); } + + void f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); } + void g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); } + + void f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); } + void g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); } + + void f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); } + void g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); } + + void f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); } + void g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); } +}; + +void void_returns_test() +{ + using namespace boost; + + V v; + + // 0 + + bind(&V::f0, &v)(); + bind(&V::f0, ref(v))(); + + bind(&V::g0, &v)(); + bind(&V::g0, v)(); + bind(&V::g0, ref(v))(); + + // 1 + + bind(&V::f1, &v, 1)(); + bind(&V::f1, ref(v), 1)(); + + bind(&V::g1, &v, 1)(); + bind(&V::g1, v, 1)(); + bind(&V::g1, ref(v), 1)(); + + // 2 + + bind(&V::f2, &v, 1, 2)(); + bind(&V::f2, ref(v), 1, 2)(); + + bind(&V::g2, &v, 1, 2)(); + bind(&V::g2, v, 1, 2)(); + bind(&V::g2, ref(v), 1, 2)(); + + // 3 + + bind(&V::f3, &v, 1, 2, 3)(); + bind(&V::f3, ref(v), 1, 2, 3)(); + + bind(&V::g3, &v, 1, 2, 3)(); + bind(&V::g3, v, 1, 2, 3)(); + bind(&V::g3, ref(v), 1, 2, 3)(); + + // 4 + + bind(&V::f4, &v, 1, 2, 3, 4)(); + bind(&V::f4, ref(v), 1, 2, 3, 4)(); + + bind(&V::g4, &v, 1, 2, 3, 4)(); + bind(&V::g4, v, 1, 2, 3, 4)(); + bind(&V::g4, ref(v), 1, 2, 3, 4)(); + + // 5 + + bind(&V::f5, &v, 1, 2, 3, 4, 5)(); + bind(&V::f5, ref(v), 1, 2, 3, 4, 5)(); + + bind(&V::g5, &v, 1, 2, 3, 4, 5)(); + bind(&V::g5, v, 1, 2, 3, 4, 5)(); + bind(&V::g5, ref(v), 1, 2, 3, 4, 5)(); + + // 6 + + bind(&V::f6, &v, 1, 2, 3, 4, 5, 6)(); + bind(&V::f6, ref(v), 1, 2, 3, 4, 5, 6)(); + + bind(&V::g6, &v, 1, 2, 3, 4, 5, 6)(); + bind(&V::g6, v, 1, 2, 3, 4, 5, 6)(); + bind(&V::g6, ref(v), 1, 2, 3, 4, 5, 6)(); + + // 7 + + bind(&V::f7, &v, 1, 2, 3, 4, 5, 6, 7)(); + bind(&V::f7, ref(v), 1, 2, 3, 4, 5, 6, 7)(); + + bind(&V::g7, &v, 1, 2, 3, 4, 5, 6, 7)(); + bind(&V::g7, v, 1, 2, 3, 4, 5, 6, 7)(); + bind(&V::g7, ref(v), 1, 2, 3, 4, 5, 6, 7)(); + + // 8 + + bind(&V::f8, &v, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&V::f8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)(); + + bind(&V::g8, &v, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&V::g8, v, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&V::g8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)(); + + BOOST_TEST( v.hash == 23558 ); +} + +// + int test_main(int, char * []) { function_test(); @@ -284,6 +416,7 @@ int test_main(int, char * []) #endif member_function_test(); nested_bind_test(); + void_returns_test(); return 0; } diff --git a/mem_fn_void_test.cpp b/mem_fn_void_test.cpp new file mode 100644 index 0000000..793cf0b --- /dev/null +++ b/mem_fn_void_test.cpp @@ -0,0 +1,173 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// mem_fn_void_test.cpp - a test for mem_fn.hpp + void returns +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + void f0() { f1(17); } + void g0() const { g1(17); } + + void f1(int a1) { hash = (hash * 17041 + a1) % 32768; } + void g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; } + + void f2(int a1, int a2) { f1(a1); f1(a2); } + void g2(int a1, int a2) const { g1(a1); g1(a2); } + + void f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); } + void g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); } + + void f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); } + void g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); } + + void f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); } + void g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); } + + void f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); } + void g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); } + + void f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); } + void g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); } + + void f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); } + void g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); } +}; + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + using boost::mem_fn; + + X x; + + X const & rcx = x; + X const * pcx = &x; + + boost::shared_ptr sp(new X); + + mem_fn(&X::f0)(x); + mem_fn(&X::f0)(&x); + mem_fn(&X::f0)(sp); + + mem_fn(&X::g0)(x); + mem_fn(&X::g0)(rcx); + mem_fn(&X::g0)(&x); + mem_fn(&X::g0)(pcx); + mem_fn(&X::g0)(sp); + + mem_fn(&X::f1)(x, 1); + mem_fn(&X::f1)(&x, 1); + mem_fn(&X::f1)(sp, 1); + + mem_fn(&X::g1)(x, 1); + mem_fn(&X::g1)(rcx, 1); + mem_fn(&X::g1)(&x, 1); + mem_fn(&X::g1)(pcx, 1); + mem_fn(&X::g1)(sp, 1); + + mem_fn(&X::f2)(x, 1, 2); + mem_fn(&X::f2)(&x, 1, 2); + mem_fn(&X::f2)(sp, 1, 2); + + mem_fn(&X::g2)(x, 1, 2); + mem_fn(&X::g2)(rcx, 1, 2); + mem_fn(&X::g2)(&x, 1, 2); + mem_fn(&X::g2)(pcx, 1, 2); + mem_fn(&X::g2)(sp, 1, 2); + + mem_fn(&X::f3)(x, 1, 2, 3); + mem_fn(&X::f3)(&x, 1, 2, 3); + mem_fn(&X::f3)(sp, 1, 2, 3); + + mem_fn(&X::g3)(x, 1, 2, 3); + mem_fn(&X::g3)(rcx, 1, 2, 3); + mem_fn(&X::g3)(&x, 1, 2, 3); + mem_fn(&X::g3)(pcx, 1, 2, 3); + mem_fn(&X::g3)(sp, 1, 2, 3); + + mem_fn(&X::f4)(x, 1, 2, 3, 4); + mem_fn(&X::f4)(&x, 1, 2, 3, 4); + mem_fn(&X::f4)(sp, 1, 2, 3, 4); + + mem_fn(&X::g4)(x, 1, 2, 3, 4); + mem_fn(&X::g4)(rcx, 1, 2, 3, 4); + mem_fn(&X::g4)(&x, 1, 2, 3, 4); + mem_fn(&X::g4)(pcx, 1, 2, 3, 4); + mem_fn(&X::g4)(sp, 1, 2, 3, 4); + + mem_fn(&X::f5)(x, 1, 2, 3, 4, 5); + mem_fn(&X::f5)(&x, 1, 2, 3, 4, 5); + mem_fn(&X::f5)(sp, 1, 2, 3, 4, 5); + + mem_fn(&X::g5)(x, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(rcx, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(&x, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(pcx, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(sp, 1, 2, 3, 4, 5); + + mem_fn(&X::f6)(x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::f6)(&x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::f6)(sp, 1, 2, 3, 4, 5, 6); + + mem_fn(&X::g6)(x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(rcx, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(&x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(pcx, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(sp, 1, 2, 3, 4, 5, 6); + + mem_fn(&X::f7)(x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::f7)(&x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::f7)(sp, 1, 2, 3, 4, 5, 6, 7); + + mem_fn(&X::g7)(x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(rcx, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(&x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(pcx, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(sp, 1, 2, 3, 4, 5, 6, 7); + + mem_fn(&X::f8)(x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::f8)(&x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::f8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); + + mem_fn(&X::g8)(x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(rcx, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(&x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(pcx, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); + + return detect_errors(x.hash == 17610 && sp->hash == 2155); +}