Enable -Wextra in tests.

This commit is contained in:
4lDO2
2023-11-12 12:07:49 +01:00
parent c76a12347f
commit 5f929ed51e
26 changed files with 158 additions and 132 deletions
+34 -31
View File
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -10,46 +11,48 @@ const char *msg2 = "second";
const char *msg3 = "third";
void cleanup1(void *arg) {
printf("Running %s cleanup callback\n", (const char *)arg);
printf("Running %s cleanup callback\n", (const char *)arg);
}
void cleanup2(void *arg) {
fprintf(stderr, "Running %s cleanup callback, to stderr\n", (const char *)arg);
fprintf(stderr, "Running %s cleanup callback, to stderr\n", (const char *)arg);
}
void cleanup3(void *arg) {
printf("Running final (%s) callback\n", (const char *)arg);
printf("Running final (%s) callback\n", (const char *)arg);
}
void *routine(void *arg) {
puts("1");
pthread_cleanup_push(cleanup1, msg1);
puts("2");
pthread_cleanup_push(cleanup2, msg2);
puts("3");
pthread_cleanup_push(cleanup3, msg3);
puts("4");
pthread_cleanup_pop(true);
puts("5");
//exit(EXIT_SUCCESS);
pthread_exit(NULL);
puts("6");
pthread_cleanup_pop(true);
pthread_cleanup_pop(true);
return NULL;
assert(arg == NULL);
puts("1");
pthread_cleanup_push(cleanup1, msg1);
puts("2");
pthread_cleanup_push(cleanup2, msg2);
puts("3");
pthread_cleanup_push(cleanup3, msg3);
puts("4");
pthread_cleanup_pop(true);
puts("5");
//exit(EXIT_SUCCESS);
pthread_exit(NULL);
puts("6");
pthread_cleanup_pop(true);
pthread_cleanup_pop(true);
return NULL;
}
int main(void) {
int result;
int result;
puts("Main thread started");
pthread_t second_thread;
if ((result = pthread_create(&second_thread, NULL, routine, NULL)) != 0) {
fprintf(stderr, "thread creation failed: %s\n", strerror(result));
return EXIT_FAILURE;
}
if ((result = pthread_join(second_thread, NULL)) != 0) {
fprintf(stderr, "failed to join thread: %s\n", strerror(result));
return EXIT_FAILURE;
}
puts("Main thread about to exit");
return EXIT_SUCCESS;
puts("Main thread started");
pthread_t second_thread;
if ((result = pthread_create(&second_thread, NULL, routine, NULL)) != 0) {
fprintf(stderr, "thread creation failed: %s\n", strerror(result));
return EXIT_FAILURE;
}
if ((result = pthread_join(second_thread, NULL)) != 0) {
fprintf(stderr, "failed to join thread: %s\n", strerror(result));
return EXIT_FAILURE;
}
puts("Main thread about to exit");
return EXIT_SUCCESS;
}
+37 -36
View File
@@ -8,60 +8,61 @@
#include "common.h"
struct arg2 {
int status;
pthread_t t1;
int status;
pthread_t t1;
};
void *routine1(void *arg) {
puts("Thread 1 spawned, waiting 1s.");
sleep(1);
puts("Thread 1 finished.");
return strdup("message from thread 1");
assert(arg == NULL);
puts("Thread 1 spawned, waiting 1s.");
sleep(1);
puts("Thread 1 finished.");
return strdup("message from thread 1");
}
void *routine2(void *arg_raw) {
struct arg2 *arg = arg_raw;
struct arg2 *arg = arg_raw;
puts("Thread 2 spawned, awaiting thread 1.");
puts("Thread 2 spawned, awaiting thread 1.");
void *retval_raw;
int status;
void *retval_raw;
int status;
if ((status = pthread_join(arg->t1, &retval_raw)) != 0) {
arg->status = fail(status, "t1 join from thread 2");
return NULL;
}
char *retval = retval_raw;
assert(strcmp(retval, "message from thread 1") == 0);
free(retval);
if ((status = pthread_join(arg->t1, &retval_raw)) != 0) {
arg->status = fail(status, "t1 join from thread 2");
return NULL;
}
char *retval = retval_raw;
assert(strcmp(retval, "message from thread 1") == 0);
free(retval);
return NULL;
}
int main(void) {
pthread_t t1;
pthread_t t2;
pthread_t t1;
pthread_t t2;
int status;
int status;
puts("Main thread.");
puts("Main thread.");
if ((status = pthread_create(&t1, NULL, routine1, NULL)) != 0) {
return fail(status, "t1 create");
}
if ((status = pthread_create(&t1, NULL, routine1, NULL)) != 0) {
return fail(status, "t1 create");
}
puts("Created thread 1.");
puts("Created thread 1.");
struct arg2 arg = { .status = 0, .t1 = t1 };
struct arg2 arg = { .status = 0, .t1 = t1 };
if ((status = pthread_create(&t2, NULL, routine2, &arg)) != 0) {
return fail(status, "t2 create");
}
if ((status = pthread_create(&t2, NULL, routine2, &arg)) != 0) {
return fail(status, "t2 create");
}
if ((status = pthread_join(t2, NULL)) != 0) {
return fail(status, "t2 join");
}
if ((status = pthread_join(t2, NULL)) != 0) {
return fail(status, "t2 join");
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}
+22 -17
View File
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -8,26 +9,30 @@
#include "common.h"
void *thread_main(void *arg) {
puts("Thread main");
return NULL;
puts("Thread main");
assert(arg == NULL);
return NULL;
}
int main(void) {
int status;
int status;
puts("Start, sleeping 1 second");
sleep(1);
pthread_t thread;
void *arg = NULL;
if ((status = pthread_create(&thread, NULL, thread_main, arg)) != 0) {
return fail(status, "create thread");
}
puts("Started");
void *retval;
if ((status = pthread_join(thread, &retval)) != 0) {
return fail(status, "join thread");
}
puts("Joined");
puts("Start, sleeping 1 second");
sleep(1);
pthread_t thread;
void *arg = NULL;
if ((status = pthread_create(&thread, NULL, thread_main, arg)) != 0) {
return fail(status, "create thread");
}
puts("Started");
void *retval;
if ((status = pthread_join(thread, &retval)) != 0) {
return fail(status, "join thread");
}
assert(retval == NULL);
puts("Joined");
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}