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