t/axmap: clean up overlap tests
[fio.git] / t / axmap.c
index cc078aa3497f09217f52b73ab9763aa6d9e8a0f3..3f6b8f16fe53e98b21074a3b1d4277aba558c4db 100644 (file)
--- a/t/axmap.c
+++ b/t/axmap.c
@@ -1,24 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
 #include <inttypes.h>
 
-#include "../mutex.h"
 #include "../lib/lfsr.h"
 #include "../lib/axmap.h"
 
-void *smalloc(size_t size)
-{
-       return malloc(size);
-}
-
-void sfree(void *ptr)
-{
-       free(ptr);
-}
-
 static int test_regular(size_t size, int seed)
 {
        struct fio_lfsr lfsr;
@@ -121,6 +107,108 @@ static int test_multi(size_t size, unsigned int bit_off)
        return err;
 }
 
+struct overlap_test {
+       unsigned int start;
+       unsigned int nr;
+       unsigned int ret;
+};
+
+static int test_overlap(void)
+{
+       struct overlap_test tests[] = {
+               {
+                       .start  = 16,
+                       .nr     = 16,
+                       .ret    = 16,
+               },
+               {
+                       .start  = 0,
+                       .nr     = 32,
+                       .ret    = 16,
+               },
+               {
+                       .start  = 48,
+                       .nr     = 32,
+                       .ret    = 32,
+               },
+               {
+                       .start  = 32,
+                       .nr     = 32,
+                       .ret    = 16,
+               },
+               {
+                       .start  = 102,
+                       .nr     = 1,
+                       .ret    = 1,
+               },
+               {
+                       .start  = 101,
+                       .nr     = 3,
+                       .ret    = 1,
+               },
+               {
+                       .start  = 106,
+                       .nr     = 4,
+                       .ret    = 4,
+               },
+               {
+                       .start  = 105,
+                       .nr     = 3,
+                       .ret    = 1,
+               },
+               {
+                       .start  = 120,
+                       .nr     = 4,
+                       .ret    = 4,
+               },
+               {
+                       .start  = 118,
+                       .nr     = 2,
+                       .ret    = 2,
+               },
+               {
+                       .start  = 118,
+                       .nr     = 2,
+                       .ret    = 0,
+               },
+               {
+                       .start  = -1U,
+               },
+       };
+       struct axmap *map;
+       int entries, i, ret, err = 0;
+
+       entries = 0;
+       for (i = 0; tests[i].start != 1U; i++) {
+               unsigned int this = tests[i].start + tests[i].nr;
+
+               if (this > entries)
+                       entries = this;
+       }
+
+       printf("Test overlaps...");
+       fflush(stdout);
+
+       map = axmap_new(entries);
+
+       for (i = 0; tests[i].start != -1U; i++) {
+               struct overlap_test *t = &tests[i];
+
+               ret = axmap_set_nr(map, t->start, t->nr);
+               if (ret != t->ret) {
+                       printf("fail\n");
+                       printf("start=%u, nr=%d, ret=%d: %d\n", t->start, t->nr,
+                                                               t->ret, ret);
+                       err = 1;
+                       break;
+               }
+       }
+
+       printf("pass!\n");
+       axmap_free(map);
+       return err;
+}
+
 int main(int argc, char *argv[])
 {
        size_t size = (1UL << 23) - 200;
@@ -138,6 +226,8 @@ int main(int argc, char *argv[])
                return 2;
        if (test_multi(size, 17))
                return 3;
+       if (test_overlap())
+               return 4;
 
        return 0;
 }