Imported Upstream version 7.50.2
[platform/upstream/curl.git] / tests / libtest / lib557.c
index 2c0d5e0..5bdb8ab 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -453,6 +453,7 @@ static int test_unsigned_int_formatting(void)
 
 #elif (SIZEOF_INT == 8)
 
+  /* !checksrc! disable LONGLINE all */
   i=1; ui_test[i].num = 0xFFFFFFFFFFFFFFFFU; ui_test[i].expected = "18446744073709551615";
   i++; ui_test[i].num = 0xFFFFFFFF00000000U; ui_test[i].expected = "18446744069414584320";
   i++; ui_test[i].num = 0x00000000FFFFFFFFU; ui_test[i].expected = "4294967295";
@@ -1344,6 +1345,7 @@ static int test_curl_off_t_formatting(void)
   num_cofft_tests = i;
 
 #endif
+  /* !checksrc! enable LONGLINE */
 
   for(i=1; i<=num_cofft_tests; i++) {
 
@@ -1351,11 +1353,12 @@ static int test_curl_off_t_formatting(void)
       co_test[i].result[j] = 'X';
     co_test[i].result[BUFSZ-1] = '\0';
 
-    (void)curl_msprintf(co_test[i].result, "%" CURL_FORMAT_CURL_OFF_T, co_test[i].num);
+    (void)curl_msprintf(co_test[i].result, "%" CURL_FORMAT_CURL_OFF_T,
+                        co_test[i].num);
 
     if(memcmp(co_test[i].result,
-               co_test[i].expected,
-               strlen(co_test[i].expected))) {
+              co_test[i].expected,
+              strlen(co_test[i].expected))) {
       printf("curl_off_t test #%.2d: Failed (Expected: %s Got: %s)\n",
              i, co_test[i].expected, co_test[i].result);
       failed++;
@@ -1371,6 +1374,53 @@ static int test_curl_off_t_formatting(void)
   return failed;
 }
 
+static int string_check(char *buf, const char *buf2)
+{
+  if(strcmp(buf, buf2)) {
+    /* they shouldn't differ */
+    printf("sprintf failed:\nwe '%s'\nsystem: '%s'\n",
+           buf, buf2);
+    return 1;
+  }
+  return 0;
+}
+
+/*
+ * The output strings in this test need to have been verified with a system
+ * sprintf() before used here.
+ */
+static int test_string_formatting(void)
+{
+  int errors = 0;
+  char buf[256];
+  curl_msnprintf(buf, sizeof(buf), "%0*d%s", 2, 9, "foo");
+  errors += string_check(buf, "09foo");
+
+  curl_msnprintf(buf, sizeof(buf), "%*.*s", 5, 2, "foo");
+  errors += string_check(buf, "   fo");
+
+  curl_msnprintf(buf, sizeof(buf), "%*.*s", 2, 5, "foo");
+  errors += string_check(buf, "foo");
+
+  curl_msnprintf(buf, sizeof(buf), "%*.*s", 0, 10, "foo");
+  errors += string_check(buf, "foo");
+
+  curl_msnprintf(buf, sizeof(buf), "%-10s", "foo");
+  errors += string_check(buf, "foo       ");
+
+  curl_msnprintf(buf, sizeof(buf), "%10s", "foo");
+  errors += string_check(buf, "       foo");
+
+  curl_msnprintf(buf, sizeof(buf), "%*.*s", -10, -10, "foo");
+  errors += string_check(buf, "foo       ");
+
+  if(!errors)
+    printf("All curl_mprintf() strings tests OK!\n");
+  else
+    printf("Some curl_mprintf() string tests Failed!\n");
+
+  return errors;
+}
 
 int test(char *URL)
 {
@@ -1391,6 +1441,8 @@ int test(char *URL)
 
   errors += test_curl_off_t_formatting();
 
+  errors += test_string_formatting();
+
   if(errors)
     return TEST_ERR_MAJOR_BAD;
   else