]> eyrie.org Git - kerberos/krb5-strength.git/blobdiff - tests/portable/snprintf-t.c
Declare fast forward from 3.1-2
[kerberos/krb5-strength.git] / tests / portable / snprintf-t.c
index ca6ae615764bfb816b7b3264e86bc66337b682ac..d14712f914d902ca78a788534f3caa6b982fb611 100644 (file)
@@ -1,14 +1,21 @@
 /*
  * snprintf test suite.
  *
- * Written by Russ Allbery <rra@stanford.edu>
- * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
- * Copyright (c) 2004, 2005, 2006
- *     by Internet Systems Consortium, Inc. ("ISC")
- * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
- *     2002, 2003 by The Internet Software Consortium and Rich Salz
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
  *
- * See LICENSE for licensing terms.
+ * Written by Russ Allbery <eagle@eyrie.org>
+ * Copyright 2000-2006, 2018-2020 Russ Allbery <eagle@eyrie.org>
+ * Copyright 2009-2010
+ *     The Board of Trustees of the Leland Stanford Junior University
+ * Copyright 1995 Patrick Powell
+ * Copyright 2001 Hrvoje Niksic
+ *
+ * This code is based on code written by Patrick Powell (papowell@astart.com)
+ * It may be used for any purpose as long as this notice remains intact
+ * on all source code distributions
+ *
+ * There is no SPDX-License-Identifier registered for this license.
  */
 
 #include <config.h>
 
 #include <tests/tap/basic.h>
 
+/*
+ * Disable the requirement that format strings be literals.  We need variable
+ * formats for easy testing.
+ */
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) || defined(__clang__)
+#    pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#endif
+
 /*
  * Intentionally don't add the printf attribute here since we pass a
  * zero-length printf format during testing and don't want warnings.
@@ -25,20 +40,12 @@ int test_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
 
 static const char string[] = "abcdefghijklmnopqrstuvwxyz0123456789";
 
+/* clang-format off */
+
 static const char *const fp_formats[] = {
-    "%-1.5f",   "%1.5f",    "%31.9f",   "%10.5f",   "% 10.5f",  "%+22.9f",
-    "%+4.9f",   "%01.3f",   "%3.1f",    "%3.2f",    "%.0f",     "%.1f",
+    "%-1.5f",   "%1.5f",    "%31.6f",   "%10.5f",   "% 10.5f",  "%+22.6f",
+    "%+4.6f",   "%01.3f",   "%3.1f",    "%3.2f",    "%.0f",     "%.1f",
     "%f",
-
-    /* %e and %g formats aren't really implemented yet. */
-#if 0
-    "%-1.5e",   "%1.5e",    "%31.9e",   "%10.5e",   "% 10.5e",  "%+22.9e",
-    "%+4.9e",   "%01.3e",   "%3.1e",    "%3.2e",    "%.0e",     "%.1e",
-    "%e",
-    "%-1.5g",   "%1.5g",    "%31.9g",   "%10.5g",   "% 10.5g",  "%+22.9g",
-    "%+4.9g",   "%01.3g",   "%3.1g",    "%3.2g",    "%.0g",     "%.1g",
-    "%g",
-#endif
     NULL
 };
 static const char *const int_formats[] = {
@@ -84,17 +91,19 @@ static unsigned long long ullong_nums[] = {
     0
 };
 
+/* clang-format on */
+
 
 static void
-test_format(bool truncate, const char *expected, int count,
-            const char *format, ...)
+test_format(bool trunc, const char *expected, int count, const char *format,
+            ...)
 {
     char buf[128];
     int result;
     va_list args;
 
     va_start(args, format);
-    result = test_vsnprintf(buf, truncate ? 32 : sizeof(buf), format, args);
+    result = test_vsnprintf(buf, trunc ? 32 : sizeof(buf), format, args);
     va_end(args);
     is_string(expected, buf, "format %s, wanted %s", format, expected);
     is_int(count, result, "...and output length correct");
@@ -104,17 +113,18 @@ test_format(bool truncate, const char *expected, int count,
 int
 main(void)
 {
-    int n, i, count;
+    int i, count;
     unsigned int j;
     long lcount;
     char lgbuf[128];
 
-    plan(8 +
-         (18 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
-          + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
-          + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
-          + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
-          + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums)) * 2);
+    plan(8
+         + (18 + (ARRAY_SIZE(fp_formats) - 1) * ARRAY_SIZE(fp_nums)
+            + (ARRAY_SIZE(int_formats) - 1) * ARRAY_SIZE(int_nums)
+            + (ARRAY_SIZE(uint_formats) - 1) * ARRAY_SIZE(uint_nums)
+            + (ARRAY_SIZE(llong_formats) - 1) * ARRAY_SIZE(llong_nums)
+            + (ARRAY_SIZE(ullong_formats) - 1) * ARRAY_SIZE(ullong_nums))
+               * 2);
 
     is_int(4, test_snprintf(NULL, 0, "%s", "abcd"), "simple string length");
     is_int(2, test_snprintf(NULL, 0, "%d", 20), "number length");
@@ -141,13 +151,12 @@ main(void)
                 string, -2.5);
     test_format(true, "abcdefghij4444", 14, "%.10s%n%d", string, &count, 4444);
     is_int(10, count, "correct output from %%n");
-    test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln",
-                &count, string, &lcount);
+    test_format(true, "abcdefghijklmnopqrstuvwxyz01234", 36, "%n%s%ln", &count,
+                string, &lcount);
     is_int(0, count, "correct output from two %%n");
     is_int(31, lcount, "correct output from long %%ln");
-    test_format(true, "(null)", 6, "%s", NULL);
+    test_format(true, "(null)", 6, "%s", (char *) NULL);
 
-    n = 26;
     for (i = 0; fp_formats[i] != NULL; i++)
         for (j = 0; j < ARRAY_SIZE(fp_nums); j++) {
             count = sprintf(lgbuf, fp_formats[i], fp_nums[j]);