cleanup unused files

This commit is contained in:
Laila van Reenen 2024-03-08 11:50:34 +01:00
parent db199dd343
commit ed27c225a7
2 changed files with 0 additions and 152 deletions

View File

@ -1,98 +0,0 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include "buffer.h"
void check_errno(int error)
{
if (error < 0)
{
perror("Error");
exit(EXIT_FAILURE);
}
}
void check(int error)
{
if (error != 0)
{
printf("Error: %s\n", strerror(error));
exit(EXIT_FAILURE);
}
}
int msleep(long msec) // millisecond sleep
{
struct timespec ts;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
return nanosleep(&ts, NULL);
}
typedef struct {
pthread_mutex_t m;
} args;
void *producer(void *arg) // function for producer thread
{
args a = *(args *)arg;
int i = 0;
do
{
check( pthread_mutex_lock(&a.m) );
if (!buffer_is_full())
{
buffer_put(i);
}
check( pthread_mutex_unlock(&a.m) );
i++;
check_errno( msleep(150) );
} while(i != 10);
return NULL;
}
void *consumer(void *arg) // function for consumer thread
{
args a = *(args *)arg;
int i;
do
{
check( pthread_mutex_lock(&a.m) );
if (buffer_get(&i))
{
check_errno( printf("%d\n", i) );
}
check( pthread_mutex_unlock(&a.m) );
check_errno( msleep(1000) );
} while (i != 9);
return NULL;
}
int main(void)
{
args a;
pthread_mutexattr_t ma;
check( pthread_mutexattr_init(&ma) );
check( pthread_mutex_init(&a.m, &ma) );
pthread_t ptc, ptp;
check( pthread_create(&ptc, NULL, consumer, &a) );
check( pthread_create(&ptp, NULL, producer, &a) );
check( pthread_join(ptc, NULL) );
check( pthread_join(ptp, NULL) );
check( pthread_mutex_destroy(&a.m) );
check( pthread_mutexattr_destroy(&ma) );
return EXIT_SUCCESS;
}

View File

@ -1,54 +0,0 @@
#include <stdio.h>
#include "buffer.h"
static int tests = 0;
static int fails = 0;
#define TEST(condition, ...) \
tests++;\
if (!(condition))\
{\
fails++;\
printf("Error: ");\
printf(__VA_ARGS__);\
printf("\n");\
}
#define PRINT_TEST_REPORT\
printf("%d tests performed: %d succeded, %d failed.\n", tests, tests - fails, fails);
void test_put_and_get(int test_value)
{
// test if test_value can be written into the buffer
TEST(buffer_put(test_value), "value %d can not be written into the buffer", test_value)
TEST(!buffer_is_empty(), "buffer still empty after writing into the buffer")
// test if test_value can be retrieved from the buffer
int retrieved_value;
TEST(buffer_get(&retrieved_value), "retrieving %d from the buffer failed", test_value)
TEST(retrieved_value == test_value, "wrong value (%d) retrieved from the buffer, expected %d", retrieved_value, test_value)
TEST(buffer_is_empty(), "buffer not empty after writing and retrieving one int to and from the buffer")
}
int main(void)
{
// test if the buffer is empty at startup
TEST(buffer_is_empty(), "buffer not empty at startup")
// test if the buffer is not full at startup
TEST(!buffer_is_full(), "buffer full at startup")
// test if value 42 can be written into and retrieved from the buffer
test_put_and_get(42);
// test if value 0 can be written into and retrieved from the buffer
test_put_and_get(0);
// write to the buffer until it is full
while (buffer_put(13)) /* do nothing */;
TEST(buffer_is_full(), "buffer not full after put failed")
// retrieve from the buffer until it is empty
int retrieved_value;
while (buffer_get(&retrieved_value)) /* do nothing */;
TEST(buffer_is_empty(), "buffer not empty after get failed")
TEST(retrieved_value == 13, "retrieved value overridden after get failed, expected 13 but got %d", retrieved_value)
PRINT_TEST_REPORT
return 0;
}