fgets_tc.c 909 字节
Newer Older
X
xiangxistu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <stdio.h>

static int fgets_entry(void)
{
    FILE *stream;
    char data[] = "test fgets";
    char gets[sizeof(data)] = {0};
    size_t size = 0;
    int ret = 0;

    stream = fopen("fopen_file.txt","w");
    if (stream == NULL)
    {
        perror("fopen fail");
        ret = -1;
        goto __exit;
    }
    fwrite(data, sizeof(data), 1, stream);
    fclose(stream);

    stream = fopen("fopen_file.txt","r");
C
chiehwarm 已提交
22 23 24 25 26 27
    if (stream == NULL)
    {
        perror("fopen fail");
        ret = -1;
        goto __exit;
    }
X
xiangxistu 已提交
28 29 30 31 32 33
    fgets(gets, sizeof(gets), stream);

    if(strcmp(gets, data))
    {
        ret = -1;
    }
C
chiehwarm 已提交
34
    fclose(stream);
X
xiangxistu 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

__exit:
    return ret;
}

#include <utest.h>
static void test_fgets(void)
{
    uassert_int_equal(fgets_entry(), 0);
}
static void testcase(void)
{
    UTEST_UNIT_RUN(test_fgets);
}
UTEST_TC_EXPORT(testcase, "posix.stdio_h.fgets.c", RT_NULL, RT_NULL, 10);