1a0698ed9Schristos #include "test/jemalloc_test.h" 2a0698ed9Schristos 3a0698ed9Schristos #include "jemalloc/internal/qr.h" 4a0698ed9Schristos 5a0698ed9Schristos /* Number of ring entries, in [2..26]. */ 6a0698ed9Schristos #define NENTRIES 9 7a0698ed9Schristos /* Split index, in [1..NENTRIES). */ 8a0698ed9Schristos #define SPLIT_INDEX 5 9a0698ed9Schristos 10a0698ed9Schristos typedef struct ring_s ring_t; 11a0698ed9Schristos 12a0698ed9Schristos struct ring_s { 13a0698ed9Schristos qr(ring_t) link; 14a0698ed9Schristos char id; 15a0698ed9Schristos }; 16a0698ed9Schristos 17a0698ed9Schristos static void 18a0698ed9Schristos init_entries(ring_t *entries) { 19a0698ed9Schristos unsigned i; 20a0698ed9Schristos 21a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 22a0698ed9Schristos qr_new(&entries[i], link); 23a0698ed9Schristos entries[i].id = 'a' + i; 24a0698ed9Schristos } 25a0698ed9Schristos } 26a0698ed9Schristos 27a0698ed9Schristos static void 28a0698ed9Schristos test_independent_entries(ring_t *entries) { 29a0698ed9Schristos ring_t *t; 30a0698ed9Schristos unsigned i, j; 31a0698ed9Schristos 32a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 33a0698ed9Schristos j = 0; 34a0698ed9Schristos qr_foreach(t, &entries[i], link) { 35a0698ed9Schristos j++; 36a0698ed9Schristos } 37*7bdf38e5Schristos expect_u_eq(j, 1, 38a0698ed9Schristos "Iteration over single-element ring should visit precisely " 39a0698ed9Schristos "one element"); 40a0698ed9Schristos } 41a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 42a0698ed9Schristos j = 0; 43a0698ed9Schristos qr_reverse_foreach(t, &entries[i], link) { 44a0698ed9Schristos j++; 45a0698ed9Schristos } 46*7bdf38e5Schristos expect_u_eq(j, 1, 47a0698ed9Schristos "Iteration over single-element ring should visit precisely " 48a0698ed9Schristos "one element"); 49a0698ed9Schristos } 50a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 51a0698ed9Schristos t = qr_next(&entries[i], link); 52*7bdf38e5Schristos expect_ptr_eq(t, &entries[i], 53a0698ed9Schristos "Next element in single-element ring should be same as " 54a0698ed9Schristos "current element"); 55a0698ed9Schristos } 56a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 57a0698ed9Schristos t = qr_prev(&entries[i], link); 58*7bdf38e5Schristos expect_ptr_eq(t, &entries[i], 59a0698ed9Schristos "Previous element in single-element ring should be same as " 60a0698ed9Schristos "current element"); 61a0698ed9Schristos } 62a0698ed9Schristos } 63a0698ed9Schristos 64a0698ed9Schristos TEST_BEGIN(test_qr_one) { 65a0698ed9Schristos ring_t entries[NENTRIES]; 66a0698ed9Schristos 67a0698ed9Schristos init_entries(entries); 68a0698ed9Schristos test_independent_entries(entries); 69a0698ed9Schristos } 70a0698ed9Schristos TEST_END 71a0698ed9Schristos 72a0698ed9Schristos static void 73a0698ed9Schristos test_entries_ring(ring_t *entries) { 74a0698ed9Schristos ring_t *t; 75a0698ed9Schristos unsigned i, j; 76a0698ed9Schristos 77a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 78a0698ed9Schristos j = 0; 79a0698ed9Schristos qr_foreach(t, &entries[i], link) { 80*7bdf38e5Schristos expect_c_eq(t->id, entries[(i+j) % NENTRIES].id, 81a0698ed9Schristos "Element id mismatch"); 82a0698ed9Schristos j++; 83a0698ed9Schristos } 84a0698ed9Schristos } 85a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 86a0698ed9Schristos j = 0; 87a0698ed9Schristos qr_reverse_foreach(t, &entries[i], link) { 88*7bdf38e5Schristos expect_c_eq(t->id, entries[(NENTRIES+i-j-1) % 89a0698ed9Schristos NENTRIES].id, "Element id mismatch"); 90a0698ed9Schristos j++; 91a0698ed9Schristos } 92a0698ed9Schristos } 93a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 94a0698ed9Schristos t = qr_next(&entries[i], link); 95*7bdf38e5Schristos expect_c_eq(t->id, entries[(i+1) % NENTRIES].id, 96a0698ed9Schristos "Element id mismatch"); 97a0698ed9Schristos } 98a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 99a0698ed9Schristos t = qr_prev(&entries[i], link); 100*7bdf38e5Schristos expect_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id, 101a0698ed9Schristos "Element id mismatch"); 102a0698ed9Schristos } 103a0698ed9Schristos } 104a0698ed9Schristos 105a0698ed9Schristos TEST_BEGIN(test_qr_after_insert) { 106a0698ed9Schristos ring_t entries[NENTRIES]; 107a0698ed9Schristos unsigned i; 108a0698ed9Schristos 109a0698ed9Schristos init_entries(entries); 110a0698ed9Schristos for (i = 1; i < NENTRIES; i++) { 111a0698ed9Schristos qr_after_insert(&entries[i - 1], &entries[i], link); 112a0698ed9Schristos } 113a0698ed9Schristos test_entries_ring(entries); 114a0698ed9Schristos } 115a0698ed9Schristos TEST_END 116a0698ed9Schristos 117a0698ed9Schristos TEST_BEGIN(test_qr_remove) { 118a0698ed9Schristos ring_t entries[NENTRIES]; 119a0698ed9Schristos ring_t *t; 120a0698ed9Schristos unsigned i, j; 121a0698ed9Schristos 122a0698ed9Schristos init_entries(entries); 123a0698ed9Schristos for (i = 1; i < NENTRIES; i++) { 124a0698ed9Schristos qr_after_insert(&entries[i - 1], &entries[i], link); 125a0698ed9Schristos } 126a0698ed9Schristos 127a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 128a0698ed9Schristos j = 0; 129a0698ed9Schristos qr_foreach(t, &entries[i], link) { 130*7bdf38e5Schristos expect_c_eq(t->id, entries[i+j].id, 131a0698ed9Schristos "Element id mismatch"); 132a0698ed9Schristos j++; 133a0698ed9Schristos } 134a0698ed9Schristos j = 0; 135a0698ed9Schristos qr_reverse_foreach(t, &entries[i], link) { 136*7bdf38e5Schristos expect_c_eq(t->id, entries[NENTRIES - 1 - j].id, 137a0698ed9Schristos "Element id mismatch"); 138a0698ed9Schristos j++; 139a0698ed9Schristos } 140a0698ed9Schristos qr_remove(&entries[i], link); 141a0698ed9Schristos } 142a0698ed9Schristos test_independent_entries(entries); 143a0698ed9Schristos } 144a0698ed9Schristos TEST_END 145a0698ed9Schristos 146a0698ed9Schristos TEST_BEGIN(test_qr_before_insert) { 147a0698ed9Schristos ring_t entries[NENTRIES]; 148a0698ed9Schristos ring_t *t; 149a0698ed9Schristos unsigned i, j; 150a0698ed9Schristos 151a0698ed9Schristos init_entries(entries); 152a0698ed9Schristos for (i = 1; i < NENTRIES; i++) { 153a0698ed9Schristos qr_before_insert(&entries[i - 1], &entries[i], link); 154a0698ed9Schristos } 155a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 156a0698ed9Schristos j = 0; 157a0698ed9Schristos qr_foreach(t, &entries[i], link) { 158*7bdf38e5Schristos expect_c_eq(t->id, entries[(NENTRIES+i-j) % 159a0698ed9Schristos NENTRIES].id, "Element id mismatch"); 160a0698ed9Schristos j++; 161a0698ed9Schristos } 162a0698ed9Schristos } 163a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 164a0698ed9Schristos j = 0; 165a0698ed9Schristos qr_reverse_foreach(t, &entries[i], link) { 166*7bdf38e5Schristos expect_c_eq(t->id, entries[(i+j+1) % NENTRIES].id, 167a0698ed9Schristos "Element id mismatch"); 168a0698ed9Schristos j++; 169a0698ed9Schristos } 170a0698ed9Schristos } 171a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 172a0698ed9Schristos t = qr_next(&entries[i], link); 173*7bdf38e5Schristos expect_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id, 174a0698ed9Schristos "Element id mismatch"); 175a0698ed9Schristos } 176a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 177a0698ed9Schristos t = qr_prev(&entries[i], link); 178*7bdf38e5Schristos expect_c_eq(t->id, entries[(i+1) % NENTRIES].id, 179a0698ed9Schristos "Element id mismatch"); 180a0698ed9Schristos } 181a0698ed9Schristos } 182a0698ed9Schristos TEST_END 183a0698ed9Schristos 184a0698ed9Schristos static void 185a0698ed9Schristos test_split_entries(ring_t *entries) { 186a0698ed9Schristos ring_t *t; 187a0698ed9Schristos unsigned i, j; 188a0698ed9Schristos 189a0698ed9Schristos for (i = 0; i < NENTRIES; i++) { 190a0698ed9Schristos j = 0; 191a0698ed9Schristos qr_foreach(t, &entries[i], link) { 192a0698ed9Schristos if (i < SPLIT_INDEX) { 193*7bdf38e5Schristos expect_c_eq(t->id, 194a0698ed9Schristos entries[(i+j) % SPLIT_INDEX].id, 195a0698ed9Schristos "Element id mismatch"); 196a0698ed9Schristos } else { 197*7bdf38e5Schristos expect_c_eq(t->id, entries[(i+j-SPLIT_INDEX) % 198a0698ed9Schristos (NENTRIES-SPLIT_INDEX) + SPLIT_INDEX].id, 199a0698ed9Schristos "Element id mismatch"); 200a0698ed9Schristos } 201a0698ed9Schristos j++; 202a0698ed9Schristos } 203a0698ed9Schristos } 204a0698ed9Schristos } 205a0698ed9Schristos 206a0698ed9Schristos TEST_BEGIN(test_qr_meld_split) { 207a0698ed9Schristos ring_t entries[NENTRIES]; 208a0698ed9Schristos unsigned i; 209a0698ed9Schristos 210a0698ed9Schristos init_entries(entries); 211a0698ed9Schristos for (i = 1; i < NENTRIES; i++) { 212a0698ed9Schristos qr_after_insert(&entries[i - 1], &entries[i], link); 213a0698ed9Schristos } 214a0698ed9Schristos 215*7bdf38e5Schristos qr_split(&entries[0], &entries[SPLIT_INDEX], link); 216a0698ed9Schristos test_split_entries(entries); 217a0698ed9Schristos 218*7bdf38e5Schristos qr_meld(&entries[0], &entries[SPLIT_INDEX], link); 219a0698ed9Schristos test_entries_ring(entries); 220a0698ed9Schristos 221*7bdf38e5Schristos qr_meld(&entries[0], &entries[SPLIT_INDEX], link); 222a0698ed9Schristos test_split_entries(entries); 223a0698ed9Schristos 224*7bdf38e5Schristos qr_split(&entries[0], &entries[SPLIT_INDEX], link); 225a0698ed9Schristos test_entries_ring(entries); 226a0698ed9Schristos 227*7bdf38e5Schristos qr_split(&entries[0], &entries[0], link); 228a0698ed9Schristos test_entries_ring(entries); 229a0698ed9Schristos 230*7bdf38e5Schristos qr_meld(&entries[0], &entries[0], link); 231a0698ed9Schristos test_entries_ring(entries); 232a0698ed9Schristos } 233a0698ed9Schristos TEST_END 234a0698ed9Schristos 235a0698ed9Schristos int 236a0698ed9Schristos main(void) { 237a0698ed9Schristos return test( 238a0698ed9Schristos test_qr_one, 239a0698ed9Schristos test_qr_after_insert, 240a0698ed9Schristos test_qr_remove, 241a0698ed9Schristos test_qr_before_insert, 242a0698ed9Schristos test_qr_meld_split); 243a0698ed9Schristos } 244