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 for (i = 1; i < nelem; i++) {
45 ossl_list_fizz_insert_tail(&a, elem + i);
49 ossl_list_buzz_insert_head(&b, elem + i);
54 if (!TEST_size_t_eq(ossl_list_fizz_num(&a), na)
55 || !TEST_size_t_eq(ossl_list_buzz_num(&b), nb)
56 || !TEST_ptr(ossl_list_fizz_head(&a))
57 || !TEST_ptr(ossl_list_fizz_tail(&a))
58 || !TEST_ptr(ossl_list_buzz_head(&b))
59 || !TEST_ptr(ossl_list_buzz_tail(&b))
60 || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 3)
61 || !TEST_int_eq(ossl_list_fizz_tail(&a)->n, na * 3)
62 || !TEST_int_eq(ossl_list_buzz_head(&b)->n, nb * 5)
63 || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 5))
65 ossl_list_fizz_remove(&a, ossl_list_fizz_head(&a));
66 ossl_list_buzz_remove(&b, ossl_list_buzz_tail(&b));
67 if (!TEST_size_t_eq(ossl_list_fizz_num(&a), --na)
68 || !TEST_size_t_eq(ossl_list_buzz_num(&b), --nb)
69 || !TEST_ptr(ossl_list_fizz_head(&a))
70 || !TEST_ptr(ossl_list_buzz_tail(&b))
71 || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 6)
72 || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 10)
73 || !TEST_ptr(ossl_list_fizz_next(ossl_list_fizz_head(&a)))
74 || !TEST_ptr(ossl_list_fizz_prev(ossl_list_fizz_tail(&a)))
75 || !TEST_int_eq(ossl_list_fizz_next(ossl_list_fizz_head(&a))->n, 9)
76 || !TEST_int_eq(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))->n, 15))
81 typedef struct int_st INTL;
84 OSSL_LIST_MEMBER(int, INTL);
87 DEFINE_LIST_OF(int, INTL);
89 static int test_insert(void)
97 ossl_list_int_init(&l);
98 for (i = 0; i < OSSL_NELEM(elem); i++)
101 /* Check various insert options - head, tail, middle */
102 ossl_list_int_insert_head(&l, elem + 3); /* 3 */
103 ossl_list_int_insert_tail(&l, elem + 6); /* 3 6 */
104 ossl_list_int_insert_before(&l, elem + 6, elem + 5); /* 3 5 6 */
105 ossl_list_int_insert_before(&l, elem + 3, elem + 1); /* 1 3 5 6 */
106 ossl_list_int_insert_after(&l, elem + 1, elem + 2); /* 1 2 3 5 6 */
107 ossl_list_int_insert_after(&l, elem + 6, elem + 7); /* 1 2 3 5 6 7 */
108 ossl_list_int_insert_after(&l, elem + 3, elem + 4); /* 1 2 3 4 5 6 7 */
109 if (!TEST_size_t_eq(ossl_list_int_num(&l), 7))
111 c = ossl_list_int_head(&l);
112 d = ossl_list_int_tail(&l);
113 while (c != NULL && d != NULL) {
114 if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 8 - n))
116 c = ossl_list_int_next(c);
117 d = ossl_list_int_prev(d);
120 if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
123 /* Check removing head, tail and middle */
124 ossl_list_int_remove(&l, elem + 1); /* 2 3 4 5 6 7 */
125 ossl_list_int_remove(&l, elem + 6); /* 2 3 4 5 7 */
126 ossl_list_int_remove(&l, elem + 7); /* 2 3 4 5 */
128 c = ossl_list_int_head(&l);
129 d = ossl_list_int_tail(&l);
130 while (c != NULL && d != NULL) {
131 if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 7 - n))
133 c = ossl_list_int_next(c);
134 d = ossl_list_int_prev(d);
137 if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
140 /* Check removing the head of a two element list works */
141 ossl_list_int_remove(&l, elem + 2); /* 3 4 5 */
142 ossl_list_int_remove(&l, elem + 4); /* 3 5 */
143 ossl_list_int_remove(&l, elem + 3); /* 5 */
144 if (!TEST_ptr(ossl_list_int_head(&l))
145 || !TEST_ptr(ossl_list_int_tail(&l))
146 || !TEST_int_eq(ossl_list_int_head(&l)->n, 5)
147 || !TEST_int_eq(ossl_list_int_tail(&l)->n, 5))
150 /* Check removing the tail of a two element list works */
151 ossl_list_int_insert_head(&l, elem); /* 0 5 */
152 ossl_list_int_remove(&l, elem + 5); /* 0 */
153 if (!TEST_ptr(ossl_list_int_head(&l))
154 || !TEST_ptr(ossl_list_int_tail(&l))
155 || !TEST_int_eq(ossl_list_int_head(&l)->n, 0)
156 || !TEST_int_eq(ossl_list_int_tail(&l)->n, 0))
159 /* Check removing the only element works */
160 ossl_list_int_remove(&l, elem);
161 if (!TEST_ptr_null(ossl_list_int_head(&l))
162 || !TEST_ptr_null(ossl_list_int_tail(&l)))
167 int setup_tests(void)
169 ADD_TEST(test_fizzbuzz);
170 ADD_TEST(test_insert);