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