Commit a92be000 authored by Nagy Tibor's avatar Nagy Tibor

tests: Even more work on error handling

realpath: Fixing undefined behaviour in the second test. If the call fails the resolved_name argument cannot be used for error checking because its state is undefined by SUSv2.
pipe: Changing the order of close and write error handling code. Errors in close could overwrite errno after write errors, returning incorrect error messages.
gmtime: Removed duplicate checks
Other fixes for fseek, rename, mktime, putwchar
parent 2d027f07
......@@ -16,5 +16,5 @@ int main(void) {
off_t pos = ftello(f);
ERROR_IF(ftello, pos, == -1);
printf("ftell: %ld\n", pos);
printf("ftello: %ld\n", pos);
}
......@@ -49,9 +49,8 @@ int main(void) {
UNEXP_IF(remove, rm_status, != 0);
// Compare file contents
if (strcmp(str, buf) == 0) {
exit(EXIT_SUCCESS);
} else {
if (strcmp(str, buf) != 0) {
puts("Comparison failed!");
exit(EXIT_FAILURE);
}
}
......@@ -7,25 +7,14 @@
#include "test_helpers.h"
int main(void) {
char* path = realpath("stdlib/realpath.c", NULL);
if (!path) {
perror("realpath");
exit(EXIT_FAILURE);
}
puts(path);
char *path_res = realpath("stdlib/realpath.c", NULL);
ERROR_IF(realpath, path_res, == NULL);
puts(path_res);
free(path_res);
free(path);
path = malloc(PATH_MAX);
memset(path, 0, PATH_MAX);
realpath("stdlib/realpath.c", path);
if (!path) {
perror("realpath");
free(path);
exit(EXIT_FAILURE);
}
puts(path);
free(path);
char path_arg[PATH_MAX] = { 0 };
char *res = realpath("stdlib/realpath.c", path_arg);
ERROR_IF(realpath, res, == NULL);
puts(path_arg);
free(path_arg);
}
......@@ -18,12 +18,4 @@ int main(void) {
info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) {
exit(EXIT_FAILURE);
}
if (info->tm_sec != expected.tm_sec || info->tm_min != expected.tm_min ||
info->tm_hour != expected.tm_hour || info->tm_mday != expected.tm_mday ||
info->tm_year != expected.tm_year || info->tm_wday != expected.tm_wday ||
info->tm_yday != expected.tm_yday || info->tm_isdst != expected.tm_isdst ||
info->tm_gmtoff != expected.tm_gmtoff || strcmp(info->tm_zone, expected.tm_zone) != 0) {
exit(EXIT_FAILURE);
}
}
......@@ -6,11 +6,15 @@
#include "test_helpers.h"
int check(time_t input) {
struct tm* t = localtime(&input);
struct tm *t = localtime(&input);
ERROR_IF(localtime, t, == NULL);
printf("%ld = %ld\n", input, mktime(t));
time_t output = mktime(t);
ERROR_IF(mktime, output, == (time_t)-1);
if (input != mktime(t)) {
printf("%ld = %ld\n", input, output);
if (input != output) {
printf(
"Year %d, Day of year: %d, Month %d, Day of month: %d, Day of week: %d, %d:%d:%d\n",
t->tm_year, t->tm_yday, t->tm_mon, t->tm_mday, t->tm_wday, t->tm_hour, t->tm_min, t->tm_sec
......@@ -41,8 +45,13 @@ int main(void) {
for (int i = 0; i < 10; i += 1) {
time_t input = (time_t) rand();
struct tm* time = localtime(&input);
struct tm *time = localtime(&input);
ERROR_IF(localtime, time, == NULL);
time_t output = mktime(time);
ERROR_IF(mktime, output, == (time_t)-1);
if (input != output) {
// asctime has newline
printf("Comparison %ld == %ld failed. Time: %s", input, output, asctime(time));
......
......@@ -7,57 +7,56 @@
#include "test_helpers.h"
int main(void) {
int pid, pip[2];
int pip[2];
char instring[20];
char * outstring = "Hello World!";
char *outstring = "Hello World!";
if (pipe(pip) < 0) {
perror("pipe");
exit(EXIT_FAILURE);
}
int pipe_status = pipe(pip);
ERROR_IF(pipe, pipe_status, == -1);
UNEXP_IF(pipe, pipe_status, != 0);
pid = fork();
if (pid == 0) /* child : sends message to parent*/
{
/* close read end */
close(pip[0]);
int pid = fork();
ERROR_IF(fork, pid, == -1);
/* send 7 characters in the string, including end-of-string */
int bytes = write(pip[1], outstring, strlen(outstring));
if (pid == 0) {
// child: sends message to parent
// close read end
int cr = close(pip[0]);
ERROR_IF(close, cr, == -1);
UNEXP_IF(close, cr, != 0);
/* close write end */
close(pip[1]);
// send 7 characters in the string, including end-of-string
int bytes = write(pip[1], outstring, strlen(outstring));
ERROR_IF(write, bytes, == -1);
/* check result */
if (bytes < 0) {
perror("pipe write");
exit(EXIT_FAILURE);
} else if (bytes != strlen(outstring)) {
// check result
if (bytes != strlen(outstring)) {
fprintf(stderr, "pipe write: %d != %ld\n", bytes, strlen(outstring));
exit(EXIT_FAILURE);
}
// close write end
int cw = close(pip[1]);
ERROR_IF(close, cw, == -1);
UNEXP_IF(close, cw, != 0);
exit(EXIT_SUCCESS);
}
else /* parent : receives message from child */
{
/* close write end */
close(pip[1]);
} else {
// parent: receives message from child
// close write end
int cw = close(pip[1]);
ERROR_IF(close, cw, == -1);
UNEXP_IF(close, cw, != 0);
/* clear memory */
// clear memory
memset(instring, 0, sizeof(instring));
/* read from the pipe */
// read from the pipe
int bytes = read(pip[0], instring, sizeof(instring) - 1);
ERROR_IF(read, bytes, == -1);
/* close read end */
close(pip[0]);
/* check result */
if (bytes < 0) {
perror("pipe read");
exit(EXIT_FAILURE);
} else if (bytes != strlen(outstring)) {
// check result
if (bytes != strlen(outstring)) {
fprintf(stderr, "pipe read: %d != %ld\n", bytes, strlen(outstring));
exit(EXIT_FAILURE);
} else if (memcmp(instring, outstring, strlen(outstring)) != 0) {
......@@ -67,6 +66,11 @@ int main(void) {
printf("%s\n", instring);
}
// close read end
int cr = close(pip[0]);
ERROR_IF(close, cr, == -1);
UNEXP_IF(close, cr, != 0);
exit(EXIT_SUCCESS);
}
}
......@@ -8,13 +8,8 @@
int main(void) {
wchar_t *wcs = L"zß水🍌";
int i;
for (i = 0; wcs[i] != L'\0'; i++)
{
if (0xFFFFFFFFu == putwchar(wcs[i]))
{
printf("Unable to putwchar() the wide character.\n");
exit(EXIT_FAILURE);
}
for (int i = 0; wcs[i] != L'\0'; i++) {
wint_t status = putwchar(wcs[i]);
ERROR_IF(putwchar, status, == WEOF);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment