summaryrefslogtreecommitdiff
path: root/cesar/lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/lib/test')
-rw-r--r--cesar/lib/test/atox/Makefile7
-rw-r--r--cesar/lib/test/atox/src/test_atox.c117
-rw-r--r--cesar/lib/test/bitstream/Config1
-rw-r--r--cesar/lib/test/bitstream/Makefile7
-rw-r--r--cesar/lib/test/bitstream/src/test_bit.c222
-rw-r--r--cesar/lib/test/blk/Makefile7
-rw-r--r--cesar/lib/test/blk/src/test_blk.c125
-rw-r--r--cesar/lib/test/circular_buffer/Makefile7
-rw-r--r--cesar/lib/test/circular_buffer/src/test_circular.c94
-rw-r--r--cesar/lib/test/crc/Makefile7
-rw-r--r--cesar/lib/test/crc/inc/crcmodel.h160
-rw-r--r--cesar/lib/test/crc/src/crcmodel.c148
-rw-r--r--cesar/lib/test/crc/src/test_crc.c294
-rw-r--r--cesar/lib/test/heap/Makefile7
-rw-r--r--cesar/lib/test/heap/src/test_heap.c272
-rw-r--r--cesar/lib/test/list/Makefile7
-rw-r--r--cesar/lib/test/list/src/test_list.c267
-rw-r--r--cesar/lib/test/read_word/Makefile7
-rw-r--r--cesar/lib/test/read_word/src/read_word.c151
-rw-r--r--cesar/lib/test/restrack/Config2
-rw-r--r--cesar/lib/test/restrack/Makefile7
-rw-r--r--cesar/lib/test/restrack/src/test_restrack.c104
-rw-r--r--cesar/lib/test/rnd/Makefile19
-rw-r--r--cesar/lib/test/rnd/mt19937ar.out213
-rw-r--r--cesar/lib/test/rnd/src/test_rnd.c164
-rw-r--r--cesar/lib/test/set/Makefile8
-rw-r--r--cesar/lib/test/set/src/test_set.c362
-rw-r--r--cesar/lib/test/test/Config1
-rw-r--r--cesar/lib/test/test/Makefile8
-rw-r--r--cesar/lib/test/test/src/test_test.c90
-rw-r--r--cesar/lib/test/trace/Config2
-rw-r--r--cesar/lib/test/trace/Makefile7
-rw-r--r--cesar/lib/test/trace/src/test_trace.c302
-rw-r--r--cesar/lib/test/try/Makefile8
-rw-r--r--cesar/lib/test/try/src/test_try.c125
-rw-r--r--cesar/lib/test/utils/Makefile7
-rw-r--r--cesar/lib/test/utils/src/test_utils.c65
37 files changed, 3401 insertions, 0 deletions
diff --git a/cesar/lib/test/atox/Makefile b/cesar/lib/test/atox/Makefile
new file mode 100644
index 0000000000..3d87a51f03
--- /dev/null
+++ b/cesar/lib/test/atox/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_atox
+test_atox_SOURCES = test_atox.c
+test_atox_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/atox/src/test_atox.c b/cesar/lib/test/atox/src/test_atox.c
new file mode 100644
index 0000000000..25d8ac6170
--- /dev/null
+++ b/cesar/lib/test/atox/src/test_atox.c
@@ -0,0 +1,117 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file lib/test/circular_list/src/test_circular.c
+ * \brief « brief description »
+ * \ingroup « module »
+ *
+ * « long description »
+ */
+#include "common/std.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "lib/test.h"
+#include "lib/atox.h"
+
+#define TEST_VAL_MAX 10
+
+static char buffer[21];
+
+void atox_basic_test_case(test_t test)
+{
+ int fd, i;
+ int i_val, i_res;
+ long l_val, l_res;
+ long long ll_val, ll_res;
+ long ul_val, ul_res;
+ long long ull_val, ull_res;
+
+ fd = open("/dev/urandom", O_RDONLY);
+
+ test_begin (test, "atoi")
+ {
+ for(i=0;i<TEST_VAL_MAX;i++)
+ {
+ read(fd, (void *)&i_val, sizeof(int));
+ sprintf(buffer, "%d", i_val);
+ i_res = atoi(buffer);
+ test_fail_if (i_res != i_val, "Wrong convertion");
+ }
+ }
+ test_end;
+
+ test_begin (test, "atol")
+ {
+ for(i=0;i<TEST_VAL_MAX;i++)
+ {
+ read(fd, (void *)&l_val, sizeof(long));
+ sprintf(buffer, "%ld", l_val);
+ l_res = atol(buffer);
+ test_fail_if (l_res != l_val, "Wrong convertion");
+ }
+ }
+ test_end;
+
+ test_begin (test, "atoll")
+ {
+ for(i=0;i<TEST_VAL_MAX;i++)
+ {
+ read(fd, (void *)&ll_val, sizeof(long long));
+ sprintf(buffer, "%lld", ll_val);
+ ll_res = atoll(buffer);
+ test_fail_if (ll_res != ll_val, "Wrong convertion");
+ }
+ }
+ test_end;
+
+ test_begin (test, "atoul")
+ {
+ for(i=0;i<TEST_VAL_MAX;i++)
+ {
+ read(fd, (void *)&ul_val, sizeof(long));
+ sprintf(buffer, "%lu", ul_val);
+ ul_res = atoul(buffer);
+ test_fail_if (ul_res != ul_val, "Wrong convertion");
+ }
+ }
+ test_end;
+
+ test_begin (test, "atoull")
+ {
+ for(i=0;i<TEST_VAL_MAX;i++)
+ {
+ read(fd, (void *)&ull_val, sizeof(long long));
+ sprintf(buffer, "%llu", ull_val);
+ ull_res = atoull(buffer);
+ test_fail_if (ull_res != ull_val, "Wrong convertion");
+ }
+ }
+ test_end;
+
+ close(fd);
+}
+
+int main (int argc, char **argv)
+{
+ test_t test;
+
+ test_init(test, argc, argv);
+ test_case_begin(test, "Basic");
+
+ atox_basic_test_case(test);
+
+ test_result(test);
+ return test_nb_failed(test) == 0 ? 0 : 1;
+}
+
diff --git a/cesar/lib/test/bitstream/Config b/cesar/lib/test/bitstream/Config
new file mode 100644
index 0000000000..7a62a6fa97
--- /dev/null
+++ b/cesar/lib/test/bitstream/Config
@@ -0,0 +1 @@
+CONFIG_DEBUG = y
diff --git a/cesar/lib/test/bitstream/Makefile b/cesar/lib/test/bitstream/Makefile
new file mode 100644
index 0000000000..e308fb303d
--- /dev/null
+++ b/cesar/lib/test/bitstream/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_bit
+test_bit_SOURCES = test_bit.c
+test_bit_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/bitstream/src/test_bit.c b/cesar/lib/test/bitstream/src/test_bit.c
new file mode 100644
index 0000000000..4c3def84f6
--- /dev/null
+++ b/cesar/lib/test/bitstream/src/test_bit.c
@@ -0,0 +1,222 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_bit.c
+ * \brief Bit stream and bit array test.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/bitstream.h"
+#include "lib/test.h"
+
+void
+bitstream_basic_test_case (test_t t)
+{
+ test_case_begin (t, "basic");
+ /* Read tests */
+ test_begin (t, "read")
+ {
+ bitstream_t bsr;
+ u8 bit8 = 0; u16 bit16 = 0; u32 bit32 = 0;
+ static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
+
+ bitstream_init (&bsr, (void*)s, sizeof (s), BITSTREAM_READ);
+ test_fail_unless (bitstream_access (&bsr, &bit8, 8)
+ && bit8 == 0x10);
+ test_fail_unless (bitstream_available_bits (&bsr) == 88);
+ test_fail_unless (bitstream_access (&bsr, &bit16, 16)
+ && bit16 == 0x5432);
+ test_fail_unless (bitstream_available_bits (&bsr) == 72);
+ test_fail_unless (bitstream_access (&bsr, &bit8, 4)
+ && bit8 == 0x6);
+ test_fail_unless (bitstream_available_bits (&bsr) == 68);
+ test_fail_unless (bitstream_access (&bsr, &bit16, 12)
+ && bit16 == 0x987);
+ test_fail_unless (bitstream_available_bits (&bsr) == 56);
+ test_fail_unless (bitstream_access (&bsr, &bit32, 32)
+ && bit32 == 0x78fedcba);
+ test_fail_unless (bitstream_available_bits (&bsr) == 24);
+ test_fail_unless (bitstream_access (&bsr, &bit32, 24)
+ && bit32 == 0x123456);
+ test_fail_unless (bitstream_available_bits (&bsr) == 0);
+ } test_end;
+ test_begin (t, "read_large")
+ {
+ u64 bit64 = 0;
+ bitstream_t bsr;
+ static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
+ bitstream_init (&bsr, (void*)s, sizeof (s), BITSTREAM_READ);
+ test_fail_unless (bitstream_access (&bsr, &bit64, 48)
+ && bit64 == 0xba9876543210LL);
+ test_fail_unless (bitstream_available_bits (&bsr) == 48);
+ test_fail_unless (bitstream_access (&bsr, &bit64, 24)
+ && bit64 == 0x78fedcLL);
+ test_fail_unless (bitstream_available_bits (&bsr) == 24);
+ test_fail_unless (bitstream_access (&bsr, &bit64, 24)
+ && bit64 == 0x123456LL);
+ test_fail_unless (bitstream_available_bits (&bsr) == 0);
+ } test_end;
+ test_begin (t, "direct_read")
+ {
+ static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
+ test_fail_unless (bitstream_direct_read ((void*)s, 0, 8) == 0x10);
+ test_fail_unless (bitstream_direct_read ((void*)s, 8, 16) == 0x5432);
+ test_fail_unless (bitstream_direct_read ((void*)s, 24, 4) == 0x6);
+ test_fail_unless (bitstream_direct_read ((void*)s, 28, 12) == 0x987);
+ test_fail_unless (bitstream_direct_read ((void*)s, 40, 32)
+ == 0x78fedcba);
+ test_fail_unless (bitstream_direct_read ((void*)s, 72, 24)
+ == 0x123456);
+
+ } test_end;
+ test_begin (t, "direct_read_large")
+ {
+ static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
+ test_fail_unless (bitstream_direct_read_large ((void*)s, 0, 48)
+ == 0xba9876543210LL);
+ test_fail_unless (bitstream_direct_read_large ((void*)s, 48, 24)
+ == 0x78fedcLL);
+ test_fail_unless (bitstream_direct_read_large ((void*)s, 72, 24)
+ == 0x123456LL);
+ } test_end;
+ /* Write tests */
+ test_begin (t, "write")
+ {
+ bitstream_t bsw;
+ u8 bit8; u16 bit16; u32 bit32;
+ u32 s[4] = { [3] = 0xabba5555 };
+ bitstream_init (&bsw, (void*)s, sizeof(s) - 4, BITSTREAM_WRITE);
+ bit8 = 0x10;
+ bitstream_access (&bsw, &bit8, 8);
+ test_fail_unless (bitstream_available_bits (&bsw) == 88);
+ bit16 = 0x5432;
+ bitstream_access (&bsw, &bit16, 16);
+ test_fail_unless (bitstream_available_bits (&bsw) == 72);
+ bit8 = 0x6;
+ bitstream_access (&bsw, &bit8, 4);
+ test_fail_unless (bitstream_available_bits (&bsw) == 68);
+ bit16 = 0x987;
+ bitstream_access (&bsw, &bit16, 12);
+ test_fail_unless (bitstream_available_bits (&bsw) == 56);
+ bit32 = 0x78fedcba;
+ bitstream_access (&bsw, &bit32, 32);
+ test_fail_unless (bitstream_available_bits (&bsw) == 24);
+ bit32 = 0x123456;
+ bitstream_access (&bsw, &bit32, 24);
+ test_fail_unless (bitstream_available_bits (&bsw) == 0);
+ test_fail_unless (bitstream_finalise (&bsw) == 32);
+ test_fail_unless (s[0] == 0x76543210
+ && s[1] == 0xfedcba98
+ && s[2] == 0x12345678
+ && s[3] == 0xabba5555);
+ } test_end;
+ test_begin (t, "write_large")
+ {
+ u64 bit64;
+ bitstream_t bsw;
+ u32 s[4] = { [3] = 0xabba5555 };
+ bitstream_init (&bsw, (void*)s, sizeof(s) - 4, BITSTREAM_WRITE);
+ bit64 = 0xba9876543210LL;
+ bitstream_access (&bsw, &bit64, 48);
+ test_fail_unless (bitstream_available_bits (&bsw) == 48);
+ bit64 = 0x78fedcLL;
+ bitstream_access (&bsw, &bit64, 24);
+ test_fail_unless (bitstream_available_bits (&bsw) == 24);
+ bit64 = 0x123456LL;
+ bitstream_access (&bsw, &bit64, 24);
+ test_fail_unless (bitstream_available_bits (&bsw) == 0);
+ test_fail_unless (bitstream_finalise (&bsw) == 32);
+ test_fail_unless (s[0] == 0x76543210
+ && s[1] == 0xfedcba98
+ && s[2] == 0x12345678
+ && s[3] == 0xabba5555);
+ } test_end;
+ test_begin (t, "direct_write")
+ {
+ u32 s[4] = { [3] = 0xabba5555 };
+ bitstream_direct_write ((void*)s, 0, 0x10, 8);
+ bitstream_direct_write ((void*)s, 8, 0x5432, 16);
+ bitstream_direct_write ((void*)s, 24, 0x6, 4);
+ bitstream_direct_write ((void*)s, 28, 0x987, 12);
+ bitstream_direct_write ((void*)s, 40, 0x78fedcba, 32);
+ bitstream_direct_write ((void*)s, 72, 0x123456, 24);
+ test_fail_unless (s[0] == 0x76543210
+ && s[1] == 0xfedcba98
+ && s[2] == 0x12345678
+ && s[3] == 0xabba5555);
+ } test_end;
+ test_begin (t, "direct_write_large")
+ {
+ u32 s[4] = { [3] = 0xabba5555 };
+ bitstream_direct_write_large ((void*)s, 0, 0xba9876543210LL, 48);
+ bitstream_direct_write_large ((void*)s, 48, 0x78fedcLL, 24);
+ bitstream_direct_write_large ((void*)s, 72, 0x123456LL, 24);
+ test_fail_unless (s[0] == 0x76543210
+ && s[1] == 0xfedcba98
+ && s[2] == 0x12345678
+ && s[3] == 0xabba5555);
+ } test_end;
+
+ test_begin (t, "read_unaligned")
+ {
+ u32 bit32 = 0;
+ bitstream_t bsr;
+ static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
+
+ bitstream_init (&bsr, (void*)((u8*)s+1), sizeof (s), BITSTREAM_READ);
+ test_fail_unless (bitstream_access (&bsr, &bit32, 24)
+ && bit32 == 0x765432);
+
+ bitstream_finalise (&bsr);
+ } test_end;
+ test_begin (t, "write_unaligned")
+ {
+ u16 bit16;
+ bitstream_t bsw;
+ u32 s[4] = { [0] = 0x55000088, [1] = 0xabba5555 };
+ bitstream_init (&bsw, (void*)((u8*)s+1),
+ sizeof(s) - 4, BITSTREAM_WRITE);
+
+ bit16 = 0x6677;
+ bitstream_access (&bsw, &bit16, 16);
+
+ bit16 = 0x6655;
+ bitstream_access (&bsw, &bit16, 16);
+
+ test_fail_unless (bitstream_finalise (&bsw) == 8);
+ test_fail_unless (s[0] == 0x55667788
+ && s[1] == 0xabba5566);
+ } test_end;
+ test_begin (t, "memcpy")
+ {
+ u8 data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00 };
+ u8 copy[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff };
+ bitstream_memcpy (copy, data, sizeof(data) - 1);
+ test_fail_unless (data[6] == copy[6] && copy[7] == 0xff);
+ } test_end;
+
+ /* TODO: -- add a random number based test here --. */
+}
+
+void
+bitstream_test_suite (test_t t)
+{
+ test_suite_begin (t, "bitstream");
+ bitstream_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ bitstream_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/blk/Makefile b/cesar/lib/test/blk/Makefile
new file mode 100644
index 0000000000..02d11eeaca
--- /dev/null
+++ b/cesar/lib/test/blk/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_blk
+test_blk_SOURCES = test_blk.c
+test_blk_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/blk/src/test_blk.c b/cesar/lib/test/blk/src/test_blk.c
new file mode 100644
index 0000000000..64e94d7840
--- /dev/null
+++ b/cesar/lib/test/blk/src/test_blk.c
@@ -0,0 +1,125 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_blk.c
+ * \brief Test BLK.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/blk.h"
+#include "lib/test.h"
+
+#include <string.h>
+
+struct test_obj_t
+{
+ void *a;
+ void *b;
+};
+typedef struct test_obj_t test_obj_t;
+
+void
+test_obj_init (test_obj_t *ctx, void *a, void *b)
+{
+ ctx->a = a;
+ blk_addref (a);
+ ctx->b = b;
+ blk_addref (b);
+}
+
+void
+test_obj_uninit (void *data)
+{
+ test_obj_t *ctx = data;
+ blk_release (ctx->a);
+ blk_release (ctx->b);
+}
+
+void
+blk_basic_test_case (test_t t)
+{
+ test_case_begin (t, "basic");
+ test_begin (t, "desc")
+ {
+ blk_t *a, *f, *l, *p;
+ /* Allocate. */
+ a = blk_alloc_desc ();
+ f = blk_alloc_desc_range (42, &l);
+ /* Write. */
+ memset ((u8 *) a + sizeof (blk_t), 42,
+ BLK_DESC_SIZE - sizeof (blk_t));
+ memset (a->data, 42, BLK_SIZE);
+ for (p = f; p != l; p = p->next)
+ {
+ memset ((u8 *) p + sizeof (blk_t), 42,
+ BLK_DESC_SIZE - sizeof (blk_t));
+ memset (p->data, 42, BLK_SIZE);
+ }
+ memset ((u8 *) p + sizeof (blk_t), 42,
+ BLK_DESC_SIZE - sizeof (blk_t));
+ memset (p->data, 42, BLK_SIZE);
+ /* Add/release refs. */
+ blk_addref_desc_range (f, l);
+ blk_addref_desc (a);
+ blk_addref_desc (f);
+ blk_addref_desc (f->next);
+ blk_addref_desc (f->next->next);
+ blk_addref_desc_range_nb (f->next->next->next, 4);
+ blk_release_desc_range (f, l);
+ blk_release_desc (a);
+ blk_release_desc (a);
+ blk_release_desc_range_nb (f->next, 6);
+ blk_release_desc_range (f, l);
+ blk_release_desc (f);
+ /* Check. */
+ test_fail_unless (blk_check_memory ());
+ } test_end;
+ test_begin (t, "obj")
+ {
+ void *a, *b, *c, *d;
+ /* Allocate. */
+ a = blk_alloc ();
+ b = blk_alloc ();
+ c = blk_alloc_zero ();
+ test_fail_unless (((u8 *)c)[42] == 0);
+ d = blk_new (test_obj_uninit);
+ test_obj_init (d, a, b);
+ /* Add/release refs. */
+ blk_release (a);
+ blk_release (b);
+ blk_addref (d);
+ blk_addref (b);
+ blk_addref (c);
+ blk_release (d);
+ blk_release (c);
+ blk_release (b);
+ blk_release (d);
+ blk_release (c);
+ /* Check. */
+ test_fail_unless (blk_check_memory ());
+ } test_end;
+ blk_print_memory ();
+}
+
+void
+blk_test_suite (test_t t)
+{
+ test_suite_begin (t, "blk");
+ blk_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ blk_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/circular_buffer/Makefile b/cesar/lib/test/circular_buffer/Makefile
new file mode 100644
index 0000000000..68b0f7c144
--- /dev/null
+++ b/cesar/lib/test/circular_buffer/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_circular_list
+test_circular_list_SOURCES = test_circular.c
+test_circular_list_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/circular_buffer/src/test_circular.c b/cesar/lib/test/circular_buffer/src/test_circular.c
new file mode 100644
index 0000000000..d7ce76e02d
--- /dev/null
+++ b/cesar/lib/test/circular_buffer/src/test_circular.c
@@ -0,0 +1,94 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file lib/test/circular_list/src/test_circular.c
+ * \brief « brief description »
+ * \ingroup « module »
+ *
+ * « long description »
+ */
+#include "common/std.h"
+
+#include "lib/circular_buffer.h"
+#include "lib/test.h"
+
+int
+main (void)
+{
+ test_t test;
+ circular_buffer_t list;
+ u8 *my_list[4];
+ bool added;
+ u8* buffer;
+ uint i;
+
+ test_init (test, 0, NULL);
+
+ circular_buffer_init (&list, my_list, 4);
+
+ test_case_begin (test, "Add and get a buffer");
+ test_begin (test, "Add and get a buffer")
+ {
+ added = circular_buffer_add (&list, &i);
+ buffer = circular_buffer_get (&list);
+ test_fail_if (buffer != (u8 *) &i, "Wrong address");
+ }
+ test_end;
+
+ test_case_begin (test, "adding address to the buffer");
+
+ test_begin (test, "Address Add")
+ {
+ for (i = 0; i < 5; i++)
+ {
+ added = circular_buffer_add (&list, buffer);
+
+ if (i < 4)
+ {
+ test_fail_if (added != true, "Address shall be added");
+ test_fail_if (my_list[i] != list.buffer[i], "Wrong address");
+ }
+ else
+ {
+ test_fail_if (added != false, "Address shall not be added");
+ }
+ }
+ }
+ test_end;
+
+
+ buffer = circular_buffer_peek (&list);
+ test_begin (test, "Add and get a buffer")
+ {
+ buffer = circular_buffer_peek (&list);
+ test_fail_if (buffer != (u8*) &i, "Wrong address");
+ }
+ test_end;
+
+
+ test_case_begin (test, "Getting address from the buffer");
+
+ test_begin (test, "Address get")
+ {
+ for (i = 0; i < 5; i++)
+ {
+ buffer = circular_buffer_get (&list);
+
+ if (i < 4)
+ test_fail_if (buffer != (u8*) &i, "Wrong address");
+ else
+ test_fail_if (buffer != NULL, "Wrong address");
+ }
+
+ }
+ test_end;
+
+
+ test_result (test);
+ return test_nb_failed (test) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/crc/Makefile b/cesar/lib/test/crc/Makefile
new file mode 100644
index 0000000000..2a3ca4fd09
--- /dev/null
+++ b/cesar/lib/test/crc/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_crc
+test_crc_SOURCES = test_crc.c crcmodel.c
+test_crc_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/crc/inc/crcmodel.h b/cesar/lib/test/crc/inc/crcmodel.h
new file mode 100644
index 0000000000..c06312e84f
--- /dev/null
+++ b/cesar/lib/test/crc/inc/crcmodel.h
@@ -0,0 +1,160 @@
+/******************************************************************************/
+/* Start of crcmodel.h */
+/******************************************************************************/
+/* */
+/* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
+/* Date : 3 June 1993. */
+/* Status : Public domain. */
+/* */
+/* Description : This is the header (.h) file for the reference */
+/* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
+/* information on the Rocksoft^tm Model CRC Algorithm, see the document */
+/* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
+/* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
+/* "ftp.adelaide.edu.au/pub/rocksoft". */
+/* */
+/* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
+/* */
+/******************************************************************************/
+/* */
+/* How to Use This Package */
+/* ----------------------- */
+/* Step 1: Declare a variable of type cm_t. Declare another variable */
+/* (p_cm say) of type p_cm_t and initialize it to point to the first */
+/* variable (e.g. p_cm_t p_cm = &cm_t). */
+/* */
+/* Step 2: Assign values to the parameter fields of the structure. */
+/* If you don't know what to assign, see the document cited earlier. */
+/* For example: */
+/* p_cm->cm_width = 16; */
+/* p_cm->cm_poly = 0x8005L; */
+/* p_cm->cm_init = 0L; */
+/* p_cm->cm_refin = TRUE; */
+/* p_cm->cm_refot = TRUE; */
+/* p_cm->cm_xorot = 0L; */
+/* Note: Poly is specified without its top bit (18005 becomes 8005). */
+/* Note: Width is one bit less than the raw poly width. */
+/* */
+/* Step 3: Initialize the instance with a call cm_ini(p_cm); */
+/* */
+/* Step 4: Process zero or more message bytes by placing zero or more */
+/* successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); */
+/* */
+/* Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); */
+/* If the CRC is a 16-bit value, it will be in the bottom 16 bits. */
+/* */
+/******************************************************************************/
+/* */
+/* Design Notes */
+/* ------------ */
+/* PORTABILITY: This package has been coded very conservatively so that */
+/* it will run on as many machines as possible. For example, all external */
+/* identifiers have been restricted to 6 characters and all internal ones to */
+/* 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid */
+/* namespace collisions. This package is endian independent. */
+/* */
+/* EFFICIENCY: This package (and its interface) is not designed for */
+/* speed. The purpose of this package is to act as a well-defined reference */
+/* model for the specification of CRC algorithms. If you want speed, cook up */
+/* a specific table-driven implementation as described in the document cited */
+/* above. This package is designed for validation only; if you have found or */
+/* implemented a CRC algorithm and wish to describe it as a set of parameters */
+/* to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation */
+/* should behave identically to this package under those parameters. */
+/* */
+/******************************************************************************/
+
+/* The following #ifndef encloses this entire */
+/* header file, rendering it indempotent. */
+#ifndef CM_DONE
+#define CM_DONE
+
+/******************************************************************************/
+
+/* The following definitions are extracted from my style header file which */
+/* would be cumbersome to distribute with this package. The DONE_STYLE is the */
+/* idempotence symbol used in my style header file. */
+
+#ifndef DONE_STYLE
+
+//#ifndef __USE_MISC
+typedef unsigned long ulong;
+//#endif
+//typedef unsigned bool;
+typedef unsigned char * p_ubyte_;
+typedef const unsigned char * cp_ubyte_;
+
+#ifndef TRUE
+#define FALSE 0
+#define TRUE 1
+#endif
+
+/* Change to the second definition if you don't have prototypes. */
+#define P_(A) A
+/* #define P_(A) () */
+
+/* Uncomment this definition if you don't have void. */
+/* typedef int void; */
+
+#endif
+
+/******************************************************************************/
+
+/* CRC Model Abstract Type */
+/* ----------------------- */
+/* The following type stores the context of an executing instance of the */
+/* model algorithm. Most of the fields are model parameters which must be */
+/* set before the first initializing call to cm_ini. */
+typedef struct
+ {
+ int cm_width; /* Parameter: Width in bits [8,32]. */
+ ulong cm_poly; /* Parameter: The algorithm's polynomial. */
+ ulong cm_init; /* Parameter: Initial register value. */
+ bool cm_refin; /* Parameter: Reflect input bytes? */
+ bool cm_refot; /* Parameter: Reflect output CRC? */
+ ulong cm_xorot; /* Parameter: XOR this to output CRC. */
+
+ ulong cm_reg; /* Context: Context during execution. */
+ } cm_t;
+typedef cm_t *p_cm_t;
+
+/******************************************************************************/
+
+/* Functions That Implement The Model */
+/* ---------------------------------- */
+/* The following functions animate the cm_t abstraction. */
+
+void cm_ini P_((p_cm_t p_cm));
+/* Initializes the argument CRC model instance. */
+/* All parameter fields must be set before calling this. */
+
+void cm_nxt P_((p_cm_t p_cm,int ch));
+/* Processes a single message byte [0,255]. */
+
+void cm_blk P_((p_cm_t p_cm,cp_ubyte_ blk_adr,ulong blk_len));
+/* Processes a block of message bytes. */
+
+ulong cm_crc P_((p_cm_t p_cm));
+/* Returns the CRC value for the message bytes processed so far. */
+
+/******************************************************************************/
+
+/* Functions For Table Calculation */
+/* ------------------------------- */
+/* The following function can be used to calculate a CRC lookup table. */
+/* It can also be used at run-time to create or check static tables. */
+
+ulong cm_tab P_((p_cm_t p_cm,int index));
+/* Returns the i'th entry for the lookup table for the specified algorithm. */
+/* The function examines the fields cm_width, cm_poly, cm_refin, and the */
+/* argument table index in the range [0,255] and returns the table entry in */
+/* the bottom cm_width bytes of the return value. */
+
+/******************************************************************************/
+
+/* End of the header file idempotence #ifndef */
+#endif
+
+/******************************************************************************/
+/* End of crcmodel.h */
+/******************************************************************************/
diff --git a/cesar/lib/test/crc/src/crcmodel.c b/cesar/lib/test/crc/src/crcmodel.c
new file mode 100644
index 0000000000..f93342a1cc
--- /dev/null
+++ b/cesar/lib/test/crc/src/crcmodel.c
@@ -0,0 +1,148 @@
+/******************************************************************************/
+/* Start of crcmodel.c */
+/******************************************************************************/
+/* */
+/* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
+/* Date : 3 June 1993. */
+/* Status : Public domain. */
+/* */
+/* Description : This is the implementation (.c) file for the reference */
+/* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
+/* information on the Rocksoft^tm Model CRC Algorithm, see the document */
+/* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
+/* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
+/* "ftp.adelaide.edu.au/pub/rocksoft". */
+/* */
+/* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
+/* */
+/******************************************************************************/
+/* */
+/* Implementation Notes */
+/* -------------------- */
+/* To avoid inconsistencies, the specification of each function is not echoed */
+/* here. See the header file for a description of these functions. */
+/* This package is light on checking because I want to keep it short and */
+/* simple and portable (i.e. it would be too messy to distribute my entire */
+/* C culture (e.g. assertions package) with this package. */
+/* */
+/******************************************************************************/
+#include "common/std.h"
+#include "inc/crcmodel.h"
+
+/******************************************************************************/
+
+/* The following definitions make the code more readable. */
+
+#define BITMASK(X) (1L << (X))
+#define MASK32 0xFFFFFFFFL
+#define LOCAL static
+
+/******************************************************************************/
+
+LOCAL ulong reflect P_((ulong v,int b));
+LOCAL ulong reflect (v,b)
+/* Returns the value v with the bottom b [0,32] bits reflected. */
+/* Example: reflect(0x3e23L,3) == 0x3e26 */
+ulong v;
+int b;
+{
+ int i;
+ ulong t = v;
+ for (i=0; i<b; i++)
+ {
+ if (t & 1L)
+ v|= BITMASK((b-1)-i);
+ else
+ v&= ~BITMASK((b-1)-i);
+ t>>=1;
+ }
+ return v;
+}
+
+/******************************************************************************/
+
+LOCAL ulong widmask P_((p_cm_t));
+LOCAL ulong widmask (p_cm)
+/* Returns a longword whose value is (2^p_cm->cm_width)-1. */
+/* The trick is to do this portably (e.g. without doing <<32). */
+p_cm_t p_cm;
+{
+ return (((1L<<(p_cm->cm_width-1))-1L)<<1)|1L;
+}
+
+/******************************************************************************/
+
+void cm_ini (p_cm)
+p_cm_t p_cm;
+{
+ p_cm->cm_reg = p_cm->cm_init;
+}
+
+/******************************************************************************/
+
+void cm_nxt (p_cm,ch)
+p_cm_t p_cm;
+int ch;
+{
+ int i;
+ ulong uch = (ulong) ch;
+ ulong topbit = BITMASK(p_cm->cm_width-1);
+
+ if (p_cm->cm_refin) uch = reflect(uch,8);
+ p_cm->cm_reg ^= (uch << (p_cm->cm_width-8));
+ for (i=0; i<8; i++)
+ {
+ if (p_cm->cm_reg & topbit)
+ p_cm->cm_reg = (p_cm->cm_reg << 1) ^ p_cm->cm_poly;
+ else
+ p_cm->cm_reg <<= 1;
+ p_cm->cm_reg &= widmask(p_cm);
+ }
+}
+
+/******************************************************************************/
+
+void cm_blk (p_cm,blk_adr,blk_len)
+p_cm_t p_cm;
+cp_ubyte_ blk_adr;
+ulong blk_len;
+{
+ while (blk_len--) cm_nxt(p_cm,*blk_adr++);
+}
+
+/******************************************************************************/
+
+ulong cm_crc (p_cm)
+p_cm_t p_cm;
+{
+ if (p_cm->cm_refot)
+ return p_cm->cm_xorot ^ reflect(p_cm->cm_reg,p_cm->cm_width);
+ else
+ return p_cm->cm_xorot ^ p_cm->cm_reg;
+}
+
+/******************************************************************************/
+
+ulong cm_tab (p_cm,index)
+p_cm_t p_cm;
+int index;
+{
+ int i;
+ ulong r;
+ ulong topbit = BITMASK(p_cm->cm_width-1);
+ ulong inbyte = (ulong) index;
+
+ if (p_cm->cm_refin) inbyte = reflect(inbyte,8);
+ r = inbyte << (p_cm->cm_width-8);
+ for (i=0; i<8; i++)
+ if (r & topbit)
+ r = (r << 1) ^ p_cm->cm_poly;
+ else
+ r<<=1;
+ if (p_cm->cm_refin) r = reflect(r,p_cm->cm_width);
+ return r & widmask(p_cm);
+}
+
+/******************************************************************************/
+/* End of crcmodel.c */
+/******************************************************************************/
diff --git a/cesar/lib/test/crc/src/test_crc.c b/cesar/lib/test/crc/src/test_crc.c
new file mode 100644
index 0000000000..2e5e9aea35
--- /dev/null
+++ b/cesar/lib/test/crc/src/test_crc.c
@@ -0,0 +1,294 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_crc.c
+ * \brief CRC test.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/rnd.h"
+#include "lib/test.h"
+
+#include "lib/crc.h"
+
+#include "inc/crcmodel.h"
+
+#include <stdio.h>
+
+void
+crc_rocksoft_model_test_case (test_t t)
+{
+ lib_rnd_t rnd[1];
+ /* Algorithms ending with ref or pat are non standard refelected or
+ * assymetric pattern algorithms in order to have a reflected CRC for each
+ * size and to test for init and xorout pattern reflections. */
+ static u32 crc32_tab[256];
+ static crc_t crc32 = { 32, 0x04c11db7, 0xffffffff, true, true, 0xffffffff,
+ 0, { .t32 = crc32_tab } };
+ static u32 crc32ref_tab[256];
+ static crc_t crc32ref = { 32, 0x04c11db7, 0xffffffff, false, false, 0xffffffff,
+ 0, { .t32 = crc32ref_tab } };
+ static u32 hpavfc_tab[256];
+ static crc_t hpavfc = { 24, 0x800063, 0xffffff, true, true, 0xffffff,
+ 0, { .t32 = hpavfc_tab } };
+ static u32 hpavfcref_tab[256];
+ static crc_t hpavfcref = { 24, 0x800063, 0xffffff, false, false, 0xffffff,
+ 0, { .t32 = hpavfcref_tab } };
+ static u16 citt_tab[256];
+ static crc_t citt = { 16, 0x1021, 0xffff, false, false, 0x0000,
+ 0, { .t16 = citt_tab } };
+ static u16 xmodem_tab[256];
+ static crc_t xmodem = { 16, 0x8408, 0x0000, true, true, 0x0000,
+ 0, { .t16 = xmodem_tab } };
+ /* This one is not that useful because HP 1.0 FC does not contain a
+ * integral number of byte. */
+ static u8 hpfc10_tab[256];
+ static crc_t hpfc10 = { 8, 0x03, 0xff, true, true, 0xff,
+ 0, { .t8 = hpfc10_tab } };
+ static u8 hpfc10ref_tab[256];
+ static crc_t hpfc10ref = { 8, 0x03, 0xff, false, false, 0xff,
+ 0, { .t8 = hpfc10ref_tab } };
+ static u32 crc32pat_tab[256];
+ static crc_t crc32pat = { 32, 0x04c11db7, 0x01234567, true, true, 0xabcdef01,
+ 0, { .t32 = crc32pat_tab } };
+ static u32 crc32patref_tab[256];
+ static crc_t crc32patref = { 32, 0x04c11db7, 0x01234567, false, false, 0xabcdef01,
+ 0, { .t32 = crc32patref_tab } };
+ static u32 crc32patbiref_tab[256];
+ static crc_t crc32patbiref = { 32, 0x04c11db7, 0x01234567, true, false, 0xabcdef01,
+ 0, { .t32 = crc32patbiref_tab } };
+ uint crc_i;
+ crc_t *crc;
+ static const struct
+ {
+ const char *name;
+ crc_t *crc;
+ } crcs[] =
+ {
+ { "crc32", &crc32 },
+ { "crc32ref", &crc32ref },
+ { "hpavfc", &hpavfc },
+ { "hpavfcref", &hpavfcref },
+ { "citt", &citt },
+ { "xmodem", &xmodem },
+ { "hpfc10", &hpfc10 },
+ { "hpfc10ref", &hpfc10ref },
+ { "crc32pat", &crc32pat },
+ { "crc32patref", &crc32patref },
+ { "crc32patbiref", &crc32patbiref },
+ };
+ lib_rnd_init (rnd, 1234);
+ test_case_begin (t, "rocksoft model");
+ for (crc_i = 0; crc_i < COUNT (crcs); crc_i++)
+ {
+ char name[32];
+ crc = crcs[crc_i].crc;
+ /* Build the corresponding Rocksoft^tm model. */
+ cm_t cm;
+ cm.cm_width = crc->width;
+ cm.cm_poly = crc->generator;
+ cm.cm_init = crc->init;
+ cm.cm_refin = crc->refin;
+ cm.cm_refot = crc->refout;
+ cm.cm_xorot = crc->xorout;
+ /* Init. */
+ snprintf (name, COUNT (name), "%s init", crcs[crc_i].name);
+ test_begin (t, name)
+ {
+ crc_init (crc);
+ } test_end;
+ /* Tables. */
+ snprintf (name, COUNT (name), "%s table", crcs[crc_i].name);
+ test_begin (t, name)
+ {
+ uint i;
+ /* Compare tables. */
+ switch (crc->width)
+ {
+ case 32:
+ case 24:
+ for (i = 0; i < 256; i++)
+ {
+ u32 ref = cm_tab (&cm, i);
+ test_fail_unless (crc->table.t32[i] == ref,
+ "table[%d] mismatch: 0x%08x != 0x%08x",
+ i, crc->table.t32[i], ref);
+ }
+ break;
+ case 16:
+ for (i = 0; i < 256; i++)
+ {
+ u16 ref = cm_tab (&cm, i);
+ test_fail_unless (crc->table.t16[i] == ref,
+ "table[%d] mismatch: 0x%04x != 0x%04x",
+ i, crc->table.t16[i], ref);
+ }
+ break;
+ case 8:
+ for (i = 0; i < 256; i++)
+ {
+ u8 ref = cm_tab (&cm, i);
+ test_fail_unless (crc->table.t8[i] == ref,
+ "table[%d] mismatch: 0x%02x != 0x%02x",
+ i, crc->table.t8[i], ref);
+ }
+ break;
+ }
+ } test_end;
+ /* Check string. */
+ snprintf (name, COUNT (name), "%s check string", crcs[crc_i].name);
+ const char *check_str = "123456789";
+ const uint check_str_len = 9;
+ test_begin (t, name)
+ {
+ u32 my1, my2, ref;
+ my1 = crc_compute_block (crc, (u8 *) check_str, check_str_len);
+ my2 = crc_compute_begin (crc);
+ my2 = crc_compute_continue_block (crc, my2, (u8 *) check_str,
+ check_str_len);
+ my2 = crc_compute_end (crc, my2);
+ test_fail_unless (my1 == my2);
+ cm_ini (&cm);
+ cm_blk (&cm, (u8 *) check_str, check_str_len);
+ ref = cm_crc (&cm);
+ test_fail_unless (my1 == ref);
+ test_verbose_print ("%s check: 0x%0*x", crcs[crc_i].name,
+ crc->width / 4, my1);
+ } test_end;
+ /* Check buffer. */
+ snprintf (name, COUNT (name), "%s check buffer", crcs[crc_i].name);
+ u8 check_buf[2048];
+ test_begin (t, name)
+ {
+ uint i;
+ for (i = 0; i < 256; i++)
+ check_buf[i] = i;
+ for (i = 256 / 4; i < COUNT (check_buf) / 4; i++)
+ *((u32 *) check_buf + i) = lib_rnd32 (rnd);
+ u32 my, ref;
+ my = crc_compute_begin (crc);
+ my = crc_compute_continue_block (crc, my, check_buf,
+ COUNT (check_buf) / 3);
+ my = crc_compute_continue_block (crc, my, check_buf
+ + COUNT (check_buf) / 3,
+ COUNT (check_buf)
+ - COUNT (check_buf) / 3);
+ my = crc_compute_end (crc, my);
+ cm_ini (&cm);
+ cm_blk (&cm, check_buf, COUNT (check_buf));
+ ref = cm_crc (&cm);
+ test_fail_unless (my == ref);
+ } test_end;
+ }
+}
+
+void
+crc_hpav_vectors_test_case (test_t t)
+{
+ test_case_begin (t, "hpav vectors");
+ /* HPAV FC */
+ static u32 hpavfc_tab[256];
+ static crc_t hpavfc = { 24, 0x800063, 0xffffff, true, true, 0xffffff,
+ 0, { .t32 = hpavfc_tab } };
+ static const u8 fcav_vector[16] =
+ { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
+ 0x0c, 0x71, 0x74, 0x69 };
+ /* The CRC is computed considering that the LSB of the first byte is the
+ * highest order bit (x^127). CRC is stored with is highest order bit
+ * first which correspond to the LSB of the CRC register. This explain
+ * that: */
+ const u32 fcav_crc = 0x697471;
+ /* This constant is defined in the Homeplug specification: */
+ const u32 fcav_rx_good = 0x380ffe;
+ test_begin (t, "fcav begin")
+ {
+ crc_init (&hpavfc);
+ } test_end;
+ test_begin (t, "fcav tx")
+ {
+ u32 crc;
+ crc = crc_compute_begin (&hpavfc);
+ crc = crc_compute_continue_block (&hpavfc, crc, fcav_vector,
+ COUNT (fcav_vector) - 3);
+ crc = crc_compute_end (&hpavfc, crc);
+ test_fail_unless (crc == fcav_crc, "CRC mismatch 0x%06x != 0x%06x",
+ crc, fcav_crc);
+ } test_end;
+ test_begin (t, "fcav rx")
+ {
+ u32 crc;
+ crc = crc_compute_block (&hpavfc, fcav_vector, COUNT (fcav_vector));
+ test_fail_unless (crc == fcav_rx_good,
+ "received CRC not good: 0x%06x != 0x%06x", crc,
+ fcav_rx_good);
+ } test_end;
+ /* PB CRC. */
+ static u32 pbenc_tab[256];
+ static crc_t pbenc = { 32, 0x04c11db7, 0xffffffff, true, true, 0xffffffff,
+ 0, { .t32 = pbenc_tab } };
+ static const u8 pbenc_vector[136] =
+ {
+ 0x00, 0x00, 0x00, 0x1e, 0x21, 0x69, 0x71, 0x5a, 0xfc, 0x31, 0x20,
+ 0x13, 0xec, 0x61, 0xb6, 0x08, 0xcf, 0xcc, 0x6a, 0x65, 0xfa, 0x9d,
+ 0x0a, 0x04, 0x4e, 0xf7, 0x50, 0x78, 0x66, 0x8a, 0x65, 0x0e, 0x2b,
+ 0x7d, 0xa6, 0x8c, 0x27, 0x7c, 0x93, 0x92, 0x5a, 0x2f, 0x67, 0x40,
+ 0xd1, 0xb4, 0x10, 0xbf, 0xe3, 0x0a, 0x43, 0xf5, 0x43, 0x08, 0xc2,
+ 0x1f, 0x5c, 0xdc, 0xf7, 0x8c, 0xe8, 0xa4, 0x48, 0x65, 0xec, 0x00,
+ 0x57, 0x87, 0x88, 0x39, 0xda, 0x85, 0x2b, 0x93, 0xa6, 0x2b, 0xa6,
+ 0x40, 0x78, 0xe9, 0x44, 0x72, 0xb2, 0x11, 0xb8, 0x18, 0xe9, 0x01,
+ 0x2c, 0x36, 0x75, 0xcf, 0xe4, 0x4d, 0x60, 0x57, 0x89, 0xa2, 0x95,
+ 0x31, 0xff, 0x5d, 0x29, 0xa1, 0xff, 0xf2, 0x5b, 0xe7, 0x67, 0xad,
+ 0xc6, 0xff, 0x61, 0xc6, 0x52, 0x81, 0xdc, 0xf9, 0xe7, 0x19, 0x33,
+ 0x0f, 0x89, 0x46, 0x29, 0xa9, 0x04, 0xcd, 0x85, 0x96, 0xe5, 0xbf,
+ 0xdb, 0xe6, 0x90, 0x95
+ };
+ const u32 pbenc_crc = 0x9590e6db;
+ /* This constant is defined in the Homeplug specification: */
+ const u32 pbenc_rx_good = 0x2144df1c;
+ test_begin (t, "pbenc begin")
+ {
+ crc_init (&pbenc);
+ } test_end;
+ test_begin (t, "pbenc tx")
+ {
+ u32 crc;
+ crc = crc_compute_begin (&pbenc);
+ crc = crc_compute_continue_block (&pbenc, crc, pbenc_vector,
+ COUNT (pbenc_vector) - 4);
+ crc = crc_compute_end (&pbenc, crc);
+ test_fail_unless (crc == pbenc_crc, "CRC mismatch 0x%08x != 0x%08x",
+ crc, pbenc_crc);
+ } test_end;
+ test_begin (t, "pbenc rx")
+ {
+ u32 crc;
+ crc = crc_compute_block (&pbenc, pbenc_vector, COUNT (pbenc_vector));
+ test_fail_unless (crc == pbenc_rx_good,
+ "received CRC not good: 0x%08x != 0x%08x", crc,
+ pbenc_rx_good);
+ } test_end;
+}
+
+void
+crc_test_suite (test_t t)
+{
+ test_suite_begin (t, "crc");
+ crc_rocksoft_model_test_case (t);
+ crc_hpav_vectors_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ crc_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/heap/Makefile b/cesar/lib/test/heap/Makefile
new file mode 100644
index 0000000000..112cf7a42d
--- /dev/null
+++ b/cesar/lib/test/heap/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_heap
+test_heap_SOURCES = test_heap.c
+test_heap_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/heap/src/test_heap.c b/cesar/lib/test/heap/src/test_heap.c
new file mode 100644
index 0000000000..20e0d9ac14
--- /dev/null
+++ b/cesar/lib/test/heap/src/test_heap.c
@@ -0,0 +1,272 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file test_heap.c
+ * \brief Test heap.
+ * \ingroup lib_test
+ */
+#include "common/std.h"
+#include "lib/rnd.h"
+#include "lib/heap.h"
+#include "lib/test.h"
+
+#if DEBUG
+# define NB_NODES 10000
+# define NB_ITER 10000
+#else
+# define NB_NODES 100000
+# define NB_ITER 100000000
+#endif
+
+/**
+ * Check heap structure.
+ * \param t test context
+ * \param heap the heap to check
+ * \return the number of elements
+ */
+static uint
+heap_check (test_t t, heap_t *heap)
+{
+ test_within (t);
+ uint n, depth, maxdepth;
+ heap_node_t *h;
+ h = heap_get_root (heap);
+ n = depth = maxdepth = 0;
+ while (h)
+ {
+ test_fail_unless (h != h->left && h != h->right);
+ test_fail_unless (!h->left || h->left->father == h);
+ test_fail_unless (!h->right || h->right->father == h);
+ if (h->left)
+ {
+ h = h->left;
+ depth++;
+ }
+ else if (h->right)
+ {
+ h = h->right;
+ depth++;
+ }
+ else
+ {
+ /* Go up... */
+ while (h)
+ {
+ test_fail_unless
+ (!h->father ||
+ !less_mod2p32 (PARENT_OF (heap_node_u32_t, node, h)->key,
+ PARENT_OF (heap_node_u32_t, node,
+ h->father)->key)
+ );
+ n++;
+ if (h->father && h->father->left == h && h->father->right)
+ {
+ /* ...and right if coming from left. */
+ h = h->father->right;
+ break;
+ }
+ else
+ {
+ h = h->father;
+ if (depth + 1 > maxdepth)
+ maxdepth = depth + 1;
+ depth--;
+ }
+ }
+ }
+ }
+ return n;
+}
+
+/**
+ * Maintain stats about right path length.
+ * \param heap the heap
+ * \param t test context
+ * \param min minimum path length
+ * \param max maximum path length
+ * \param sum sum of path length (to compute average)
+ */
+static void
+heap_stats (test_t t, heap_t *heap, uint *min, uint *max, unsigned long long *sum)
+{
+ test_within (t);
+ heap_node_t *n;
+ uint path_length;
+#if CONFIG_HEAP_LEFTIST
+ uint null_path_length = heap_get_root (heap)->null_path_length + 1;
+#endif
+ for (n = heap_get_root (heap), path_length = 0;
+ n;
+ n = n->right)
+ {
+#if CONFIG_HEAP_LEFTIST
+ test_fail_unless (null_path_length = n->null_path_length + 1);
+ null_path_length = n->null_path_length;
+#endif
+ path_length++;
+ }
+#if CONFIG_HEAP_LEFTIST
+ test_fail_unless (null_path_length == 1);
+#endif
+ *min = MIN (*min, path_length);
+ *max = MAX (*max, path_length);
+ *sum += path_length;
+}
+
+/**
+ * Return a limited random number.
+ * \param rnd rnd context.
+ * \return the random number.
+ *
+ * As we use a mod2p32 logic, all values should be distant no more than 2^31.
+ */
+static inline u32
+limited_rnd (lib_rnd_t *rnd)
+{
+ return 0xc0000000 + (lib_rnd32 (rnd) & 0x7fffffff);
+}
+
+void
+heap_basic_test_case (test_t t)
+{
+ uint i;
+ uint min = 0, max = 0;
+ unsigned long long sum = 0;
+ lib_rnd_t rnd;
+ heap_t heap, heap2;
+ heap_node_u32_t nodes[NB_NODES];
+ test_case_begin (t, "basic");
+ lib_rnd_init (&rnd, 1234);
+ /* Initialise heap & nodes. */
+ heap_init (&heap, &heap_node_u32_less_mod2p32);
+ heap_init (&heap2, &heap_node_u32_less_mod2p32);
+ for (i = 0; i < COUNT (nodes); i++)
+ heap_node_init (&nodes[i].node);
+ /* Insert them with random key. */
+ test_begin (t, "insert")
+ {
+ for (i = 0; i < COUNT (nodes) / 2; i++)
+ {
+ nodes[i].key = limited_rnd (&rnd);
+ heap_insert (&heap, &nodes[i].node);
+ test_fail_unless (DEBUG && i + 1 == heap_check (t, &heap));
+ }
+ for (i = 0; i < COUNT (nodes) / 2; i++)
+ {
+ uint j = i + COUNT (nodes) / 2;
+ nodes[j].key = limited_rnd (&rnd);
+ heap_insert (&heap2, &nodes[j].node);
+ test_fail_unless (DEBUG && i + 1 == heap_check (t, &heap2));
+ }
+ } test_end;
+ /* Merge. */
+ test_begin (t, "merge")
+ {
+ heap_merge (&heap, &heap2);
+ test_fail_unless (DEBUG && COUNT (nodes) == heap_check (t, &heap));
+ test_fail_unless (DEBUG && heap_empty (&heap2));
+ } test_end;
+ /* Remove a node and insert it back. */
+ test_begin (t, "remove and insert")
+ {
+ for (i = 0; i < NB_ITER; i++)
+ {
+ heap_node_u32_t *r;
+ if (lib_rnd_flip_coin (&rnd, LIB_RND_RATIO (0.8)))
+ {
+ if (lib_rnd_flip_coin (&rnd, LIB_RND_RATIO (0.5)))
+ {
+ r = PARENT_OF (heap_node_u32_t, node,
+ heap_get_root (&heap));
+ heap_remove_root (&heap);
+ }
+ else
+ {
+ r = &nodes[lib_rnd_uniform (&rnd, COUNT (nodes))];
+ heap_remove (&heap, &r->node);
+ }
+ test_fail_unless
+ (DEBUG && COUNT (nodes) - 1 == heap_check (t, &heap));
+ r->key = limited_rnd (&rnd);
+ heap_insert (&heap, &r->node);
+ }
+ else
+ {
+ if (lib_rnd_flip_coin (&rnd, LIB_RND_RATIO (0.5)))
+ {
+ r = PARENT_OF (heap_node_u32_t, node,
+ heap_get_root (&heap));
+ }
+ else
+ {
+ r = &nodes[lib_rnd_uniform (&rnd, COUNT (nodes))];
+ }
+ if (lib_rnd_flip_coin (&rnd, LIB_RND_RATIO (0.5)))
+ {
+ r->key = limited_rnd (&rnd);
+ }
+ else
+ {
+ r->key += (lib_rnd32 (&rnd) & 0xff) - 0x80;
+ if (r->key < 0xc0000000 && r->key >= 0x80000000)
+ r->key = 0xc0000000;
+ else if (r->key > 0x3fffffff && r->key < 0x80000000)
+ r->key = 0x3fffffff;
+ }
+ heap_adjust (&heap, &r->node);
+ }
+ test_fail_unless
+ (DEBUG && COUNT (nodes) == heap_check (t, &heap));
+ if (DEBUG)
+ heap_stats (t, &heap, &min, &max, &sum);
+ }
+ } test_end;
+ /* Remove all nodes and check order. */
+ test_begin (t, "check order")
+ {
+ i = COUNT (nodes);
+ u32 last = PARENT_OF (heap_node_u32_t, node,
+ heap_get_root (&heap))->key;
+ while (!heap_empty (&heap))
+ {
+ heap_node_u32_t *r = PARENT_OF (heap_node_u32_t, node,
+ heap_get_root (&heap));
+ test_fail_unless (!less_mod2p32 (r->key, last));
+ last = r->key;
+ heap_remove_root (&heap);
+ i--;
+ test_fail_unless (DEBUG && i == heap_check (t, &heap));
+ }
+ } test_end;
+ /* Print stats about right path length. */
+ if (DEBUG)
+ {
+ test_begin (t, "check depth")
+ {
+ test_verbose_print ("min=%d, max=%d, avg=%d", min, max,
+ (uint) (sum / NB_ITER));
+ } test_end;
+ }
+}
+
+void
+heap_test_suite (test_t t)
+{
+ test_suite_begin (t, "heap");
+ heap_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ heap_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/list/Makefile b/cesar/lib/test/list/Makefile
new file mode 100644
index 0000000000..819511bbd7
--- /dev/null
+++ b/cesar/lib/test/list/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_list
+test_list_SOURCES = test_list.c
+test_list_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/list/src/test_list.c b/cesar/lib/test/list/src/test_list.c
new file mode 100644
index 0000000000..af02728069
--- /dev/null
+++ b/cesar/lib/test/list/src/test_list.c
@@ -0,0 +1,267 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_list.c
+ * \brief Test lists.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/rnd.h"
+#include "lib/list.h"
+#include "lib/test.h"
+
+#define NB_ITEMS 1000
+#define NB_ITER 100000
+#define NB_LISTS 5
+
+struct test_list_t
+{
+ list_node_t node;
+ int list;
+ int pos;
+};
+typedef struct test_list_t test_list_t;
+
+void
+list_basic_test_case (test_t t)
+{
+ uint i, j;
+ list_t lists[NB_LISTS];
+ uint lists_item_nb[NB_LISTS];
+ lib_rnd_t rnd[1];
+ test_list_t items[NB_ITEMS];
+ test_case_begin (t, "basic");
+ /* Init. */
+ lib_rnd_init (rnd, 1234);
+ for (i = 0; i < COUNT (lists); i++)
+ {
+ list_init (&lists[i]);
+ lists_item_nb[i] = 0;
+ }
+ for (i = 0; i < COUNT (items); i++)
+ {
+ list_init_node (&items[i].node);
+ j = lib_rnd_uniform (rnd, NB_LISTS);
+ items[i].list = j;
+ items[i].pos = lists_item_nb[j]++;
+ list_push (&lists[j], &items[i].node);
+ }
+ /* Test loop. */
+ test_begin (t, "remove and insert")
+ {
+ for (i = 0; i < NB_ITER; i++)
+ {
+ int pos, nb;
+ list_node_t *first, *last, *n;
+ int src_list_i, src_pos;
+ int dst_list_i, dst_pos;
+ list_t *src_list, *dst_list;
+ list_node_t *src_after, *dst_after;
+ bool src_center, src_begin, range;
+ bool dst_center, dst_begin;
+ /* Choose source. */
+ src_center = lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.3));
+ range = lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.3));
+ if (src_center)
+ {
+ /* Arbitrary node. */
+ first = &items[lib_rnd_uniform (rnd, NB_ITEMS)].node;
+ test_list_t *tn = PARENT_OF (test_list_t, node, first);
+ src_list_i = tn->list;
+ nb = range ? lib_rnd_uniform (rnd, lists_item_nb[src_list_i] -
+ tn->pos) + 1 : 1;
+ last = first;
+ for (j = 0; j < (uint) nb - 1; j++)
+ last = list_next (last);
+ src_after = list_next (last);
+ src_pos = tn->pos + nb;
+ }
+ else
+ {
+ /* Node at one end. */
+ src_begin = lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.5));
+ do {
+ src_list_i = lib_rnd_uniform (rnd, NB_LISTS);
+ } while (lists_item_nb[src_list_i] == 0);
+ nb = range ? lib_rnd_uniform (rnd, lists_item_nb[src_list_i]) + 1
+ : 1;
+ if (src_begin)
+ {
+ first = list_begin (&lists[src_list_i]);
+ last = first;
+ for (j = 0; j < (uint) nb - 1; j++)
+ last = list_next (last);
+ src_after = list_next (last);
+ src_pos = nb;
+ }
+ else
+ {
+ last = list_rbegin (&lists[src_list_i]);
+ first = last;
+ for (j = 0; j < (uint) nb - 1; j++)
+ first = list_prev (first);
+ src_after = list_next (last);
+ src_pos = -1;
+ }
+ }
+ /* Choose destination. */
+ dst_center = lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.3));
+ if (dst_center)
+ {
+ /* Arbitrary node. */
+ test_list_t *tn;
+ do {
+ tn = &items[lib_rnd_uniform (rnd, NB_ITEMS)];
+ } while (tn->list == src_list_i);
+ dst_list_i = tn->list;
+ dst_after = &tn->node;
+ dst_pos = tn->pos;
+ }
+ else
+ {
+ /* Node at one end. */
+ dst_begin = lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.5));
+ do {
+ dst_list_i = lib_rnd_uniform (rnd, NB_LISTS);
+ } while (src_list_i == dst_list_i);
+ if (dst_begin)
+ {
+ dst_after = list_begin (&lists[dst_list_i]);
+ dst_pos = 0;
+ }
+ else
+ {
+ dst_after = list_end (&lists[dst_list_i]);
+ dst_pos = lists_item_nb[dst_list_i];
+ }
+ }
+ /* Apply the operation. */
+ src_list = &lists[src_list_i];
+ dst_list = &lists[dst_list_i];
+ if (range)
+ {
+ /* Range operations. */
+ if (dst_center)
+ list_insert_range (dst_list, src_list, dst_after, first,
+ list_next (last));
+ else if (dst_begin)
+ list_unshift_range (dst_list, src_list, first, list_next
+ (last));
+ else
+ list_push_range (dst_list, src_list, first, list_next (last));
+ }
+ else
+ {
+ /* Single operations, remove... */
+ if (src_center)
+ list_remove (src_list, first);
+ else
+ {
+ if (src_begin)
+ n = list_shift (src_list);
+ else
+ n = list_pop (src_list);
+ dbg_assert (n == first);
+ }
+ /* ...and insert. */
+ if (dst_center)
+ list_insert (dst_list, dst_after, first);
+ else if (dst_begin)
+ list_unshift (dst_list, first);
+ else
+ list_push (dst_list, first);
+ }
+ /* Update pos fields of moved nodes. */
+ for (n = first, pos = dst_pos; n != list_next (last);
+ n = list_next (n), pos++)
+ {
+ test_list_t *tn = PARENT_OF (test_list_t, node, n);
+ test_fail_unless (tn->list == src_list_i);
+ tn->list = dst_list_i;
+ tn->pos = pos;
+ }
+ /* Update pos fields in source list. */
+ for (n = src_after, pos = src_pos;
+ n != list_end (src_list);
+ n = list_next (n), pos++)
+ {
+ test_list_t *tn = PARENT_OF (test_list_t, node, n);
+ test_fail_unless (tn->list == src_list_i);
+ test_fail_unless (tn->pos == pos);
+ tn->pos = pos - nb;
+ }
+ lists_item_nb[src_list_i] -= nb;
+ /* Update pos fields in destination list. */
+ for (n = dst_after, pos = dst_pos;
+ n != list_end (dst_list);
+ n = list_next (n), pos++)
+ {
+ test_list_t *tn = PARENT_OF (test_list_t, node, n);
+ test_fail_unless (tn->list == dst_list_i);
+ test_fail_unless (tn->pos == pos);
+ tn->pos = pos + nb;
+ }
+ lists_item_nb[dst_list_i] += nb;
+ /* Check lists. */
+ uint check_items = 0;
+ for (j = 0; j < COUNT (lists); j++)
+ {
+ test_fail_unless
+ ((lists_item_nb[j] == 0 && list_empty (&lists[j])) ||
+ (lists_item_nb[j] != 0 && !list_empty (&lists[j])));
+ if (lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.5)))
+ {
+ /* Check forward. */
+ pos = 0;
+ for (n = list_begin (&lists[j]); n != list_end (&lists[j]);
+ n = list_next (n), pos++)
+ {
+ test_list_t *tn = PARENT_OF (test_list_t, node, n);
+ test_fail_unless (tn->list == (int) j);
+ test_fail_unless (tn->pos == pos);
+ check_items++;
+ }
+ test_fail_unless (pos == (int) lists_item_nb[j]);
+ }
+ else
+ {
+ /* Check backward. */
+ pos = lists_item_nb[j] - 1;
+ for (n = list_rbegin (&lists[j]); n != list_rend (&lists[j]);
+ n = list_prev (n), pos--)
+ {
+ test_list_t *tn = PARENT_OF (test_list_t, node, n);
+ test_fail_unless (tn->list == (int) j);
+ test_fail_unless (tn->pos == pos);
+ check_items++;
+ }
+ test_fail_unless (pos == -1);
+ }
+ }
+ test_fail_unless (check_items == NB_ITEMS);
+ }
+ } test_end;
+}
+
+void
+list_test_suite (test_t t)
+{
+ test_suite_begin (t, "list");
+ list_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ list_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/read_word/Makefile b/cesar/lib/test/read_word/Makefile
new file mode 100644
index 0000000000..0cf7c380b6
--- /dev/null
+++ b/cesar/lib/test/read_word/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_read_word
+test_read_word_SOURCES = read_word.c
+test_read_word_MODULES = lib
+
+include $(BASE)/common/make/top.mk \ No newline at end of file
diff --git a/cesar/lib/test/read_word/src/read_word.c b/cesar/lib/test/read_word/src/read_word.c
new file mode 100644
index 0000000000..9d4b55254c
--- /dev/null
+++ b/cesar/lib/test/read_word/src/read_word.c
@@ -0,0 +1,151 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file test_sparc_inte.c
+ * \brief test the whole sar_mf.c source
+ * \ingroup mac/sar/test/unit_test/lib_sar_test
+ *
+ */
+
+#include "common/std.h"
+#include "lib/test.h"
+#include "lib/read_word.h"
+#include <stdio.h>
+#include <string.h>
+
+int main (void)
+{
+ test_t test;
+ u8 buffer[1000];
+ uint length;
+ mac_t mac;
+
+ test_init (test, 0, NULL);
+
+ *(uint *) (buffer + 504) = 0x567801BA;
+ *(uint *) (buffer + 508) = 0x1234;
+
+ length = read_bytes_from_word (buffer + 504, 2) >> 2;
+
+ length = length & 0xFFFF;
+ test_begin(test, "test1")
+ {
+ test_fail_if (length != 110, "function does not work");
+ }
+ test_end;
+
+ *(uint *) (buffer + 504) = 0x01BAFFFF;
+ *(uint *) (buffer + 508) = 0x12345678;
+
+ length = read_bytes_from_word (buffer + 506, 2) >> 2;
+
+ length = length & 0xFFFF;
+ test_begin(test, "test2")
+ {
+ test_fail_if (length != 110, "function does not work");
+ }
+ test_end;
+
+ *(uint *) (buffer + 504) = 0x01BAFFFF;
+ *(uint *) (buffer + 508) = 0x12345678;
+
+ length = read_u24_from_word (buffer + 507);
+
+ test_begin(test, "test3")
+ {
+ test_fail_if (length != 0x567801, "function does not work");
+ }
+ test_end;
+
+ length = read_u24_from_word (buffer + 504);
+
+ test_begin(test, "test4")
+ {
+ test_fail_if (length != 0xBAFFFF, "function does not work");
+ }
+ test_end;
+
+ length = read_u16_from_word (buffer + 507);
+
+ test_begin(test, "test5")
+ {
+ test_fail_if (length != 0x7801, "function does not work");
+ }
+ test_end;
+
+ length = read_u16_from_word (buffer + 504);
+
+ test_begin(test, "test6")
+ {
+ test_fail_if (length != 0xFFFF, "function does not work");
+ }
+ test_end;
+
+ length = read_u8_from_word (buffer + 507);
+
+ test_begin(test, "test7")
+ {
+ test_fail_if (length != 0x01, "function does not work");
+ }
+ test_end;
+
+ length = read_u8_from_word (buffer + 504);
+
+ test_begin(test, "test8")
+ {
+ test_fail_if (length != 0xFF, "function does not work");
+ }
+ test_end;
+
+ *(uint *) (buffer + 508) = 0x12345678;
+ length = read_u32_from_word (buffer + 508);
+ test_begin(test, "test9")
+ {
+ test_fail_if (length != 0x12345678, "function does not work");
+ }
+ test_end;
+
+ *(uint *) (buffer + 508) = 0x56780000;
+ *(uint *) (buffer + 512) = 0x00001234;
+ length = read_u32_from_word (buffer + 510);
+ test_begin(test, "test10")
+ {
+ test_fail_if (length != 0x12345678, "function does not work");
+ }
+ test_end;
+
+ *(uint *) buffer = 0xDB1F0F00;
+ *(uint *) (buffer + 4)= 0x12347F6A;
+
+ mac = read_u48_from_word (buffer);
+
+ test_begin(test, "read_mac_address")
+ {
+ test_fail_if (mac != 0x7F6ADB1F0F00ull, "Wrong mac address");
+ }
+ test_end;
+
+ mac = read_u56_from_word (buffer);
+
+ test_begin(test, "read_mac_address with one more byte.")
+ {
+ test_fail_if (mac != 0x347F6ADB1F0F00ull, "Wrong mac address");
+ }
+ test_end;
+
+ mac = read_u64_from_word (buffer);
+
+ test_begin(test, "read_mac_address with two more byte.")
+ {
+ test_fail_if (mac != 0x12347F6ADB1F0F00ull, "Wrong mac address");
+ }
+ test_end;
+
+ test_result (test);
+ return test_nb_failed (test) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/restrack/Config b/cesar/lib/test/restrack/Config
new file mode 100644
index 0000000000..e2899974cd
--- /dev/null
+++ b/cesar/lib/test/restrack/Config
@@ -0,0 +1,2 @@
+CONFIG_RESTRACK=y
+CONFIG_RESTRACK_KEEP=y
diff --git a/cesar/lib/test/restrack/Makefile b/cesar/lib/test/restrack/Makefile
new file mode 100644
index 0000000000..29a0d9c417
--- /dev/null
+++ b/cesar/lib/test/restrack/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_restrack
+test_restrack_SOURCES = test_restrack.c
+test_restrack_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/restrack/src/test_restrack.c b/cesar/lib/test/restrack/src/test_restrack.c
new file mode 100644
index 0000000000..e2ce11bb4d
--- /dev/null
+++ b/cesar/lib/test/restrack/src/test_restrack.c
@@ -0,0 +1,104 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_restrack.c
+ * \brief Test resources tracker.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/restrack.h"
+#include "lib/blk.h"
+#include "lib/test.h"
+
+void
+restrack_alone_test_case (test_t t)
+{
+ test_case_begin (t, "alone");
+ test_begin (t, "basic")
+ {
+ int a, b, c, i;
+ restrack_create (NULL, &a, __func__, __LINE__, 1);
+ restrack_create (NULL, &b, __func__, __LINE__, 1);
+ restrack_create (NULL, &c, __func__, __LINE__, 1);
+ for (i = 0; i < 5; i++)
+ {
+ restrack_update (NULL, &a, __func__, __LINE__, 1);
+ restrack_update (NULL, &b, __func__, __LINE__, 1);
+ restrack_update (NULL, &b, __func__, __LINE__, 2);
+ restrack_update (NULL, &b, __func__, __LINE__, -1);
+ }
+ test_fail_unless (!restrack_check ());
+ for (i = 0; i < 5; i++)
+ {
+ restrack_update (NULL, &a, __func__, __LINE__, -1);
+ restrack_update (NULL, &b, __func__, __LINE__, -2);
+ }
+ restrack_destroy (NULL, &a, __func__, __LINE__, -1);
+ restrack_destroy (NULL, &b, __func__, __LINE__, -1);
+ restrack_destroy (NULL, &c, __func__, __LINE__, -1);
+ test_fail_unless (restrack_check ());
+ } test_end;
+}
+
+void
+restrack_blk_test_case (test_t t)
+{
+ test_case_begin (t, "blk");
+ test_begin (t, "basic")
+ {
+ void *a, *b;
+ blk_t *f, *l;
+ int i;
+ a = blk_alloc ();
+ b = blk_alloc ();
+ f = blk_alloc_desc_range (5, &l);
+ for (i = 0; i < 5; i++)
+ {
+ blk_addref (a);
+ blk_addref (b);
+ blk_addref (b);
+ blk_release (b);
+ blk_addref_desc_range (f, l);
+ blk_addref_desc (f);
+ blk_addref_desc (l);
+ blk_release_desc_range (f, l);
+ }
+ test_fail_unless (!restrack_check ());
+ blk_release_desc_range (f, l);
+ test_fail_unless (!restrack_check ());
+ for (i = 0; i < 5; i++)
+ {
+ blk_release (b);
+ blk_release (a);
+ blk_release_desc (f);
+ blk_release_desc (l);
+ }
+ blk_release (b);
+ blk_release (a);
+ test_fail_unless (restrack_check ());
+ } test_end;
+}
+
+void
+restrack_test_suite (test_t t)
+{
+ test_suite_begin (t, "restrack");
+ restrack_alone_test_case (t);
+ restrack_blk_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ restrack_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/rnd/Makefile b/cesar/lib/test/rnd/Makefile
new file mode 100644
index 0000000000..988cae2faa
--- /dev/null
+++ b/cesar/lib/test/rnd/Makefile
@@ -0,0 +1,19 @@
+BASE = ../../..
+
+# TODO, only host for the moment.
+ECOS = y
+TARGET = sparc
+
+HOST_PROGRAMS = test_rnd
+test_rnd_SOURCES = test_rnd.c
+test_rnd_MODULES = lib
+
+test_data_header = obj/inc/mt19937ar.out.h
+CLEAN_FILES = $(test_data_header)
+
+include $(BASE)/common/make/top.mk
+
+$(call src2obj,src/test_rnd.c,host): $(test_data_header)
+$(call src2obj,src/test_rnd.c,target): $(test_data_header)
+$(test_data_header): mt19937ar.out
+ perl $< > $@
diff --git a/cesar/lib/test/rnd/mt19937ar.out b/cesar/lib/test/rnd/mt19937ar.out
new file mode 100644
index 0000000000..c29480ed32
--- /dev/null
+++ b/cesar/lib/test/rnd/mt19937ar.out
@@ -0,0 +1,213 @@
+#!/usr/bin/perl
+print <<EOF;
+/* Autogenerated file, do not edit. */
+static const u32 data[] = {
+EOF
+while (<DATA>)
+{
+ die unless /^(\s*\d+)(\s*\d+)(\s*\d+)(\s*\d+)(\s*\d+)\s$/;
+ print " $1u,$2u,$3u,$4u,$5u,\n";
+ die unless $1 && $2 && $3 && $4 && $5;
+}
+print "};\n";
+__DATA__
+1067595299 955945823 477289528 4107218783 4228976476
+3344332714 3355579695 227628506 810200273 2591290167
+2560260675 3242736208 646746669 1479517882 4245472273
+1143372638 3863670494 3221021970 1773610557 1138697238
+1421897700 1269916527 2859934041 1764463362 3874892047
+3965319921 72549643 2383988930 2600218693 3237492380
+2792901476 725331109 605841842 271258942 715137098
+3297999536 1322965544 4229579109 1395091102 3735697720
+2101727825 3730287744 2950434330 1661921839 2895579582
+2370511479 1004092106 2247096681 2111242379 3237345263
+4082424759 219785033 2454039889 3709582971 835606218
+2411949883 2735205030 756421180 2175209704 1873865952
+2762534237 4161807854 3351099340 181129879 3269891896
+ 776029799 2218161979 3001745796 1866825872 2133627728
+ 34862734 1191934573 3102311354 2916517763 1012402762
+2184831317 4257399449 2899497138 3818095062 3030756734
+1282161629 420003642 2326421477 2741455717 1278020671
+3744179621 271777016 2626330018 2560563991 3055977700
+4233527566 1228397661 3595579322 1077915006 2395931898
+1851927286 3013683506 1999971931 3006888962 1049781534
+1488758959 3491776230 104418065 2448267297 3075614115
+3872332600 891912190 3936547759 2269180963 2633455084
+1047636807 2604612377 2709305729 1952216715 207593580
+2849898034 670771757 2210471108 467711165 263046873
+3569667915 1042291111 3863517079 1464270005 2758321352
+3790799816 2301278724 3106281430 7974801 2792461636
+ 555991332 621766759 1322453093 853629228 686962251
+1455120532 957753161 1802033300 1021534190 3486047311
+1902128914 3701138056 4176424663 1795608698 560858864
+3737752754 3141170998 1553553385 3367807274 711546358
+2475125503 262969859 251416325 2980076994 1806565895
+ 969527843 3529327173 2736343040 2987196734 1649016367
+2206175811 3048174801 3662503553 3138851612 2660143804
+1663017612 1816683231 411916003 3887461314 2347044079
+1015311755 1203592432 2170947766 2569420716 813872093
+1105387678 1431142475 220570551 4243632715 4179591855
+2607469131 3090613241 282341803 1734241730 1391822177
+1001254810 827927915 1886687171 3935097347 2631788714
+3905163266 110554195 2447955646 3717202975 3304793075
+3739614479 3059127468 953919171 2590123714 1132511021
+3795593679 2788030429 982155079 3472349556 859942552
+2681007391 2299624053 647443547 233600422 608168955
+3689327453 1849778220 1608438222 3968158357 2692977776
+2851872572 246750393 3582818628 3329652309 4036366910
+1012970930 950780808 3959768744 2538550045 191422718
+2658142375 3276369011 2927737484 1234200027 1920815603
+3536074689 1535612501 2184142071 3276955054 428488088
+2378411984 4059769550 3913744741 2732139246 64369859
+3755670074 842839565 2819894466 2414718973 1010060670
+1839715346 2410311136 152774329 3485009480 4102101512
+2852724304 879944024 1785007662 2748284463 1354768064
+3267784736 2269127717 3001240761 3179796763 895723219
+ 865924942 4291570937 89355264 1471026971 4114180745
+3201939751 2867476999 2460866060 3603874571 2238880432
+3308416168 2072246611 2755653839 3773737248 1709066580
+4282731467 2746170170 2832568330 433439009 3175778732
+ 26248366 2551382801 183214346 3893339516 1928168445
+1337157619 3429096554 3275170900 1782047316 4264403756
+1876594403 4289659572 3223834894 1728705513 4068244734
+2867840287 1147798696 302879820 1730407747 1923824407
+1180597908 1569786639 198796327 560793173 2107345620
+2705990316 3448772106 3678374155 758635715 884524671
+ 486356516 1774865603 3881226226 2635213607 1181121587
+1508809820 3178988241 1594193633 1235154121 326117244
+2304031425 937054774 2687415945 3192389340 2003740439
+1823766188 2759543402 10067710 1533252662 4132494984
+ 82378136 420615890 3467563163 541562091 3535949864
+2277319197 3330822853 3215654174 4113831979 4204996991
+2162248333 3255093522 2219088909 2978279037 255818579
+2859348628 3097280311 2569721123 1861951120 2907080079
+2719467166 998319094 2521935127 2404125338 259456032
+2086860995 1839848496 1893547357 2527997525 1489393124
+2860855349 76448234 2264934035 744914583 2586791259
+1385380501 66529922 1819103258 1899300332 2098173828
+1793831094 276463159 360132945 4178212058 595015228
+ 177071838 2800080290 1573557746 1548998935 378454223
+1460534296 1116274283 3112385063 3709761796 827999348
+3580042847 1913901014 614021289 4278528023 1905177404
+ 45407939 3298183234 1184848810 3644926330 3923635459
+1627046213 3677876759 969772772 1160524753 1522441192
+ 452369933 1527502551 832490847 1003299676 1071381111
+2891255476 973747308 4086897108 1847554542 3895651598
+2227820339 1621250941 2881344691 3583565821 3510404498
+ 849362119 862871471 797858058 2867774932 2821282612
+3272403146 3997979905 209178708 1805135652 6783381
+2823361423 792580494 4263749770 776439581 3798193823
+2853444094 2729507474 1071873341 1329010206 1289336450
+3327680758 2011491779 80157208 922428856 1158943220
+1667230961 2461022820 2608845159 387516115 3345351910
+1495629111 4098154157 3156649613 3525698599 4134908037
+ 446713264 2137537399 3617403512 813966752 1157943946
+3734692965 1680301658 3180398473 3509854711 2228114612
+1008102291 486805123 863791847 3189125290 1050308116
+3777341526 4291726501 844061465 1347461791 2826481581
+ 745465012 2055805750 4260209475 2386693097 2980646741
+ 447229436 2077782664 1232942813 4023002732 1399011509
+3140569849 2579909222 3794857471 900758066 2887199683
+1720257997 3367494931 2668921229 955539029 3818726432
+1105704962 3889207255 2277369307 2746484505 1761846513
+2413916784 2685127085 4240257943 1166726899 4215215715
+3082092067 3960461946 1663304043 2087473241 4162589986
+2507310778 1579665506 767234210 970676017 492207530
+1441679602 1314785090 3262202570 3417091742 1561989210
+3011406780 1146609202 3262321040 1374872171 1634688712
+1280458888 2230023982 419323804 3262899800 39783310
+1641619040 1700368658 2207946628 2571300939 2424079766
+ 780290914 2715195096 3390957695 163151474 2309534542
+1860018424 555755123 280320104 1604831083 2713022383
+1728987441 3639955502 623065489 3828630947 4275479050
+3516347383 2343951195 2430677756 635534992 3868699749
+ 808442435 3070644069 4282166003 2093181383 2023555632
+1568662086 3422372620 4134522350 3016979543 3259320234
+2888030729 3185253876 4258779643 1267304371 1022517473
+ 815943045 929020012 2995251018 3371283296 3608029049
+2018485115 122123397 2810669150 1411365618 1238391329
+1186786476 3155969091 2242941310 1765554882 279121160
+4279838515 1641578514 3796324015 13351065 103516986
+1609694427 551411743 2493771609 1316337047 3932650856
+4189700203 463397996 2937735066 1855616529 2626847990
+ 55091862 3823351211 753448970 4045045500 1274127772
+1124182256 92039808 2126345552 425973257 386287896
+2589870191 1987762798 4084826973 2172456685 3366583455
+3602966653 2378803535 2901764433 3716929006 3710159000
+2653449155 3469742630 3096444476 3932564653 2595257433
+ 318974657 3146202484 853571438 144400272 3768408841
+ 782634401 2161109003 570039522 1886241521 14249488
+2230804228 1604941699 3928713335 3921942509 2155806892
+ 134366254 430507376 1924011722 276713377 196481886
+3614810992 1610021185 1785757066 851346168 3761148643
+2918835642 3364422385 3012284466 3735958851 2643153892
+3778608231 1164289832 205853021 2876112231 3503398282
+3078397001 3472037921 1748894853 2740861475 316056182
+1660426908 168885906 956005527 3984354789 566521563
+1001109523 1216710575 2952284757 3834433081 3842608301
+2467352408 3974441264 3256601745 1409353924 1329904859
+2307560293 3125217879 3622920184 3832785684 3882365951
+2308537115 2659155028 1450441945 3532257603 3186324194
+1225603425 1124246549 175808705 3009142319 2796710159
+3651990107 160762750 1902254979 1698648476 1134980669
+ 497144426 3302689335 4057485630 3603530763 4087252587
+ 427812652 286876201 823134128 1627554964 3745564327
+2589226092 4202024494 62878473 3275585894 3987124064
+2791777159 1916869511 2585861905 1375038919 1403421920
+ 60249114 3811870450 3021498009 2612993202 528933105
+2757361321 3341402964 2621861700 273128190 4015252178
+3094781002 1621621288 2337611177 1796718448 1258965619
+4241913140 2138560392 3022190223 4174180924 450094611
+3274724580 617150026 2704660665 1469700689 1341616587
+ 356715071 1188789960 2278869135 1766569160 2795896635
+ 57824704 2893496380 1235723989 1630694347 3927960522
+ 428891364 1814070806 2287999787 4125941184 3968103889
+3548724050 1025597707 1404281500 2002212197 92429143
+2313943944 2403086080 3006180634 3561981764 1671860914
+1768520622 1803542985 844848113 3006139921 1410888995
+1157749833 2125704913 1789979528 1799263423 741157179
+2405862309 767040434 2655241390 3663420179 2172009096
+2511931187 1680542666 231857466 1154981000 157168255
+1454112128 3505872099 1929775046 2309422350 2143329496
+2960716902 407610648 2938108129 2581749599 538837155
+2342628867 430543915 740188568 1937713272 3315215132
+2085587024 4030765687 766054429 3517641839 689721775
+1294158986 1753287754 4202601348 1974852792 33459103
+3568087535 3144677435 1686130825 4134943013 3005738435
+3599293386 426570142 754104406 3660892564 1964545167
+ 829466833 821587464 1746693036 1006492428 1595312919
+1256599985 1024482560 1897312280 2902903201 691790057
+1037515867 3176831208 1968401055 2173506824 1089055278
+1748401123 2941380082 968412354 1818753861 2973200866
+3875951774 1119354008 3988604139 1647155589 2232450826
+3486058011 3655784043 3759258462 847163678 1082052057
+ 989516446 2871541755 3196311070 3929963078 658187585
+3664944641 2175149170 2203709147 2756014689 2456473919
+3890267390 1293787864 2830347984 3059280931 4158802520
+1561677400 2586570938 783570352 1355506163 31495586
+3789437343 3340549429 2092501630 896419368 671715824
+3530450081 3603554138 1055991716 3442308219 1499434728
+3130288473 3639507000 17769680 2259741420 487032199
+4227143402 3693771256 1880482820 3924810796 381462353
+4017855991 2452034943 2736680833 2209866385 2128986379
+ 437874044 595759426 641721026 1636065708 3899136933
+ 629879088 3591174506 351984326 2638783544 2348444281
+2341604660 2123933692 143443325 1525942256 364660499
+ 599149312 939093251 1523003209 106601097 376589484
+1346282236 1297387043 764598052 3741218111 933457002
+1886424424 3219631016 525405256 3014235619 323149677
+2038881721 4100129043 2851715101 2984028078 1888574695
+2014194741 3515193880 4180573530 3461824363 2641995497
+3179230245 2902294983 2217320456 4040852155 1784656905
+3311906931 87498458 2752971818 2635474297 2831215366
+3682231106 2920043893 3772929704 2816374944 309949752
+2383758854 154870719 385111597 1191604312 1840700563
+ 872191186 2925548701 1310412747 2102066999 1504727249
+3574298750 1191230036 3330575266 3180292097 3539347721
+ 681369118 3305125752 3648233597 950049240 4173257693
+1760124957 512151405 681175196 580563018 1169662867
+4015033554 2687781101 699691603 2673494188 1137221356
+ 123599888 472658308 1053598179 1012713758 3481064843
+3759461013 3981457956 3830587662 1877191791 3650996736
+ 988064871 3515461600 4089077232 2225147448 1249609188
+2643151863 3896204135 2416995901 1397735321 3460025646
diff --git a/cesar/lib/test/rnd/src/test_rnd.c b/cesar/lib/test/rnd/src/test_rnd.c
new file mode 100644
index 0000000000..ae460d5d97
--- /dev/null
+++ b/cesar/lib/test/rnd/src/test_rnd.c
@@ -0,0 +1,164 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file test_rnd.c
+ * \brief Test the random number generator.
+ * \ingroup lib_test
+ *
+ * \todo Replace all the stdio calls to the future test library calls.
+ */
+#include "common/std.h"
+#include "lib/rnd.h"
+#include "lib/test.h"
+
+#include "mt19937ar.out.h"
+
+#include <string.h>
+
+void
+rnd_basic_test_case (test_t t)
+{
+ uint i;
+ u32 r;
+ lib_rnd_t ctx;
+ static const u32 init[4] = { 0x123, 0x234, 0x345, 0x456 };
+ test_case_begin (t, "basic");
+ /* Initialise. */
+ lib_rnd_init_by_array (&ctx, init, COUNT (init));
+ test_begin (t, "compare")
+ {
+ /* Compare with original Mersenne Twister code. */
+ for (i = 0; i < COUNT (data); i++)
+ {
+ r = lib_rnd32 (&ctx);
+ test_fail_unless (r == data[i], "i=%u, r=%u", i, r);
+ }
+ } test_end;
+ /* Check lib_rnd_uniform bound. */
+ test_begin (t, "bound")
+ {
+ bool ub, ut;
+ ub = ut = false;
+ for (i = 0; i < 100000; i++)
+ {
+ r = lib_rnd_uniform (&ctx, 1000);
+ test_fail_if (r >= 1000, "bound overflow i=%u", i);
+ if (r == 0) ub = true;
+ if (r == 999) ut = true;
+ }
+ test_fail_if (!ub || !ut, "bound not hit");
+ } test_end;
+ /* Check lib_rnd_uniform uniformity. */
+ test_begin (t, "uniformity")
+ {
+ int less, more;
+ less = more = 0;
+ for (i = 0; i < 100000; i++)
+ {
+ r = lib_rnd_uniform (&ctx, 0xc0000000);
+ if (r > 0xc0000000 / 2)
+ more++;
+ else
+ less++;
+ }
+ test_fail_if (ABS (more - less) > 1000,
+ "not uniform more=%d, less=%d", more, less);
+ } test_end;
+}
+
+void
+rnd_buf_run (test_t t, lib_rnd_t *rnd, u8 *buf1, u8 *buf2, uint pos,
+ uint size)
+{
+ test_within (t);
+ uint i;
+ dbg_assert_ptr (buf1);
+ dbg_assert_ptr (buf2);
+ /* Generate two buffers. */
+ lib_rnd_buffer (rnd, buf1 + pos, size);
+ lib_rnd_buffer (rnd, buf2 + pos, size);
+ /* Consider that the two buffers should have different content (or tweak
+ * lib_rnd_init until this is true!). */
+ for (i = pos; i < pos + size; i++)
+ test_fail_if (buf1[i] == buf2[i], "improbable match at %d", i);
+}
+
+void
+rnd_buf_test_case (test_t t)
+{
+ u8 buf[2][256];
+ uint pos, size;
+ lib_rnd_t rnd[1];
+ test_case_begin (t, "buf");
+ /* Initialise. */
+ lib_rnd_init (rnd, 12345);
+ memset (buf, 0x42, sizeof (buf));
+ pos = 0;
+ test_begin (t, "padded buffer")
+ {
+ size = 64;
+ dbg_assert ((pos & 3) == 0 && ((pos + size) & 3) == 0);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+ test_begin (t, "unpadded end")
+ {
+ size = 63;
+ dbg_assert ((pos & 3) == 0 && ((pos + size) & 3) == 3);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+ test_begin (t, "unpadded both")
+ {
+ size = 63;
+ dbg_assert ((pos & 3) == 3 && ((pos + size) & 3) == 2);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+ test_begin (t, "small")
+ {
+ size = 3;
+ dbg_assert ((pos & 3) == 2 && ((pos + size) & 3) == 1
+ && pos / 4 + 1 == (pos + size) / 4);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+ test_begin (t, "tiny")
+ {
+ size = 2;
+ dbg_assert ((pos & 3) == 1 && ((pos + size) & 3) == 3
+ && pos / 4 == (pos + size) / 4);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+ test_begin (t, "unpadded begin")
+ {
+ size = COUNT (buf[0]) - pos;
+ dbg_assert ((pos & 3) == 3 && ((pos + size) & 3) == 0);
+ rnd_buf_run (t, rnd, buf[0], buf[1], pos, size);
+ } test_end;
+ pos += size;
+}
+
+void
+rnd_test_suite (test_t t)
+{
+ test_suite_begin (t, "rnd");
+ rnd_basic_test_case (t);
+ rnd_buf_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ rnd_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/set/Makefile b/cesar/lib/test/set/Makefile
new file mode 100644
index 0000000000..7d601a0a1d
--- /dev/null
+++ b/cesar/lib/test/set/Makefile
@@ -0,0 +1,8 @@
+BASE = ../../..
+
+
+HOST_PROGRAMS = test_set
+test_set_SOURCES = test_set.c
+test_set_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/set/src/test_set.c b/cesar/lib/test/set/src/test_set.c
new file mode 100644
index 0000000000..971c699cec
--- /dev/null
+++ b/cesar/lib/test/set/src/test_set.c
@@ -0,0 +1,362 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_set.c
+ * \brief Set test.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/test.h"
+#include "lib/rnd.h"
+#include "lib/set.h"
+
+#define DEBUG_PRINT 0
+
+#if DEBUG_PRINT
+# include <stdio.h>
+# define NB_NODES 8
+# define NB_ITER 100
+# define NOT_TOO_BIG(x) ((x) & (NB_NODES * 2 - 1))
+#elif DEBUG
+# define NB_NODES 16384
+# define NB_ITER 10000
+# define NOT_TOO_BIG(x) (x)
+#else
+# define NB_NODES 131072
+# define NB_ITER 1000000
+# define NOT_TOO_BIG(x) (x)
+#endif
+
+/** Set node with an u32 data. */
+struct set_node_u32_t
+{
+ set_node_t node;
+ u32 data;
+};
+typedef struct set_node_u32_t set_node_u32_t;
+
+/**
+ * Comparison function for an u32 node.
+ * \param l left hand node
+ * \param r right hand node
+ * \return true iff left is lesser than right
+ */
+static bool
+set_u32_less (set_node_t *l, set_node_t *r)
+{
+ u32 ld, rd;
+ dbg_assert (l);
+ dbg_assert (r);
+ ld = PARENT_OF (set_node_u32_t, node, l)->data;
+ rd = PARENT_OF (set_node_u32_t, node, r)->data;
+ return ld < rd;
+}
+
+/**
+ * Check a node and recurse.
+ * \param t test context
+ * \param node node to check
+ * \param depth tree depth
+ * \param less comparison function
+ * \return tree size
+ */
+static uint
+set_check_node (test_t t, set_node_t *node, uint *depth, set_node_less_t less)
+{
+ test_within (t);
+ uint size, ldepth, rdepth;
+ dbg_assert (node && node != node->left);
+ dbg_assert (depth);
+ /* AA-tree structure checks. */
+ test_fail_unless (node->level > node->right->right->level);
+ test_fail_unless (node->level == node->left->level + 1);
+ test_fail_unless (node->level == node->right->level
+ || node->level == node->right->level + 1);
+ test_fail_unless (node->left == node->left->left
+ || node->left->father == node);
+ test_fail_unless (node->right == node->right->left
+ || node->right->father == node);
+ /* Binary search tree checks & stats. */
+ dbg_assert (node->left);
+ dbg_assert (node->right);
+ size = 1;
+ ldepth = rdepth = 0;
+ if (node->left != node->left->left)
+ {
+ test_fail_unless (less (node->left, node));
+ size += set_check_node (t, node->left, &ldepth, less);
+ }
+ if (node->right != node->right->left)
+ {
+ test_fail_unless (less (node, node->right));
+ size += set_check_node (t, node->right, &rdepth, less);
+ }
+ *depth = MAX (ldepth, rdepth) + 1;
+ return size;
+}
+
+/**
+ * Recursively check a tree.
+ * \param t test context
+ * \param set the tree to check
+ * \param depth tree depth
+ * \return tree size
+ */
+uint
+set_check (test_t t, set_t *set, uint *depth)
+{
+ test_within (t);
+ dbg_assert (set);
+ dbg_assert (depth);
+ if (set->root == set->root->left)
+ {
+ *depth = 0;
+ test_fail_unless (set_empty (set));
+ return 0;
+ }
+ else
+ {
+ test_fail_unless (!set_empty (set));
+ return set_check_node (t, set->root, depth, set->less);
+ }
+}
+
+#if DEBUG_PRINT
+
+/**
+ * Travel the tree at the \a p depth and print.
+ * \param node current node
+ * \param size number of characters available
+ * \param p depth to print
+ * \param c current recursion depth
+ */
+static void
+set_print_nodes (set_node_t *node, uint size, uint p, uint c)
+{
+ dbg_assert (node);
+ dbg_assert (p >= c);
+ if (p == c)
+ {
+ if (node->left == node)
+ {
+ printf ("%*s", size, " ");
+ }
+ else
+ {
+ uint before = (size + 1) / 2;
+ uint after = size - before;
+ uint data = PARENT_OF (set_node_u32_t, node, node)->data;
+ uint level = node->level;
+ /* ASCII codes... */
+#define GREY "\e[30;1m"
+#define BLACK "\e[0m"
+ printf ("%*u" GREY "%-*u" BLACK, before, data, after, level);
+ }
+ }
+ else
+ {
+ set_print_nodes (node->left, size, p, c + 1);
+ set_print_nodes (node->right, size, p, c + 1);
+ }
+}
+
+/**
+ * Print a tree.
+ * \param set tree to print
+ * \param depth tree depth
+ */
+static void
+set_print (set_t *set, uint depth)
+{
+ uint i, size;
+ dbg_assert (set);
+ printf ("depth: %d\n", depth);
+ /* Screen size is limited... */
+ size = 4 << (MIN (depth, 5u) - 1);
+ for (i = 0; i < MIN (depth, 6u); i++)
+ {
+ set_print_nodes (set->root, size / (1 << i), i, 0);
+ printf ("\n");
+ }
+ if (depth > 6u)
+ printf ("...\n");
+}
+
+#else /* !DEBUG_PRINT */
+# define set_print(set, depth)
+#endif /* !DEBUG_PRINT */
+
+void
+set_basic_test_case (test_t t)
+{
+ uint i, maxdepth, depth;
+ u64 sumdepth;
+ lib_rnd_t rnd[1];
+ set_t set;
+ set_node_u32_t nodes[NB_NODES];
+ test_case_begin (t, "basic");
+ lib_rnd_init (rnd, 1234);
+ /* Initialise. */
+ set_init (&set, set_u32_less);
+ for (i = 0; i < COUNT (nodes) / 2; i++)
+ {
+ set_node_init (&nodes[i].node);
+ nodes[i].data = i;
+ }
+ for (; i < COUNT (nodes); i++)
+ {
+ set_node_init (&nodes[i].node);
+ /* Ensure there is no duplicates. */
+ nodes[i].data = NOT_TOO_BIG ((lib_rnd32 (rnd) & ~(NB_NODES - 1)) | i);
+ }
+ /* Empty set tests. */
+ test_begin (t, "empty")
+ {
+ test_fail_unless (set_empty (&set));
+ test_fail_unless (set_begin (&set) == set_end (&set));
+ } test_end;
+ /* Insert. */
+ test_begin (t, "insert")
+ {
+ maxdepth = 0;
+ test_fail_unless (0 == set_check (t, &set, &depth));
+ for (i = 0; i < COUNT (nodes); i++)
+ {
+ test_fail_unless (set_insert (&set, &nodes[i].node));
+ if (DEBUG)
+ {
+ test_fail_unless (i + 1 == set_check (t, &set, &depth));
+ if (maxdepth < depth)
+ maxdepth = depth;
+ }
+ set_print (&set, depth);
+ }
+ if (DEBUG)
+ test_verbose_print ("count = %d, max depth = %d", COUNT (nodes),
+ maxdepth);
+ } test_end;
+ /* Insert duplicates, should be denied. */
+ test_begin (t, "insert dup")
+ {
+ set_node_u32_t dup;
+ set_node_init (&dup.node);
+ for (i = 0; i < COUNT (nodes); i++)
+ {
+ dup.data = nodes[i].data;
+ test_fail_if (set_insert (&set, &dup.node), "dup accepted");
+ }
+ } test_end;
+ /* Find nodes. */
+ test_begin (t, "find nodes")
+ {
+ for (i = 0; i < NB_ITER; i++)
+ {
+ set_node_u32_t k;
+ set_node_init (&k.node);
+ if (lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.5)))
+ {
+ /* Node in the set. */
+ set_node_u32_t *n;
+ n = &nodes[lib_rnd_uniform (rnd, NB_NODES)];
+ k.data = n->data;
+ test_fail_unless (set_find (&set, &k.node) == &n->node);
+ }
+ else
+ {
+ /* Node not in the set. */
+ u32 r;
+ do {
+ r = lib_rnd32 (rnd);
+ } while (nodes[r & (NB_NODES - 1)].data == r);
+ k.data = r;
+ test_fail_unless (set_find (&set, &k.node) == NULL);
+ }
+ }
+ } test_end;
+ /* Now, remove a node and insert it back. */
+ test_begin (t, "remove and insert")
+ {
+ sumdepth = 0;
+ maxdepth = 0;
+ for (i = 0; i < NB_ITER; i++)
+ {
+ set_node_u32_t *n, k;
+ u32 r;
+ set_node_init (&k.node);
+ n = &nodes[lib_rnd_uniform (rnd, NB_NODES)];
+ do {
+ r = NOT_TOO_BIG (lib_rnd32 (rnd));
+ k.data = r;
+ } while (set_find (&set, &k.node));
+ /* Remove. */
+ set_remove (&set, &n->node);
+ if (DEBUG)
+ {
+ test_fail_unless (COUNT (nodes) - 1
+ == set_check (t, &set, &depth));
+ if (maxdepth < depth)
+ maxdepth = depth;
+ }
+ set_print (&set, depth);
+ /* Insert. */
+ n->data = r;
+ test_fail_unless (set_insert (&set, &n->node));
+ if (DEBUG)
+ {
+ test_fail_unless (COUNT (nodes)
+ == set_check (t, &set, &depth));
+ if (maxdepth < depth)
+ maxdepth = depth;
+ sumdepth += depth;
+ }
+ set_print (&set, depth);
+ }
+ if (DEBUG)
+ test_verbose_print ("count = %d, avg depth = %d, max depth = %d",
+ COUNT (nodes), (uint) (sumdepth / NB_ITER),
+ maxdepth);
+ } test_end;
+ /* Check order. */
+ test_begin (t, "order")
+ {
+ uint size, last, data;
+ set_node_t *n;
+ n = set_begin (&set);
+ test_fail_if (!n || n == set_end (&set));
+ last = PARENT_OF (set_node_u32_t, node, n)->data;
+ size = 1;
+ for (n = set_next (&set, n); n != set_end (&set);
+ n = set_next (&set, n))
+ {
+ test_fail_unless (n);
+ data = PARENT_OF (set_node_u32_t, node, n)->data;
+ test_fail_unless (last < data, "broken order");
+ last = data;
+ size++;
+ }
+ test_fail_unless (size == COUNT (nodes));
+ } test_end;
+}
+
+void
+set_test_suite (test_t t)
+{
+ test_suite_begin (t, "set");
+ set_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ set_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/test/Config b/cesar/lib/test/test/Config
new file mode 100644
index 0000000000..1221024089
--- /dev/null
+++ b/cesar/lib/test/test/Config
@@ -0,0 +1 @@
+CONFIG_DEBUG_FATAL_CATCH = y
diff --git a/cesar/lib/test/test/Makefile b/cesar/lib/test/test/Makefile
new file mode 100644
index 0000000000..9d10954870
--- /dev/null
+++ b/cesar/lib/test/test/Makefile
@@ -0,0 +1,8 @@
+BASE = ../../..
+
+
+HOST_PROGRAMS = test_test
+test_test_SOURCES = test_test.c
+test_test_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/test/src/test_test.c b/cesar/lib/test/test/src/test_test.c
new file mode 100644
index 0000000000..78409f8f5d
--- /dev/null
+++ b/cesar/lib/test/test/src/test_test.c
@@ -0,0 +1,90 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_test.c
+ * \brief Test the test infrastructure.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/test.h"
+
+void
+mytest_basic_test_case (test_t t)
+{
+ uint i;
+ test_case_begin (t, "basic");
+ test_begin (t, "test no fail")
+ {
+ } test_end;
+ test_begin (t, "test_fail_unless ok")
+ {
+ test_fail_unless (1 == 1);
+ } test_end;
+ test_begin (t, "test_fail_unless nok")
+ {
+ test_fail_unless (1 == 2);
+ } test_end;
+ test_begin (t, "test_fail_if print nok")
+ {
+ test_fail_if (1 != 2, "%d is not %d", 1, 2);
+ } test_end;
+ test_begin (t, "test_fail_if loop nok after 5")
+ {
+ for (i = 0; i < 10; i++)
+ {
+ test_verbose_print ("i = %d", i);
+ if (i > 5)
+ test_fail ("fail at i = %d", i);
+ }
+ } test_end;
+ test_begin (t, "catch assert")
+ {
+ dbg_fatal_try_begin
+ {
+ dbg_assert (1 == 2);
+ }
+ dbg_fatal_try_catch (const char *the_error)
+ {
+ test_verbose_print ("catched: %s", the_error);
+ }
+ dbg_fatal_try_end;
+ } test_end;
+ test_begin (t, "no catch assert")
+ {
+ dbg_assert (1 == 2);
+ } test_end;
+ test_begin (t, "division by zero")
+ {
+ uint a = 1;
+ uint b = 0;
+ uint c = a / b;
+ test_verbose_print ("can you compute this: %d", c);
+ } test_end;
+ test_begin (t, "segmentation fault")
+ {
+ *(u32 *) NULL = 0;
+ } test_end;
+}
+
+void
+mytest_test_suite (test_t t)
+{
+ test_suite_begin (t, "mytest");
+ mytest_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ mytest_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/trace/Config b/cesar/lib/test/trace/Config
new file mode 100644
index 0000000000..79839151a9
--- /dev/null
+++ b/cesar/lib/test/trace/Config
@@ -0,0 +1,2 @@
+CONFIG_DEBUG_MORE = y
+CONFIG_TRACE = y
diff --git a/cesar/lib/test/trace/Makefile b/cesar/lib/test/trace/Makefile
new file mode 100644
index 0000000000..475dfc75b9
--- /dev/null
+++ b/cesar/lib/test/trace/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_trace
+test_trace_SOURCES = test_trace.c
+test_trace_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/trace/src/test_trace.c b/cesar/lib/test/trace/src/test_trace.c
new file mode 100644
index 0000000000..c2d1f0e8ff
--- /dev/null
+++ b/cesar/lib/test/trace/src/test_trace.c
@@ -0,0 +1,302 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_trace.c
+ * \brief Trace test.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/rnd.h"
+#include "lib/trace.h"
+#include "lib/test.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+#define NB_ITER 100000
+#define MAX_ARGS 8
+
+#define NB_PRINT 1000
+
+struct growing_buffer_t
+{
+ char *buf;
+ uint buf_pos;
+ uint buf_size;
+};
+typedef struct growing_buffer_t growing_buffer_t;
+
+int
+dump_callback (void *user, char *text, uint text_size)
+{
+ dbg_assert (user);
+ dbg_assert (text && text_size != 0);
+ growing_buffer_t *g = user;
+ /* Grow buffer if necessary. */
+ if (g->buf_pos + text_size >= g->buf_size)
+ {
+ g->buf_size = (g->buf_size + text_size) * 2;
+ g->buf = realloc (g->buf, g->buf_size);
+ dbg_assert (g->buf);
+ }
+ /* Copy data. */
+ memcpy (g->buf + g->buf_pos, text, text_size);
+ g->buf_pos += text_size;
+ return text_size;
+}
+
+void
+growing_sprintf (growing_buffer_t *g, const char *fmt, ...)
+{
+ dbg_assert (g);
+ dbg_assert (fmt);
+ va_list ap;
+ va_start (ap, fmt);
+ do
+ {
+ int ret = vsnprintf (g->buf + g->buf_pos, g->buf_size - g->buf_pos,
+ fmt, ap);
+ // Hum, ecos does not use the same meaning for the return value.
+ if (ret != -1 && ret < (int) (g->buf_size - g->buf_pos - 1))
+ {
+ g->buf_pos += ret;
+ break;
+ }
+ else
+ {
+ g->buf_size *= 2;
+ g->buf = realloc (g->buf, g->buf_size);
+ dbg_assert (g->buf);
+ }
+ } while (1);
+}
+
+void
+trace_basic_test_case (test_t t)
+{
+ uint i, j;
+ lib_rnd_t rnd[1];
+ trace_buffer_t trace_buf[3];
+ uint sum[COUNT (trace_buf)] = { };
+ /* Initialise. */
+ test_case_begin (t, "basic");
+ lib_rnd_init (rnd, 1234);
+ trace_init ();
+ /* Test. */
+ test_begin (t, "massive print and drop")
+ {
+ trace_buffer_add (&trace_buf[0], "one", 2, 4, true, NULL);
+ trace_buffer_add (&trace_buf[1], "two", 2, 1, false, NULL);
+ trace_buffer_add (&trace_buf[2], "three", 0, 1, false, NULL);
+ for (i = 0; i < NB_ITER; i++)
+ {
+ if (lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.995)))
+ {
+ /* Trace. */
+ uint buf_i = lib_rnd_uniform (rnd, COUNT (trace_buf));
+ uint arg_nb = lib_rnd_uniform (rnd, MAX_ARGS + 1);
+ uint args[MAX_ARGS];
+ for (j = 0; j < arg_nb; j++)
+ args[j] = lib_rnd32 (rnd);
+ /* trace_(fast_)?print\d (...) {{{ */
+ if (trace_buf[buf_i].locked)
+ {
+ switch (arg_nb)
+ {
+ case 8:
+ trace_fast_print8 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2], args[3], args[4],
+ args[5], args[6], args[7]);
+ break;
+ case 7:
+ trace_fast_print7 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2], args[3], args[4],
+ args[5], args[6]);
+ break;
+ case 6:
+ trace_fast_print6 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2], args[3], args[4],
+ args[5]);
+ break;
+ case 5:
+ trace_fast_print5 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2], args[3],
+ args[4]);
+ break;
+ case 4:
+ trace_fast_print4 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2], args[3]);
+ break;
+ case 3:
+ trace_fast_print3 (&trace_buf[buf_i], i, args[0],
+ args[1], args[2]);
+ break;
+ case 2:
+ trace_fast_print2 (&trace_buf[buf_i], i, args[0],
+ args[1]);
+ break;
+ case 1:
+ trace_fast_print1 (&trace_buf[buf_i], i, args[0]);
+ break;
+ case 0:
+ trace_fast_print0 (&trace_buf[buf_i], i);
+ break;
+ }
+ }
+ else
+ {
+ switch (arg_nb)
+ {
+ case 8:
+ trace_print8 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2], args[3], args[4], args[5],
+ args[6], args[7]);
+ break;
+ case 7:
+ trace_print7 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2], args[3], args[4], args[5],
+ args[6]);
+ break;
+ case 6:
+ trace_print6 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2], args[3], args[4], args[5]);
+ break;
+ case 5:
+ trace_print5 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2], args[3], args[4]);
+ break;
+ case 4:
+ trace_print4 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2], args[3]);
+ break;
+ case 3:
+ trace_print3 (&trace_buf[buf_i], i, args[0], args[1],
+ args[2]);
+ break;
+ case 2:
+ trace_print2 (&trace_buf[buf_i], i, args[0], args[1]);
+ break;
+ case 1:
+ trace_print1 (&trace_buf[buf_i], i, args[0]);
+ break;
+ case 0:
+ trace_print0 (&trace_buf[buf_i], i);
+ break;
+ }
+ }
+ /* }}} */
+ }
+ else
+ {
+ /* Drop chunks. */
+ uint chunks_nb = 0;
+ for (j = 0; j < COUNT (trace_buf); j++)
+ {
+ dbg_assert (trace_buf[j].chunks_nb
+ >= trace_buf[j].preload);
+ chunks_nb += trace_buf[j].chunks_nb
+ - trace_buf[j].preload;
+ }
+ trace_drop_chunks (1 + (chunks_nb > 30
+ ? lib_rnd_uniform (rnd, chunks_nb - 30)
+ : 0));
+ }
+ for (j = 0; j < COUNT (trace_buf); j++)
+ sum[j] += trace_buf[j].chunks_nb;
+ }
+ test_verbose_print ("avg chunks_nb: %d %d %d",
+ sum[0] / NB_ITER,
+ sum[1] / NB_ITER,
+ sum[2] / NB_ITER);
+ trace_buffer_remove (&trace_buf[0]);
+ trace_buffer_remove (&trace_buf[1]);
+ trace_buffer_remove (&trace_buf[2]);
+ } test_end;
+ test_begin (t, "dump")
+ {
+#define TEST_TRACE_TRACE(id, args...) \
+ TRACE_SHORT (TEST_TRACE_TRACE_, &trace_buf[0], id, ## args)
+ enum
+ {
+ TEST_TRACE_TRACE_ONE,
+ TEST_TRACE_TRACE_TWO,
+ TEST_TRACE_TRACE_THREE,
+ TEST_TRACE_TRACE_FOUR,
+ };
+ static const trace_event_id_t ei[] = {
+ TRACE_EVENT (TEST_TRACE_TRACE_ONE, "one"),
+ TRACE_EVENT (TEST_TRACE_TRACE_TWO, "two %d", TIMESTAMP),
+ TRACE_EVENT (TEST_TRACE_TRACE_THREE, "three %u, %x, %d, %b, %b"),
+ TRACE_EVENT (TEST_TRACE_TRACE_FOUR, "four %m, %d"),
+ };
+ trace_namespace_t ns;
+ trace_namespace_init (&ns, ei, COUNT (ei));
+ trace_buffer_add (&trace_buf[0], "trace", 2, 1, false, &ns);
+ /* Trace and record expected trace. */
+ growing_buffer_t expected_dump;
+ expected_dump.buf_pos = 0;
+ expected_dump.buf_size = 100;
+ expected_dump.buf = malloc (expected_dump.buf_size);
+ dbg_assert (expected_dump.buf);
+ for (i = 0; i < NB_PRINT; i++)
+ {
+ trace_print0 (&trace_buf[0], TEST_TRACE_TRACE_ONE);
+ growing_sprintf (&expected_dump, "[.] one\n");
+ TEST_TRACE_TRACE (TWO, 0x123abc ^ i, -123456789 + i);
+ growing_sprintf (&expected_dump, "[0x%08x] two %d\n",
+ 0x123abc ^ i, -123456789 + i);
+ TEST_TRACE_TRACE (THREE, 123456789 - i, 0xabcdef ^ i, 1, true,
+ false);
+ growing_sprintf (&expected_dump,
+ "[.] three %u, 0x%08x, %d, %s, %s\n",
+ 123456789 - i, 0xabcdef ^ i, 1, "true", "false");
+ TEST_TRACE_TRACE (FOUR, TRACE_U64 (0xbc9a78563412LL), i);
+ growing_sprintf (&expected_dump,
+ "[.] four 12:34:56:78:9a:bc, %d\n", i);
+ }
+ test_verbose_print ("%s", expected_dump.buf);
+ /* Dump to memory. */
+ growing_buffer_t dump;
+ dump.buf_pos = 0;
+ dump.buf_size = 100;
+ dump.buf = malloc (dump.buf_size);
+ dbg_assert (dump.buf);
+ int ret = trace_buffer_dump (&trace_buf[0], dump_callback, &dump);
+ /* Check dump. */
+ test_fail_unless (expected_dump.buf_pos == dump.buf_pos);
+ test_fail_unless ((int) dump.buf_pos == ret);
+ test_fail_unless (memcmp (expected_dump.buf, dump.buf, dump.buf_pos)
+ == 0);
+ /* Cleanup. */
+ free (expected_dump.buf);
+ free (dump.buf);
+ trace_buffer_remove (&trace_buf[0]);
+ } test_end;
+ /* Uninit. */
+ trace_uninit ();
+}
+
+void
+trace_test_suite (test_t t)
+{
+ test_suite_begin (t, "trace");
+ trace_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ trace_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/try/Makefile b/cesar/lib/test/try/Makefile
new file mode 100644
index 0000000000..dd7f7b4771
--- /dev/null
+++ b/cesar/lib/test/try/Makefile
@@ -0,0 +1,8 @@
+BASE = ../../..
+
+
+HOST_PROGRAMS = test_try
+test_try_SOURCES = test_try.c
+test_try_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/try/src/test_try.c b/cesar/lib/test/try/src/test_try.c
new file mode 100644
index 0000000000..86a843da5b
--- /dev/null
+++ b/cesar/lib/test/try/src/test_try.c
@@ -0,0 +1,125 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_try.c
+ * \brief Test light exception support.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/try.h"
+#include "lib/test.h"
+
+void
+try_basic_test_case (test_t t)
+{
+ test_case_begin (t, "basic");
+ test_begin (t, "no throw")
+ {
+ bool always = false;
+ try_begin
+ {
+ }
+ try_catch (100)
+ {
+ test_fail ("threw");
+ }
+ try_end;
+ try_begin
+ {
+ }
+ try_catch (100)
+ {
+ test_fail ("threw");
+ }
+ try_always
+ {
+ always = true;
+ }
+ try_end;
+ test_fail_unless (always, "did not execute the always block");
+ } test_end;
+ test_begin (t, "throw catch")
+ {
+ bool caught = false;
+ bool always = false;
+ try_begin
+ {
+ try_throw (100);
+ }
+ try_catch (100)
+ {
+ caught = true;
+ }
+ try_end;
+ test_fail_unless (caught, "did not execute the catch block");
+ caught = false;
+ try_begin
+ {
+ try_throw (100);
+ }
+ try_catch (100)
+ {
+ caught = true;
+ }
+ try_always
+ {
+ always = true;
+ }
+ try_end;
+ test_fail_unless (caught, "did not execute the catch block");
+ test_fail_unless (always, "did not execute the always block");
+ } test_end;
+ test_begin (t, "throw again")
+ {
+ int pass = 3;
+ try_begin
+ {
+ try_begin
+ {
+ try_throw (100);
+ }
+ try_catch (101)
+ {
+ test_fail ("threw 101");
+ }
+ try_always
+ {
+ pass--;
+ }
+ try_end;
+ }
+ try_catch (100)
+ {
+ pass--;
+ }
+ try_always
+ {
+ pass--;
+ }
+ try_end;
+ test_fail_unless (pass == 0, "did not execute some block");
+ } test_end;
+}
+
+void
+try_test_suite (test_t t)
+{
+ test_suite_begin (t, "try");
+ try_basic_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ try_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}
diff --git a/cesar/lib/test/utils/Makefile b/cesar/lib/test/utils/Makefile
new file mode 100644
index 0000000000..5d78d56e6b
--- /dev/null
+++ b/cesar/lib/test/utils/Makefile
@@ -0,0 +1,7 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test_utils
+test_utils_SOURCES = test_utils.c
+test_utils_MODULES = lib
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/lib/test/utils/src/test_utils.c b/cesar/lib/test/utils/src/test_utils.c
new file mode 100644
index 0000000000..23ef0307cf
--- /dev/null
+++ b/cesar/lib/test/utils/src/test_utils.c
@@ -0,0 +1,65 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file src/test_utils.c
+ * \brief Utilities tests.
+ * \ingroup test
+ */
+#include "common/std.h"
+
+#include "lib/test.h"
+
+void
+bits_test_case (test_t t)
+{
+ test_case_begin (t, "bits");
+ test_begin (t, "ones")
+ {
+ test_fail_unless (BITS_ONES_COUNT ((u8) 0x00) == 0);
+ test_fail_unless (BITS_ONES_COUNT ((u8) 0x0f) == 4);
+ test_fail_unless (BITS_ONES_COUNT ((u8) 0xf0) == 4);
+ test_fail_unless (BITS_ONES_COUNT ((u8) 0xa5) == 4);
+ test_fail_unless (BITS_ONES_COUNT ((u8) 0xff) == 8);
+ test_fail_unless (BITS_ONES_COUNT ((u16) 0x0000) == 0);
+ test_fail_unless (BITS_ONES_COUNT ((u16) 0x0f0f) == 8);
+ test_fail_unless (BITS_ONES_COUNT ((u16) 0xf0f0) == 8);
+ test_fail_unless (BITS_ONES_COUNT ((u16) 0xa5a5) == 8);
+ test_fail_unless (BITS_ONES_COUNT ((u16) 0xffff) == 16);
+ test_fail_unless (BITS_ONES_COUNT ((u32) 0x00000000) == 0);
+ test_fail_unless (BITS_ONES_COUNT ((u32) 0x0f0f0f0f) == 16);
+ test_fail_unless (BITS_ONES_COUNT ((u32) 0xf0f0f0f0) == 16);
+ test_fail_unless (BITS_ONES_COUNT ((u32) 0xa5a5a5a5) == 16);
+ test_fail_unless (BITS_ONES_COUNT ((u32) 0xffffffff) == 32);
+ test_fail_unless (BITS_ONES_COUNT ((u64) 0x0000000000000000ull) == 0);
+ test_fail_unless (BITS_ONES_COUNT ((u64) 0x0f0f0f0f0f0f0f0full)
+ == 32);
+ test_fail_unless (BITS_ONES_COUNT ((u64) 0xf0f0f0f0f0f0f0f0ull)
+ == 32);
+ test_fail_unless (BITS_ONES_COUNT ((u64) 0xa5a5a5a5a5a5a5a5ull)
+ == 32);
+ test_fail_unless (BITS_ONES_COUNT ((u64) 0xffffffffffffffffull)
+ == 64);
+ } test_end;
+}
+
+void
+utils_test_suite (test_t t)
+{
+ test_suite_begin (t, "utils");
+ bits_test_case (t);
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ test_init (t, argc, argv);
+ utils_test_suite (t);
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}