Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | #include <inttypes.h> |
| 7 | |
| 8 | #include "../lib/lfsr.h" |
Jens Axboe | 32bbd3a | 2013-01-23 11:02:07 -0700 | [diff] [blame] | 9 | #include "../lib/axmap.h" |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 10 | |
| 11 | void *smalloc(size_t size) |
| 12 | { |
| 13 | return malloc(size); |
| 14 | } |
| 15 | |
| 16 | void sfree(void *ptr) |
| 17 | { |
| 18 | free(ptr); |
| 19 | } |
| 20 | |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 21 | static int test_regular(size_t size, int seed) |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 22 | { |
| 23 | struct fio_lfsr lfsr; |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 24 | struct axmap *map; |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 25 | size_t osize; |
Jens Axboe | 32bbd3a | 2013-01-23 11:02:07 -0700 | [diff] [blame] | 26 | uint64_t ff; |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 27 | int err; |
| 28 | |
| 29 | printf("Using %llu entries...", (unsigned long long) size); |
| 30 | fflush(stdout); |
| 31 | |
| 32 | lfsr_init(&lfsr, size, seed, seed & 0xF); |
| 33 | map = axmap_new(size); |
| 34 | osize = size; |
| 35 | err = 0; |
| 36 | |
| 37 | while (size--) { |
| 38 | uint64_t val; |
| 39 | |
Jens Axboe | ce596f2 | 2014-11-24 08:07:03 -0700 | [diff] [blame] | 40 | if (lfsr_next(&lfsr, &val)) { |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 41 | printf("lfsr: short loop\n"); |
| 42 | err = 1; |
| 43 | break; |
| 44 | } |
| 45 | if (axmap_isset(map, val)) { |
| 46 | printf("bit already set\n"); |
| 47 | err = 1; |
| 48 | break; |
| 49 | } |
| 50 | axmap_set(map, val); |
| 51 | if (!axmap_isset(map, val)) { |
| 52 | printf("bit not set\n"); |
| 53 | err = 1; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (err) |
| 59 | return err; |
| 60 | |
| 61 | ff = axmap_next_free(map, osize); |
| 62 | if (ff != (uint64_t) -1ULL) { |
| 63 | printf("axmap_next_free broken: got %llu\n", (unsigned long long) ff); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | printf("pass!\n"); |
| 68 | axmap_free(map); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int test_multi(size_t size, unsigned int bit_off) |
| 73 | { |
| 74 | unsigned int map_size = size; |
| 75 | struct axmap *map; |
| 76 | uint64_t val = bit_off; |
| 77 | int i, err; |
| 78 | |
| 79 | printf("Test multi %llu entries %u offset...", (unsigned long long) size, bit_off); |
| 80 | fflush(stdout); |
| 81 | |
| 82 | map = axmap_new(map_size); |
| 83 | while (val + 128 <= map_size) { |
| 84 | err = 0; |
| 85 | for (i = val; i < val + 128; i++) { |
| 86 | if (axmap_isset(map, val + i)) { |
| 87 | printf("bit already set\n"); |
| 88 | err = 1; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (err) |
| 94 | break; |
| 95 | |
| 96 | err = axmap_set_nr(map, val, 128); |
| 97 | if (err != 128) { |
| 98 | printf("only set %u bits\n", err); |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | err = 0; |
| 103 | for (i = 0; i < 128; i++) { |
| 104 | if (!axmap_isset(map, val + i)) { |
| 105 | printf("bit not set: %llu\n", (unsigned long long) val + i); |
| 106 | err = 1; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | val += 128; |
| 112 | if (err) |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | if (!err) |
| 117 | printf("pass!\n"); |
| 118 | |
| 119 | axmap_free(map); |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char *argv[]) |
| 124 | { |
| 125 | size_t size = (1UL << 23) - 200; |
Jens Axboe | 4e59017 | 2012-12-21 21:32:50 +0100 | [diff] [blame] | 126 | int seed = 1; |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 127 | |
Jens Axboe | 4e59017 | 2012-12-21 21:32:50 +0100 | [diff] [blame] | 128 | if (argc > 1) { |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 129 | size = strtoul(argv[1], NULL, 10); |
Jens Axboe | 4e59017 | 2012-12-21 21:32:50 +0100 | [diff] [blame] | 130 | if (argc > 2) |
| 131 | seed = strtoul(argv[2], NULL, 10); |
| 132 | } |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 133 | |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 134 | if (test_regular(size, seed)) |
Jens Axboe | 32bbd3a | 2013-01-23 11:02:07 -0700 | [diff] [blame] | 135 | return 1; |
Jens Axboe | a744812 | 2013-03-29 09:14:23 -0600 | [diff] [blame] | 136 | if (test_multi(size, 0)) |
| 137 | return 2; |
| 138 | if (test_multi(size, 17)) |
| 139 | return 3; |
Jens Axboe | 32bbd3a | 2013-01-23 11:02:07 -0700 | [diff] [blame] | 140 | |
Jens Axboe | ad1f90a | 2012-11-28 21:29:14 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |