Simplify selftest functions using macros

This reduces clutter, making the functions more readable.

Also, it makes lcov see each line as covered. This is not cheating, as the
lines that were previously seen as not covered are not supposed to be reached
anyway (failing branches of the selftests).

Thanks to this and previous test suite enhancements, lcov now sees chacha20.c
and poly1305.c at 100% line coverage, and for chachapoly.c only two lines are
not covered (error returns from lower-level module that should never happen
except perhaps if an alternative implementation returns an unexpected error).
diff --git a/library/chacha20.c b/library/chacha20.c
index d89000d..5a753eb 100644
--- a/library/chacha20.c
+++ b/library/chacha20.c
@@ -519,18 +519,29 @@
     375U
 };
 
+#define ASSERT( cond, args )            \
+    do                                  \
+    {                                   \
+        if( ! ( cond ) )                \
+        {                               \
+            if( verbose != 0 )          \
+                mbedtls_printf args;    \
+                                        \
+            return( -1 );               \
+        }                               \
+    }                                   \
+    while( 0 )
+
 int mbedtls_chacha20_self_test( int verbose )
 {
     unsigned char output[381];
     unsigned i;
     int result;
 
-    for ( i = 0U; i < 2U; i++ )
+    for( i = 0U; i < 2U; i++ )
     {
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "  ChaCha20 test %u ", i );
-        }
 
         result = mbedtls_chacha20_crypt( test_keys[i],
                                          test_nonces[i],
@@ -538,36 +549,18 @@
                                          test_lengths[i],
                                          test_input[i],
                                          output );
-        if ( result != 0)
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "error code: %i\n", result );
-            }
 
-            return( -1 );
-        }
+        ASSERT( 0 == result, ( "error code: %i\n", result ) );
 
-        if ( 0 != memcmp( output, test_output[i], test_lengths[i] ) )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "failed\n" );
-            }
+        ASSERT( 0 == memcmp( output, test_output[i], test_lengths[i] ),
+                ( "failed (output)\n" ) );
 
-            return( -1 );
-        }
-
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "passed\n" );
-        }
     }
 
     if( verbose != 0 )
-    {
         mbedtls_printf( "\n" );
-    }
 
     return( 0 );
 }
diff --git a/library/chachapoly.c b/library/chachapoly.c
index fd05886..ebf25bb 100644
--- a/library/chachapoly.c
+++ b/library/chachapoly.c
@@ -452,6 +452,19 @@
     }
 };
 
+#define ASSERT( cond, args )            \
+    do                                  \
+    {                                   \
+        if( ! ( cond ) )                \
+        {                               \
+            if( verbose != 0 )          \
+                mbedtls_printf args;    \
+                                        \
+            return( -1 );               \
+        }                               \
+    }                                   \
+    while( 0 )
+
 int mbedtls_chachapoly_self_test( int verbose )
 {
     mbedtls_chachapoly_context ctx;
@@ -460,24 +473,15 @@
     unsigned char output[200];
     unsigned char mac[16];
 
-    for ( i = 0U; i < 1U; i++ )
+    for( i = 0U; i < 1U; i++ )
     {
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "  ChaCha20-Poly1305 test %u ", i );
-        }
 
         mbedtls_chachapoly_init( &ctx );
 
         result = mbedtls_chachapoly_setkey( &ctx, test_key[i] );
-        if ( result != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "setkey() error code: %i\n", result );
-            }
-            return( -1 );
-        }
+        ASSERT( 0 == result, ( "setkey() error code: %i\n", result ) );
 
         result = mbedtls_chachapoly_crypt_and_tag( &ctx,
                                                    MBEDTLS_CHACHAPOLY_ENCRYPT,
@@ -488,45 +492,23 @@
                                                    test_input[i],
                                                    output,
                                                    mac );
-        if ( result != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "crypt_and_tag() error code: %i\n", result );
-            }
-            return( -1 );
-        }
 
-        if ( memcmp( output, test_output[i], test_input_len[i] ) != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "failure (wrong output)\n" );
-            }
-            return( -1 );
-        }
+        ASSERT( 0 == result, ( "crypt_and_tag() error code: %i\n", result ) );
 
-        if ( memcmp( mac, test_mac[i], 16U ) != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "failure (wrong MAC)\n" );
-            }
-            return( -1 );
-        }
+        ASSERT( 0 == memcmp( output, test_output[i], test_input_len[i] ),
+                ( "failure (wrong output)\n" ) );
+
+        ASSERT( 0 == memcmp( mac, test_mac[i], 16U ),
+                ( "failure (wrong MAC)\n" ) );
 
         mbedtls_chachapoly_free( &ctx );
 
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "passed\n" );
-        }
     }
 
     if( verbose != 0 )
-    {
         mbedtls_printf( "\n" );
-    }
 
     return( 0 );
 }
diff --git a/library/poly1305.c b/library/poly1305.c
index 0aa4533..a9fff47 100644
--- a/library/poly1305.c
+++ b/library/poly1305.c
@@ -487,53 +487,44 @@
     }
 };
 
+#define ASSERT( cond, args )            \
+    do                                  \
+    {                                   \
+        if( ! ( cond ) )                \
+        {                               \
+            if( verbose != 0 )          \
+                mbedtls_printf args;    \
+                                        \
+            return( -1 );               \
+        }                               \
+    }                                   \
+    while( 0 )
+
 int mbedtls_poly1305_self_test( int verbose )
 {
     unsigned char mac[16];
     unsigned i;
     int result;
 
-    for ( i = 0U; i < 2U; i++ )
+    for( i = 0U; i < 2U; i++ )
     {
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "  Poly1305 test %u ", i );
-        }
 
         result = mbedtls_poly1305_mac( test_keys[i],
                                        test_data[i],
                                        test_data_len[i],
                                        mac );
-        if ( result != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "error code: %i\n", result );
-            }
+        ASSERT( 0 == result, ( "error code: %i\n", result ) );
 
-            return( -1 );
-        }
+        ASSERT( 0 == memcmp( mac, test_mac[i], 16U ), ( "failed (mac)\n" ) );
 
-        if ( memcmp( mac, test_mac[i], 16U ) != 0 )
-        {
-            if ( verbose != 0 )
-            {
-                mbedtls_printf( "failed\n" );
-            }
-
-            return( -1 );
-        }
-
-        if ( verbose != 0 )
-        {
+        if( verbose != 0 )
             mbedtls_printf( "passed\n" );
-        }
     }
 
     if( verbose != 0 )
-    {
         mbedtls_printf( "\n" );
-    }
 
     return( 0 );
 }