[pulseaudio-discuss] [PATCH 2/6] tests: Make sure tests assert on failures and return error status

Maarten Bosmans mkbosmans at gmail.com
Fri Oct 28 13:44:20 PDT 2011


When a test program exits with a nonzero return value (or an assert is hit),
the test is regarded as a FAIL.
This makes `make check` a little more useful.
---
 src/tests/extended-test.c        |    2 +-
 src/tests/get-binary-name-test.c |    4 ++--
 src/tests/interpol-test.c        |    4 ++--
 src/tests/ipacl-test.c           |   11 +++++------
 src/tests/memblockq-test.c       |   20 +++++++-------------
 src/tests/thread-test.c          |    9 ++++-----
 6 files changed, 21 insertions(+), 29 deletions(-)

diff --git a/src/tests/extended-test.c b/src/tests/extended-test.c
index 631fdc8..0647aea 100644
--- a/src/tests/extended-test.c
+++ b/src/tests/extended-test.c
@@ -156,7 +156,7 @@ static void context_state_callback(pa_context *c, void *userdata) {
 
 int main(int argc, char *argv[]) {
     pa_mainloop* m = NULL;
-    int i, ret = 0;
+    int i, ret = 1;
 
     for (i = 0; i < SAMPLE_HZ; i++)
         data[i] = (float) sin(((double) i/SAMPLE_HZ)*2*M_PI*SINE_HZ)/2;
diff --git a/src/tests/get-binary-name-test.c b/src/tests/get-binary-name-test.c
index 4afe81b..c35160f 100644
--- a/src/tests/get-binary-name-test.c
+++ b/src/tests/get-binary-name-test.c
@@ -43,12 +43,12 @@ int main(int argc, char *argv[]) {
         if (strlen(exename) < allocated - 1) {
             printf("%s\n", exename);
             pa_xfree(exename);
-            break;
+            return 0;
         }
 
         pa_xfree(exename);
         allocated *= 2;
     }
 
-    return 0;
+    return 1;
 }
diff --git a/src/tests/interpol-test.c b/src/tests/interpol-test.c
index ffe4ab3..1a7c4b4 100644
--- a/src/tests/interpol-test.c
+++ b/src/tests/interpol-test.c
@@ -25,13 +25,13 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
-#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <pulse/pulseaudio.h>
 #include <pulse/mainloop.h>
 
+#include <pulsecore/macro.h>
 #include <pulsecore/thread.h>
 
 #define INTERPOLATE
@@ -87,7 +87,7 @@ static void stream_latency_cb(pa_stream *p, void *userdata) {
 
 /* This is called whenever the context status changes */
 static void context_state_callback(pa_context *c, void *userdata) {
-    assert(c);
+    pa_assert(c);
 
     switch (pa_context_get_state(c)) {
         case PA_CONTEXT_CONNECTING:
diff --git a/src/tests/ipacl-test.c b/src/tests/ipacl-test.c
index 7a7267d..74bafd7 100644
--- a/src/tests/ipacl-test.c
+++ b/src/tests/ipacl-test.c
@@ -5,7 +5,6 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <unistd.h>
-#include <assert.h>
 #include <string.h>
 
 #ifdef HAVE_NETINET_IN_H
@@ -27,12 +26,12 @@ static void do_ip_acl_check(const char *s, int fd, int expected) {
     pa_ip_acl *acl;
     int result;
 
-    acl = pa_ip_acl_new(s);
-    assert(acl);
+    pa_assert_se(acl = pa_ip_acl_new(s));
     result = pa_ip_acl_check(acl, fd);
     pa_ip_acl_free(acl);
 
     printf("%-20s result=%u (should be %u)\n", s, result, expected);
+    pa_assert(result == expected);
 }
 
 int main(int argc, char *argv[]) {
@@ -44,14 +43,14 @@ int main(int argc, char *argv[]) {
     int r;
 
     fd = socket(PF_INET, SOCK_STREAM, 0);
-    assert(fd >= 0);
+    pa_assert(fd >= 0);
 
     sa.sin_family = AF_INET;
     sa.sin_port = htons(22);
     sa.sin_addr.s_addr = inet_addr("127.0.0.1");
 
     r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
-    assert(r >= 0);
+    pa_assert(r >= 0);
 
     do_ip_acl_check("127.0.0.1", fd, 1);
     do_ip_acl_check("127.0.0.2/0", fd, 1);
@@ -76,7 +75,7 @@ int main(int argc, char *argv[]) {
     pa_assert_se(inet_pton(AF_INET6, "::1", &sa6.sin6_addr) == 1);
 
     r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
-    assert(r >= 0);
+    pa_assert(r >= 0);
 
     do_ip_acl_check("::1", fd, 1);
     do_ip_acl_check("::1/9", fd, 1);
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index 085d70e..09d2142 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -22,12 +22,12 @@
 #endif
 
 #include <stdlib.h>
-#include <assert.h>
 #include <stdio.h>
 #include <signal.h>
 
 #include <pulsecore/memblockq.h>
 #include <pulsecore/log.h>
+#include <pulsecore/macro.h>
 
 static void dump_chunk(const pa_memchunk *chunk) {
     size_t n;
@@ -86,33 +86,27 @@ int main(int argc, char *argv[]) {
 
     p = pa_mempool_new(FALSE, 0);
 
-    silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
-    assert(silence.memblock);
+    pa_assert_se(silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1));
     silence.index = 0;
     silence.length = pa_memblock_get_length(silence.memblock);
 
-    bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence);
-    assert(bq);
+    pa_assert_se(bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence));
 
-    chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1);
+    pa_assert_se(chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1));
     chunk1.index = 0;
     chunk1.length = 2;
-    assert(chunk1.memblock);
 
-    chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1);
+    pa_assert_se(chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1));
     chunk2.index = 2;
     chunk2.length = 2;
-    assert(chunk2.memblock);
 
-    chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1);
+    pa_assert_se(chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1));
     chunk3.index = 0;
     chunk3.length = 4;
