You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2020/07/29 14:10:35 UTC

[qpid-dispatch] 04/06: DISPATCH-1568 c-unittests: replace #define with C++ const, use SUBCASEs more

This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git

commit 3d26b4da995f275a3eb4c96745d03f73efc55571
Author: Jiri Danek <jd...@redhat.com>
AuthorDate: Thu Jul 23 10:08:33 2020 +0200

    DISPATCH-1568 c-unittests: replace #define with C++ const, use SUBCASEs more
---
 tests/c_unittests/test_terminus.cpp | 129 +++++++++++++++++++-----------------
 1 file changed, 68 insertions(+), 61 deletions(-)

diff --git a/tests/c_unittests/test_terminus.cpp b/tests/c_unittests/test_terminus.cpp
index 5f2100a..70b1fa8 100644
--- a/tests/c_unittests/test_terminus.cpp
+++ b/tests/c_unittests/test_terminus.cpp
@@ -19,81 +19,88 @@
 
 #include "qdr_doctest.h"
 
-#define TESTING 1 //not used?
 extern "C" {
-#include <stdio.h>
-#include <stdint.h>
+#include <../src/router_core/router_core_private.h>
+#include <../src/terminus_private.h>
 #include <inttypes.h>
 #include <qpid/dispatch/router_core.h>
-#include <../src/router_core/router_core_private.h>
-int safe_snprintf(char *str, size_t size, const char *format, ...);
+#include <stdint.h>
+#include <stdio.h>
 }
 
-#define OUTPUT_SIZE 128
-#define TEST_MESSAGE "something"
-#define LEN strlen(TEST_MESSAGE)
+TEST_CASE("test_safe_snprintf") {
+    const int   OUTPUT_SIZE = 128;
+    const char *TEST_MESSAGE = "something";
+    const int   LEN = strlen(TEST_MESSAGE);
 
-TEST_CASE("test_safe_snprintf_valid_input") {
-    //size_t size = OUTPUT_SIZE;
     size_t len;
-    char output[OUTPUT_SIZE];
-
-    len = safe_snprintf(output, LEN+10, TEST_MESSAGE);
-    CHECK(LEN == len);
-    CHECK(output == TEST_MESSAGE);
-
-    len = safe_snprintf(output, LEN+1, TEST_MESSAGE);
-    CHECK(LEN == len);
-    CHECK(output == TEST_MESSAGE);
-
-    len = safe_snprintf(output, LEN, TEST_MESSAGE);
-    CHECK(LEN-1 == len);
-    CHECK(output == "somethin");
-
-    len = safe_snprintf(output, 0, TEST_MESSAGE);
-    CHECK(0 == len);
-
-    output[0] = 'a';
-    len = safe_snprintf(output, 1, TEST_MESSAGE);
-    CHECK(0 == len);
-    CHECK('\0' == output[0]);
-
-    len = safe_snprintf(output, (int)-1, TEST_MESSAGE);
-    CHECK(0 == len); //or worst negative?
-
+    char   output[OUTPUT_SIZE];
+
+    SUBCASE("valid_inputs") {
+        SUBCASE("") {
+            len = safe_snprintf(output, LEN + 10, TEST_MESSAGE);
+            CHECK(LEN == len);
+            CHECK(output == TEST_MESSAGE);
+        }
+
+        SUBCASE("") {
+            len = safe_snprintf(output, LEN + 1, TEST_MESSAGE);
+            CHECK(LEN == len);
+            CHECK(output == TEST_MESSAGE);
+        }
+
+        SUBCASE("") {
+            len = safe_snprintf(output, LEN, TEST_MESSAGE);
+            CHECK(LEN - 1 == len);
+            CHECK(output == "somethin");
+        }
+
+        SUBCASE("") {
+            len = safe_snprintf(output, 0, TEST_MESSAGE);
+            CHECK(0 == len);
+        }
+
+        SUBCASE("") {
+            output[0] = 'a';
+            len = safe_snprintf(output, 1, TEST_MESSAGE);
+            CHECK(0 == len);
+            CHECK('\0' == output[0]);
+        }
+
+        SUBCASE("") {
+            len = safe_snprintf(output, (int)-1, TEST_MESSAGE);
+            CHECK(0 == len);
+        }
+    }
 }
 
-TEST_CASE("test_qdr_terminus_format_coordinator") {
-    qdr_terminus_t t;
-#define SIZE 128
-#define EXPECTED "{<coordinator>}"
-#define EXPECTED_LEN strlen(EXPECTED)
-    size_t size = SIZE;
-    char output[SIZE];
+TEST_CASE("test_qdr_terminus_format") {
+    SUBCASE("coordinator") {
+        const int   SIZE = 128;
+        const char *EXPECTED = "{<coordinator>}";
+        const int   EXPECTED_LEN = strlen(EXPECTED);
 
-    t.coordinator=true;
+        size_t size = SIZE;
+        char   output[SIZE];
 
-    qdr_terminus_format(&t, output, &size);
-    CHECK(output == EXPECTED);
-    //EXPECT_STREQ(output, "wrong_but_continues");
-    CHECK(size == SIZE - EXPECTED_LEN);
+        qdr_terminus_t t;
+        t.coordinator = true;
 
-#undef SIZE
-#undef EXPECTED
-#undef EXPECTED_LEN
-}
+        qdr_terminus_format(&t, output, &size);
+        CHECK(output == EXPECTED);
+        CHECK(size == SIZE - EXPECTED_LEN);
+    }
 
-TEST_CASE("test_qdr_terminus_format_empty") {
-    char output[3];
-    size_t size = 3;
-    output[2]='A';
+    SUBCASE("empty") {
+        char   output[3];
+        size_t size = 3;
+        output[2] = 'A';
 
-    qdr_terminus_format(NULL, output, &size);
+        SUBCASE("") {
+            qdr_terminus_format(NULL, output, &size);
 
-    SUBCASE("Sample subcase 1") {
-    CHECK(output == "{}");
-    }
-    SUBCASE("Sample subcase 2") {
-    CHECK(size == 1);
+            CHECK(output == "{}");
+            CHECK(size == 1);
+        }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org