1 #include <linux/compiler.h> 2 #include <linux/bitmap.h> 3 #include "cpumap.h" 4 #include "mem2node.h" 5 #include "tests.h" 6 7 static struct node { 8 int node; 9 const char *map; 10 } test_nodes[] = { 11 { .node = 0, .map = "0" }, 12 { .node = 1, .map = "1-2" }, 13 { .node = 3, .map = "5-7,9" }, 14 }; 15 16 #define T TEST_ASSERT_VAL 17 18 static unsigned long *get_bitmap(const char *str, int nbits) 19 { 20 struct cpu_map *map = cpu_map__new(str); 21 unsigned long *bm = NULL; 22 int i; 23 24 bm = bitmap_alloc(nbits); 25 26 if (map && bm) { 27 for (i = 0; i < map->nr; i++) { 28 set_bit(map->map[i], bm); 29 } 30 } 31 32 if (map) 33 cpu_map__put(map); 34 else 35 free(bm); 36 37 return bm && map ? bm : NULL; 38 } 39 40 int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused) 41 { 42 struct mem2node map; 43 struct memory_node nodes[3]; 44 struct perf_env env = { 45 .memory_nodes = (struct memory_node *) &nodes[0], 46 .nr_memory_nodes = ARRAY_SIZE(nodes), 47 .memory_bsize = 0x100, 48 }; 49 unsigned int i; 50 51 for (i = 0; i < ARRAY_SIZE(nodes); i++) { 52 nodes[i].node = test_nodes[i].node; 53 nodes[i].size = 10; 54 55 T("failed: alloc bitmap", 56 (nodes[i].set = get_bitmap(test_nodes[i].map, 10))); 57 } 58 59 T("failed: mem2node__init", !mem2node__init(&map, &env)); 60 T("failed: mem2node__node", 0 == mem2node__node(&map, 0x50)); 61 T("failed: mem2node__node", 1 == mem2node__node(&map, 0x100)); 62 T("failed: mem2node__node", 1 == mem2node__node(&map, 0x250)); 63 T("failed: mem2node__node", 3 == mem2node__node(&map, 0x500)); 64 T("failed: mem2node__node", 3 == mem2node__node(&map, 0x650)); 65 T("failed: mem2node__node", -1 == mem2node__node(&map, 0x450)); 66 T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050)); 67 68 for (i = 0; i < ARRAY_SIZE(nodes); i++) 69 free(nodes[i].set); 70 71 mem2node__exit(&map); 72 return 0; 73 } 74