vfscanf.c 3.7 KB
Newer Older
G
ganlan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "test.h"

const char *file = "/data/tests/libc-test/src/functionalext/supplement/stdio/vfscanf.txt";

int readFile(FILE *stream, char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    int result = vfscanf(stream, fmt, ap);
    va_end(ap);
    return result;
}

/**
 * @tc.name      : vfscanf_0100
 * @tc.desc      : Reads data from the stream and stores them according to the parameter format.
 * @tc.level     : Level 0
 */
void vfscanf_0100(void)
{
    FILE *fp = NULL;
    int val = 0;
    char buffer[BUFSIZ];
    fp = fopen(file, "w+");
    if (fp == NULL) {
        t_error("%s fopen failed\n", __func__);
        return;
    }
    if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
        t_error("%s fprintf failed\n", __func__);
        return;
    }
    rewind(fp);

    int result = readFile(fp, "%s %d", buffer, &val);
    if (result < 0) {
        t_error("%s vfscanf get result is %d are less 0\n", __func__, result);
    }
    if (strcmp(buffer, "vfscanftest") != 0) {
        t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer);
    }
    if (val != 123) {
        t_error("%s vfscanf get is %d are not 123\n", __func__, val);
    }
    fclose(fp);
    remove(file);
}

/**
 * @tc.name      : vfscanf_0200
 * @tc.desc      : Test conversion of different formats
 * @tc.level     : Level 1
 */
void vfscanf_0200(void)
{
    FILE *fp = NULL;
    char val[BUFSIZ];
    char buffer[BUFSIZ];
    fp = fopen(file, "w+");
    if (fp == NULL) {
        t_error("%s fopen failed\n", __func__);
        return;
    }
    if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
        t_error("%s fprintf failed\n", __func__);
        return;
    }
    rewind(fp);

    int result = readFile(fp, "%s %s", buffer, val);
    if (result < 0) {
        t_error("%s vfscanf get result is %d are less 0\n", __func__, result);
    }
    if (strcmp(buffer, "vfscanftest") != 0) {
        t_error("%s vfscanf get is '%s' are not 'vfscanftest'\n", __func__, buffer);
    }
    if (strcmp(val, "123") != 0) {
        t_error("%s vfscanf get is '%s' are not '123'\n", __func__, val);
    }
    fclose(fp);
    remove(file);
}

/**
 * @tc.name      : vfscanf_0300
 * @tc.desc      : The test corresponding position does not match the format
 * @tc.level     : Level 2
 */
void vfscanf_0300(void)
{
    FILE *fp = NULL;
    int val1 = 0;
    int val2 = 0;
    fp = fopen(file, "w+");
    if (fp == NULL) {
        t_error("%s fopen failed", __func__);
        return;
    }
    if (fprintf(fp, "%s %d", "vfscanftest", 123) < 0) {
        t_error("%s fprintf failed", __func__);
        return;
    }
    rewind(fp);

    int result = readFile(fp, "%d %d", val1, val2);
    if (result > 0) {
        t_error("%s vfscanf get result is %d are more than the 0\n", __func__, result);
    }
    if (val1 != 0) {
        t_error("%s vfscanf get is %d are not 0\n", __func__, val1);
    }
    if (val2 != 0) {
        t_error("%s vfscanf get is %d are not 0\n", __func__, val2);
    }
    fclose(fp);
    remove(file);
}

int main(int argc, char *argv[])
{
    vfscanf_0100();
    vfscanf_0200();
    vfscanf_0300();
    return t_status;
}