test_pque.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "priority_queue.h"
  5. int intcmp(void *_a, void *_b) {
  6. int a = *(int *)_a;
  7. int b = *(int *)_b;
  8. if (a < b) return 1;
  9. if (a > b) return -1;
  10. return 0;
  11. }
  12. int main() {
  13. printf("[TEST] pque\n");
  14. priority_queue_t pq;
  15. init_priority_queue(&pq, 3, sizeof(int), intcmp);
  16. int elems[10] = {1, 3, 2, 4, 6, 5, 9, 7, 8, 10};
  17. for (int i = 0; i < 10; i++) {
  18. int e = elems[i];
  19. priority_queue_push(&pq, &e);
  20. }
  21. for (int i = 1; i < 11; i++) {
  22. int *top = priority_queue_top(&pq);
  23. assert(i == *top);
  24. priority_queue_pop(&pq);
  25. }
  26. assert(priority_queue_top(&pq) == NULL);
  27. int elems2[10] = {10, 8, 7, 9, 5, 6, 4, 2, 3, 1};
  28. int expected[10] = {10, 8, 7, 7, 5, 5, 4, 2, 2, 1};
  29. for (int i = 0; i < 10; i++) {
  30. int e = elems2[i];
  31. priority_queue_push(&pq, &e);
  32. int *top = priority_queue_top(&pq);
  33. assert(*top == expected[i]);
  34. }
  35. printf("[PASS] pque\n");
  36. return 0;
  37. }