catch an exception on discovery_viewer
[platform/core/ml/aitt.git] / external / C-Mock / v0.4.0 / cmock / cmock-function-class-mockers.h
1 // Copyright 2021, Hubert Jagodziński
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 //
27 // Author: hubert.jagodzinski@gmail.com (Hubert Jagodziński)
28
29 // C Mock - Google Mock's extension allowing a function mocking.
30 //
31 // This file implements CMockMocker class, the generic macros CMOCK_MOCK_METHOD & CMOCK_MOCK_FUNCTION
32 // and old-style CMOCK_MOCK_FUNCTIONn() macros of various arities.
33
34 #ifndef CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
35 #define CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_
36
37 #include <dlfcn.h>
38
39 #include <sstream>
40 #include <stdexcept>
41
42 #include "cmock/cmock-internal.h"
43
44 // Allows finding an instance of a class:
45 //   class Foo : public CMockMocker<Foo> { ... }
46 //   Foo *p = CMockMocker<Foo>::cmock_get_instance();
47 template<typename T>
48 class CMockMocker
49 {
50 public:
51         CMockMocker()
52         {
53                 instance = (T *)this;
54         }
55
56         ~CMockMocker()
57         {
58                 instance = (T *)NULL;
59         }
60
61         static T *cmock_get_instance() { return instance; }
62
63 private:
64         static T *instance;
65 };
66
67 template<typename T>
68 T *CMockMocker<T>::instance = NULL;
69
70 // Find the real implementation of a mocked function
71 static inline void *
72 cmock_lookup(const char *fname)
73 {
74     return dlsym(RTLD_NEXT, fname);
75 }
76
77 #define CMOCK_MOCK_METHOD(_Ret, _FunctionName, _Args) \
78     MOCK_METHOD(_Ret, _FunctionName, _Args); \
79 \
80     typedef _Ret (*_FunctionName##_type)(GMOCK_PP_REPEAT(CMOCK_INTERNAL_NO_PARAMETER_NAME, (GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)), GMOCK_PP_NARG0 _Args)); \
81     static _FunctionName##_type cmock_real_##_FunctionName;
82
83 #define CMOCK_INTERNAL_IMPLEMENT_FUNCTION(_ClassName, _FunctionName, _RealFunctionPtr, _N, _Signature) \
84     CMOCK_INTERNAL_RETURN_TYPE(_Signature) _FunctionName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) { \
85         _ClassName *mock = _ClassName::cmock_get_instance(); \
86         if (mock != nullptr) { \
87             return mock->_FunctionName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, _Signature, _N)); \
88         } \
89 \
90         if (_RealFunctionPtr == nullptr) { \
91             std::ostringstream msg; \
92             msg << "Error: Function " << #_FunctionName; \
93             msg << " not found. Neither mock nor real function is present."; \
94             throw std::logic_error(msg.str()); \
95         } \
96         return _RealFunctionPtr(GMOCK_PP_REPEAT(GMOCK_INTERNAL_FORWARD_ARG, _Signature, _N)); \
97     }
98
99 #define CMOCK_REAL_FUNCTION(_ClassName, _FunctionName) \
100     _ClassName::cmock_real_##_FunctionName
101
102 #define CMOCK_MOCK_FUNCTION(_ClassName,  _Ret, _FunctionName, _Args) \
103     _ClassName::_FunctionName##_type CMOCK_REAL_FUNCTION(_ClassName, _FunctionName) = (_ClassName::_FunctionName##_type)cmock_lookup(#_FunctionName); \
104     CMOCK_INTERNAL_IMPLEMENT_FUNCTION(_ClassName, _FunctionName, CMOCK_REAL_FUNCTION(_ClassName, _FunctionName), GMOCK_PP_NARG0 _Args, (GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)))
105
106 #define CMOCK_INTERNAL_MOCK_FUNCTIONN(_ClassName, _FunctionName, _N, _Signature) \
107     typedef CMOCK_INTERNAL_RETURN_TYPE(_Signature) (*_ClassName##_##_FunctionName##_type)(GMOCK_PP_REPEAT(CMOCK_INTERNAL_NO_PARAMETER_NAME, _Signature, _N)); \
108     static _ClassName##_##_FunctionName##_type cmock_real_##_FunctionName = (_ClassName##_##_FunctionName##_type)cmock_lookup(#_FunctionName); \
109     CMOCK_INTERNAL_IMPLEMENT_FUNCTION(_ClassName, _FunctionName, cmock_real_##_FunctionName, _N, _Signature)
110
111 #define CMOCK_MOCK_FUNCTION0(c, n, ...) \
112     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 0, (::testing::internal::identity_t<__VA_ARGS__>))
113
114 #define CMOCK_MOCK_FUNCTION1(c, n, ...) \
115     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 1, (::testing::internal::identity_t<__VA_ARGS__>))
116
117 #define CMOCK_MOCK_FUNCTION2(c, n, ...) \
118     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 2, (::testing::internal::identity_t<__VA_ARGS__>))
119
120 #define CMOCK_MOCK_FUNCTION3(c, n, ...) \
121     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 3, (::testing::internal::identity_t<__VA_ARGS__>))
122
123 #define CMOCK_MOCK_FUNCTION4(c, n, ...) \
124     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 4, (::testing::internal::identity_t<__VA_ARGS__>))
125
126 #define CMOCK_MOCK_FUNCTION5(c, n, ...) \
127     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 5, (::testing::internal::identity_t<__VA_ARGS__>))
128
129 #define CMOCK_MOCK_FUNCTION6(c, n, ...) \
130     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 6, (::testing::internal::identity_t<__VA_ARGS__>))
131
132 #define CMOCK_MOCK_FUNCTION7(c, n, ...) \
133     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 7, (::testing::internal::identity_t<__VA_ARGS__>))
134
135 #define CMOCK_MOCK_FUNCTION8(c, n, ...) \
136     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 8, (::testing::internal::identity_t<__VA_ARGS__>))
137
138 #define CMOCK_MOCK_FUNCTION9(c, n, ...) \
139     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 9, (::testing::internal::identity_t<__VA_ARGS__>))
140
141 #define CMOCK_MOCK_FUNCTION10(c, n, ...) \
142     CMOCK_INTERNAL_MOCK_FUNCTIONN(c, n, 10, (::testing::internal::identity_t<__VA_ARGS__>))
143
144 #endif // CMOCK_INCLUDE_CMOCK_CMOCK_FUNCTION_CLASS_MOCKERS_H_