Skip to content

Add strtol conformance tests #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions testcases/open_posix_testsuite/conformance/interfaces/strtol/1-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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:
* This function will convert the initial portion of the string pointed to by
* nptr to a type long.
*
* method:
* -Convert LONG_MAX to string using sprintf
* as strtol() needs input in string format.
* -Convert string to long using strtol() function.
* -Compare the return value with LONG_MAX.
*
*/

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

#define TNAME "strtol/1-1.c"
#define MAX_ARRAY_SIZE 50

int main(void)
{
char nptr[MAX_ARRAY_SIZE];
long result;
int char_written;
errno = 0;

/* convert integer to string */
char_written = sprintf(nptr, "%ld", LONG_MAX);
if (char_written != strlen(nptr)) {
printf(TNAME " Error at sprintf(), errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

errno = 0;
result = strtol(nptr, NULL, 10);
if (result == 0) {
printf(TNAME " Unexpected return from strtol(), expected: %ld, But got: %ld,"
" errno = %d\n", LONG_MAX, result, errno);
exit(PTS_FAIL);
}

if (result == LONG_MAX) {
printf(TNAME " Test passed, String got converted into long.\n");
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed, String conversion to long got failed, Expected: %ld,"
" But got: %ld.\n", LONG_MAX, result);
exit(PTS_FAIL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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:
* If no conversion could be performed:
* 0 will be returned.
*
* method:
* -Take a string with first invalid byte, for octal integer.
* -Convert it to long using strtol()
* with base 8.
* -check whether the result is 0.
*/

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

#define TNAME "strtol/10-1.c"

int main(void)
{
char nptr[] = "H1234";
long result;

result = strtol(nptr, NULL, 8);
if (result == 0) {
printf(TNAME " Test passed. Returned zero as expected.\n");
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed. Expected: %d ,But got: %ld\n", 0, result);
exit(PTS_FAIL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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:
* If the value of base is not supported:
* 0 will be returned and errno will be set to [EINVAL]
*
* method:
* -Convert LONG_MAX to string using sprintf
* as strtol() needs input in string format.
* -Convert string to long using strtol() function
* with base which is not supported by strtol(<2 and >36)
* -Compare the return value and errno.
*
*/

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

#define TNAME "strtol/11-1.c"
#define MAX_ARRAY_SIZE 50

int main(void)
{
char nptr[MAX_ARRAY_SIZE] = {};
long result;
int char_written;
errno = 0;

/* convert integer to string */
char_written = sprintf(nptr, "%ld", LONG_MAX);
if (char_written != strlen(nptr)) {
printf(TNAME " Error at sprintf(), errno = %d\n", errno);
exit(PTS_UNRESOLVED);
}

errno = 0;
result = strtol(nptr, NULL, 37);
if (result == 0 && errno == EINVAL) {
printf(TNAME " Test passed, conversion error occured for unsupported base.\n");
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed, Expected result: %d, But obtained: %ld"
" and Expected errno: %d, But obtained errno: %d\n"
, 0, result, EINVAL, errno);
exit(PTS_FAIL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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:
* If the correct value is outside the range of representable values:
* {LONG_MIN}, {LONG_MAX}, will be returned (according to the sign
* of the value), and errno set to [ERANGE].
*
* method:
* -Define a string which has an integer greater
* than LONG_MAX range.
* -Convert string to long using strtol() function.
* -Compare the return value with LONG_MAX and check errno.
* -Define a string which has an integer lesser
* than LONG_MIN range.
* -Convert string to long using strtol() function.
* -Compare the return value with LONG_MIN and check errno.
*
*/

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

#define TNAME "strtol/12-1.c"

int main(void)
{
char nptr_1[] = "922337203685477580798";
char nptr_2[] = "-922337203685477580798";
long result;
errno = 0;

result = strtol(nptr_1, NULL, 10);
if (result == LONG_MAX && errno == ERANGE) {
printf(TNAME " Test passed, LONG_MAX is returned and errno is set to indicate "
"out of range error.\n");
} else {
printf(TNAME " Test Failed, Expected result: %ld, But got result: %ld\n"
" \t\t\t and Expected errno: %d, But obtained errno: %d\n"
, LONG_MAX, result, ERANGE, errno);
exit(PTS_FAIL);
}

errno = 0;
result = strtol(nptr_2, NULL, 10);
if (result == LONG_MIN && errno == ERANGE) {
printf(TNAME " Test passed, LONG_MIN is returned and errno is set to indicate "
"out of range error.\n");
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed, Expected result: %ld, But got result: %ld\n"
"\t\t\t and Expected errno: %d, But obtained errno: %d\n",
LONG_MIN, result, ERANGE, errno);
exit(PTS_FAIL);
}
}
71 changes: 71 additions & 0 deletions testcases/open_posix_testsuite/conformance/interfaces/strtol/2-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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:
* If the input string is empty or consists entirely of white-space characters,
* or if the first non-white-space character is other than a sign or a permissible
* letter or digit:
* The subject sequence will contain no characters.
* A subject sequence is interpreted as an integer represented in some radix
* determined by the value of base.
*
* method:
* -Take a input string with first non-white-space character
* other than permissible letter.
* -Convert string to long using strtol() function
* with base 10.
* -Compare the value stored in nptr and endptr.
* -Repeat above steps for empty string and string
* having white-space characters with base 8 and 16
* respectively.
*/

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

#define TNAME "strtol/2-1.c"

int main(void)
{
char nptr_1[] = "Z123";
char nptr_2[] = {};
char nptr_3[] = " ";
char *endptr = NULL;

/* Check with first non-white-space character other than permissible letter */
strtol(nptr_1, &endptr, 10);
if (nptr_1 == endptr) {
printf(TNAME " Test passed.\n");
} else {
printf(TNAME " Test Failed. Expected: %s, But got: %s.\n", nptr_1, endptr);
exit(PTS_FAIL);
}

/* Check with empty string */
strtol(nptr_2, &endptr, 8);
if (nptr_2 == endptr) {
printf(TNAME " Test passed.\n");
} else {
printf(TNAME " Test Failed. Expected: %s, But got: %s.\n", nptr_2, endptr);
exit(PTS_FAIL);
}

/* Check with white-space characters */
strtol(nptr_3, &endptr, 16);
if (nptr_3 == endptr) {
printf(TNAME " Test passed.\n");
exit(PTS_PASS);
} else {
printf(TNAME " Test Failed. Expected: %s, But got: %s.\n", nptr_3, endptr);
exit(PTS_FAIL);
}
}
Loading