Skip to content

Commit

Permalink
Adds conformance tests for fscanf
Browse files Browse the repository at this point in the history
Adds a set of conformance testcases for fscanf

Signed-off-by: Ritul Jasuja <[email protected]>
  • Loading branch information
Rjasuja committed Feb 11, 2019
1 parent 989bcfc commit ce71c04
Show file tree
Hide file tree
Showing 10 changed files with 1,370 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* This file is licensed under the GPL license. For the full content
* of this license, see the COPYING file at the top level of this
* source tree.
*/

/*
* assertion:
* When the length of the input item ( i.e. the longest sequence of input
* bytes which is an initial subsequence of a matching sequence) is 0
* (when EOF,encoding error or read error has not occurred), the execution
* of the conversion specification fails and it is a matching failure.
*
* method:
* -open file in write mode
* -write 1 sample string into a file
* -close the file
* -open the same file in read mode
* -read data from the file using fscanf() and "%d" conversion specifier
* -check the return value of fscanf, errno and feof() return value
*/

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "posixtest.h"

#define TNAME "fscanf/10-1.c"
#define FNAME "in_file"
#define STR_CONST "POSIX CONFORMANCE TEST"

static void remove_file(FILE *in_fp)
{
errno = 0;
if (in_fp != NULL) {
if (fclose(in_fp) == EOF) {
printf(TNAME " Error at fclose(), errno = %d\n", errno);
unlink(FNAME);
exit(PTS_UNRESOLVED);
}
}
unlink(FNAME);
}

int main(void)
{
FILE *in_fp = NULL;
int sample_value = 0;
int ret = 0;

errno = 0;

in_fp = fopen(FNAME, "w");
if (in_fp == NULL) {
printf(TNAME " Error in fopen(), errno: %d\n", errno);
exit(PTS_UNRESOLVED);
}

ret = fprintf(in_fp, "%s", STR_CONST);
if (ret < 0) {
printf(TNAME " Error in fprintf()\n");
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
if (fclose(in_fp) == EOF) {
printf(TNAME " Error at fclose(), errno = %d\n", errno);
unlink(FNAME);
exit(PTS_UNRESOLVED);
}

errno = 0;
in_fp = fopen(FNAME, "r");
if (in_fp == NULL) {
printf(TNAME " Error in fopen(), errno = %d\n", errno);
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
ret = fscanf(in_fp, "%d", &sample_value);
if (ret == 0 && errno == 0 && feof(in_fp) == 0) {
printf(TNAME " Test Passed\n");
remove_file(in_fp);
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed\n");
printf(TNAME
" Expected values: ret = 0, errno = 0, feof(in_fp) = 0\n\t\t\t"
"Obtained values: ret = %d, errno = %d, feof(in_fp) = %d\n",
ret, errno, feof(in_fp));
remove_file(in_fp);
exit(PTS_FAIL);
}
}
107 changes: 107 additions & 0 deletions testcases/open_posix_testsuite/conformance/interfaces/fscanf/12-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is licensed under the GPL license. For the full content
* of this license, see the COPYING file at the top level of this
* source tree.
*/

/*
* assertion:
*
* When assignment suppression is indicated by "*", the result of the
* conversion introduced by "%" is not stored in any object.
*
* method:
* -open file in write mode
* -write a string and an integer into the file
* -close the file
* -open the same file in read mode
* -read the data from the file using fscanf(),
* but integer with "*" assignment suppression
* -check the integer value
*/

#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "posixtest.h"

#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"

#define TNAME "fscanf/12-1.c"
#define FNAME "in_file"
#define INT_CONST 10
#define STR_CONST "JOHN"

static void remove_file(FILE *in_fp)
{
errno = 0;
if (in_fp != NULL)
if (fclose(in_fp) != 0) {
output(TNAME " Error in closing the file, errno = %d\n",
errno);
unlink(FNAME);
exit(PTS_UNRESOLVED);
}
unlink(FNAME);
}

int main(void)
{
char sample_str[sizeof(STR_CONST)];
int sample_int = 0;
FILE *in_fp;
int ret;

errno = 0;

in_fp = fopen(FNAME, "w");
if (in_fp == NULL) {
output(TNAME " Error in opening the file, errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

ret = fprintf(in_fp, "%s %d", STR_CONST, INT_CONST);
if (ret < 0) {
output(TNAME " Error in writing into the file\n");
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
ret = fclose(in_fp);
if (ret != 0) {
output(TNAME " Error in closing the file, errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

errno = 0;
in_fp = fopen(FNAME, "r");
if (in_fp == NULL) {
output(TNAME " Error in opening the file, errno = %d\n", errno);
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
ret = fscanf(in_fp, "%s %*d", sample_str, &sample_int);
if (ret != 1) {
output(TNAME
" Unexpected return from fscanf. Expected 1 got %d, errno = %d\n",
ret, errno);
remove_file(in_fp);
exit(PTS_FAIL);
}

remove_file(in_fp);

if (sample_int != INT_CONST) {
output(TNAME " Test Passed\n");
exit(PTS_PASS);
} else {
output(TNAME " Test Failed\n");
exit(PTS_FAIL);
}
}
107 changes: 107 additions & 0 deletions testcases/open_posix_testsuite/conformance/interfaces/fscanf/13-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is licensed under the GPL license. For the full content
* of this license, see the COPYING file at the top level of this
* source tree.
*/

/*
* assertion:
*
* When assignment suppression is indicated by "*", the result of the
* conversion introduced by "%n$" is not stored in any object.
*
* method:
* -open file in write mode
* -write a string and an integer into the file
* -close the file
* -open the same file in read mode
* -read the data from the file using fscanf(),
* but integer with "*" assignment suppression
* -check the integer value
*/

#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "posixtest.h"

#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"

#define TNAME "fscanf/13-1.c"
#define FNAME "in_file"
#define INT_CONST 10
#define STR_CONST "JOHN"

static void remove_file(FILE *in_fp)
{
errno = 0;
if (in_fp != NULL)
if (fclose(in_fp) != 0) {
output(TNAME " Error in closing the file, errno = %d\n",
errno);
unlink(FNAME);
exit(PTS_UNRESOLVED);
}
unlink(FNAME);
}

int main(void)
{
char sample_str[sizeof(STR_CONST)];
int sample_int = 0;
FILE *in_fp;
int ret;

errno = 0;

in_fp = fopen(FNAME, "w");
if (in_fp == NULL) {
output(TNAME " Error in opening the file, errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

ret = fprintf(in_fp, "%s %d", STR_CONST, INT_CONST);
if (ret < 0) {
output(TNAME " Error in writing into the file\n");
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
ret = fclose(in_fp);
if (ret != 0) {
output(TNAME " Error in closing the file, errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

errno = 0;
in_fp = fopen(FNAME, "r");
if (in_fp == NULL) {
output(TNAME " Error in opening the file, errno = %d\n", errno);
remove_file(in_fp);
exit(PTS_UNRESOLVED);
}

errno = 0;
ret = fscanf(in_fp, "%1$s %2$*d", sample_str, &sample_int);
if (ret != 1) {
output(TNAME
" Unexpected return from fscanf. Expected 1 got %d, errno = %d\n",
ret, errno);
remove_file(in_fp);
exit(PTS_FAIL);
}

remove_file(in_fp);

if (sample_int != INT_CONST) {
output(TNAME " Test Passed\n");
exit(PTS_PASS);
} else {
output(TNAME " Test Failed\n");
exit(PTS_FAIL);
}
}
Loading

0 comments on commit ce71c04

Please sign in to comment.