2
0

list_test.c 5.9 KB

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