📄 t-scanf.c
字号:
{ "hello%Qd", "hello \t0", "0", 1, -1 }, { "hello%Qdworld", "hello 0world", "0", 1, -1 }, { "hello%Qd", "hello3/2", "3/2", 1, -1 }, { "hello%*Qd", "hello0", "-999/121", 0, -1 }, { "hello%*Qd", "hello 0", "-999/121", 0, -1 }, { "hello%*Qd", "hello \t0", "-999/121", 0, -1 }, { "hello%*Qdworld", "hello 0world", "-999/121", 0, -1 }, { "hello%*Qdworld", "hello3/2world", "-999/121", 0, -1 }, { "%Qd", "", "-999/121", -1, -1 }, { "%Qd", " ", "-999/121", -1, -1 }, { " %Qd", "", "-999/121", -1, -1 }, { "xyz%Qd", "", "-999/121", -1, -1 }, { "%*Qd", "", "-999/121", -1, -1 }, { " %*Qd", "", "-999/121", -1, -1 }, { "xyz%*Qd", "", "-999/121", -1, -1 }, /* match something, but invalid */ { "%Qd", "-", "-999/121", 0, 1 }, { "%Qd", "+", "-999/121", 0, 1 }, { "%Qd", "/-", "-999/121", 0, 1 }, { "%Qd", "/+", "-999/121", 0, 1 }, { "%Qd", "-/", "-999/121", 0, 1 }, { "%Qd", "+/", "-999/121", 0, 1 }, { "%Qd", "-/-", "-999/121", 0, 1 }, { "%Qd", "-/+", "-999/121", 0, 1 }, { "%Qd", "+/+", "-999/121", 0, 1 }, { "%Qd", "/123", "-999/121", 0, 1 }, { "%Qd", "-/123", "-999/121", 0, 1 }, { "%Qd", "+/123", "-999/121", 0, 1 }, { "%Qd", "123/", "-999/121", 0, 1 }, { "%Qd", "123/-", "-999/121", 0, 1 }, { "%Qd", "123/+", "-999/121", 0, 1 }, { "xyz%Qd", "xyz-", "-999/121", 0, 4 }, { "xyz%Qd", "xyz+", "-999/121", 0, 4 }, { "%1Qi", "12/57", "1", 1, 1 }, { "%2Qi", "12/57", "12", 1, 2 }, { "%3Qi", "12/57", "-999/121", 0, -1 }, { "%4Qi", "12/57", "12/5", 1, 4 }, { "%5Qi", "12/57", "12/57", 1, 5 }, { "%6Qi", "12/57", "12/57", 1, 5 }, { "%7Qi", "12/57", "12/57", 1, 5 }, { "%1Qi", "012/057", "0", 1, 1 }, { "%2Qi", "012/057", "01", 1, 2 }, { "%3Qi", "012/057", "012", 1, 3 }, { "%4Qi", "012/057", "-999/121", 0, -1 }, { "%5Qi", "012/057", "012/0", 1, 5 }, { "%6Qi", "012/057", "012/5", 1, 6 }, { "%7Qi", "012/057", "012/057", 1, 7 }, { "%8Qi", "012/057", "012/057", 1, 7 }, { "%9Qi", "012/057", "012/057", 1, 7 }, { "%1Qi", "0x12/0x57", "0", 1, 1 }, { "%2Qi", "0x12/0x57", "0", 1, 2 }, { "%3Qi", "0x12/0x57", "0x1", 1, 3 }, { "%4Qi", "0x12/0x57", "0x12", 1, 4 }, { "%5Qi", "0x12/0x57", "-999/121", 0, -1 }, { "%6Qi", "0x12/0x57", "0x12/0", 1, 6 }, { "%7Qi", "0x12/0x57", "0x12/0", 1, 7 }, { "%8Qi", "0x12/0x57", "0x12/0x5", 1, 8 }, { "%9Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%10Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%11Qi", "0x12/0x57", "0x12/0x57", 1, 9 }, { "%Qd", "xyz", "0", 0, 0 }, }; int i, j, ignore, got_ret, want_ret, got_upto, want_upto; mpq_t got, want; long got_l, want_ftell; int error = 0; fun_t fun; const char *name; char fmt[128]; mpq_init (got); mpq_init (want); for (i = 0; i < numberof (data); i++) { mpq_set_str_or_abort (want, data[i].want, 0); ASSERT_ALWAYS (strlen (data[i].fmt) + 2 < sizeof (fmt)); strcpy (fmt, data[i].fmt); strcat (fmt, "%n"); ignore = (strchr (fmt, '*') != NULL); for (j = 0; j <= 3; j++) { want_ret = data[i].ret; want_ftell = data[i].ftell; if (want_ftell == -1) want_ftell = strlen (data[i].input); want_upto = want_ftell; if (want_ret == -1 || (want_ret == 0 && ! ignore)) { want_ftell = -1; want_upto = -555; } switch (j) { case 0: name = "gmp_sscanf"; fun = fun_gmp_sscanf; break; case 1: name = "gmp_fscanf"; fun = fun_gmp_fscanf; break; case 2: if (strchr (data[i].input, '/') != NULL) continue; if (! libc_scanf_convert (fmt)) continue; name = "standard sscanf"; fun = fun_sscanf; break; case 3: if (strchr (data[i].input, '/') != NULL) continue; if (! libc_scanf_convert (fmt)) continue; name = "standard fscanf"; fun = fun_fscanf; break; default: ASSERT_ALWAYS (0); break; } got_upto = -555; got_ftell = -1; switch (j) { case 0: case 1: mpq_set_si (got, -999L, 121L); if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, got, &got_upto); break; case 2: case 3: got_l = -999L; if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL); else got_ret = (*fun) (data[i].input, fmt, &got_l, &got_upto); mpq_set_si (got, got_l, (got_l == -999L ? 121L : 1L)); break; default: ASSERT_ALWAYS (0); break; } MPZ_CHECK_FORMAT (mpq_numref (got)); MPZ_CHECK_FORMAT (mpq_denref (got)); if (got_ret != want_ret) { printf ("%s wrong return value\n", name); error = 1; } /* use direct mpz compares, since some of the test data is non-canonical and can trip ASSERTs in mpq_equal */ if (want_ret == 1 && ! (mpz_cmp (mpq_numref(want), mpq_numref(got)) == 0 && mpz_cmp (mpq_denref(want), mpq_denref(got)) == 0)) { printf ("%s wrong result\n", name); error = 1; } if (got_upto != want_upto) { printf ("%s wrong upto\n", name); error = 1; } if (got_ftell != -1 && want_ftell != -1 && got_ftell != want_ftell) { printf ("%s wrong ftell\n", name); error = 1; } if (error) { printf (" fmt \"%s\"\n", data[i].fmt); printf (" input \"%s\"\n", data[i].input); printf (" ret want=%d\n", want_ret); printf (" got =%d\n", got_ret); mpq_trace (" value want", want); mpq_trace (" got ", got); printf (" upto want=%d\n", want_upto); printf (" got =%d\n", got_upto); if (got_ftell != -1) { printf (" ftell want =%ld\n", want_ftell); printf (" got =%ld\n", got_ftell); } abort (); } } } mpq_clear (got); mpq_clear (want);}voidcheck_f (void){ static const struct { const char *fmt; const char *input; const char *want; int ret; long ftell; } data[] = { { "%Ff", "0", "0", 1, -1 }, { "%Fe", "0", "0", 1, -1 }, { "%FE", "0", "0", 1, -1 }, { "%Fg", "0", "0", 1, -1 }, { "%FG", "0", "0", 1, -1 }, { "%Ff", "123", "123", 1, -1 }, { "%Ff", "+123", "123", 1, -1 }, { "%Ff", "-123", "-123", 1, -1 }, { "%Ff", "123.", "123", 1, -1 }, { "%Ff", "+123.", "123", 1, -1 }, { "%Ff", "-123.", "-123", 1, -1 }, { "%Ff", "123.0", "123", 1, -1 }, { "%Ff", "+123.0", "123", 1, -1 }, { "%Ff", "-123.0", "-123", 1, -1 }, { "%Ff", "123e", "123", 1, -1 }, { "%Ff", "-123e", "-123", 1, -1 }, { "%Ff", "123e-", "123", 1, -1 }, { "%Ff", "-123e-", "-123", 1, -1 }, { "%Ff", "123e+", "123", 1, -1 }, { "%Ff", "-123e+", "-123", 1, -1 }, { "%Ff", "123.456e3", "123456", 1, -1 }, { "%Ff", "-123.456e3", "-123456", 1, -1 }, { "%Ff", "123.456e+3", "123456", 1, -1 }, { "%Ff", "-123.456e+3", "-123456", 1, -1 }, { "%Ff", "123000e-3", "123", 1, -1 }, { "%Ff", "-123000e-3", "-123", 1, -1 }, { "%Ff", "123000.e-3", "123", 1, -1 }, { "%Ff", "-123000.e-3", "-123", 1, -1 }, { "%Ff", "123.456E3", "123456", 1, -1 }, { "%Ff", "-123.456E3", "-123456", 1, -1 }, { "%Ff", "123.456E+3", "123456", 1, -1 }, { "%Ff", "-123.456E+3", "-123456", 1, -1 }, { "%Ff", "123000E-3", "123", 1, -1 }, { "%Ff", "-123000E-3", "-123", 1, -1 }, { "%Ff", "123000.E-3", "123", 1, -1 }, { "%Ff", "-123000.E-3", "-123", 1, -1 }, { "%Ff", ".456e3", "456", 1, -1 }, { "%Ff", "-.456e3", "-456", 1, -1 }, { "%Ff", ".456e+3", "456", 1, -1 }, { "%Ff", "-.456e+3", "-456", 1, -1 }, { "%Ff", " 0", "0", 1, -1 }, { "%Ff", " 0", "0", 1, -1 }, { "%Ff", " 0", "0", 1, -1 }, { "%Ff", "\t0", "0", 1, -1 }, { "%Ff", "\t\t0", "0", 1, -1 }, { "hello%Fg", "hello0", "0", 1, -1 }, { "hello%Fg", "hello 0", "0", 1, -1 }, { "hello%Fg", "hello \t0", "0", 1, -1 }, { "hello%Fgworld", "hello 0world", "0", 1, -1 }, { "hello%Fg", "hello3.0", "3.0", 1, -1 }, { "hello%*Fg", "hello0", "-999", 0, -1 }, { "hello%*Fg", "hello 0", "-999", 0, -1 }, { "hello%*Fg", "hello \t0", "-999", 0, -1 }, { "hello%*Fgworld", "hello 0world", "-999", 0, -1 }, { "hello%*Fgworld", "hello3.0world", "-999", 0, -1 }, { "%Ff", "", "-999", -1, -1 }, { "%Ff", " ", "-999", -1, -1 }, { "%Ff", "\t", "-999", -1, -1 }, { "%Ff", " \t", "-999", -1, -1 }, { " %Ff", "", "-999", -1, -1 }, { "xyz%Ff", "", "-999", -1, -1 }, { "%*Ff", "", "-999", -1, -1 }, { " %*Ff", "", "-999", -1, -1 }, { "xyz%*Ff", "", "-999", -1, -1 }, { "%Ff", "xyz", "0", 0 }, /* various non-empty but invalid */ { "%Ff", "-", "-999", 0 }, { "%Ff", "+", "-999", 0 }, { "xyz%Ff", "xyz-", "-999", 0 }, { "xyz%Ff", "xyz+", "-999", 0 }, { "xyz%Ff", "-.", "-999", 0 }, { "xyz%Ff", "+.", "-999", 0 }, { "xyz%Ff", ".e", "-999", 0 }, { "xyz%Ff", "-.e", "-999", 0 }, { "xyz%Ff", "+.e", "-999", 0 }, { "xyz%Ff", ".E", "-999", 0 }, { "xyz%Ff", "-.E", "-999", 0 }, { "xyz%Ff", "+.E", "-999", 0 }, { "xyz%Ff", ".e123", "-999", 0 }, { "xyz%Ff", "-.e123", "-999", 0 }, { "xyz%Ff", "+.e123", "-999", 0 }, }; int i, j, ignore, got_ret, want_ret, got_upto, want_upto; mpf_t got, want; double got_d; long want_ftell; int error = 0; fun_t fun; const char *name; char fmt[128]; mpf_init (got); mpf_init (want); for (i = 0; i < numberof (data); i++) { mpf_set_str_or_abort (want, data[i].want, 0); ASSERT_ALWAYS (strlen (data[i].fmt) + 2 < sizeof (fmt)); strcpy (fmt, data[i].fmt); strcat (fmt, "%n"); ignore = (strchr (fmt, '*') != NULL); for (j = 0; j <= 3; j++) { want_ret = data[i].ret; want_ftell = data[i].ftell; if (want_ftell == -1) want_ftell = strlen (data[i].input); want_upto = want_ftell; if (want_ret == -1 || (want_ret == 0 && ! ignore)) { want_ftell = -1; want_upto = -555; } switch (j) { case 0: name = "gmp_sscanf"; fun = fun_gmp_sscanf; break; case 1: name = "gmp_fscanf"; fun = fun_gmp_fscanf; break; case 2: if (! libc_scanf_convert (fmt)) continue; name = "standard sscanf"; fun = fun_sscanf; break; case 3: if (! libc_scanf_convert (fmt)) continue; name = "standard fscanf"; fun = fun_fscanf; break; default: ASSERT_ALWAYS (0); break; } got_upto = -555; got_ftell = -1; switch (j) { case 0: case 1: mpf_set_si (got, -999L); if (ignore) got_ret = (*fun) (data[i].input, fmt, &got_upto, NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -