Pārlūkot izejas kodu

faux.sched: Test for sched. Not full

Serj Kalichev 3 gadi atpakaļ
vecāks
revīzija
abecf217ad
4 mainītis faili ar 66 papildinājumiem un 3 dzēšanām
  1. 1 0
      faux/sched.h
  2. 3 3
      faux/sched/Makefile.am
  3. 59 0
      faux/sched/testc_sched.c
  4. 3 0
      faux/testc_module/testc_module.c

+ 1 - 0
faux/sched.h

@@ -5,6 +5,7 @@
 #ifndef _faux_sched_h
 #define _faux_sched_h
 
+#include <faux/list.h>
 #include <faux/faux.h>
 #include <faux/time.h>
 

+ 3 - 3
faux/sched/Makefile.am

@@ -2,6 +2,6 @@ libfaux_la_SOURCES += \
 	faux/sched/ev.c \
 	faux/sched/sched.c
 
-#if TESTC
-#libfaux_la_SOURCES += faux/event/testc_event.c
-#endif
+if TESTC
+libfaux_la_SOURCES += faux/sched/testc_sched.c
+endif

+ 59 - 0
faux/sched/testc_sched.c

@@ -0,0 +1,59 @@
+#include <sys/time.h>
+#include <time.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#include "faux/time.h"
+#include "faux/sched.h"
+
+int testc_faux_sched(void)
+{
+	faux_sched_t *sched = NULL;
+	long long int nsec = 500000000l;
+	struct timespec pol_s = {}; // One half of second
+	struct timespec now = {};
+	struct timespec t = {};
+	int id = 78;
+	char *str = "test";
+	int e_id = 0;
+	void *e_str = NULL;
+	struct timespec twait = {};
+
+	faux_nsec_to_timespec(&pol_s, nsec);
+	faux_timespec_now(&now);
+	faux_timespec_sum(&t, &now, &pol_s);
+
+	sched = faux_sched_new();
+	if (!sched)
+		return -1;
+
+	// Wait and get event
+	faux_sched_once(sched, &t, id, str);
+	nanosleep(&pol_s, NULL); // wait
+	if (faux_sched_pop(sched, &e_id, &e_str) < 0)
+		return -1;
+	if (e_id != id)
+		return -1;
+	if (e_str != str)
+		return -1;
+
+	// Don't wait so pop must return -1
+	faux_timespec_sum(&t, &t, &pol_s);
+	faux_sched_once(sched, &t, id, str);
+	// Don't wait. Pop must return -1
+	if (faux_sched_pop(sched, &e_id, &e_str) == 0)
+		return -1;
+	// Get next event interval. It must be greater than 0 and greater
+	// than full interval (half of second)
+	if (faux_sched_next_interval(sched, &twait) < 0)
+		return -1;
+	if (faux_timespec_cmp(&twait, &(struct timespec){0, 0}) <= 0)
+		return -1;
+	if (faux_timespec_cmp(&twait, &pol_s) >= 0)
+		return -1;
+
+	faux_sched_free(sched);
+
+	return 0;
+}

+ 3 - 0
faux/testc_module/testc_module.c

@@ -26,6 +26,9 @@ const char *testc_module[][2] = {
 	{"testc_faux_timespec_sum", "Sum of timespec structures"},
 	{"testc_faux_timespec_now", "Timespec now and before now functions"},
 
+	// sched
+	{"testc_faux_sched", "Scheduling of events"},
+
 	// log
 	{"testc_faux_log_facility_id", "Converts syslog facility string to id"},
 	{"testc_faux_log_facility_str", "Converts syslog facility id to string"},