📄 printf.c
字号:
res = g_snprintf (buf, 128, "%.3X", 5); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "005"); res = g_snprintf (buf, 128, "%.3X", 31); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "01F"); res = g_snprintf (buf, 128, "%5.3X", 5); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " 005"); /* %X, flags */ res = g_snprintf (buf, 128, "%-X", 5); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "5"); res = g_snprintf (buf, 128, "%03X", 5); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "005"); res = g_snprintf (buf, 128, "%#X", 31); g_assert_cmpint (res, ==, 4); g_assert_cmpstr (buf, ==, "0X1F"); res = g_snprintf (buf, 128, "%#X", 0); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "0");}static voidtest_f (void){ gchar buf[128]; gint res; /* %f, basic formattting */ res = g_snprintf (buf, 128, "%f", G_PI); g_assert_cmpint (res, ==, 8); g_assert (0 == strncmp (buf, "3.14159", 7)); res = g_snprintf (buf, 128, "%.8f", G_PI); g_assert_cmpint (res, ==, 10); g_assert (0 == strncmp (buf, "3.1415926", 9)); res = g_snprintf (buf, 128, "%.0f", G_PI); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "3"); res = g_snprintf (buf, 128, "%1.f", G_PI); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "3"); res = g_snprintf (buf, 128, "%3.f", G_PI); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, " 3"); /* %f, flags */ res = g_snprintf (buf, 128, "%+f", G_PI); g_assert_cmpint (res, ==, 9); g_assert (0 == strncmp (buf, "+3.14159", 8)); res = g_snprintf (buf, 128, "% f", G_PI); g_assert_cmpint (res, ==, 9); g_assert (0 == strncmp (buf, " 3.14159", 8)); res = g_snprintf (buf, 128, "%#.0f", G_PI); g_assert_cmpint (res, ==, 2); g_assert_cmpstr (buf, ==, "3."); res = g_snprintf (buf, 128, "%05.2f", G_PI); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "03.14");}static gbooleansame_value (const gchar *actual, const gchar *expected){ gdouble actual_value, expected_value; actual_value = g_ascii_strtod (actual, NULL); expected_value = g_ascii_strtod (expected, NULL); return actual_value == expected_value;}static voidtest_e (void){ gchar buf[128]; gint res; /* %e, basic formatting */ /* for %e we can't expect to reproduce exact strings and lengths, since SUS * only guarantees that the exponent shall always contain at least two * digits. On Windows, it seems to be at least three digits long. * Therefore, we compare the results of parsing the expected result and the * actual result. */ res = g_snprintf (buf, 128, "%e", G_PI); g_assert_cmpint (res, >=, 12); g_assert (same_value (buf, "3.141593e+00")); res = g_snprintf (buf, 128, "%.8e", G_PI); g_assert_cmpint (res, >=, 14); g_assert (same_value (buf, "3.14159265e+00")); res = g_snprintf (buf, 128, "%.0e", G_PI); g_assert_cmpint (res, >=, 5); g_assert (same_value (buf, "3e+00")); res = g_snprintf (buf, 128, "%.1e", 0.0); g_assert_cmpint (res, >=, 7); g_assert (same_value (buf, "0.0e+00")); res = g_snprintf (buf, 128, "%.1e", 0.00001); g_assert_cmpint (res, >=, 7); g_assert (same_value (buf, "1.0e-05")); res = g_snprintf (buf, 128, "%.1e", 10000.0); g_assert_cmpint (res, >=, 7); g_assert (same_value (buf, "1.0e+04")); /* %e, flags */ res = g_snprintf (buf, 128, "%+e", G_PI); g_assert_cmpint (res, >=, 13); g_assert (same_value (buf, "+3.141593e+00")); res = g_snprintf (buf, 128, "% e", G_PI); g_assert_cmpint (res, >=, 13); g_assert (same_value (buf, " 3.141593e+00")); res = g_snprintf (buf, 128, "%#.0e", G_PI); g_assert_cmpint (res, >=, 6); g_assert (same_value (buf, "3.e+00")); res = g_snprintf (buf, 128, "%09.2e", G_PI); g_assert_cmpint (res, >=, 9); g_assert (same_value (buf, "03.14e+00"));}static voidtest_c (void){ gchar buf[128]; gint res; res = g_snprintf (buf, 128, "%c", 'a'); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "a");}static voidtest_s (void){ gchar buf[128]; gint res; res = g_snprintf (buf, 128, "%.2s", "abc"); g_assert_cmpint (res, ==, 2); g_assert_cmpstr (buf, ==, "ab"); res = g_snprintf (buf, 128, "%.6s", "abc"); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "abc"); res = g_snprintf (buf, 128, "%5s", "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " abc"); res = g_snprintf (buf, 128, "%-5s", "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "abc "); res = g_snprintf (buf, 128, "%5.2s", "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " ab"); res = g_snprintf (buf, 128, "%*s", 5, "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " abc");#if 0 /* HP-UX doesn't get this right */ res = g_snprintf (buf, 128, "%*s", -5, "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "abc ");#endif res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc"); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " ab");}static voidtest_n (void){ gchar buf[128]; gint res; gint i; glong l; res = g_snprintf (buf, 128, "abc%n", &i); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "abc"); g_assert_cmpint (i, ==, 3); res = g_snprintf (buf, 128, "abc%ln", &l); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "abc"); g_assert_cmpint (l, ==, 3);}static voidtest_percent (void){ gchar buf[128]; gint res; res = g_snprintf (buf, 128, "%%"); g_assert_cmpint (res, ==, 1); g_assert_cmpstr (buf, ==, "%");}static voidtest_positional_params (void){ gchar buf[128]; gint res; res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a'); g_assert_cmpint (res, ==, 3); g_assert_cmpstr (buf, ==, "a b"); res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, " ab"); res = g_snprintf (buf, 128, "%1$s%1$s", "abc"); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "abcabc");}static voidtest_64bit (void){ gchar buf[128]; gint res; res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "123456"); res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "-123456"); res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "123456"); res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "361100"); res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "0361100"); res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "1e240"); res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "0x1e240"); res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "1E240");#ifdef G_OS_WIN32 /* On Win32, test that the "ll" modifier also works, for backward * compatibility. One really should use the G_GINT64_MODIFIER (which * on Win32 is the "I64" that the (msvcrt) C library's printf uses), * but "ll" used to work with the "trio" g_printf implementation in * GLib 2.2, so it's best if it continues to work. */ res = g_snprintf (buf, 128, "%" "lli", (gint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "123456"); res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "-123456"); res = g_snprintf (buf, 128, "%" "llu", (guint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "123456"); res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456); g_assert_cmpint (res, ==, 6); g_assert_cmpstr (buf, ==, "361100"); res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "0361100"); res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "1e240"); res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456); g_assert_cmpint (res, ==, 7); g_assert_cmpstr (buf, ==, "0x1e240"); res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456); g_assert_cmpint (res, ==, 5); g_assert_cmpstr (buf, ==, "1E240");#endif}intmain (int argc, char *argv[]){ g_test_init (&argc, &argv, NULL); g_test_add_func ("/printf/test-retval-and-trunc", test_retval_and_trunc); g_test_add_func ("/printf/test-d", test_d); g_test_add_func ("/printf/test-o", test_o); g_test_add_func ("/printf/test-u", test_u); g_test_add_func ("/printf/test-x", test_x); g_test_add_func ("/printf/test-X", test_X); g_test_add_func ("/printf/test-f", test_f); g_test_add_func ("/printf/test-e", test_e); g_test_add_func ("/printf/test-c", test_c); g_test_add_func ("/printf/test-s", test_s); g_test_add_func ("/printf/test-n", test_n); g_test_add_func ("/printf/test-percent", test_percent); g_test_add_func ("/printf/test-positional-params", test_positional_params); g_test_add_func ("/printf/test-64bit", test_64bit); return g_test_run();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -