From 10610a1a8acb5315d324211220466d2563935075 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Thu, 17 Aug 2023 13:13:48 +0800 Subject: [PATCH] Add test for fscanf Issue:I7TWMD Signed-off-by: yinchuang Test:libctest --- libc-test/src/functional/fscanf.c | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/libc-test/src/functional/fscanf.c b/libc-test/src/functional/fscanf.c index 45e0c5e6..4a518b53 100644 --- a/libc-test/src/functional/fscanf.c +++ b/libc-test/src/functional/fscanf.c @@ -5,6 +5,11 @@ #include #include "test.h" +#define CONTENT "0.1234567\n" +#define EXPECT 0.1234567 +#define TMP_FILE "/data/local/tmp/fscanf_test.txt" +#define TOTAL 515 + #define TEST(r, f, x, m) ( \ errno=0, ((r) = (f)) == (x) || \ (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) @@ -25,6 +30,45 @@ static FILE *writetemp(const char *data) return f; } +void write_content() +{ + FILE *fp; + fp = fopen(TMP_FILE, "w"); + if (fp == NULL) { + t_error("FAIL %s create failed!\n", TMP_FILE); + } + for (size_t i = 0; i < TOTAL; i++) { + fwrite(CONTENT, sizeof(char), strlen(CONTENT), fp); + } + fclose(fp); +} + +void check_content() +{ + FILE *fp; + int res; + double value = 0; + fp = fopen(TMP_FILE, "r"); + if (fp == NULL) { + t_error("FAIL %s open failed!\n", TMP_FILE); + } + for (size_t i = 0; i < TOTAL; i++) { + res = fscanf(fp, "%lf", &value); + if (res != 1 || value != EXPECT) { + t_error("FAIL: fscanf expect %lf but get %lf, index=%d\n", EXPECT, value, i); + } + } + fclose(fp); +} + +void test_read_large_data() +{ + write_content(); + check_content(); + remove(TMP_FILE); +} + + int main(void) { int i, x, y; @@ -131,5 +175,7 @@ int main(void) fclose(f); } + test_read_large_data(); + return t_status; } -- GitLab