xref: /openssl/test/priority_queue_test.c (revision f0a49358)
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include <openssl/opensslconf.h>
14 #include <internal/priority_queue.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
17 
18 #include "internal/nelem.h"
19 #include "testutil.h"
20 
21 #define MAX_SAMPLES 500000
22 
23 DEFINE_PRIORITY_QUEUE_OF(size_t);
24 
25 static size_t num_rec_freed;
26 
size_t_compare(const size_t * a,const size_t * b)27 static int size_t_compare(const size_t *a, const size_t *b)
28 {
29     if (*a < *b)
30         return -1;
31     if (*a > *b)
32         return 1;
33     return 0;
34 }
35 
qsort_size_t_compare(const void * a,const void * b)36 static int qsort_size_t_compare(const void *a, const void *b)
37 {
38     return size_t_compare((size_t *)a, (size_t *)b);
39 }
40 
qsort_size_t_compare_rev(const void * a,const void * b)41 static int qsort_size_t_compare_rev(const void *a, const void *b)
42 {
43     return size_t_compare((size_t *)b, (size_t *)a);
44 }
45 
free_checker(ossl_unused size_t * p)46 static void free_checker(ossl_unused size_t *p)
47 {
48     num_rec_freed++;
49 }
50 
test_size_t_priority_queue_int(int reserve,int order,int count,int remove,int random,int popfree)51 static int test_size_t_priority_queue_int(int reserve, int order, int count,
52                                           int remove, int random, int popfree)
53 {
54     PRIORITY_QUEUE_OF(size_t) *pq = NULL;
55     static size_t values[MAX_SAMPLES], sorted[MAX_SAMPLES], ref[MAX_SAMPLES];
56     size_t n;
57     int i, res = 0;
58     static const char *orders[3] = { "unordered", "ascending", "descending" };
59 
60     TEST_info("testing count %d, %s, %s, values %s, remove %d, %sfree",
61               count, orders[order], reserve ? "reserve" : "grow",
62               random ? "random" : "deterministic", remove,
63               popfree ? "pop " : "");
64 
65     if (!TEST_size_t_le(count, MAX_SAMPLES))
66         return 0;
67 
68     memset(values, 0, sizeof(values));
69     memset(sorted, 0, sizeof(sorted));
70     memset(ref, 0, sizeof(ref));
71 
72     for (i = 0; i < count; i++)
73         values[i] = random ? test_random() : (size_t)(count - i);
74     memcpy(sorted, values, sizeof(*sorted) * count);
75     qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare);
76 
77     if (order == 1)
78         memcpy(values, sorted, sizeof(*values) * count);
79     else if (order == 2)
80         qsort(values, count, sizeof(*values), &qsort_size_t_compare_rev);
81 
82     if (!TEST_ptr(pq = ossl_pqueue_size_t_new(&size_t_compare))
83             || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), 0))
84         goto err;
85 
86     if (reserve && !TEST_true(ossl_pqueue_size_t_reserve(pq, count)))
87         goto err;
88 
89     for (i = 0; i < count; i++)
90         if (!TEST_true(ossl_pqueue_size_t_push(pq, values + i, ref + i)))
91             goto err;
92 
93     if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), *sorted)
94             || !TEST_size_t_eq(ossl_pqueue_size_t_num(pq), count))
95         goto err;
96 
97     if (remove) {
98         while (remove-- > 0) {
99             i = test_random() % count;
100             if (values[i] != SIZE_MAX) {
101                 if (!TEST_ptr_eq(ossl_pqueue_size_t_remove(pq, ref[i]),
102                                  values + i))
103                     goto err;
104                 values[i] = SIZE_MAX;
105             }
106         }
107         memcpy(sorted, values, sizeof(*sorted) * count);
108         qsort(sorted, count, sizeof(*sorted), &qsort_size_t_compare);
109     }
110     for (i = 0; ossl_pqueue_size_t_peek(pq) != NULL; i++)
111         if (!TEST_size_t_eq(*ossl_pqueue_size_t_peek(pq), sorted[i])
112                 || !TEST_size_t_eq(*ossl_pqueue_size_t_pop(pq), sorted[i]))
113                 goto err;
114 
115     if (popfree) {
116         num_rec_freed = 0;
117         n = ossl_pqueue_size_t_num(pq);
118         ossl_pqueue_size_t_pop_free(pq, &free_checker);
119         pq = NULL;
120         if (!TEST_size_t_eq(num_rec_freed, n))
121             goto err;
122     }
123     res = 1;
124  err:
125     ossl_pqueue_size_t_free(pq);
126     return res;
127 }
128 
129 static const int test_size_t_priority_counts[] = {
130     10, 11, 6, 5, 3, 1, 2, 7500
131 };
132 
test_size_t_priority_queue(int n)133 static int test_size_t_priority_queue(int n)
134 {
135     int reserve, order, count, remove, random, popfree;
136 
137     count = n % OSSL_NELEM(test_size_t_priority_counts);
138     n /= OSSL_NELEM(test_size_t_priority_counts);
139     order = n % 3;
140     n /= 3;
141     random = n % 2;
142     n /= 2;
143     reserve = n % 2;
144     n /= 2;
145     remove = n % 6;
146     n /= 6;
147     popfree = n % 2;
148 
149     count = test_size_t_priority_counts[count];
150     return test_size_t_priority_queue_int(reserve, order, count, remove,
151                                           random, popfree);
152 }
153 
test_large_priority_queue(void)154 static int test_large_priority_queue(void)
155 {
156     return test_size_t_priority_queue_int(0, 0, MAX_SAMPLES, MAX_SAMPLES / 100,
157                                           1, 1);
158 }
159 
setup_tests(void)160 int setup_tests(void)
161 {
162     ADD_ALL_TESTS(test_size_t_priority_queue,
163                   OSSL_NELEM(test_size_t_priority_counts)   /* count */
164                   * 3                                       /* order */
165                   * 2                                       /* random */
166                   * 2                                       /* reserve */
167                   * 6                                       /* remove */
168                   * 2);                                     /* pop & free */
169     ADD_TEST(test_large_priority_queue);
170     return 1;
171 }
172