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