• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _GTS_NANOAPPS_SHARED_MACROS_H_
18 #define _GTS_NANOAPPS_SHARED_MACROS_H_
19 
20 #include "send_message.h"
21 
22 /**
23  * A helper macro to perform an assertion in tests that leverage
24  * nanoapp_testing::sendFailureToHost and returns from the current function.
25  *
26  * This macro can be used in the following ways:
27  * 1. EXPECT_FAIL_RETURN(const char *message)
28  * 2. EXPECT_FAIL_RETURN(const char *message, uint32_t *value)
29  *
30  * In usage (2), an integer value will be encoded at the end of the message
31  * string, and will be forwarded to the test host through sendFailureToHost.
32  *
33  * Note that this macro can only be used in functions that return a `void`.
34  */
35 #define EXPECT_FAIL_RETURN(...)                      \
36   do {                                               \
37     nanoapp_testing::sendFailureToHost(__VA_ARGS__); \
38     return;                                          \
39   } while (0)
40 
41 /**
42  * An additional helper macro that can be used to print a uint8 instead of
43  * uint32 in usage (2) of EXPECT_FAIL_RETURN.
44  *
45  * TODO(b/396134028): Consolidate this with the EXPECT_FAIL_RETURN macro.
46  */
47 #define EXPECT_FAIL_RETURN_UINT8(message, value)    \
48   static_assert(sizeof(value) <= sizeof(uint32_t)); \
49   do {                                              \
50     uint32_t valueU32 = value;                      \
51     EXPECT_FAIL_RETURN(message, &valueU32);         \
52   } while (0)
53 
54 /**
55  * Asserts the two provided values are equal. If the assertion fails, then a
56  * fatal failure occurs.
57  */
58 #define EXPECT_EQ_OR_RETURN(val1, val2, failureMessage) \
59   if ((val1) != (val2)) EXPECT_FAIL_RETURN(failureMessage)
60 
61 /**
62  * Asserts the two provided values are not equal. If the assertion fails, then
63  * a fatal failure occurs.
64  */
65 #define EXPECT_NE_OR_RETURN(val1, val2, failureMessage) \
66   if ((val1) == (val2)) EXPECT_FAIL_RETURN(failureMessage)
67 
68 /**
69  * Asserts the given value is greater than or equal to value of lower. If the
70  * value fails this assertion, then a fatal failure occurs.
71  */
72 #define EXPECT_GE_OR_RETURN(value, lower, failureMessage) \
73   if ((value) < (lower)) EXPECT_FAIL_RETURN(failureMessage)
74 
75 /**
76  * Asserts the given value is less than or equal to value of upper. If the value
77  * fails this assertion, then a fatal failure occurs.
78  */
79 #define EXPECT_LE_OR_RETURN(value, upper, failureMessage) \
80   if ((value) > (upper)) EXPECT_FAIL_RETURN(failureMessage)
81 
82 /**
83  * Asserts the given value is less than the value of upper. If the value fails
84  * this assertion, then a fatal failure occurs.
85  */
86 #define EXPECT_LT_OR_RETURN(value, upper, failureMessage) \
87   if ((value) >= (upper)) EXPECT_FAIL_RETURN(failureMessage)
88 
89 /**
90  * Asserts the given value is within the range defined by lower and upper
91  * (inclusive). If the value is outside the range, then a fatal failure occurs.
92  */
93 #define EXPECT_IN_RANGE_OR_RETURN(value, lower, upper, failureMessage) \
94   EXPECT_GE_OR_RETURN((value), (lower), failureMessage);               \
95   EXPECT_LE_OR_RETURN((value), (upper), failureMessage)
96 
97 #endif  // _GTS_NANOAPPS_SHARED_MACROS_H_