bitmap.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include <linux/bitmap.h>
  4. #include <perf/cpumap.h>
  5. #include <internal/cpumap.h>
  6. #include "tests.h"
  7. #include "debug.h"
  8. #define NBITS 100
  9. static unsigned long *get_bitmap(const char *str, int nbits)
  10. {
  11. struct perf_cpu_map *map = perf_cpu_map__new(str);
  12. unsigned long *bm = NULL;
  13. int i;
  14. bm = bitmap_alloc(nbits);
  15. if (map && bm) {
  16. for (i = 0; i < map->nr; i++)
  17. set_bit(map->map[i], bm);
  18. }
  19. if (map)
  20. perf_cpu_map__put(map);
  21. return bm;
  22. }
  23. static int test_bitmap(const char *str)
  24. {
  25. unsigned long *bm = get_bitmap(str, NBITS);
  26. char buf[100];
  27. int ret;
  28. bitmap_scnprintf(bm, NBITS, buf, sizeof(buf));
  29. pr_debug("bitmap: %s\n", buf);
  30. ret = !strcmp(buf, str);
  31. free(bm);
  32. return ret;
  33. }
  34. int test__bitmap_print(struct test *test __maybe_unused, int subtest __maybe_unused)
  35. {
  36. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1"));
  37. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,5"));
  38. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3,5,7,9,11,13,15,17,19,21-40"));
  39. TEST_ASSERT_VAL("failed to convert map", test_bitmap("2-5"));
  40. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
  41. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1,3-6,8-10,24,35-37"));
  42. TEST_ASSERT_VAL("failed to convert map", test_bitmap("1-10,12-20,22-30,32-40"));
  43. return 0;
  44. }