fixed a bug with old value copies from .old(...) by storing them in a queue instead of a stack (same was already done for old value copies from init code outside .old(...))

This commit is contained in:
U-CODE1\310206419
2016-11-26 19:09:03 -08:00
parent e7a64edfb2
commit 58b8b96a2a
8 changed files with 262 additions and 326 deletions

View File

@@ -118,6 +118,7 @@ test-suite public_function :
[ subdir-run public_function : virtual_sparse ]
[ subdir-run public_function : virtual_access ]
[ subdir-run public_function : virtual_access_multi ]
[ subdir-run public_function : old_virtual ]
[ subdir-run public_function : protected ]
[ subdir-compile-fail public_function : protected_error ]

View File

@@ -74,7 +74,6 @@ result_type& t<Id>::f(s_type& s, boost::contract::virtual_* v) {
.postcondition([&] (result_type const& result) {
out << Id << "::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(z.value == old_z->value + old_s->value);
std::clog << Id << "::f::post OUT" << std::endl;
BOOST_CONTRACT_ASSERT(s.value.find(old_z->value) !=
std::string::npos);
BOOST_CONTRACT_ASSERT(result.value == old_s->value);
@@ -90,15 +89,6 @@ result_type& t<Id>::f(s_type& s, boost::contract::virtual_* v) {
return result;
}
struct u {
u() { std::cout << "!!!!! u default ctor" << std::endl; }
u(u const&) { std::cout << "!!!!! u copy ctor" << std::endl; }
~u() { std::cout << "!!!!! u dtor" << std::endl; }
private:
u& operator=(u const&);
};
// Test base with other bases, multiple inheritance, and no subcontracting from
// protected and private bases (even if fully contracted).
struct c
@@ -119,9 +109,7 @@ struct c
typedef boost::contract::test::detail::counter<y_tag, std::string> y_type;
y_type y;
c() : uu() { y.value = "c"; }
u uu;
c() { y.value = "c"; }
virtual result_type& f(s_type& s, boost::contract::virtual_* v = 0)
/* override */ {
@@ -129,7 +117,6 @@ struct c
boost::contract::old_ptr<y_type> old_y =
BOOST_CONTRACT_OLD(v, y_type::eval(y));
boost::contract::old_ptr<s_type> old_s;
boost::contract::old_ptr<u> old_u;
boost::contract::check c = boost::contract::public_function<
override_f>(v, result, &c::f, this, s)
.precondition([&] {
@@ -139,12 +126,9 @@ struct c
.old([&] {
out << "c::f::old" << std::endl;
old_s = BOOST_CONTRACT_OLD(v, s_type::eval(s));
// TODO: Fix bug for this old_u...
old_u = BOOST_CONTRACT_OLD(v, uu);
})
.postcondition([&] (result_type const& result) {
out << "c::f::post" << std::endl;
std::clog << "c::f::post OUT" << std::endl;
BOOST_CONTRACT_ASSERT(y.value == old_y->value + old_s->value);
BOOST_CONTRACT_ASSERT(s.value.find(old_y->value) !=
std::string::npos);

View File

@@ -1,261 +0,0 @@
#ifndef BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_CONTRACTS_HPP_
#define BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_CONTRACTS_HPP_
// Copyright (C) 2008-2016 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
// Test public member function subcontracting (also with old and return values).
#include "../detail/oteststream.hpp"
#include "../detail/counter.hpp"
#include <boost/contract/public_function.hpp>
#include <boost/contract/base_types.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/check.hpp>
#include <boost/contract/override.hpp>
#include <string>
boost::contract::test::detail::oteststream out;
struct s_tag;
typedef boost::contract::test::detail::counter<s_tag, std::string> s_type;
struct except_error {};
struct result_type {
std::string value;
explicit result_type(std::string const& s) : value(s) {}
private: // Test non-copyable and non-default-constructible result.
result_type();
result_type(result_type const&);
result_type& operator=(result_type const&);
};
// Test base without additional bases and pure virtual.
template<char Id>
struct t {
static void static_invariant() { out << Id << "::static_inv" << std::endl; }
void invariant() const {
out << Id << "::inv" << std::endl;
BOOST_CONTRACT_ASSERT(z.value != "");
}
struct z_tag;
typedef boost::contract::test::detail::counter<z_tag, std::string> z_type;
z_type z;
t() { z.value.push_back(Id); }
virtual result_type& f(s_type& s, boost::contract::virtual_* v = 0) = 0;
};
template<char Id> // Requires: Only pass lower case Id so it'll never be 'X'.
result_type& t<Id>::f(s_type& s, boost::contract::virtual_* v) {
std::ostringstream r; r << "none-" << Id;
static result_type result(r.str());
boost::contract::old_ptr<z_type> old_z =
BOOST_CONTRACT_OLD(v, z_type::eval(z));
boost::contract::old_ptr<s_type> old_s;
boost::contract::check c = boost::contract::public_function(v, result, this)
.precondition([&] {
out << Id << "::f::pre" << std::endl;
BOOST_CONTRACT_ASSERT(s.value[0] == Id || s.value[0] == 'X');
})
.old([&] {
out << Id << "::f::old" << std::endl;
old_s = BOOST_CONTRACT_OLD(v, s_type::eval(s));
})
.postcondition([&] (result_type const& result) {
out << Id << "::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(z.value == old_z->value + old_s->value);
BOOST_CONTRACT_ASSERT(s.value.find(old_z->value) !=
std::string::npos);
BOOST_CONTRACT_ASSERT(result.value == old_s->value);
})
.except([&] {
out << Id << "::f::except" << std::endl;
BOOST_CONTRACT_ASSERT(z.value == old_z->value);
BOOST_CONTRACT_ASSERT(s.value == old_s->value);
})
;
out << "t<" << Id << ">::f::body" << std::endl;
if(s.value == "X") throw except_error();
return result;
}
// Test base with other bases, multiple inheritance, and no subcontracting from
// protected and private bases (even if fully contracted).
struct c
#define BASES public t<'d'>, protected t<'p'>, private t<'q'>, public t<'e'>
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
static void static_invariant() { out << "c::static_inv" << std::endl; }
void invariant() const {
out << "c::inv" << std::endl;
BOOST_CONTRACT_ASSERT(y.value != "");
}
struct y_tag;
typedef boost::contract::test::detail::counter<y_tag, std::string> y_type;
y_type y;
c() { y.value = "c"; }
virtual result_type& f(s_type& s, boost::contract::virtual_* v = 0)
/* override */ {
static result_type result("none-c");
boost::contract::old_ptr<y_type> old_y =
BOOST_CONTRACT_OLD(v, y_type::eval(y));
boost::contract::old_ptr<s_type> old_s;
boost::contract::check c = boost::contract::public_function<
override_f>(v, result, &c::f, this, s)
.precondition([&] {
out << "c::f::pre" << std::endl;
BOOST_CONTRACT_ASSERT(s.value == "C" || s.value == "X");
})
.old([&] {
out << "c::f::old" << std::endl;
old_s = BOOST_CONTRACT_OLD(v, s_type::eval(s));
})
.postcondition([&] (result_type const& result) {
out << "c::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(y.value == old_y->value + old_s->value);
BOOST_CONTRACT_ASSERT(s.value.find(old_y->value) !=
std::string::npos);
BOOST_CONTRACT_ASSERT(result.value == old_s->value);
})
.except([&] {
out << "c::f::except" << std::endl;
BOOST_CONTRACT_ASSERT(y.value == old_y->value);
BOOST_CONTRACT_ASSERT(s.value == old_s->value);
})
;
out << "c::f::body" << std::endl;
if(s.value == "X") throw except_error();
std::string save_s = s.value;
std::string save = y.value;
y.value += save_s;
s.value = save;
save = t<'d'>::z.value;
t<'d'>::z.value += save_s;
s.value += save;
save = t<'e'>::z.value;
t<'e'>::z.value += save_s;
s.value += save;
result.value = save_s;
return result;
}
BOOST_CONTRACT_OVERRIDE(f)
};
// Test no subcontracting from not (fully) contracted base.
struct b {
static void static_invariant() { out << "b::static_inv" << std::endl; }
void invariant() const { out << "b::inv" << std::endl; }
virtual ~b() {}
// No contract (no virtual_ so this is not actually overridden by a::f).
virtual result_type& f(s_type& s) {
static result_type result("none-b");
out << "b::f::body" << std::endl;
result.value = s.value;
return result;
}
};
// Test public function with both non-contracted and contracted bases.
struct a
#define BASES public b, public c
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
static void static_invariant() { out << "a::static_inv" << std::endl; }
void invariant() const {
out << "a::inv" << std::endl;
BOOST_CONTRACT_ASSERT(x.value != "");
}
struct x_tag;
typedef boost::contract::test::detail::counter<x_tag, std::string> x_type;
x_type x;
a() { x.value = "a"; }
// Must use virtual_ even if no longer decl virtual for correct overloading.
result_type& f(s_type& s, boost::contract::virtual_* v = 0)
/* override */ {
static result_type result("none-a");
boost::contract::old_ptr<x_type> old_x =
BOOST_CONTRACT_OLD(v, x_type::eval(x));
boost::contract::old_ptr<s_type> old_s;
boost::contract::check c = boost::contract::public_function<
override_f>(v, result, &a::f, this, s)
.precondition([&] {
out << "a::f::pre" << std::endl;
BOOST_CONTRACT_ASSERT(s.value == "A" || s.value == "X");
})
.old([&] {
out << "a::f::old" << std::endl;
old_s = BOOST_CONTRACT_OLD(v, s_type::eval(s));
})
.postcondition([&] (result_type const& result) {
out << "a::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(x.value == old_x->value + old_s->value);
BOOST_CONTRACT_ASSERT(s.value.find(old_x->value) !=
std::string::npos);
BOOST_CONTRACT_ASSERT(result.value == old_s->value);
})
.except([&] {
out << "a::f::except" << std::endl;
BOOST_CONTRACT_ASSERT(x.value == old_x->value);
BOOST_CONTRACT_ASSERT(s.value == old_s->value);
})
;
out << "a::f::body" << std::endl;
if(s.value == "X") throw except_error();
std::string save_s = s.value;
std::string save = x.value;
x.value += save_s;
s.value = save;
save = y.value;
y.value += save_s;
s.value += save;
save = t<'d'>::z.value;
t<'d'>::z.value += save_s;
s.value += save;
save = t<'e'>::z.value;
t<'e'>::z.value += save_s;
s.value += save;
result.value = save_s;
return result;
}
BOOST_CONTRACT_OVERRIDE(f)
};
#endif // #include guard

View File

@@ -0,0 +1,214 @@
// Copyright (C) 2008-2016 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
// Test old inits/ftors and of mixed types up inheritance tree.
#include "../detail/oteststream.hpp"
#include <boost/contract/public_function.hpp>
#include <boost/contract/base_types.hpp>
#include <boost/contract/assert.hpp>
#include <boost/contract/old.hpp>
#include <boost/contract/check.hpp>
#include <boost/contract/override.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <sstream>
#include <string>
boost::contract::test::detail::oteststream out;
struct num {
static num make(int i) { // Test no ctor (not even explicit) but for copy.
num n;
n.value(i);
return n;
}
num(num const& other) : value_(other.value_) {}
void value(int i) { value_ = boost::lexical_cast<std::string>(i); }
int value() const { return boost::lexical_cast<int>(value_); }
num operator+(int left) {
num n;
n.value(value() + left);
return n;
}
private:
num() {} // Test no visible default ctor (only copy ctor).
num& operator=(num const&); // Test no copy operator (only copy ctor).
std::string value_; // Test this size-of != from other old type `int` below.
};
struct c {
virtual void f(int& i, num& n, boost::contract::virtual_* v = 0) {
boost::contract::old_ptr<int> old_a = BOOST_CONTRACT_OLD(v, i + 1);
boost::contract::old_ptr<num> old_b = BOOST_CONTRACT_OLD(v, n + 2);
boost::contract::old_ptr<int> old_x;
boost::contract::old_ptr<num> old_y;
boost::contract::check c = boost::contract::public_function(v, this)
.old([&] {
out << "c::f::old" << std::endl;
old_x = BOOST_CONTRACT_OLD(v, i + 3);
old_y = BOOST_CONTRACT_OLD(v, n + 4);
})
.postcondition([&] {
out << "c::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(*old_a == n.value() + 1);
BOOST_CONTRACT_ASSERT(old_b->value() == i + 2);
BOOST_CONTRACT_ASSERT(*old_x == n.value() + 3);
BOOST_CONTRACT_ASSERT(old_y->value() == i + 4);
})
;
out << "c::f::body" << std::endl;
int tmp = i;
i = n.value();
n.value(tmp);
}
};
struct b
#define BASES public c
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
virtual void f(int& i, num& n, boost::contract::virtual_* v = 0)
/* override */ {
boost::contract::old_ptr<int> old_a = BOOST_CONTRACT_OLD(v, i + 1);
boost::contract::old_ptr<num> old_b = BOOST_CONTRACT_OLD(v, n + 2);
boost::contract::old_ptr<int> old_x;
boost::contract::old_ptr<num> old_y;
boost::contract::check c = boost::contract::public_function<
override_f>(v, &c::f, this, i, n)
.old([&] {
out << "b::f::old" << std::endl;
old_x = BOOST_CONTRACT_OLD(v, i + 3);
old_y = BOOST_CONTRACT_OLD(v, n + 4);
})
.postcondition([&] {
out << "b::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(*old_a == n.value() + 1);
BOOST_CONTRACT_ASSERT(old_b->value() == i + 2);
BOOST_CONTRACT_ASSERT(*old_x == n.value() + 3);
BOOST_CONTRACT_ASSERT(old_y->value() == i + 4);
})
;
out << "b::f::body" << std::endl;
int tmp = i;
i = n.value();
n.value(tmp);
}
BOOST_CONTRACT_OVERRIDE(f)
};
struct a
#define BASES public b
: BASES
{
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
virtual void f(int& i, num& n, boost::contract::virtual_* v = 0)
/* override */ {
boost::contract::old_ptr<int> old_a = BOOST_CONTRACT_OLD(v, i + 1);
boost::contract::old_ptr<num> old_b = BOOST_CONTRACT_OLD(v, n + 2);
boost::contract::old_ptr<int> old_x;
boost::contract::old_ptr<num> old_y;
boost::contract::check c = boost::contract::public_function<
override_f>(v, &c::f, this, i, n)
.old([&] {
out << "a::f::old" << std::endl;
old_x = BOOST_CONTRACT_OLD(v, i + 3);
old_y = BOOST_CONTRACT_OLD(v, n + 4);
})
.postcondition([&] {
out << "a::f::post" << std::endl;
BOOST_CONTRACT_ASSERT(*old_a == n.value() + 1);
BOOST_CONTRACT_ASSERT(old_b->value() == i + 2);
BOOST_CONTRACT_ASSERT(*old_x == n.value() + 3);
BOOST_CONTRACT_ASSERT(old_y->value() == i + 4);
})
;
out << "a::f::body" << std::endl;
int tmp = i;
i = n.value();
n.value(tmp);
}
BOOST_CONTRACT_OVERRIDE(f)
};
int main() {
std::ostringstream ok;
int i = 0;
num n = num::make(0);
i = 123;
n.value(-123);
a aa; // Test virtual call with 2 bases.
out.str("");
aa.f(i, n);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "c::f::old" << std::endl
<< "b::f::old" << std::endl
<< "a::f::old" << std::endl
#endif
<< "a::f::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "c::f::old" << std::endl
<< "c::f::post" << std::endl
<< "b::f::old" << std::endl
<< "b::f::post" << std::endl
// No old call here because not a base object.
<< "a::f::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
i = 456;
n.value(-456);
b bb; // Test virtual call with 1 base.
out.str("");
bb.f(i, n);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "c::f::old" << std::endl
<< "b::f::old" << std::endl
#endif
<< "b::f::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "c::f::old" << std::endl
<< "c::f::post" << std::endl
// No old call here because not a base object.
<< "b::f::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
i = 789;
n.value(-789);
c cc; // Test virtual call with no bases.
out.str("");
cc.f(i, n);
ok.str(""); ok
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
<< "c::f::old" << std::endl
#endif
<< "c::f::body" << std::endl
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
// No old call here because not a base object.
<< "c::f::post" << std::endl
#endif
;
BOOST_TEST(out.eq(ok.str()));
return boost::report_errors();
}