tests: More work on error handling
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
#include "test_helpers.h"
|
||||
|
||||
int main(void) {
|
||||
freopen("stdio/stdio.in", "r", stdin);
|
||||
char in[6];
|
||||
fgets(in, 6, stdin);
|
||||
printf("%s\n", in); // should print Hello
|
||||
FILE *f = freopen("stdio/stdio.in", "r", stdin);
|
||||
ERROR_IF(freopen, f, == NULL);
|
||||
|
||||
char in[6];
|
||||
fgets(in, 6, stdin);
|
||||
printf("%s\n", in); // should print Hello
|
||||
}
|
||||
|
||||
+8
-5
@@ -7,11 +7,14 @@ int main(void) {
|
||||
FILE *f = fopen("stdio/stdio.in", "r");
|
||||
ERROR_IF(fopen, f, == NULL);
|
||||
|
||||
if (fseek(f, 14, SEEK_CUR) < 0) {
|
||||
puts("fseek error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int status = fseek(f, 14, SEEK_CUR);
|
||||
ERROR_IF(fseek, status, == -1);
|
||||
UNEXP_IF(fseek, status, != 0);
|
||||
|
||||
char buffer[256];
|
||||
printf("%s", fgets(buffer, 256, f));
|
||||
printf("ftell: %ld\n", ftello(f));
|
||||
|
||||
off_t pos = ftello(f);
|
||||
ERROR_IF(ftello, pos, == -1);
|
||||
printf("ftell: %ld\n", pos);
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
int main(void) {
|
||||
FILE *fp = popen("ls -1 example_dir", "r");
|
||||
ERROR_IF(fopen, fp, == NULL);
|
||||
ERROR_IF(popen, fp, == NULL);
|
||||
|
||||
char path[256] = { 0 };
|
||||
while (fgets(path, 256, fp) != NULL) {
|
||||
|
||||
+36
-9
@@ -9,19 +9,46 @@
|
||||
static char oldpath[] = "old-name.out";
|
||||
static char newpath[] = "new-name.out";
|
||||
static char str[] = "Hello, World!";
|
||||
int str_len = 13;
|
||||
|
||||
int main(void) {
|
||||
char buf[14];
|
||||
buf[13] = 0x00;
|
||||
char buf[14] = { 0 };
|
||||
|
||||
// Create old file
|
||||
int fd = creat(oldpath, 0777);
|
||||
write(fd, str, str_len);
|
||||
close(fd);
|
||||
rename(oldpath, newpath);
|
||||
ERROR_IF(creat, fd, == -1);
|
||||
UNEXP_IF(creat, fd, < 0);
|
||||
|
||||
int written_bytes = write(fd, str, strlen(str));
|
||||
ERROR_IF(write, written_bytes, == -1);
|
||||
|
||||
int c1 = close(fd);
|
||||
ERROR_IF(close, c1, == -1);
|
||||
UNEXP_IF(close, c1, != 0);
|
||||
|
||||
// Rename old file to new file
|
||||
int rn_status = rename(oldpath, newpath);
|
||||
ERROR_IF(rename, rn_status, == -1);
|
||||
UNEXP_IF(rename, rn_status, != 0);
|
||||
|
||||
// Read new file
|
||||
fd = open(newpath, O_RDONLY);
|
||||
read(fd, buf, str_len);
|
||||
close(fd);
|
||||
remove(newpath);
|
||||
ERROR_IF(open, fd, == -1);
|
||||
UNEXP_IF(open, fd, < 0);
|
||||
|
||||
int read_bytes = read(fd, buf, strlen(str));
|
||||
ERROR_IF(read, read_bytes, == -1);
|
||||
UNEXP_IF(read, read_bytes, < 0);
|
||||
|
||||
int c2 = close(fd);
|
||||
ERROR_IF(close, c2, == -1);
|
||||
UNEXP_IF(close, c2, != 0);
|
||||
|
||||
// Remove new file
|
||||
int rm_status = remove(newpath);
|
||||
ERROR_IF(remove, rm_status, == -1);
|
||||
UNEXP_IF(remove, rm_status, != 0);
|
||||
|
||||
// Compare file contents
|
||||
if (strcmp(str, buf) == 0) {
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user