xref: /openssl/test/list_test.c (revision 30773411)
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 <openssl/err.h>
15 #include <openssl/crypto.h>
16 
17 #include "internal/list.h"
18 #include "internal/nelem.h"
19 #include "testutil.h"
20 
21 typedef struct testl_st TESTL;
22 struct testl_st {
23     int n;
24     OSSL_LIST_MEMBER(fizz, TESTL);
25     OSSL_LIST_MEMBER(buzz, TESTL);
26 };
27 
28 DEFINE_LIST_OF(fizz, TESTL);
29 DEFINE_LIST_OF(buzz, TESTL);
30 
test_fizzbuzz(void)31 static int test_fizzbuzz(void)
32 {
33     OSSL_LIST(fizz) a;
34     OSSL_LIST(buzz) b;
35     TESTL elem[20];
36     const int nelem = OSSL_NELEM(elem);
37     int i, na = 0, nb = 0;
38 
39     ossl_list_fizz_init(&a);
40     ossl_list_buzz_init(&b);
41 
42     if (!TEST_true(ossl_list_fizz_is_empty(&a)))
43         return 0;
44 
45     for (i = 1; i < nelem; i++) {
46         ossl_list_fizz_init_elem(elem + i);
47         ossl_list_buzz_init_elem(elem + i);
48         elem[i].n = i;
49         if (i % 3 == 0) {
50             ossl_list_fizz_insert_tail(&a, elem + i);
51             na++;
52         }
53         if (i % 5 == 0) {
54             ossl_list_buzz_insert_head(&b, elem + i);
55             nb++;
56         }
57     }
58 
59     if (!TEST_false(ossl_list_fizz_is_empty(&a))
60             || !TEST_size_t_eq(ossl_list_fizz_num(&a), na)
61             || !TEST_size_t_eq(ossl_list_buzz_num(&b), nb)
62             || !TEST_ptr(ossl_list_fizz_head(&a))
63             || !TEST_ptr(ossl_list_fizz_tail(&a))
64             || !TEST_ptr(ossl_list_buzz_head(&b))
65             || !TEST_ptr(ossl_list_buzz_tail(&b))
66             || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 3)
67             || !TEST_int_eq(ossl_list_fizz_tail(&a)->n, na * 3)
68             || !TEST_int_eq(ossl_list_buzz_head(&b)->n, nb * 5)
69             || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 5))
70         return 0;
71     ossl_list_fizz_remove(&a, ossl_list_fizz_head(&a));
72     ossl_list_buzz_remove(&b, ossl_list_buzz_tail(&b));
73     if (!TEST_size_t_eq(ossl_list_fizz_num(&a), --na)
74             || !TEST_size_t_eq(ossl_list_buzz_num(&b), --nb)
75             || !TEST_ptr(ossl_list_fizz_head(&a))
76             || !TEST_ptr(ossl_list_buzz_tail(&b))
77             || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 6)
78             || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 10)
79             || !TEST_ptr(ossl_list_fizz_next(ossl_list_fizz_head(&a)))
80             || !TEST_ptr(ossl_list_fizz_prev(ossl_list_fizz_tail(&a)))
81             || !TEST_int_eq(ossl_list_fizz_next(ossl_list_fizz_head(&a))->n, 9)
82             || !TEST_int_eq(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))->n, 15))
83         return 0;
84     return 1;
85 }
86 
87 typedef struct int_st INTL;
88 struct int_st {
89     int n;
90     OSSL_LIST_MEMBER(int, INTL);
91 };
92 
93 DEFINE_LIST_OF(int, INTL);
94 
test_insert(void)95 static int test_insert(void)
96 {
97     INTL *c, *d;
98     OSSL_LIST(int) l;
99     INTL elem[20];
100     size_t i;
101     int n = 1;
102 
103     ossl_list_int_init(&l);
104     for (i = 0; i < OSSL_NELEM(elem); i++) {
105         ossl_list_int_init_elem(elem + i);
106         elem[i].n = i;
107     }
108 
109     /* Check various insert options - head, tail, middle */
110     ossl_list_int_insert_head(&l, elem + 3);                /* 3 */
111     ossl_list_int_insert_tail(&l, elem + 6);                /* 3 6 */
112     ossl_list_int_insert_before(&l, elem + 6, elem + 5);    /* 3 5 6 */
113     ossl_list_int_insert_before(&l, elem + 3, elem + 1);    /* 1 3 5 6 */
114     ossl_list_int_insert_after(&l, elem + 1, elem + 2);     /* 1 2 3 5 6 */
115     ossl_list_int_insert_after(&l, elem + 6, elem + 7);     /* 1 2 3 5 6 7 */
116     ossl_list_int_insert_after(&l, elem + 3, elem + 4);     /* 1 2 3 4 5 6 7 */
117     if (!TEST_size_t_eq(ossl_list_int_num(&l), 7))
118         return 0;
119     c = ossl_list_int_head(&l);
120     d = ossl_list_int_tail(&l);
121     while (c != NULL && d != NULL) {
122         if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 8 - n))
123             return 0;
124         c = ossl_list_int_next(c);
125         d = ossl_list_int_prev(d);
126         n++;
127     }
128     if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
129         return 0;
130 
131     /* Check removing head, tail and middle */
132     ossl_list_int_remove(&l, elem + 1);                     /* 2 3 4 5 6 7 */
133     ossl_list_int_remove(&l, elem + 6);                     /* 2 3 4 5 7 */
134     ossl_list_int_remove(&l, elem + 7);                     /* 2 3 4 5 */
135     n = 2;
136     c = ossl_list_int_head(&l);
137     d = ossl_list_int_tail(&l);
138     while (c != NULL && d != NULL) {
139         if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 7 - n))
140             return 0;
141         c = ossl_list_int_next(c);
142         d = ossl_list_int_prev(d);
143         n++;
144     }
145     if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
146         return 0;
147 
148     /* Check removing the head of a two element list works */
149     ossl_list_int_remove(&l, elem + 2);                     /* 3 4 5 */
150     ossl_list_int_remove(&l, elem + 4);                     /* 3 5 */
151     ossl_list_int_remove(&l, elem + 3);                     /* 5 */
152     if (!TEST_ptr(ossl_list_int_head(&l))
153             || !TEST_ptr(ossl_list_int_tail(&l))
154             || !TEST_int_eq(ossl_list_int_head(&l)->n, 5)
155             || !TEST_int_eq(ossl_list_int_tail(&l)->n, 5))
156         return 0;
157 
158     /* Check removing the tail of a two element list works */
159     ossl_list_int_insert_head(&l, elem);                    /* 0 5 */
160     ossl_list_int_remove(&l, elem + 5);                     /* 0 */
161     if (!TEST_ptr(ossl_list_int_head(&l))
162             || !TEST_ptr(ossl_list_int_tail(&l))
163             || !TEST_int_eq(ossl_list_int_head(&l)->n, 0)
164             || !TEST_int_eq(ossl_list_int_tail(&l)->n, 0))
165         return 0;
166 
167     /* Check removing the only element works */
168     ossl_list_int_remove(&l, elem);
169     if (!TEST_ptr_null(ossl_list_int_head(&l))
170             || !TEST_ptr_null(ossl_list_int_tail(&l)))
171         return 0;
172     return 1;
173 }
174 
setup_tests(void)175 int setup_tests(void)
176 {
177     ADD_TEST(test_fizzbuzz);
178     ADD_TEST(test_insert);
179     return 1;
180 }
181