bitmap.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <linux/compiler.h>
  2. #include <linux/bitmap.h>
  3. #include "tests.h"
  4. #include "cpumap.h"
  5. #include "debug.h"
  6. #define NBITS 100
  7. static unsigned long *get_bitmap(const char *str, int nbits)
  8. {
  9. struct cpu_map *map = cpu_map__new(str);
  10. unsigned long *bm = NULL;
  11. int i;
  12. bm = bitmap_alloc(nbits);
  13. if (map && bm) {
  14. bitmap_zero(bm, nbits);
  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(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. }