| 1 | #include <stdlib.h> |
| 2 | #include "cgds/Queue.h" |
| 3 | #include "helpers.h" |
| 4 | #include "lut.h" |
| 5 | |
| 6 | void t_queue_clear() |
| 7 | { |
| 8 | Queue* q = queue_new(int); |
| 9 | |
| 10 | queue_push(q, 0); |
| 11 | queue_push(q, 0); |
| 12 | queue_push(q, 0); |
| 13 | |
| 14 | queue_clear(q); |
| 15 | lu_assert(queue_empty(q)); |
| 16 | |
| 17 | queue_destroy(q); |
| 18 | } |
| 19 | |
| 20 | void t_queue_size() |
| 21 | { |
| 22 | Queue* q = queue_new(int); |
| 23 | |
| 24 | queue_push(q, 0); |
| 25 | queue_push(q, 0); |
| 26 | queue_push(q, 0); |
| 27 | lu_assert_int_eq(queue_size(q), 3); |
| 28 | |
| 29 | queue_push(q, 0); |
| 30 | queue_push(q, 0); |
| 31 | lu_assert_int_eq(queue_size(q), 5); |
| 32 | |
| 33 | queue_push(q, 0); |
| 34 | queue_push(q, 0); |
| 35 | queue_push(q, 0); |
| 36 | lu_assert_int_eq(queue_size(q), 8); |
| 37 | |
| 38 | queue_destroy(q); |
| 39 | } |
| 40 | |
| 41 | void t_queue_push_pop_basic() |
| 42 | { |
| 43 | int n = 10; |
| 44 | |
| 45 | Queue* q = queue_new(double); |
| 46 | for (int i = 0; i < n; i++) |
| 47 | queue_push(q, (double) i); |
| 48 | // iterate and check values |
| 49 | double ckValue = 0.0; |
| 50 | while (!queue_empty(q)) |
| 51 | { |
| 52 | double d; |
| 53 | queue_peek(q, d); |
| 54 | lu_assert_dbl_eq(d, ckValue); |
| 55 | ckValue += 1.0; |
| 56 | queue_pop(q); |
| 57 | } |
| 58 | |
| 59 | lu_assert(queue_empty(q)); |
| 60 | queue_destroy(q); |
| 61 | } |
| 62 | |
| 63 | void t_queue_push_pop_evolved() |
| 64 | { |
| 65 | int n = 10; |
| 66 | |
| 67 | Queue* q = queue_new(StructTest1); |
| 68 | StructTest1* st1 = (StructTest1*) safe_malloc(n * sizeof (StructTest1)); |
| 69 | for (int i = 0; i < n; i++) |
| 70 | { |
| 71 | st1[i].a = rand() % 42; |
| 72 | st1[i].b = (double) rand() / RAND_MAX; |
| 73 | queue_push(q, *(st1 + i)); |
| 74 | } |
| 75 | for (int i = 0; i < n; i++) |
| 76 | { |
| 77 | StructTest1 st1Cell; |
| 78 | queue_peek(q, st1Cell); |
| 79 | lu_assert_int_eq(st1Cell.a, st1[i].a); |
| 80 | lu_assert_dbl_eq(st1Cell.b, st1[i].b); |
| 81 | queue_pop(q); |
| 82 | } |
| 83 | safe_free(st1); |
| 84 | queue_destroy(q); |
| 85 | |
| 86 | q = queue_new(StructTest2*); |
| 87 | StructTest2* st2 = (StructTest2*) safe_malloc(n * sizeof (StructTest2)); |
| 88 | for (int i = 0; i < n; i++) |
| 89 | { |
| 90 | st2[i].a = (float) rand() / RAND_MAX; |
| 91 | st2[i].b = (StructTest1*) safe_malloc(sizeof (StructTest1)); |
| 92 | st2[i].b->a = rand() % 42; |
| 93 | st2[i].b->b = (double) rand() / RAND_MAX; |
| 94 | queue_push(q, st2 + i); |
| 95 | } |
| 96 | for (int i = 0; i < n; i++) |
| 97 | { |
| 98 | StructTest2* st2Cell; |
| 99 | queue_peek(q, st2Cell); |
| 100 | lu_assert_dbl_eq(st2Cell->a, st2[i].a); |
| 101 | lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); |
| 102 | lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); |
| 103 | queue_pop(q); |
| 104 | safe_free(st2Cell->b); |
| 105 | } |
| 106 | safe_free(st2); |
| 107 | queue_destroy(q); |
| 108 | } |
| 109 | |
| 110 | void t_queue_copy() |
| 111 | { |
| 112 | int n = 10; |
| 113 | |
| 114 | Queue* q = queue_new(int); |
| 115 | for (int i = 0; i < n; i++) |
| 116 | queue_push(q, rand() % 42); |
| 117 | Queue* qc = queue_copy(q); |
| 118 | |
| 119 | lu_assert_int_eq(queue_size(q), queue_size(qc)); |
| 120 | int a, b; |
| 121 | for (int i = 0; i < n; i++) |
| 122 | { |
| 123 | queue_peek(q, a); |
| 124 | queue_peek(qc, b); |
| 125 | lu_assert_int_eq(a, b); |
| 126 | queue_pop(q); |
| 127 | queue_pop(qc); |
| 128 | } |
| 129 | queue_destroy(q); |
| 130 | queue_destroy(qc); |
| 131 | } |