tests: More refactoring, add helper header to every test, override exit for better error reporting

This commit is contained in:
Tibor Nagy
2019-02-21 21:13:28 +01:00
parent 6266d29242
commit 9a0ea6ff34
99 changed files with 323 additions and 88 deletions
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char *str = (char *) alloca(17);
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <unistd.h>
#include "test_helpers.h"
int main(int argc, char *argv[]) {
for(int i = 0; i < argc; i++) {
write(STDOUT_FILENO, argv[i], strlen(argv[i]));
+2 -1
View File
@@ -3,6 +3,8 @@
#include <string.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
uint32_t hl = 0xBADFACED;
uint32_t nl = htonl(hl);
@@ -20,5 +22,4 @@ int main(void) {
struct in_addr* addr = malloc(sizeof addr);
inet_aton(addr_str, addr);
assert(strcmp(inet_ntoa(*addr), addr_str) == 0);
}
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
assert(1 == 1);
assert(1 + 1 == 2);
+1
View File
@@ -1,4 +1,5 @@
#include <stdio.h>
#include "test_helpers.h"
__attribute__((constructor))
void constructor_no_priority(void) {
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
struct test_case {
int c;
int isalnum;
+1
View File
@@ -1,4 +1,5 @@
#include <stdio.h>
#include "test_helpers.h"
__attribute__((destructor))
void destructor_no_priority(void) {
+2
View File
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
printf("%lu\n", sizeof(struct dirent));
+5 -4
View File
@@ -3,17 +3,18 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int filter(const struct dirent* dirent) {
return strstr(dirent->d_name, "3") == NULL;
}
int main(void) {
struct dirent** array;
int len = scandir("example_dir/", &array, filter, alphasort);
if (len < 0) {
perror("scandir");
exit(EXIT_FAILURE);
}
ERROR_IF(scandir, len, == -1);
UNEXP_IF(scandir, len, < 0);
for(int i = 0; i < len; i += 1) {
puts(array[i]->d_name);
+2
View File
@@ -3,6 +3,8 @@
#include <string.h>
#include <errno.h>
#include "test_helpers.h"
int main(void) {
chdir("nonexistent");
printf("errno: %d = %s\n", errno, strerror(errno));
+7 -8
View File
@@ -2,14 +2,13 @@
#include <stdlib.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
int fd = creat("create.out", 0755);
if (fd >= 0) {
write(fd, "Hello World!\n", 13);
close(fd);
exit(EXIT_SUCCESS);
} else {
write(STDERR_FILENO, "creat failed\n", 13);
exit(EXIT_FAILURE);
}
ERROR_IF(creat, fd, == -1);
UNEXP_IF(creat, fd, < 0);
write(fd, "Hello World!\n", 13);
close(fd);
}
+15 -2
View File
@@ -2,12 +2,25 @@
#include <stdio.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
//Lose our fd and pull it again
creat("fcntl.out", 0777);
// Lose our fd and pull it again
{
int fd = creat("fcntl.out", 0777);
ERROR_IF(creat, fd, == -1);
UNEXP_IF(creat, fd, < 0);
}
int newfd = open("fcntl.out", 0);
ERROR_IF(open, newfd, == -1);
UNEXP_IF(open, newfd, < 0);
int newfd2 = fcntl(newfd, F_DUPFD, 0);
// TODO: The standard doesn't define errors for F_DUPFD
printf("fd %d duped into fd %d\n", newfd, newfd2);
close(newfd);
close(newfd2);
}
+2
View File
@@ -1,6 +1,8 @@
#include <fnmatch.h>
#include <stdio.h>
#include "test_helpers.h"
void test(char* pattern, char* input, int flags) {
if (!fnmatch(pattern, input, flags)) {
printf("\"%s\" matches \"%s\"\n", pattern, input);
+2
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
typedef struct {
char * in;
char * expected_out;
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
// TODO: Implement locale properly and test it here
char* val = setlocale(LC_ALL, NULL);
+2
View File
@@ -1,6 +1,8 @@
#include <math.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
double pi = 3.14;
float c = cos(pi);
+2
View File
@@ -8,6 +8,8 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include "test_helpers.h"
int main(void) {
struct addrinfo hints, *res;
int errcode;
+2
View File
@@ -35,6 +35,8 @@
#include <unistd.h>
#include <errno.h>
#include "test_helpers.h"
int error_count;
static void
output_servent (const char *call, struct servent *sptr)
+2
View File
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
void print(struct passwd *pwd) {
printf("pw_name: %s\n", pwd->pw_name);
printf("pw_password: %s\n", pwd->pw_passwd);
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
regex_t regex;
char error_buf[256];
+5 -4
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <sys/resource.h>
#include "test_helpers.h"
void ptimeval(struct timeval* val) {
printf("{ tv_sec: %ld, tv_usec: %ld }\n", val->tv_sec, val->tv_usec);
}
@@ -10,10 +12,9 @@ void ptimeval(struct timeval* val) {
int main(void) {
struct rusage r_usage;
if (getrusage(RUSAGE_SELF, &r_usage) == -1) {
perror("getrusage");
exit(EXIT_FAILURE);
}
int status = getrusage(RUSAGE_SELF, &r_usage);
ERROR_IF(getrusage, status, == -1);
UNEXP_IF(getrusage, status, != 0);
printf("ru_utime:");
ptimeval(&r_usage.ru_utime);
+2
View File
@@ -3,6 +3,8 @@
#include <sys/select.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
int fd = open("select.c", 0, 0);
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <setjmp.h>
#include "test_helpers.h"
int main(void) {
jmp_buf buf;
if (setjmp(buf)) {
+2
View File
@@ -4,6 +4,8 @@
#include <unistd.h>
#include <errno.h>
#include "test_helpers.h"
void handler(int sig) {
puts("Signal handler called!");
}
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
FILE *f = fopen("stdio/stdio.in", "r");
printf("%c\n", fgetc(f));
+2
View File
@@ -1,5 +1,7 @@
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
// Tests what used to be a bug with buffering
fwrite("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1, 999, stdout);
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
//FILE *f = fopen("/etc/ssl/certs/ca-certificates.crt", "r");
FILE *f = fopen("stdio/stdio.in", "r");
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <errno.h>
#include "test_helpers.h"
int main(void) {
FILE *f = fopen("stdio/fputs.out", "w");
char *in = "Hello World!";
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
FILE *fp = fopen("stdio/fread.in", "rb");
+2
View File
@@ -1,5 +1,7 @@
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
freopen("stdio/stdio.in", "r", stdin);
char in[6];
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
FILE *f = fopen("stdio/stdio.in", "r");
if (fseek(f, 14, SEEK_CUR) < 0) {
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <errno.h>
#include "test_helpers.h"
int main(void) {
FILE *f = fopen("stdio/fwrite.out", "w");
const char ptr[] = "Hello World!";
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
ungetc('h', stdin);
char c;
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
FILE* f = fopen("stdio/stdio.in", "r");
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
FILE *fp;
int status;
+2
View File
@@ -1,5 +1,7 @@
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
int sofar = 0;
int len = printf(
+2
View File
@@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>
#include "test_helpers.h"
static char oldpath[] = "old-name.out";
static char newpath[] = "new-name.out";
static char str[] = "Hello, World!";
+2
View File
@@ -1,6 +1,8 @@
#include <stdarg.h>
#include <stdio.h>
#include "test_helpers.h"
struct params {
short sa;
int ia;
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
setvbuf(stdout, 0, _IONBF, 0);
FILE *f = fopen("stdio/stdio.in", "r");
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char buffer[72];
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char * s = "azAZ9."; // test boundaries
long l = a64l(s);
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
char * ptr = (char *)malloc(256);
printf("malloc %p\n", ptr);
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
double d = atof("-3.14");
printf("%f\n", d);
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%d\n", atoi(" -42"));
printf("%d\n", atoi(" +555"));
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int int_cmp(const void* a, const void* b) {
return *(const int*) a - *(const int*) b;
}
+18 -15
View File
@@ -1,20 +1,23 @@
#include <stdlib.h>
volatile float f;
volatile long double ld;
volatile unsigned long long ll;
lldiv_t mydivt;
#include "test_helpers.h"
volatile float f;
volatile long double ld;
volatile unsigned long long ll;
lldiv_t mydivt;
int main(void) {
char* tmp;
f = strtof("gnu", &tmp);
ld = strtold("gnu", &tmp);
ll = strtoll("gnu", &tmp, 10);
ll = strtoull("gnu", &tmp, 10);
ll = llabs(10);
mydivt = lldiv(10,1);
ll = mydivt.quot;
ll = mydivt.rem;
ll = atoll("10");
_Exit(0);
char* tmp;
f = strtof("gnu", &tmp);
ld = strtold("gnu", &tmp);
ll = strtoll("gnu", &tmp, 10);
ll = strtoull("gnu", &tmp, 10);
ll = llabs(10);
mydivt = lldiv(10,1);
ll = mydivt.quot;
ll = mydivt.rem;
ll = atoll("10");
_Exit(0);
}
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
//puts(getenv("SHELL"));
//puts(getenv("CC"));
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char* file_name = (char*) calloc(18, sizeof(char));
strcpy(file_name, "tempXXXXXX.suffix");
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char* string = (char*) calloc(20, sizeof(char));
strcpy(string, "tempXXXXXX");
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%d\n", rand());
srand(259);
+2
View File
@@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char* path = realpath("stdlib/realpath.c", NULL);
if (!path) {
+2
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char* endptr = 0;
double d;
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%ld\n", strtol(" -42", NULL, 0));
printf("%ld\n", strtol(" +555", NULL, 0));
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%ld\n", strtoul(" -42", NULL, 0));
printf("%ld\n", strtoul(" +555", NULL, 0));
+4 -1
View File
@@ -1,5 +1,8 @@
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
system("echo test of system");
int status = system("echo test of system");
ERROR_IF(system, status, == -1);
}
+2
View File
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
puts("# mem #");
char arr[100];
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char dest1[12] = "hello";
printf("%s\n", strcat(dest1, " world")); // should be hello world
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%s\n", strchr("hello", 'e')); // should be ello
printf("%s\n", strchr("world", 'l')); // should be ld
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
char dst[20];
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char *world = "world";
printf("%ld\n", strcspn("hello", world)); // should be 2
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%d\n", strncmp("a", "aa", 2));
printf("%d\n", strncmp("a", "", 2));
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char* source = "The quick drawn fix jumps over the lazy bug";
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
char s0[] = "hello, world";
char* ptr = strrchr(s0, 'l');
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char *hello = "hello";
char *world = "world";
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
printf("%s\n", strstr("In relibc we trust", "rust"));
printf("%s\n", strstr("In relibc we trust", "libc"));
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char source[] = "I'd just like to interject for a moment. What you're referring to as Linux, "
"is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
+2
View File
@@ -1,6 +1,8 @@
#include <string.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
char source[] = "I'd just like to interject for a moment. What you're referring to as Linux, "
"is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
+2
View File
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <strings.h>
#include "test_helpers.h"
int main(void) {
assert(!bcmp("hello", "hehe", 2));
assert(bcmp("hello", "haha", 2));
+10 -10
View File
@@ -1,19 +1,19 @@
#include <stdio.h>
#include <sys/utsname.h>
#include "test_helpers.h"
int main(void) {
struct utsname system_info;
int result = uname(&system_info);
ERROR_IF(uname, result, == -1);
UNEXP_IF(uname, result, < 0);
if (result < 0) {
perror("uname");
} else {
printf("sysname: '%s'\n", system_info.sysname);
printf("nodename: '%s'\n", system_info.nodename);
printf("release: '%s'\n", system_info.release);
printf("version: '%s'\n", system_info.version);
printf("machine: '%s'\n", system_info.machine);
//printf("domainname: '%s'\n", system_info.domainname);
}
printf("sysname: '%s'\n", system_info.sysname);
printf("nodename: '%s'\n", system_info.nodename);
printf("release: '%s'\n", system_info.release);
printf("version: '%s'\n", system_info.version);
printf("machine: '%s'\n", system_info.machine);
//printf("domainname: '%s'\n", system_info.domainname);
}
+20 -7
View File
@@ -1,27 +1,40 @@
#ifndef _TEST_HELPERS
#define _TEST_HELPERS
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
// Throws an error on a well-defined error value.
// Don't pass functions as status or condition, it might evaluate them multiple times.
#define ERROR_IF(func, status, condition) { \
if (status condition) { \
fprintf(stderr, "%s:%d: %s returned an error in function %s: %s (%d)\n", \
__FILE__, __LINE__, #func, __func__, strerror(errno), errno); \
exit(EXIT_FAILURE); \
fprintf(stderr, "%s:%s:%d: '%s' returned an error: %s (%d)\n", \
__FILE__, __func__, __LINE__, #func, strerror(errno), errno); \
_exit(EXIT_FAILURE); \
}\
}
// Throws an error on an return value not defined by the standards.
// Used for sanity checking the return values.
// Don't pass functions as status or condition it might evaluate them multiple times.
#define UNEXP_IF(func, status, condition) { \
if (status condition) { \
fprintf(stderr, "%s:%d: %s returned a value not defined by the standards in function %s: %d\n", \
__FILE__, __LINE__, #func, __func__, status); \
exit(EXIT_FAILURE); \
fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: %d\n", \
__FILE__, __func__, __LINE__, #func, status); \
_exit(EXIT_FAILURE); \
}\
}
// A convenience macro to show where the test fail.
#define exit(code) { \
if (code != EXIT_SUCCESS) { \
fprintf(stderr, "%s:%s:%d: Test failed with exit(%s)\n", \
__FILE__, __func__, __LINE__, #code); \
} \
_exit(code); \
}
#endif /* _TEST_HELPERS */
+2
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
time_t a = 0;
struct tm *time_info = gmtime(&a);
+7 -1
View File
@@ -1,8 +1,14 @@
#include <sys/time.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
// gettimeofday always returns 0, no errors are defined
int gtod = gettimeofday(&tv, NULL);
UNEXP_IF(gettimeofday, gtod, != 0);
printf("%ld: %ld\n", tv.tv_sec, tv.tv_usec);
}
+2
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
time_t a = 0;
struct tm expected = { .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_year = 70,
+2
View File
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <time.h>
#include "test_helpers.h"
int main(void) {
int day = 60 * 60 * 24;
time_t inputs[] = { -(day * 33), -day, -1, -500, 0, 1, 1531454950 };
+2
View File
@@ -1,6 +1,8 @@
#include <assert.h>
#include <sys/time.h>
#include "test_helpers.h"
int main(void) {
struct timeval x = { .tv_usec = 15 };
struct timeval y = { 0 };
+2
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <time.h>
#include "test_helpers.h"
int check(time_t input) {
struct tm* t = localtime(&input);
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <time.h>
#include "test_helpers.h"
void print(time_t timestamp, char* fmt) {
char* out = malloc(50);
size_t n = strftime(out, 50, fmt, localtime(&timestamp));
+5 -12
View File
@@ -2,24 +2,17 @@
#include <stdio.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
struct timespec tm = {0, 0};
int cgt = clock_gettime(CLOCK_REALTIME, &tm);
if (cgt == -1) {
perror("clock_gettime");
exit(EXIT_FAILURE);
}
ERROR_IF(clock_gettime, cgt, == -1);
time_t t = time(NULL);
if (t == (time_t)-1) {
perror("time");
exit(EXIT_FAILURE);
}
ERROR_IF(time, t, == (time_t)-1);
clock_t c = clock();
if (c == (clock_t)-1) {
perror("clock");
exit(EXIT_FAILURE);
}
ERROR_IF(clock, c, == (clock_t)-1);
}
+5 -1
View File
@@ -2,9 +2,13 @@
#include <sys/times.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
struct tms tms;
printf("return: %ld\n", times(&tms));
int status = times(&tms);
ERROR_IF(times, status, == (time_t)-1);
printf("tm_utime: %ld\n", tms.tms_utime);
printf("tm_stime: %ld\n", tms.tms_stime);
+2
View File
@@ -2,6 +2,8 @@
#include <unistd.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
if (access("example_dir/1-never-gonna-give-you-up", R_OK | W_OK)) {
perror("access");
+32 -5
View File
@@ -2,15 +2,42 @@
#include <fcntl.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
creat("dup.out", 0777);
int fd0 = creat("dup.out", 0777);
ERROR_IF(creat, fd0, == -1);
UNEXP_IF(creat, fd0, < 0);
int fd1 = open("dup.out", 0);
ERROR_IF(open, fd1, == -1);
UNEXP_IF(open, fd1, < 0);
int fd2 = dup(fd1);
ERROR_IF(dup, fd2, == -1);
UNEXP_IF(dup, fd2, < 0);
printf("fd %d duped into fd %d\n", fd1, fd2);
close(fd1);
close(fd2);
int c1 = close(fd1);
ERROR_IF(close, c1, == -1);
UNEXP_IF(close, c1, != 0);
int c2 = close(fd2);
ERROR_IF(close, c2, == -1);
UNEXP_IF(close, c2, != 0);
int fd3 = open("dup.out", 0x0002, 0x1000);
dup2(fd3, 1);
ERROR_IF(open, fd3, == -1);
UNEXP_IF(open, fd3, < 0);
int fd4 = dup2(fd3, 1);
ERROR_IF(dup2, fd4, == -1);
UNEXP_IF(dup2, fd4, < 0);
printf("hello fd %d", fd3);
close(fd3);
int c3 = close(fd3);
ERROR_IF(close, c3, == -1);
UNEXP_IF(close, c3, != 0);
}
+2
View File
@@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
char first[PATH_MAX] = { 0 };
getcwd(first, PATH_MAX);
+2
View File
@@ -1,6 +1,8 @@
#include <unistd.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
gid_t egid = getegid();
uid_t euid = geteuid();
+2
View File
@@ -1,6 +1,8 @@
#include <unistd.h>
#include <stdio.h>
#include "test_helpers.h"
#define RUN(...) \
{ \
optind = 1; \
+2
View File
@@ -1,6 +1,8 @@
#include <getopt.h>
#include <stdio.h>
#include "test_helpers.h"
#define RUN(...) { \
optind = 1; \
optarg = NULL; \
+2
View File
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
int status = isatty(STDOUT_FILENO);
+2
View File
@@ -4,6 +4,8 @@
#include <sys/stat.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
printf("%ld\n", sizeof(struct stat));
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <unistd.h>
#include "test_helpers.h"
#define PC(N) { \
errno = 0; \
printf("%s (%d): %ld (%d)\n", #N, _PC_ ## N, fpathconf(0, _PC_ ## N), errno); \
+2
View File
@@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
int pid, pip[2];
char instring[20];
+16 -12
View File
@@ -5,20 +5,24 @@
#include <sys/types.h>
#include <unistd.h>
int main(void) {
if( setpgid( getpid(), 0 ) == -1 ) {
perror( "setpgid" );
}
printf( "%d belongs to process group %d\n",
getpid(), getpgrp() );
#include "test_helpers.h"
int main(void) {
int pg_status = setpgid(getpid(), 0);
ERROR_IF(setpgid, pg_status, == -1);
UNEXP_IF(setpgid, pg_status, != 0);
printf("%d belongs to process group %d\n", getpid(), getpgrp());
int reg_status = setregid(-1, -1);
ERROR_IF(setregid, reg_status, == -1);
UNEXP_IF(setregid, reg_status, != 0);
if( setregid(-1, -1) == -1 ) {
perror( "setregid" );
}
printf("%d has egid %d and gid %d\n", getpid(), getegid(), getgid());
if( setreuid(-1, -1) == -1 ) {
perror( "setreuid" );
}
int reu_status = setreuid(-1, -1);
ERROR_IF(setreuid, reu_status, == -1);
UNEXP_IF(setreuid, reu_status, != 0);
printf("%d has euid %d and uid %d\n", getpid(), geteuid(), getuid());
}
+2
View File
@@ -2,6 +2,8 @@
#include <unistd.h>
#include <stdio.h>
#include "test_helpers.h"
int main(void) {
sleep(2);
perror("sleep");
+6 -5
View File
@@ -4,15 +4,16 @@
#include <sys/stat.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
printf("%ld\n", sizeof(struct stat));
printf("sizeof(struct stat): %ld\n", sizeof(struct stat));
struct stat buf;
if (stat("unistd/stat.c", &buf)) {
perror("stat");
exit(EXIT_FAILURE);
}
int stat_status = stat("unistd/stat.c", &buf);
ERROR_IF(stat, stat_status, == -1);
UNEXP_IF(stat, stat_status, != 0);
printf("st_size: %lu\n", buf.st_size);
printf("st_blksize: %lu\n", buf.st_blksize);
+2
View File
@@ -2,6 +2,8 @@
#include <stdio.h>
#include <unistd.h>
#include "test_helpers.h"
#define SC(N) { \
errno = 0; \
printf("%s (%d): %ld (%d)\n", #N, _SC_ ## N, sysconf(_SC_ ## N), errno); \
+2
View File
@@ -1,5 +1,7 @@
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
write(STDOUT_FILENO, "Hello World!\n", 13);
}
+2
View File
@@ -2,6 +2,8 @@
#include <unistd.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
pid_t pid = fork();
if (pid == 0) {
+2
View File
@@ -2,6 +2,8 @@
#include <string.h>
#include <wchar.h>
#include "test_helpers.h"
int main(void) {
mbstate_t state;
memset(&state, 0, sizeof state);
+2
View File
@@ -2,6 +2,8 @@
#include <string.h>
#include <wchar.h>
#include "test_helpers.h"
void print_as_wide(const char* mbstr)
{
mbstate_t state;
+2
View File
@@ -3,6 +3,8 @@
#include <wchar.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
wchar_t *wcs = L"zß水🍌";
+2
View File
@@ -3,6 +3,8 @@
#include <wchar.h>
#include <stdlib.h>
#include "test_helpers.h"
int main(void) {
mbstate_t state;
memset(&state, 0, sizeof state);