未验证 提交 51ac0563 编写于 作者: O openharmony_ci 提交者: Gitee

!1032 Add test for fscanf

Merge pull request !1032 from yinchuang/add_test_for_fscanf
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
#include <unistd.h> #include <unistd.h>
#include "test.h" #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) ( \ #define TEST(r, f, x, m) ( \
errno=0, ((r) = (f)) == (x) || \ errno=0, ((r) = (f)) == (x) || \
(t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) ) (t_error("%s failed (" m ")\n", #f, r, x, strerror(errno)), 0) )
...@@ -25,6 +30,45 @@ static FILE *writetemp(const char *data) ...@@ -25,6 +30,45 @@ static FILE *writetemp(const char *data)
return f; 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 main(void)
{ {
int i, x, y; int i, x, y;
...@@ -131,5 +175,7 @@ int main(void) ...@@ -131,5 +175,7 @@ int main(void)
fclose(f); fclose(f);
} }
test_read_large_data();
return t_status; return t_status;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册