-    assert(chunk3.memblock);
 
-    chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1);
+    pa_assert_se(chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1));
     chunk4.index = 0;
     chunk4.length = 8;
-    assert(chunk4.memblock);
 
     ret = pa_memblockq_push(bq, &chunk1);
     assert(ret == 0);
diff --git a/src/tests/thread-test.c b/src/tests/thread-test.c
index 4071e42..aaa79fb 100644
--- a/src/tests/thread-test.c
+++ b/src/tests/thread-test.c
@@ -21,12 +21,13 @@
 #include <config.h>
 #endif
 
+#include <pulse/xmalloc.h>
 #include <pulsecore/thread.h>
+#include <pulsecore/macro.h>
 #include <pulsecore/mutex.h>
 #include <pulsecore/once.h>
 #include <pulsecore/log.h>
 #include <pulsecore/core-util.h>
-#include <pulse/xmalloc.h>
 
 static pa_mutex *mutex = NULL;
 static pa_cond *cond1 = NULL, *cond2 = NULL;
@@ -100,8 +101,7 @@ int main(int argc, char *argv[]) {
     tls = pa_tls_new(pa_xfree);
 
     for (i = 0; i < THREADS_MAX; i++) {
-        t[i] = pa_thread_new("test", thread_func, pa_sprintf_malloc("Thread #%i", i+1));
-        assert(t[i]);
+        pa_assert_se(t[i] = pa_thread_new("test", thread_func, pa_sprintf_malloc("Thread #%i", i+1)));
     }
 
     pa_mutex_lock(mutex);
@@ -109,8 +109,7 @@ int main(int argc, char *argv[]) {
     pa_log("loop-init");
 
     for (k = 0; k < 100; k++) {
-        assert(magic_number == 0);
-
+        pa_assert(magic_number == 0);
 
         magic_number = (int) rand() % 0x10000;
 
-- 
1.7.4.1



More information about the pulseaudio-discuss mailing list