Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef TEST_ASSERTIONS_H |
| 20 | #define TEST_ASSERTIONS_H |
| 21 | |
| 22 | #include <stddef.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | namespace { |
| 26 | template <typename... Args> |
| 27 | std::string string_format(std::ostringstream &stringStream) { |
| 28 | return stringStream.str(); |
| 29 | } |
| 30 | |
| 31 | template <typename T, typename... Args> |
| 32 | std::string string_format(std::ostringstream &stringStream, T t, Args... args) { |
| 33 | stringStream << t; |
| 34 | return string_format(stringStream, args...); |
| 35 | } |
| 36 | |
| 37 | class TestFailureException : public std::exception { |
| 38 | public: |
| 39 | template <typename... Args> |
| 40 | TestFailureException(const char *msg, Args... args) { |
| 41 | std::ostringstream stringStream; |
| 42 | this->msg = string_format(stringStream, msg, args...); |
| 43 | } |
| 44 | const char *what() const throw() { |
| 45 | return msg.c_str(); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | std::string msg; |
| 50 | }; |
| 51 | } // namespace |
| 52 | |
| 53 | #define TEST_ASSERT(v) \ |
| 54 | do { \ |
| 55 | if (!(v)) { \ |
| 56 | throw TestFailureException(__FILE__, ":", __LINE__, " ERROR test failed: '", #v, "'"); \ |
| 57 | } \ |
| 58 | } while (0) |
| 59 | |
| 60 | #define FAIL() TEST_ASSERT(false) |
| 61 | |
| 62 | #endif |