test-threadmap.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <perf/threadmap.h>
  5. #include <internal/tests.h>
  6. #include "tests.h"
  7. static int libperf_print(enum libperf_print_level level,
  8. const char *fmt, va_list ap)
  9. {
  10. return vfprintf(stderr, fmt, ap);
  11. }
  12. static int test_threadmap_array(int nr, pid_t *array)
  13. {
  14. struct perf_thread_map *threads;
  15. int i;
  16. threads = perf_thread_map__new_array(nr, array);
  17. __T("Failed to allocate new thread map", threads);
  18. __T("Unexpected number of threads", perf_thread_map__nr(threads) == nr);
  19. for (i = 0; i < nr; i++) {
  20. __T("Unexpected initial value of thread",
  21. perf_thread_map__pid(threads, i) == (array ? array[i] : -1));
  22. }
  23. for (i = 1; i < nr; i++)
  24. perf_thread_map__set_pid(threads, i, i * 100);
  25. __T("Unexpected value of thread 0",
  26. perf_thread_map__pid(threads, 0) == (array ? array[0] : -1));
  27. for (i = 1; i < nr; i++) {
  28. __T("Unexpected thread value",
  29. perf_thread_map__pid(threads, i) == i * 100);
  30. }
  31. perf_thread_map__put(threads);
  32. return 0;
  33. }
  34. #define THREADS_NR 10
  35. int test_threadmap(int argc, char **argv)
  36. {
  37. struct perf_thread_map *threads;
  38. pid_t thr_array[THREADS_NR];
  39. int i;
  40. __T_START;
  41. libperf_init(libperf_print);
  42. threads = perf_thread_map__new_dummy();
  43. if (!threads)
  44. return -1;
  45. perf_thread_map__get(threads);
  46. perf_thread_map__put(threads);
  47. perf_thread_map__put(threads);
  48. test_threadmap_array(THREADS_NR, NULL);
  49. for (i = 0; i < THREADS_NR; i++)
  50. thr_array[i] = i + 100;
  51. test_threadmap_array(THREADS_NR, thr_array);
  52. __T_END;
  53. return tests_failed == 0 ? 0 : -1;
  54. }