blob: 25f831208b96310b900cc42006169841ad2cca6b [file] [log] [blame]
Ronald Cron4b8b1992020-06-09 13:52:23 +02001/**
2 * \file macros.h
3 *
4 * \brief This file contains generic macros for the purpose of testing.
5 */
6
7/* Copyright (C) 2020, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24
25#ifndef TEST_MACROS_H
26#define TEST_MACROS_H
27
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "mbedtls/config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Ronald Cron849930a2020-06-03 08:06:47 +020034#include <stdlib.h>
35
36#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdio.h>
40#define mbedtls_fprintf fprintf
41#define mbedtls_snprintf snprintf
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#define mbedtls_exit exit
45#define mbedtls_time time
46#define mbedtls_time_t time_t
47#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
48#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
49#endif
50
51#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
52#include "mbedtls/memory_buffer_alloc.h"
53#endif
54
55#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
56{ \
57 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
58 __FILE__, __LINE__, #a ); \
59 mbedtls_exit( 1 ); \
60}
61
62#if defined(__GNUC__)
63/* Test if arg and &(arg)[0] have the same type. This is true if arg is
64 * an array but not if it's a pointer. */
65#define IS_ARRAY_NOT_POINTER( arg ) \
66 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
67 __typeof__( &( arg )[0] ) ) )
68#else
69/* On platforms where we don't know how to implement this check,
70 * omit it. Oh well, a non-portable check is better than nothing. */
71#define IS_ARRAY_NOT_POINTER( arg ) 1
72#endif
73
74/* A compile-time constant with the value 0. If `const_expr` is not a
75 * compile-time constant with a nonzero value, cause a compile-time error. */
76#define STATIC_ASSERT_EXPR( const_expr ) \
77 ( 0 && sizeof( struct { int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
78/* Return the scalar value `value` (possibly promoted). This is a compile-time
79 * constant if `value` is. `condition` must be a compile-time constant.
80 * If `condition` is false, arrange to cause a compile-time error. */
81#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
82 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
83
84#define ARRAY_LENGTH_UNSAFE( array ) \
85 ( sizeof( array ) / sizeof( *( array ) ) )
86/** Return the number of elements of a static or stack array.
87 *
88 * \param array A value of array (not pointer) type.
89 *
90 * \return The number of elements of the array.
91 */
92#define ARRAY_LENGTH( array ) \
93 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
94 ARRAY_LENGTH_UNSAFE( array ) ) )
95
96/** Return the smaller of two values.
97 *
98 * \param x An integer-valued expression without side effects.
99 * \param y An integer-valued expression without side effects.
100 *
101 * \return The smaller of \p x and \p y.
102 */
103#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
104
105/** Return the larger of two values.
106 *
107 * \param x An integer-valued expression without side effects.
108 * \param y An integer-valued expression without side effects.
109 *
110 * \return The larger of \p x and \p y.
111 */
112#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
113
114/*
115 * 32-bit integer manipulation macros (big endian)
116 */
117#ifndef GET_UINT32_BE
118#define GET_UINT32_BE(n,b,i) \
119{ \
120 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
121 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
122 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
123 | ( (uint32_t) (b)[(i) + 3] ); \
124}
125#endif
126
127#ifndef PUT_UINT32_BE
128#define PUT_UINT32_BE(n,b,i) \
129{ \
130 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
131 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
132 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
133 (b)[(i) + 3] = (unsigned char) ( (n) ); \
134}
135#endif
136
Ronald Cron4b8b1992020-06-09 13:52:23 +0200137#endif /* TEST_MACROS_H */