]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: exercise printfrr()
authorDavid Lamparter <equinox@diac24.net>
Mon, 20 May 2019 17:51:03 +0000 (19:51 +0200)
committerDavid Lamparter <equinox@diac24.net>
Mon, 3 Jun 2019 16:57:31 +0000 (18:57 +0200)
Signed-off-by: David Lamparter <equinox@diac24.net>
tests/.gitignore
tests/lib/test_printfrr.c [new file with mode: 0644]
tests/lib/test_printfrr.py [new file with mode: 0644]
tests/subdir.am

index 380172487d84062ec74f2d6a5ace0914e4de7462..7177165e4a2fdafe7bf5f925edeb83dc7b80443d 100644 (file)
@@ -30,6 +30,7 @@
 /lib/test_idalloc
 /lib/test_memory
 /lib/test_nexthop_iter
+/lib/test_printfrr
 /lib/test_privs
 /lib/test_ringbuf
 /lib/test_segv
diff --git a/tests/lib/test_printfrr.c b/tests/lib/test_printfrr.c
new file mode 100644 (file)
index 0000000..c8ef150
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * printfrr() unit test
+ * Copyright (C) 2019  David Lamparter
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "zebra.h"
+
+#include <math.h>
+
+#include "lib/printfrr.h"
+#include "lib/memory.h"
+#include "lib/prefix.h"
+
+static int errors;
+
+static void printcmp(const char *fmt, ...)
+{
+       va_list ap;
+       char buf[256], bufrr[256], *p;
+       int cmp;
+       memset(bufrr, 0xcc, sizeof(bufrr));
+
+       va_start(ap, fmt);
+       vsnprintf(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
+
+       va_start(ap, fmt);
+       vsnprintfrr(bufrr, sizeof(bufrr), fmt, ap);
+       va_end(ap);
+
+       cmp = strcmp(buf, bufrr);
+
+       /* OS dependent "+nan" vs. "nan" */
+       if (cmp && (p = strstr(bufrr, "+nan"))) {
+               p[0] = ' ';
+               if (!strcmp(buf, bufrr))
+                       cmp = 0;
+               p[0] = '+';
+       }
+       printf("fmt: \"%s\"\nsys: \"%s\"\nfrr: \"%s\"\n%s\n\n",
+              fmt, buf, bufrr, cmp ? "ERROR" : "ok");
+
+       if (cmp)
+               errors++;
+}
+
+static void printchk(const char *ref, const char *fmt, ...)
+{
+       va_list ap;
+       char bufrr[256];
+       memset(bufrr, 0xcc, sizeof(bufrr));
+
+       va_start(ap, fmt);
+       vsnprintfrr(bufrr, sizeof(bufrr), fmt, ap);
+       va_end(ap);
+
+       printf("fmt: \"%s\"\nref: \"%s\"\nfrr: \"%s\"\n%s\n\n",
+              fmt, ref, bufrr, strcmp(ref, bufrr) ? "ERROR" : "ok");
+       if (strcmp(ref, bufrr))
+               errors++;
+}
+
+int main(int argc, char **argv)
+{
+       size_t i;
+       float flts[] = {
+               123.456789,
+               23.456789e-30,
+               3.456789e+30,
+               INFINITY,
+               NAN,
+       };
+       uint64_t ui64 = 0xfeed1278cafef00d;
+       struct in_addr ip;
+       char *p;
+       char buf[256];
+
+       printcmp("%d %u %d %u", 123, 123, -456, -456);
+       printcmp("%lld %llu %lld %llu", 123LL, 123LL, -456LL, -456LL);
+
+       printcmp("%-20s,%20s,%.20s", "test", "test", "test");
+       printcmp("%-3s,%3s,%.3s", "test", "test", "test");
+       printcmp("%-6.3s,%6.3s,%6.3s", "test", "test", "test");
+       printcmp("%*s,%*s,%.*s", -3, "test", 3, "test", 3, "test");
+
+       for (i = 0; i < array_size(flts); i++) {
+               printcmp("%-6.3e,%6.3e,%+06.3e", flts[i], flts[i], flts[i]);
+               printcmp("%-6.3f,%6.3f,%+06.3f", flts[i], flts[i], flts[i]);
+               printcmp("%-6.3g,%6.3g,%+06.3g", flts[i], flts[i], flts[i]);
+               printcmp("%-6.3a,%6.3a,%+06.3a", flts[i], flts[i], flts[i]);
+       }
+
+       printchk("-77385308584349683 18369358765125201933 feed1278cafef00d",
+                "%Ld %Lu %Lx", ui64, ui64, ui64);
+
+       inet_aton("192.168.1.2", &ip);
+       printchk("192.168.1.2", "%pI4", &ip);
+       printchk("         192.168.1.2", "%20pI4", &ip);
+
+       printcmp("%p", &ip);
+
+       snprintfrr(buf, sizeof(buf), "test%s", "#1");
+       csnprintfrr(buf, sizeof(buf), "test%s", "#2");
+       assert(strcmp(buf, "test#1test#2") == 0);
+
+       p = asnprintfrr(MTYPE_TMP, buf, sizeof(buf), "test%s", "#3");
+       assert(p == buf);
+       assert(strcmp(buf, "test#3") == 0);
+
+       p = asnprintfrr(MTYPE_TMP, buf, 4, "test%s", "#4");
+       assert(p != buf);
+       assert(strcmp(p, "test#4") == 0);
+       XFREE(MTYPE_TMP, p);
+
+       p = asprintfrr(MTYPE_TMP, "test%s", "#5");
+       assert(strcmp(p, "test#5") == 0);
+       XFREE(MTYPE_TMP, p);
+
+       struct prefix_sg sg;
+       sg.src.s_addr = INADDR_ANY;
+       sg.grp.s_addr = INADDR_ANY;
+       printchk("(*,*)", "%pSG4", &sg);
+
+       inet_aton("192.168.1.2", &sg.src);
+       printchk("(192.168.1.2,*)", "%pSG4", &sg);
+
+       inet_aton("224.1.2.3", &sg.grp);
+       printchk("(192.168.1.2,224.1.2.3)", "%pSG4", &sg);
+
+       sg.src.s_addr = INADDR_ANY;
+       printchk("(*,224.1.2.3)", "%pSG4", &sg);
+
+       return !!errors;
+}
diff --git a/tests/lib/test_printfrr.py b/tests/lib/test_printfrr.py
new file mode 100644 (file)
index 0000000..4fe2386
--- /dev/null
@@ -0,0 +1,6 @@
+import frrtest
+
+class TestPrintfrr(frrtest.TestMultiOut):
+    program = './test_printfrr'
+
+TestPrintfrr.exit_cleanly()
index ec5fea705e74b5e4d73740a6e43bbd71da10bef0..bbab8cd86a777189791e66eac48039a2bd3f8e06 100644 (file)
@@ -56,6 +56,7 @@ check_PROGRAMS = \
        tests/lib/test_idalloc \
        tests/lib/test_memory \
        tests/lib/test_nexthop_iter \
+       tests/lib/test_printfrr \
        tests/lib/test_privs \
        tests/lib/test_ringbuf \
        tests/lib/test_srcdest_table \
@@ -232,6 +233,10 @@ tests_lib_test_nexthop_iter_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_nexthop_iter_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_nexthop_iter_LDADD = $(ALL_TESTS_LDADD)
 tests_lib_test_nexthop_iter_SOURCES = tests/lib/test_nexthop_iter.c tests/helpers/c/prng.c
+tests_lib_test_printfrr_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_printfrr_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_printfrr_LDADD = $(ALL_TESTS_LDADD)
+tests_lib_test_printfrr_SOURCES = tests/lib/test_printfrr.c
 tests_lib_test_privs_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_privs_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_privs_LDADD = $(ALL_TESTS_LDADD)
@@ -319,6 +324,7 @@ EXTRA_DIST += \
        tests/lib/northbound/test_oper_data.refout \
        tests/lib/test_atomlist.py \
        tests/lib/test_nexthop_iter.py \
+       tests/lib/test_printfrr.py \
        tests/lib/test_ringbuf.py \
        tests/lib/test_srcdest_table.py \
        tests/lib/test_stream.py \