diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/10-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/10-1.c new file mode 100644 index 00000000000..5c42620f5e9 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/10-1.c @@ -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 +#include +#include +#include +#include +#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); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/12-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/12-1.c new file mode 100644 index 00000000000..65ae3ccc50f --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/12-1.c @@ -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 +#include +#include +#include +#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); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/13-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/13-1.c new file mode 100644 index 00000000000..e9204206ab9 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/13-1.c @@ -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 +#include +#include +#include +#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); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/2-1.c new file mode 100644 index 00000000000..60fdb995da5 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/2-1.c @@ -0,0 +1,115 @@ +/* + * 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 “%n$” conversion specifier is used, conversion is applied to the + * nth argument after the format in the argument list (where n is a decimal + * integer in range [1,{NL_ARGMAX}]). + * + * method: + * -open file in write mode + * -write 3 integers into the file + * -close the file + * -open the same file in read mode + * -read 2 integers from the file using "%n$", order for reading the arguments + * is: specifying "%3$d" ensures the first input is fed to the third argument + * in the argument list and "%1$s" ensures the next input is given to the + * first argument in the argument list + * -check the values in the arguments specified as described above + * + */ + +#include +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/2-1.c" +#define FNAME "in_file" +#define INT_CONST 123 +#define FLOAT_CONST 456.123789F +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +static void remove_file(FILE *in_fp) +{ + errno = 0; + if (in_fp != NULL) { + if (fclose(in_fp) != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + } + unlink(FNAME); +} + +int main(void) +{ + int int_var = 0; + float float_var = 0.0F; + FILE *in_fp; + int ret; + + errno = 0; + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening file for writing, errno = %d\n", + errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%d %f", INT_CONST, FLOAT_CONST); + if (ret < 0) { + printf(TNAME " Error in writing to the file\n"); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fclose(in_fp); + if (ret != 0) { + printf(TNAME + " Error in closing the file descriptor errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + + errno = 0; + in_fp = fopen(FNAME, "r"); + if (in_fp == NULL) { + printf(TNAME + " Error in opening the file for reading, errno = %d\n", + errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fscanf(in_fp, "%2$d %1$f", &float_var, &int_var); + remove_file(in_fp); + if (ret == 2 && + (fabs(float_var - FLOAT_CONST) < + DBL_EPSILON * max(fabs(float_var), fabs(FLOAT_CONST))) && + int_var == INT_CONST) { + printf(TNAME " Test Passed\n"); + exit(PTS_PASS); + } else { + printf(TNAME + " Test Failed with values as Expected:\t ret: %d got %d" + "\n\t\tInteger value: %d got %d \n\t\t Float value: %f" + " got %f", 2, ret, INT_CONST, int_var, FLOAT_CONST, + float_var); + exit(PTS_FAIL); + } + +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/3-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/3-1.c new file mode 100644 index 00000000000..6e0d144f33a --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/3-1.c @@ -0,0 +1,125 @@ +/* + * 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 “%n$” conversion specifier is used, “%%” and “%*” + * specifiers are permissible. + * + * method: + * -open file in write mode + * -write 3 long long integers into the file + * -close the file + * -open the same file in read mode + * -read the data from the file using %n$ and %* as directives in fscanf + * -rewind the file + * -read the data from the file using %n$ and %% as directives in fscanf + * -compare the values + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/3-1.c" +#define FNAME "in_file" +#define INT_CONST 123 +#define FLOAT_CONST 456.123789F +#define CONST_NUM 789 + +static void remove_file(FILE *in_fp) +{ + errno = 0; + if (in_fp != NULL) { + if (fclose(in_fp) != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + } + unlink(FNAME); +} + +int main(void) +{ + FILE *in_fp; + int ret; + int int_var = 0, var1 = 0; + + errno = 0; + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening file for writing, errno = %d\n", + errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%d %f %d %%", INT_CONST, FLOAT_CONST, CONST_NUM); + if (ret < 0) { + printf(TNAME " Error in writing to the file\n"); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fclose(in_fp); + if (ret != 0) { + printf(TNAME + " Error in closing the file descriptor errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + + errno = 0; + in_fp = fopen(FNAME, "r"); + if (in_fp == NULL) { + printf(TNAME + " Error in opening the file for reading, errno = %d\n", + errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fscanf(in_fp, "%1$d %*f", &int_var); + if (ret != 1 && feof(in_fp) == 0 && errno == 0) { + printf(TNAME " Error: %s\n", strerror(errno)); + remove_file(in_fp); + exit(PTS_FAIL); + } + + if (int_var == INT_CONST) { + printf(TNAME " Test passed for %%*\n"); + } else { + printf(TNAME + " Test Failed for %%*. Expected integer value: %d got %d", + INT_CONST, int_var); + exit(PTS_FAIL); + } + + errno = 0; + ret = fscanf(in_fp, "%1$d %%", &var1); + remove_file(in_fp); + if (ret != 1 && feof(in_fp) == 0 && errno == 0) { + printf(TNAME " Error: %s\n", strerror(errno)); + exit(PTS_FAIL); + } + + if (var1 == CONST_NUM) { + printf(TNAME " Test Passed for %%%%\n"); + exit(PTS_PASS); + } else { + printf(TNAME " Test Failed for \%\%. Expected var1: %d got %d", + CONST_NUM, var1); + exit(PTS_FAIL); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/4-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/4-1.c new file mode 100644 index 00000000000..fbca83e8bb8 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/4-1.c @@ -0,0 +1,121 @@ +/* + * 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: + * + * A directive composed of one or more white-space characters is executed + * by reading input until no more valid input can be read. + * + * method: + * -open file in write mode + * -write 2 strings and a character with one white-space between the + * strings and two white-spaces between string and character + * -close the file + * -open the same file in read mode + * -read the data from the file using fscanf() + * with wrong conversion specifier for third argument. + * -check the position of the file pointer + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/4-1.c" +#define FNAME "in_file" +#define STR_CONST_1 "JOHN" +#define STR_CONST_2 "LINUX" +#define CHAR_CONST 'A' + +static void remove_file(FILE *in_fp) +{ + errno = 0; + if (in_fp != NULL) { + if (fclose(in_fp) != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + } + unlink(FNAME); +} + +int main(void) +{ + char sample_str_1[sizeof(STR_CONST_1)]; + char sample_str_2[sizeof(STR_CONST_2)]; + int sample_int; + FILE *in_fp; + int ret, read_values; + int file_ptr_pos, expected_file_ptr_pos; + + errno = 0; + + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%s %s %c", STR_CONST_1, STR_CONST_2, CHAR_CONST); + if (ret < 0) { + printf(TNAME " Error in writing into the file\n"); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fclose(in_fp); + if (ret != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + + errno = 0; + in_fp = fopen(FNAME, "r"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + read_values = fscanf(in_fp, "%s %s %d", + sample_str_1, sample_str_2, + &sample_int); + if (read_values != 2) { + printf(TNAME + " Unexpected return from fscanf. Expected 2 got %d, errno = %d\n", + ret, errno); + remove_file(in_fp); + exit(PTS_FAIL); + } + + file_ptr_pos = ftell(in_fp); + + expected_file_ptr_pos = strlen(sample_str_1) + 1 + strlen(sample_str_2) + + 2; + + remove_file(in_fp); + + if (file_ptr_pos == expected_file_ptr_pos) { + printf(TNAME " Test Passed\n"); + exit(PTS_PASS); + } else { + printf(TNAME + " Test Failed. Expected position for the file pointer is %d" + " considering the first string, a white space, second string and 2" + " whitespaces but current position is %d\n", + expected_file_ptr_pos, file_ptr_pos); + exit(PTS_FAIL); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/5-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/5-1.c new file mode 100644 index 00000000000..1337d061600 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/5-1.c @@ -0,0 +1,115 @@ +/* + * 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: + * + * A directive composed of one or more white-space characters is executed + * by reading input until up to the first byte which is not a white-space + * character, which remains unread. + * + * method: + * -open file in write mode + * -write 2 strings into the file with 2 white spaces in between + * -close the file + * -open the same file in read mode + * -read the data from the file using fscanf() + * with a white space in the directive + * -check the position of file pointer + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/5-1.c" +#define FNAME "in_file" +#define STR_CONST_1 "LINUX" +#define STR_CONST_2 "POSIX" + +static void remove_file(FILE *in_fp) +{ + errno = 0; + if (in_fp != NULL) { + if (fclose(in_fp) != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + } + unlink(FNAME); +} + +int main(void) +{ + FILE *in_fp; + int ret, read_values; + char sample_str[sizeof(STR_CONST_1)]; + int file_ptr_pos, expected_file_ptr_pos; + + errno = 0; + + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%s %s", STR_CONST_1, STR_CONST_2); + if (ret < 0) { + printf(TNAME " Error in writing into the file\n"); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fclose(in_fp); + if (ret != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + + errno = 0; + in_fp = fopen(FNAME, "r"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + read_values = fscanf(in_fp, "%s ", sample_str); + if (read_values != 1) { + printf(TNAME + " Unexpected return from fscanf. Expected 1 got %d, errno = %d\n", + ret, errno); + remove_file(in_fp); + exit(PTS_FAIL); + } + + file_ptr_pos = ftell(in_fp); + + expected_file_ptr_pos = strlen(sample_str) + 2; + + remove_file(in_fp); + + if (file_ptr_pos == expected_file_ptr_pos) { + printf(TNAME " Test Passed\n"); + exit(PTS_PASS); + } else { + printf(TNAME + " Test Failed. Expected position for the file pointer is %d" + " considering the first string read and 2 white-sapces but" + " current position is %d\n", + expected_file_ptr_pos, file_ptr_pos); + exit(PTS_FAIL); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/6-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/6-1.c new file mode 100644 index 00000000000..37bbd13087c --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/6-1.c @@ -0,0 +1,119 @@ +/* + * 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 comparison between the byte read and the byte comprising the + * directive are not equivalent, the directive will fail, and the differing + * and subsequent bytes remain unread. + * + * method: + * -open file in write mode + * -write 2 strings and a character into the file + * -close the file + * -open the same file in read mode + * -read the data from the file using fscanf() with + * wrong conversion specifier for second argument, + * -check the position of the file pointer + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/6-1.c" +#define FNAME "in_file" +#define STR_CONST_1 "LINUX" +#define STR_CONST_2 "POSIX" +#define CHAR_CONST 'K' + +static void remove_file(FILE *in_fp) +{ + errno = 0; + if (in_fp != NULL) { + if (fclose(in_fp) != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", + errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + } + unlink(FNAME); +} + +int main(void) +{ + char sample_str_1[sizeof(STR_CONST_1)]; + char sample_str_2[sizeof(STR_CONST_2)]; + int sample_int; + FILE *in_fp; + int ret, read_values; + int file_ptr_pos, expected_file_ptr_pos; + + errno = 0; + + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%s %s %c", STR_CONST_1, STR_CONST_2, CHAR_CONST); + if (ret < 0) { + printf(TNAME " Error in writing into the file\n"); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fclose(in_fp); + if (ret != 0) { + printf(TNAME " Error in closing the file, errno = %d\n", errno); + unlink(FNAME); + exit(PTS_UNRESOLVED); + } + + errno = 0; + in_fp = fopen(FNAME, "r"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + read_values = fscanf(in_fp, "%s %d %s", sample_str_1, &sample_int, + sample_str_2); + if (read_values != 1) { + printf(TNAME + " Unexpected return from fscanf. Expected 1 got %d, errno = %d\n", + ret, errno); + remove_file(in_fp); + exit(PTS_FAIL); + } + + file_ptr_pos = ftell(in_fp); + + expected_file_ptr_pos = strlen(sample_str_1) + 1; + + remove_file(in_fp); + + if (file_ptr_pos == expected_file_ptr_pos) { + printf(TNAME " Test Passed\n"); + exit(PTS_PASS); + } else { + printf(TNAME + " Test Failed. Expected position for the file pointer is %d" + " considering the first string read and a white-space but current" + " position is %d\n", + expected_file_ptr_pos, file_ptr_pos); + exit(PTS_FAIL); + } +} diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/8-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/8-1.c new file mode 100644 index 00000000000..83f4207b5a8 --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/8-1.c @@ -0,0 +1,342 @@ +/* + * 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: + * + * Input white space characters (as specified by isspace( ))are skipped + * unless [, c, C, or n conversion specifiers are used. + * + * method: + * -open file in write mode + * -write whitespace characters ('\n', '\t', '\f', '\r', '\v') into the + * file + * -close the file + * -open the same file in read mode + * -read data from the file using fscanf() and conversion specifier + * -compare the values read into the arguments with input whitespace + * characters + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/8-1.c" +#define FNAME "in_file" +#define SPACE ' ' +#define NEW_LINE '\n' +#define HORIZONTAL_TAB '\t' +#define FORM_FEED '\f' +#define CARRIAGE_RETURN '\r' +#define VERTICAL_TAB '\v' +#define STRING_CONST "LINUX" +#define INTEGER_CONST 10 +#define INPUT_COUNT 6 + +static const char *const char_names[] = { + "SPACE", + "NEW LINE", + "HORIZONTAL TAB", + "FORM FEED", + "CARRIAGE RETURN", + "VERTICAL TAB" +}; + +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) +{ + // Testcase for conversion specifier c: + // Input file contains : SPACE NEW_LINE HORIZONTAL_TAB FORM_FEED + // CARRIAGE_RETURN VERTICAL_TAB + FILE *in_fp = NULL; + int ret = 0, count = 0, sample_int = 0, byte_count = 0; + char sample_char; + wchar_t sample_wide_char = 0; + char sample_string[sizeof(STRING_CONST)]; + + errno = 0; + + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error at fopen(), errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%c%c%c%c%c%c", SPACE, NEW_LINE, HORIZONTAL_TAB, + FORM_FEED, CARRIAGE_RETURN, VERTICAL_TAB); + if (ret < 0) { + printf(TNAME " Error at 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 at fopen(), errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + while (fscanf(in_fp, "%c", &sample_char) == 1 && count < INPUT_COUNT) { + count++; + if (sample_char == SPACE && count == 1) + continue; + else if (sample_char == NEW_LINE && count == 2) + continue; + else if (sample_char == HORIZONTAL_TAB && count == 3) + continue; + else if (sample_char == FORM_FEED && count == 4) + continue; + else if (sample_char == CARRIAGE_RETURN && count == 5) + continue; + else if (sample_char == VERTICAL_TAB && count == 6) + continue; + else { + printf(TNAME " Conversion specifier c: Test Failed\n"); + printf(TNAME " Whitespace char at failure = %s\n", + char_names[count-1]); + remove_file(in_fp); + exit(PTS_FAIL); + } + } + if (count == INPUT_COUNT) { + printf(TNAME + " Test Passed: fscanf successfully read all whitespace " + "characters with 'c' conversion specifier\n"); + } else { + printf(TNAME " Error in fscanf, count = %d\n ", count); + remove_file(in_fp); + exit(PTS_FAIL); + } + + // Testcase for conversion specifier 'n' + errno = 0; + rewind(in_fp); + count = 0; + + while (fscanf(in_fp, "%c%n", &sample_char, &byte_count) == 1) { + count++; + if (sample_char == SPACE && count == 1 && byte_count == 1) + continue; + else if (sample_char == NEW_LINE && count == 2 + && byte_count == 1) + continue; + else if (sample_char == HORIZONTAL_TAB && count == 3 + && byte_count == 1) + continue; + else if (sample_char == FORM_FEED && count == 4 + && byte_count == 1) + continue; + else if (sample_char == CARRIAGE_RETURN && count == 5 + && byte_count == 1) + continue; + else if (sample_char == VERTICAL_TAB && count == 6 + && byte_count == 1) + continue; + else { + printf(TNAME " Conversion specifier n: Test Failed\n"); + printf(TNAME " Whitespace char at failure = %s\n", + char_names[count-1]); + remove_file(in_fp); + exit(PTS_FAIL); + } + } + if (count == INPUT_COUNT) { + printf(TNAME + " Test Passed: fscanf successfully read all whitespace " + "characters with 'n' conversion specifier\n"); + } else { + printf(TNAME " Error in fscanf, count = %d\n ", count); + remove_file(in_fp); + exit(PTS_FAIL); + } + + // Testcase for conversion specifier 'C' + errno = 0; + rewind(in_fp); + count = 0; + + while (fscanf(in_fp, "%C", &sample_wide_char) == 1) { + count++; + if (sample_wide_char == SPACE && count == 1) + continue; + else if (sample_wide_char == NEW_LINE && count == 2) + continue; + else if (sample_wide_char == HORIZONTAL_TAB && count == 3) + continue; + else if (sample_wide_char == FORM_FEED && count == 4) + continue; + else if (sample_wide_char == CARRIAGE_RETURN && count == 5) + continue; + else if (sample_wide_char == VERTICAL_TAB && count == 6) + continue; + else { + printf(TNAME " Conversion specifier C: Test Failed\n"); + printf(TNAME " Whitespace char at failure = %s\n", + char_names[count-1]); + remove_file(in_fp); + exit(PTS_FAIL); + } + } + if (count == INPUT_COUNT) { + printf(TNAME + " Test Passed: fscanf successfully read all whitespace " + "characters with 'C' conversion specifier\n"); + } else { + printf(TNAME " Error in fscanf, count = %d\n ", count); + remove_file(in_fp); + exit(PTS_FAIL); + } + + remove_file(in_fp); + + // Testcase for conversion specifier [: + // Input file contains : SPACE|NEW_LINE|HORIZONTAL_TAB|FORM_FEED| + // CARRIAGE_RETURN|VERTICAL_TAB + errno = 0; + count = 0; + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error in opening the file, errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%c%c%c%c%c%c%c%c%c%c%c", SPACE, '|', NEW_LINE, + '|', HORIZONTAL_TAB, '|', FORM_FEED, '|', + CARRIAGE_RETURN, '|', VERTICAL_TAB); + 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 at fopen(), errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + while ((ret = fscanf(in_fp, "%[^|]%*c", &sample_char)) == 1) { + count++; + if (sample_char == SPACE && count == 1) + continue; + else if (sample_char == NEW_LINE && count == 2) + continue; + else if (sample_char == HORIZONTAL_TAB && count == 3) + continue; + else if (sample_char == FORM_FEED && count == 4) + continue; + else if (sample_char == CARRIAGE_RETURN && count == 5) + continue; + else if (sample_char == VERTICAL_TAB && count == 6) + continue; + else { + printf(TNAME " Conversion specifier [: Test Failed\n"); + printf(TNAME " Whitespace char at failure = %s\n", + char_names[count-1]); + remove_file(in_fp); + exit(PTS_FAIL); + } + } + if (count == INPUT_COUNT) { + printf(TNAME + " Test Passed: fscanf successfully read all whitespace " + "characters with '[' conversion specifier\n"); + } else { + printf(TNAME " Error in fscanf, count =%d\n ", count); + remove_file(in_fp); + exit(PTS_FAIL); + } + + remove_file(in_fp); + + // Testcase for conversion specifier other than c,C,n,[: + errno = 0; + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error at fopen(), errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%d%c%c%c%c%c%c%s", INTEGER_CONST, SPACE, NEW_LINE, + HORIZONTAL_TAB, FORM_FEED, CARRIAGE_RETURN, + VERTICAL_TAB, STRING_CONST); + if (ret < 0) { + printf(TNAME " Error at 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 at fopen(), errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + errno = 0; + ret = fscanf(in_fp, "%d%s", &sample_int, sample_string); + if (ret == 2 && sample_int == INTEGER_CONST && + (strcmp(sample_string, STRING_CONST) == 0)) { + printf(TNAME " Test Passed: As expected fscanf skipped " + "whitespace character when conversion specifer " + "is not c, C, n or [\n"); + remove_file(in_fp); + exit(PTS_PASS); + } else { + printf(TNAME " Test Failed when conversion specifier is " + "not c, C, n or [:\n\t\t\tExpected values: " + "sample_int = %d sample_string = %s\n\t\t\t" + "Obtained values: sample_int = %d sample_string = %s\n", + INTEGER_CONST, STRING_CONST, sample_int, + sample_string); + remove_file(in_fp); + exit(PTS_FAIL); + } +} + diff --git a/testcases/open_posix_testsuite/conformance/interfaces/fscanf/9-1.c b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/9-1.c new file mode 100644 index 00000000000..30143b19d7c --- /dev/null +++ b/testcases/open_posix_testsuite/conformance/interfaces/fscanf/9-1.c @@ -0,0 +1,121 @@ +/* + * 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: + * + * An item is read from the input, unless the conversion specification + * includes an n conversion specifier + * + * method: + * -open file in write mode + * -write a sample integer and sample string into the file + * -close the file + * -open the same file in read mode + * -read data from the file using fscanf() and conversion specifier "%n" + * -check if the return value of fscanf is 0 + * -rewind file pointer and read data from file again but this time without + * "%n" conversion specifier + * -check the return value of fscanf and compare the values read into the + * arguments with input values + */ + +#include +#include +#include +#include +#include +#include "posixtest.h" + +#define TNAME "fscanf/9-1.c" +#define FNAME "in_file" +#define STRING_CONST "LINUX" +#define INTEGER_CONST 32 + +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; + char sample_string[strlen(STRING_CONST)]; + int sample_integer = 0, byte_count = 0, ret = 0; + + errno = 0; + + in_fp = fopen(FNAME, "w"); + if (in_fp == NULL) { + printf(TNAME " Error at fopen(), errno = %d\n", errno); + exit(PTS_UNRESOLVED); + } + + ret = fprintf(in_fp, "%s %d", STRING_CONST, INTEGER_CONST); + if (ret < 0) { + printf(TNAME " Error at 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 at fopen(), errno = %d\n", errno); + remove_file(in_fp); + exit(PTS_UNRESOLVED); + } + + // When conversion specifier is %n only, the return value should be 0 + errno = 0; + ret = fscanf(in_fp, "%n", &byte_count); + if (ret == 0) { + printf(TNAME " When n conversion specifier is used no input is " + "consumed\n"); + } else { + printf(TNAME + "Test Failled. Expected return value of fscanf = 0 " + "but obtained value = %d\n", ret); + exit(PTS_FAIL); + remove_file(in_fp); + } + + // When conversion specifier does not contain %n, ret = number of + // inputs successfully read + errno = 0; + ret = fscanf(in_fp, "%s %d", sample_string, &sample_integer); + remove_file(in_fp); + if (ret == 2 && (strcmp(sample_string, STRING_CONST) == 0) && + sample_integer == INTEGER_CONST) { + printf(TNAME " When n conversion specifier is not used input is" + " consumed\n"); + printf(TNAME " Test Passed\n"); + exit(PTS_PASS); + } else { + printf(TNAME " Test Failed\n"); + printf(TNAME " Expected values: ret = 2, sample_string = %s, " + "sample_integer = %d\n\t\t\tObtained values: ret = %d, " + "sample_string = %s, sample_integer = %d\n", + STRING_CONST, INTEGER_CONST, ret, sample_string, + sample_integer); + exit(PTS_FAIL); + } +}