File: max_args.hpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (210 lines) | stat: -rw-r--r-- 7,732 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

// Copyright (C) 2008-2018 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 max argument number for public function (with and without result).

#include "../detail/oteststream.hpp"
#include <boost/contract/public_function.hpp>
#include <boost/contract/check.hpp>
#include <boost/contract/base_types.hpp>
#include <boost/contract/override.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/control/expr_iif.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <sstream>

boost::contract::test::detail::oteststream out;

#if defined(BOOST_GCC)
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wunused-parameter" // aN from macros.
#elif defined(BOOST_CLANG)
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wunused-parameter" // aN from macros.
#endif
        
#define BOOST_CONTRACT_TEST_MAX_ARGS_PARAM_COMMA_(z, n, unused) \
    int BOOST_PP_CAT(a, n) ,

#define BOOST_CONTRACT_TEST_MAX_ARGS_COMMA_ARG_(z, n, unused) \
    , BOOST_PP_CAT(a, n)
    
#define BOOST_CONTRACT_TEST_MAX_ARGS_N_(z, n, unused) \
    n

struct b {
    static void static_invariant() { out << "b::static_inv" << std::endl; }
    void invariant() const { out << "b::inv" << std::endl; }

    #define BOOST_CONTRACT_TEST_MAX_ARGS_B_F_(z, n, unused) \
        virtual int BOOST_PP_CAT(f, n)( \
            BOOST_PP_REPEAT_ ## z( \
                    n, BOOST_CONTRACT_TEST_MAX_ARGS_PARAM_COMMA_, ~) \
            boost::contract::virtual_* v = 0 \
        ) { \
            int result = 0; \
            boost::contract::check c = boost::contract::public_function( \
                    v, result, this) \
                .precondition([] { \
                    out << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::pre" << std::endl; \
                }) \
                .old([] { \
                    out << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::old" << std::endl; \
                }) \
                .postcondition([] (int result) { \
                    out << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::post" << std::endl; \
                }) \
            ; \
            out << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                    "::body" << std::endl; \
            return result; \
        }

    BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_CONTRACT_MAX_ARGS),
            BOOST_CONTRACT_TEST_MAX_ARGS_B_F_, ~)
};

struct a
    #define BASES public b
    : 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; }

    #define BOOST_CONTRACT_TEST_MAX_ARGS_A_F_(z, n, unused) \
        int BOOST_PP_CAT(f, n)( \
            BOOST_PP_REPEAT_ ## z( \
                    n, BOOST_CONTRACT_TEST_MAX_ARGS_PARAM_COMMA_, ~) \
            boost::contract::virtual_* v = 0 \
        ) /* override */ { \
            int result = 0; \
            boost::contract::check c = boost::contract::public_function< \
                BOOST_PP_CAT(override_, BOOST_PP_CAT(f, n)) \
            >( \
                v, result, &a::BOOST_PP_CAT(f, n), this \
                BOOST_PP_REPEAT_ ## z( \
                        n, BOOST_CONTRACT_TEST_MAX_ARGS_COMMA_ARG_, ~) \
            ) \
                .precondition([] { \
                    out << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::pre" << std::endl; \
                }) \
                .old([] { \
                    out << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::old" << std::endl; \
                }) \
                .postcondition([] (int result) { \
                    out << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                            "::post" << std::endl; \
                }) \
            ; \
            out << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                    "::body" << std::endl; \
            return result; \
        } \
        BOOST_CONTRACT_OVERRIDE(BOOST_PP_CAT(f, n))

    BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_CONTRACT_MAX_ARGS),
            BOOST_CONTRACT_TEST_MAX_ARGS_A_F_, ~)
};

#if defined(BOOST_GCC)
    #pragma GCC diagnostic pop
#elif defined(BOOST_CLANG)
    #pragma clang diagnostic pop
#endif

int main() {
    std::ostringstream ok;
    a aa;

    #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
        #define BOOST_CONTRACT_TEST_entry_inv 1
    #else
        #define BOOST_CONTRACT_TEST_entry_inv 0
    #endif
    #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
        #define BOOST_CONTRACT_TEST_pre 1
    #else
        #define BOOST_CONTRACT_TEST_pre 0
    #endif
    #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
        #define BOOST_CONTRACT_TEST_exit_inv 1
    #else
        #define BOOST_CONTRACT_TEST_exit_inv 0
    #endif
    #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
        #define BOOST_CONTRACT_TEST_post 1
    #else
        #define BOOST_CONTRACT_TEST_post 0
    #endif
    #ifndef BOOST_CONTRACT_NO_OLDS
        #define BOOST_CONTRACT_TEST_old 1
    #else
        #define BOOST_CONTRACT_TEST_old 0
    #endif
    
    #define BOOST_CONTRACT_TEST_MAX_ARGS_TEST_(z, n, unused) \
        out.str(""); \
        aa.BOOST_PP_CAT(f, n)(BOOST_PP_ENUM_ ## z( \
                n, BOOST_CONTRACT_TEST_MAX_ARGS_N_, ~)); \
        ok.str(""); ok \
            BOOST_PP_EXPR_IIF(BOOST_CONTRACT_TEST_entry_inv, \
                << "b::static_inv\n" \
                << "b::inv\n"\
                << "a::static_inv\n" \
                << "a::inv\n" \
            ) \
            BOOST_PP_EXPR_IIF(BOOST_CONTRACT_TEST_pre, \
                << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::pre\n" \
            ) \
            BOOST_PP_EXPR_IIF(BOOST_CONTRACT_TEST_old, \
                << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::old\n" \
                << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::old\n" \
            ) \
            << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << "::body\n" \
            BOOST_PP_EXPR_IIF(BOOST_CONTRACT_TEST_exit_inv, \
                << "b::static_inv\n" \
                << "b::inv\n"\
                << "a::static_inv\n" \
                << "a::inv\n" \
            ) \
            BOOST_PP_EXPR_IIF(BOOST_CONTRACT_TEST_post, \
                << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::old\n" \
                << "b::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::post\n" \
                << "a::" << BOOST_PP_STRINGIZE(BOOST_PP_CAT(f, n)) << \
                        "::post\n" \
            ) \
        ; \
        BOOST_TEST(out.eq(ok.str()));
    
    BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_CONTRACT_MAX_ARGS),
            BOOST_CONTRACT_TEST_MAX_ARGS_TEST_, ~)
    
    #undef BOOST_CONTRACT_TEST_entry_inv
    #undef BOOST_CONTRACT_TEST_pre
    #undef BOOST_CONTRACT_TEST_exit_inv
    #undef BOOST_CONTRACT_TEST_post
    #undef BOOST_CONTRACT_TEST_old
    return boost::report_errors();
}