2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/opensslconf.h>
14 #include <openssl/err.h>
15 #include <openssl/crypto.h>
17 #include "internal/list.h"
18 #include "internal/nelem.h"
21 typedef struct testl_st TESTL;
24 OSSL_LIST_MEMBER(fizz, TESTL);
25 OSSL_LIST_MEMBER(buzz, TESTL);
28 DEFINE_LIST_OF(fizz, TESTL);
29 DEFINE_LIST_OF(buzz, TESTL);
31 static int test_fizzbuzz(void)
36 const int nelem = OSSL_NELEM(elem);
37 int i, na = 0, nb = 0;
39 ossl_list_fizz_init(&a);
40 ossl_list_buzz_init(&b);
42 if (!TEST_true(ossl_list_fizz_is_empty(&a)))
45 for (i = 1; i < nelem; i++) {
46 ossl_list_fizz_init_elem(elem + i);
47 ossl_list_buzz_init_elem(elem + i);
50 ossl_list_fizz_insert_tail(&a, elem + i);
54 ossl_list_buzz_insert_head(&b, elem + i);
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))
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))
87 typedef struct int_st INTL;
90 OSSL_LIST_MEMBER(int, INTL);
93 DEFINE_LIST_OF(int, INTL);
95 static int test_insert(void)
103 ossl_list_int_init(&l);
104 for (i = 0; i < OSSL_NELEM(elem); i++) {
105 ossl_list_int_init_elem(elem + i);
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))
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))
124 c = ossl_list_int_next(c);
125 d = ossl_list_int_prev(d);
128 if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
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 */
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))
141 c = ossl_list_int_next(c);
142 d = ossl_list_int_prev(d);
145 if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
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))
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))
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)))
175 int setup_tests(void)
177 ADD_TEST(test_fizzbuzz);
178 ADD_TEST(test_insert);