summary refs log tree commit diff stats
path: root/plugins/fishlim/tests/tests.c
diff options
context:
space:
mode:
authorPatrick Griffis <tingping@tingping.se>2021-11-03 09:59:24 -0500
committerPatrick Griffis <tingping@tingping.se>2021-11-03 09:59:24 -0500
commit4f3ef3505a5f34c85e0935dc1ca4d14f27590ae5 (patch)
treedfb6229091899ae1f0d4a250d5be4c379d4724f3 /plugins/fishlim/tests/tests.c
parentb54593e7527566e1bb46e404ea8a91ee4e493f57 (diff)
fishlim: Minor test improvements
- Don't have tests repeat themselves, meson has a `--repeat` flag
- Fix a minor leak of a GRand
- Speed up a test
- Increase timeout

This still needs a lot of improvements, it runs at lot of loops within
loops generating random strings that could be optimized. This means
it can take a very long time on some computers.

Closes #2629
Diffstat (limited to 'plugins/fishlim/tests/tests.c')
-rw-r--r--plugins/fishlim/tests/tests.c65
1 files changed, 30 insertions, 35 deletions
diff --git a/plugins/fishlim/tests/tests.c b/plugins/fishlim/tests/tests.c
index 0d385221..393194d8 100644
--- a/plugins/fishlim/tests/tests.c
+++ b/plugins/fishlim/tests/tests.c
@@ -129,17 +129,17 @@ static void
 test_base64_len (void)
 {
     char *b64 = NULL;
-    int i, message_len = 0;
     char message[1000];
+    int message_end = sizeof (message) - 1;
 
-    for (i = 0; i < 10; ++i) {
-        for (message_len = 1; message_len < 1000; ++message_len) {
-            random_string(message, message_len);
-            b64 = g_base64_encode((const unsigned char *) message, message_len);
-            g_assert_nonnull(b64);
-            g_assert_cmpuint(strlen(b64), == , base64_len(message_len));
-            g_free(b64);
-        }
+    random_string(message, message_end);
+
+    for (; message_end >= 0; --message_end) {
+        message[message_end] = '\0'; /* Truncate instead of generating new strings */
+        b64 = g_base64_encode((const unsigned char *) message, message_end);
+        g_assert_nonnull(b64);
+        g_assert_cmpuint(strlen(b64), == , base64_len(message_end));
+        g_free(b64);
     }
 }
 
@@ -150,18 +150,15 @@ static void
 test_base64_fish_len (void)
 {
     char *b64 = NULL;
-    int i, message_len = 0;
+    int message_len = 0;
     char message[1000];
 
-    for (i = 0; i < 10; ++i) {
-
-        for (message_len = 1; message_len < 1000; ++message_len) {
-            random_string(message, message_len);
-            b64 = fish_base64_encode(message, message_len);
-            g_assert_nonnull(b64);
-            g_assert_cmpuint(strlen(b64), == , base64_fish_len(message_len));
-            g_free(b64);
-        }
+    for (message_len = 1; message_len < 1000; ++message_len) {
+        random_string(message, message_len);
+        b64 = fish_base64_encode(message, message_len);
+        g_assert_nonnull(b64);
+        g_assert_cmpuint(strlen(b64), == , base64_fish_len(message_len));
+        g_free(b64);
     }
 }
 
@@ -242,30 +239,28 @@ test_foreach_utf8_data_chunks(void)
 {
     GRand *rand = NULL;
     GString *chunks = NULL;
-    int tests, max_chunks_len, chunks_len;
+    int  max_chunks_len, chunks_len;
     char ascii_message[1001];
     char *data_chunk = NULL;
 
     rand = g_rand_new();
+    max_chunks_len = g_rand_int_range(rand, 2, 301);
+    random_string(ascii_message, 1000);
 
-    for (tests = 0; tests < 1000; ++tests) {
+    data_chunk = ascii_message;
 
-        max_chunks_len = g_rand_int_range(rand, 2, 301);
-        random_string(ascii_message, 1000);
+    chunks = g_string_new(NULL);
 
-        data_chunk = ascii_message;
-
-        chunks = g_string_new(NULL);
-
-        while (foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
-            g_string_append(chunks, g_strndup(data_chunk, chunks_len));
-            /* Next chunk */
-            data_chunk += chunks_len;
-        }
-        /* Check data loss */
-        g_assert_cmpstr(chunks->str, == , ascii_message);
-        g_string_free(chunks, TRUE);
+    while (foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
+        g_string_append(chunks, g_strndup(data_chunk, chunks_len));
+        /* Next chunk */
+        data_chunk += chunks_len;
     }
+    /* Check data loss */
+    g_assert_cmpstr(chunks->str, == , ascii_message);
+
+    g_string_free(chunks, TRUE);
+    g_rand_free (rand);
 }
 
 int