Add a more representative test for the snapshot.

This commit is contained in:
Patrick Pelissier committed 2017-10-11 22:35:57 +02:00
1 parent 23694d69f5
commit 535ea729ed
1 file changed
+64
+64
View File
@@ -23,6 +23,7 @@
#include <gmp.h> // For testing purpose only.
#include "m-snapshot.h"
#include "m-mutex.h"
#include "coverage.h"
START_COVERAGE
@@ -74,9 +75,72 @@ static void test_uint(void)
assert (*snapshot_uint_look(t) == 3);
}
typedef struct {
unsigned int n;
unsigned int c;
unsigned int p;
} data_t;
static void data_crc(data_t *p) { p->p = -p->n; p->c = ~p->n; }
static void data_init(data_t *p) { p->n = 0; data_crc(p); }
static void data_clear(data_t *p) { (void) p;}
static void data_set(data_t *p, data_t o) { p->p = o.p; data_crc(p); }
SNAPSHOT_DEF(snapshot_data, data_t,
(INIT(data_init M_IPTR), INIT_SET(data_set M_IPTR), SET(data_set M_IPTR), CLEAR(data_clear M_IPTR))
)
snapshot_data_t g_buff;
static void conso(void *arg)
{
assert (arg == NULL);
while (true) {
const data_t *p = snapshot_data_look(g_buff);
assert (p->n == -p->p);
assert (p->n == ~p->c);
if (p->n == 0)
return;
}
}
static void prod(void *arg)
{
assert (arg == NULL);
data_t *p = snapshot_data_get_write_buffer(g_buff);
for(unsigned int i = 1; i < 100000;i++) {
p->n = i * i;
data_crc(p);
p = snapshot_data_take(g_buff);
}
p->n = 0;
data_crc(p);
p = snapshot_data_take(g_buff);
}
static void test_global(void)
{
m_thread_t idx[2];
snapshot_data_init(g_buff);
data_t *p = snapshot_data_take(g_buff);
p->n = 42;
data_crc(p);
p = snapshot_data_take(g_buff);
m_thread_create (idx[0], conso, NULL);
m_thread_create (idx[1], prod, NULL);
m_thread_join(idx[0]);
m_thread_join(idx[1]);
snapshot_data_clear(g_buff);
}
int main(void)
{
test_uint();
test_global();
exit(0);
}