mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-24 06:12:27 +00:00
Further improve scanf
This commit is contained in:
parent
6ef2a471e4
commit
7e08a97cea
3 changed files with 76 additions and 37 deletions
|
@ -21,7 +21,10 @@
|
|||
#include "libc/intrin/bits.h"
|
||||
#include "libc/inttypes.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
#define sscanf1(STR, FMT) \
|
||||
|
@ -336,3 +339,31 @@ TEST(sscanf, lupluser) {
|
|||
EXPECT_EQ(1, sscanf("+123", "%li", &x));
|
||||
EXPECT_EQ(123, x);
|
||||
}
|
||||
|
||||
TEST(fscanf, stuff) {
|
||||
int x;
|
||||
char *s = "1 12 123\n"
|
||||
"4 44\n";
|
||||
FILE *f = fmemopen(s, strlen(s), "r+");
|
||||
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(1, x);
|
||||
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(12, x);
|
||||
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(123, x);
|
||||
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(4, x);
|
||||
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(44, x);
|
||||
EXPECT_EQ(-1, fscanf(f, "%d", &x));
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(fscanf, wantDecimalButGotLetter_returnsZeroMatches) {
|
||||
int x = 666;
|
||||
char *s = "a1\n";
|
||||
FILE *f = fmemopen(s, strlen(s), "r+");
|
||||
EXPECT_EQ(0, fscanf(f, "%d", &x));
|
||||
EXPECT_EQ(666, x);
|
||||
fclose(f);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue