diff mbox

lib: fwts_framework: assert on FWTS_REGISTER names being too long

Message ID 1356903384-16429-1-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King Dec. 30, 2012, 9:36 p.m. UTC
From: Colin Ian King <colin.king@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@canonical.com>
---
 src/lib/include/fwts_framework.h | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

Comments

Alex Hung Jan. 2, 2013, 12:53 a.m. UTC | #1
On 12/31/2012 05:36 AM, Colin King wrote:
> From: Colin Ian King <colin.king@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@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
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Keng-Yu Lin Jan. 14, 2013, 6:22 a.m. UTC | #2
On Mon, Dec 31, 2012 at 5:36 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@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@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@canonical.com>
diff mbox

Patch

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