[PATCH] lib: fwts_framework: assert on FWTS_REGISTER names being too long

Keng-Yu Lin kengyu at canonical.com
Mon Jan 14 06:22:41 UTC 2013


On Mon, Dec 31, 2012 at 5:36 AM, Colin King <colin.king at canonical.com> wrote:
> From: Colin Ian King <colin.king at canonical.com>
>
> We don't want tests registered with stupidly long names so add in
> the FWTS_ASSERT macro that can be used to throw a compile time
> error if the name of a given test with FWTS_REGISTER is too long.
>
> The FWTS_ASSERT macro is generic enough that we can use it for
> other compile time sanity checks if required.
>
> Signed-off-by: Colin Ian King <colin.king at canonical.com>
> ---
>  src/lib/include/fwts_framework.h | 19 ++++++++++++++++++-
>  1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/src/lib/include/fwts_framework.h b/src/lib/include/fwts_framework.h
> index 0c0c276..7a540ad 100644
> --- a/src/lib/include/fwts_framework.h
> +++ b/src/lib/include/fwts_framework.h
> @@ -240,13 +240,30 @@ static inline int fwts_tests_passed(const fwts_framework *fw)
>         (flags & (FWTS_FLAG_INTERACTIVE | \
>                   FWTS_FLAG_INTERACTIVE_EXPERIMENTAL))
>
> +#define FWTS_ARRAY_LEN(s) (sizeof(s)/sizeof(s[0]))
> +
> +/*
> + * FWTS_ASSERT(test, message)
> + *     compile time assertion that throws a division by zero
> + *     error to stop compilation if condition "test" is not true.
> + *     See http://www.pixelbeat.org/programming/gcc/static_assert.html
> + *
> + */
> +#define FWTS_CONCAT(a, b) a ## b
> +#define FWTS_CONCAT_EXPAND(a,b) FWTS_CONCAT(a, b)
> +#define FWTS_ASSERT(e, m)      \
> +enum { FWTS_CONCAT_EXPAND(FWTS_ASSERT_ ## m ## _in_line_, __LINE__) = 1 / !!(e) }
> +
>  #define FWTS_REGISTER(name, ops, priority, flags)              \
> +/* Ensure name is not too long */                              \
> +FWTS_ASSERT(FWTS_ARRAY_LEN(name) < 16,                         \
> +       fwts_register_name_too_long);                           \
>                                                                 \
>  static void __test_init (void) __attribute__ ((constructor));  \
>                                                                 \
>  static void __test_init (void)                                 \
>  {                                                              \
>         fwts_framework_test_add(name, ops, priority, flags);    \
> -}                                                              \
> +}
>
>  #endif
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu at canonical.com>



More information about the fwts-devel mailing list