| 1 | // Copyright (C) 2003, Fernando Luis Cacciola Carballal. |
|---|
| 2 | // |
|---|
| 3 | // Use, modification, and distribution is subject to the Boost Software |
|---|
| 4 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | // |
|---|
| 7 | // See http://www.boost.org/lib/optional for documentation. |
|---|
| 8 | // |
|---|
| 9 | // You are welcome to contact the author at: |
|---|
| 10 | // fernando_cacciola@hotmail.com |
|---|
| 11 | // |
|---|
| 12 | #include<stdexcept> |
|---|
| 13 | #include<string> |
|---|
| 14 | #include<sstream> |
|---|
| 15 | |
|---|
| 16 | #define BOOST_ENABLE_ASSERT_HANDLER |
|---|
| 17 | |
|---|
| 18 | #include "boost/optional/optional.hpp" |
|---|
| 19 | #include "boost/optional/optional_io.hpp" |
|---|
| 20 | #include "boost/none.hpp" |
|---|
| 21 | |
|---|
| 22 | #include "boost/test/minimal.hpp" |
|---|
| 23 | |
|---|
| 24 | #ifdef ENABLE_TRACE |
|---|
| 25 | #define TRACE(msg) std::cout << msg << std::endl ; |
|---|
| 26 | #else |
|---|
| 27 | #define TRACE(msg) |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | namespace boost { |
|---|
| 31 | |
|---|
| 32 | void assertion_failed (char const * expr, char const * func, char const * file, long ) |
|---|
| 33 | { |
|---|
| 34 | using std::string ; |
|---|
| 35 | string msg = string("Boost assertion failure for \"") |
|---|
| 36 | + string(expr) |
|---|
| 37 | + string("\" at file \"") |
|---|
| 38 | + string(file) |
|---|
| 39 | + string("\" function \"") |
|---|
| 40 | + string(func) |
|---|
| 41 | + string("\"") ; |
|---|
| 42 | |
|---|
| 43 | TRACE(msg); |
|---|
| 44 | |
|---|
| 45 | throw std::logic_error(msg); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | using namespace std ; |
|---|
| 51 | using namespace boost ; |
|---|
| 52 | |
|---|
| 53 | template<class Opt> |
|---|
| 54 | void test2( Opt o, Opt buff ) |
|---|
| 55 | { |
|---|
| 56 | stringstream s ; |
|---|
| 57 | |
|---|
| 58 | s << o ; |
|---|
| 59 | s >> buff ; |
|---|
| 60 | |
|---|
| 61 | BOOST_ASSERT( buff == o ) ; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | template<class T> |
|---|
| 66 | void test( T v, T w ) |
|---|
| 67 | { |
|---|
| 68 | test2( make_optional(v), optional<T> ()); |
|---|
| 69 | test2( make_optional(v), make_optional(w)); |
|---|
| 70 | test2( optional<T> () , optional<T> ()); |
|---|
| 71 | test2( optional<T> () , make_optional(w)); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int test_main( int, char* [] ) |
|---|
| 75 | { |
|---|
| 76 | try |
|---|
| 77 | { |
|---|
| 78 | test(1,2); |
|---|
| 79 | test(string("hello"),string("buffer")); |
|---|
| 80 | test(string(""),string("buffer")); |
|---|
| 81 | } |
|---|
| 82 | catch ( ... ) |
|---|
| 83 | { |
|---|
| 84 | BOOST_ERROR("Unexpected Exception caught!"); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return 0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|