0001
0002 #include <linux/compiler.h>
0003 #include <linux/bitmap.h>
0004 #include <perf/cpumap.h>
0005 #include <internal/cpumap.h>
0006 #include "tests.h"
0007 #include "debug.h"
0008
0009 #define NBITS 100
0010
0011 static unsigned long *get_bitmap(const char *str, int nbits)
0012 {
0013 struct perf_cpu_map *map = perf_cpu_map__new(str);
0014 unsigned long *bm = NULL;
0015 int i;
0016
0017 bm = bitmap_zalloc(nbits);
0018
0019 if (map && bm) {
0020 for (i = 0; i < perf_cpu_map__nr(map); i++)
0021 set_bit(perf_cpu_map__cpu(map, i).cpu, bm);
0022 }
0023
0024 if (map)
0025 perf_cpu_map__put(map);
0026 return bm;
0027 }
0028
0029 static int test_bitmap(const char *str)
0030 {
0031 unsigned long *bm = get_bitmap(str, NBITS);
0032 char buf[100];
0033 int ret;
0034
0035 bitmap_scnprintf(bm, NBITS, buf, sizeof(buf));
0036 pr_debug("bitmap: %s\n", buf);
0037
0038 ret = !strcmp(buf, str);
0039 free(bm);
0040 return ret;
0041 }
0042
0043 static int test__bitmap_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0044 {
0045 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1"));
0046 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,5"));
0047 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3,5,7,9,11,13,15,17,19,21-40"));
0048 TEST_ASSERT_VAL("failed to convert map", test_bitmap("2-5"));
0049 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
0050 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
0051 TEST_ASSERT_VAL("failed to convert map", test_bitmap("1-10,12-20,22-30,32-40"));
0052 return 0;
0053 }
0054
0055 DEFINE_SUITE("Print bitmap", bitmap_print);