]> git.ozlabs.org Git - ccan/commitdiff
order: total_order_cmp() helper and better tests
authorDavid Gibson <david@gibson.dropbear.id.au>
Sun, 18 Oct 2015 10:10:58 +0000 (21:10 +1100)
committerDavid Gibson <david@gibson.dropbear.id.au>
Sun, 18 Oct 2015 10:10:58 +0000 (21:10 +1100)
Add a wrapper macro total_order_cmp() to more conveniently use the
total_order structures.  Add some tests for it, which also improve tests
the "fancy_cmp" function.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/order/order.h
ccan/order/test/fancy_cmp.h
ccan/order/test/run-fancy.c [new file with mode: 0644]

index edceab5551e2e68dd14bee39bf7ac4a12663841b..23f030b2c8c1e9e3e50751ec275f3a882eb3daec 100644 (file)
@@ -31,6 +31,9 @@ struct _total_order {
                _ctx ctx;                               \
        } _name
 
+#define total_order_cmp(_order, _a, _b)                                        \
+       ((_order).cb((_a), (_b), (_order).ctx))
+
 #define _DECL_ONAME(_oname, _itype)                                    \
        extern int _order_##_oname(const void *, const void *, void *); \
        extern int order_##_oname(const _itype *, const _itype *, void *); \
index b7940377a36536b84f52eb566ddbc5d7fa3731dc..a3e513d902e445161dd4361a910ca7e18c544948 100644 (file)
@@ -8,7 +8,7 @@ struct cmp_info {
 
 struct item {
        unsigned value;
-       char *str;
+       const char *str;
 };
 
 static inline int fancy_cmp(const struct item *a, const struct item *b,
diff --git a/ccan/order/test/run-fancy.c b/ccan/order/test/run-fancy.c
new file mode 100644 (file)
index 0000000..be5ff3f
--- /dev/null
@@ -0,0 +1,68 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <ccan/order/order.h>
+
+#include <ccan/tap/tap.h>
+
+#include "fancy_cmp.h"
+
+int main(int argc, char *argv[])
+{
+       struct item item1 = {
+               .value = 0,
+               .str = "aaa",
+       };
+       struct item item2 = {
+               .value = 0,
+               .str = "abb",
+       };
+       struct item item3 = {
+               .value = 0x1000,
+               .str = "baa",
+       };
+       struct cmp_info ctx1 = {
+               .xcode = 0,
+               .offset = 0,
+       };
+       struct cmp_info ctx2 = {
+               .xcode = 0x1000,
+               .offset = 1,
+       };
+       total_order(order1, struct item, struct cmp_info *) = {
+               fancy_cmp, &ctx1,
+       };
+       total_order(order2, struct item, struct cmp_info *) = {
+               fancy_cmp, &ctx2,
+       };
+
+       plan_tests(18);
+
+       ok1(total_order_cmp(order1, &item1, &item1) == 0);
+       ok1(total_order_cmp(order1, &item2, &item2) == 0);
+       ok1(total_order_cmp(order1, &item3, &item3) == 0);
+
+       ok1(total_order_cmp(order1, &item1, &item2) == -1);
+       ok1(total_order_cmp(order1, &item2, &item3) == -1);
+       ok1(total_order_cmp(order1, &item1, &item3) == -1);
+
+       ok1(total_order_cmp(order1, &item2, &item1) == 1);
+       ok1(total_order_cmp(order1, &item3, &item2) == 1);
+       ok1(total_order_cmp(order1, &item3, &item1) == 1);
+
+
+       ok1(total_order_cmp(order2, &item1, &item1) == 0);
+       ok1(total_order_cmp(order2, &item2, &item2) == 0);
+       ok1(total_order_cmp(order2, &item3, &item3) == 0);
+
+       ok1(total_order_cmp(order2, &item1, &item2) == 1);
+       ok1(total_order_cmp(order2, &item2, &item3) == 1);
+       ok1(total_order_cmp(order2, &item1, &item3) == 1);
+
+       ok1(total_order_cmp(order2, &item2, &item1) == -1);
+       ok1(total_order_cmp(order2, &item3, &item2) == -1);
+       ok1(total_order_cmp(order2, &item3, &item1) == -1);
+       
+       exit(0);
+}