git » libfilo » commit 1f06780

Add some dirty tests.

author Alberto Bertogli
2005-04-23 19:51:36 UTC
committer Alberto Bertogli
2005-04-23 19:51:36 UTC
parent 608460d2fde24225f749a204884e544d57d78143

Add some dirty tests.

tests/lockloco.c +86 -0
tests/test1.c +77 -0

diff --git a/tests/lockloco.c b/tests/lockloco.c
new file mode 100644
index 0000000..641de28
--- /dev/null
+++ b/tests/lockloco.c
@@ -0,0 +1,86 @@
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sched.h>
+
+#include "libfilo.h"
+
+
+#define MAXSTART	100
+#define MAXLEN		200
+#define MAXDELAY	100000
+
+filock_t l;
+
+void *locoth(void *unused) {
+	int mode;
+	long delay;
+	off_t start, len;
+	struct timespec ts;
+	unsigned int count = 0;
+
+	for (;;) {
+		start = rand() % MAXSTART;
+		len = rand() % MAXLEN;
+		delay = rand() % MAXDELAY;
+
+		/* 2 readers, 1 writer */
+		mode = rand() % 3;
+		mode = mode ? FL_RD_MODE : FL_WR_MODE;
+
+		ts.tv_sec = 0;
+		ts.tv_nsec = delay;
+#if 1
+		//printf("%lu\tl %d\t %lu\t %lu\n", pthread_self(), mode, start, len);
+		filo_lock(&l, start, len, mode);
+#else
+		if (filo_trylock(&l, start, len, mode) == 0) {
+			printf("%lu\tC %d\t %lu\t %lu\n", pthread_self(), mode, start, len);
+			filo_lock(&l, start, len, mode);
+			printf("%lu\tA %d\t %lu\t %lu\n", pthread_self(), mode, start, len);
+
+		} else {
+			printf("%lu\tl %d\t %lu\t %lu\n", pthread_self(), mode, start, len);
+		}
+#endif
+		/* sleep in 1 out of 20 */
+		/*
+		if ((start + len) % 20 == 0) {
+			nanosleep(&ts, NULL);
+		} else {
+			sched_yield();
+		}
+		*/
+		sched_yield();
+		//printf("%lu\tu %d\t %lu\t %lu\n", pthread_self(), mode, start, len);
+		filo_unlock(&l, start, len);
+
+		count++;
+		if (count % 50000 == 0) {
+			printf("%lu\t %d\n", pthread_self(), count);
+			//printf("\t %d\t %lu\t %lu\n", mode, start, len);
+			//fflush(stdout);
+		}
+	}
+	return NULL;
+}
+
+int main(void) {
+	pthread_t t1, t2, t3;
+
+	filo_init(&l);
+
+	srand(time(NULL));
+
+	pthread_create(&t1, NULL, &locoth, NULL);
+	pthread_create(&t2, NULL, &locoth, NULL);
+	pthread_create(&t3, NULL, &locoth, NULL);
+
+	pthread_join(t1, NULL);
+	pthread_join(t2, NULL);
+	pthread_join(t3, NULL);
+	return 0;
+
+}
+
diff --git a/tests/test1.c b/tests/test1.c
new file mode 100644
index 0000000..2031299
--- /dev/null
+++ b/tests/test1.c
@@ -0,0 +1,77 @@
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "libfilo.h"
+
+#define LOCK(start, len, mode)	 			\
+	do {						\
+		printf("%lu: about to lock\n", pthread_self()); \
+		filo_lock(&l, start, len, mode);	\
+		printf("%lu: lock\t %d %d %d\n", pthread_self(), start, len, mode); \
+	} while(0);
+
+#define UNLOCK(start, len)	 			\
+	do {						\
+		printf("%lu: about to unlock\n", pthread_self()); \
+		filo_unlock(&l, start, len);		\
+		printf("%lu: unlock\t %d %d\n", pthread_self(), start, len); \
+	} while(0);
+
+#define LU_S(start, len, mode, sl)			\
+	do {						\
+		LOCK(start, len, mode);			\
+		sleep(sl);				\
+		UNLOCK(start, len);			\
+	} while(0);
+
+filock_t l;
+
+void *lth1(void *unused) {
+	LU_S(0, 50, FL_RD_MODE, 5);
+	return NULL;
+}
+
+void *lth2(void *unused) {
+	LU_S(51, 50, FL_RD_MODE, 7);
+	return NULL;
+}
+
+
+void *lth3(void *unused) {
+	sleep(1);
+	printf("about to lock wr\n");
+	filo_lock(&l, 10, 100, FL_WR_MODE);
+	printf("got!\n");
+	return NULL;
+}
+
+int main(void) {
+	pthread_t t1, t2, t3;
+
+	filo_init(&l);
+
+	filo_lock(&l, 10, 100, FL_WR_MODE);
+	filo_unlock(&l, 10, 100);
+
+	filo_lock(&l, 0, 100, FL_WR_MODE);
+	filo_unlock(&l, 10, 10);
+	printf("aca vamos de nuevo\n");
+	filo_lock(&l, 90, 50, FL_WR_MODE);
+	filo_lock(&l, 10, 100, FL_RD_MODE);
+	//filo_lock(&l, 10, 100, FL_WR_MODE);
+	filo_unlock(&l, 10, 100);
+	filo_unlock(&l, 0, 200);
+
+	printf("done static\n\n");
+	pthread_create(&t1, NULL, &lth1, NULL);
+	pthread_create(&t2, NULL, &lth2, NULL);
+	pthread_create(&t3, NULL, &lth3, NULL);
+
+	pthread_join(t1, NULL);
+	pthread_join(t2, NULL);
+	pthread_join(t3, NULL);
+	return 0;
+
+}
+