Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 4 | * |
| 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
| 6 | * this file except in compliance with the License. You can obtain a copy |
| 7 | * in the file LICENSE in the source distribution or at |
| 8 | * https://www.openssl.org/source/license.html |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <limits.h> |
| 14 | |
| 15 | #include <openssl/crypto.h> |
| 16 | #include <internal/nelem.h> |
| 17 | |
| 18 | #include "internal/sparse_array.h" |
| 19 | #include "testutil.h" |
| 20 | |
| 21 | /* The macros below generate unused functions which error out one of the clang |
| 22 | * builds. We disable this check here. |
| 23 | */ |
| 24 | #ifdef __clang__ |
| 25 | #pragma clang diagnostic ignored "-Wunused-function" |
| 26 | #endif |
| 27 | |
| 28 | DEFINE_SPARSE_ARRAY_OF(char); |
| 29 | |
| 30 | static int test_sparse_array(void) |
| 31 | { |
| 32 | static const struct { |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 33 | ossl_uintmax_t n; |
Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 34 | char *v; |
| 35 | } cases[] = { |
| 36 | { 22, "a" }, { 0, "z" }, { 1, "b" }, { 290, "c" }, |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 37 | { INT_MAX, "m" }, { 6666666, "d" }, { (ossl_uintmax_t)-1, "H" }, |
Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 38 | { 99, "e" } |
| 39 | }; |
| 40 | SPARSE_ARRAY_OF(char) *sa; |
| 41 | size_t i, j; |
| 42 | int res = 0; |
| 43 | |
| 44 | if (!TEST_ptr(sa = ossl_sa_char_new()) |
| 45 | || !TEST_ptr_null(ossl_sa_char_get(sa, 3)) |
| 46 | || !TEST_ptr_null(ossl_sa_char_get(sa, 0)) |
| 47 | || !TEST_ptr_null(ossl_sa_char_get(sa, UINT_MAX))) |
| 48 | goto err; |
| 49 | |
| 50 | for (i = 0; i < OSSL_NELEM(cases); i++) { |
| 51 | if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) { |
| 52 | TEST_note("iteration %zu", i + 1); |
| 53 | goto err; |
| 54 | } |
| 55 | for (j = 0; j <= i; j++) |
| 56 | if (!TEST_str_eq(ossl_sa_char_get(sa, cases[j].n), cases[j].v)) { |
| 57 | TEST_note("iteration %zu / %zu", i + 1, j + 1); |
| 58 | goto err; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | res = 1; |
| 63 | err: |
| 64 | ossl_sa_char_free(sa); |
| 65 | return res; |
| 66 | } |
| 67 | |
| 68 | static int test_sparse_array_num(void) |
| 69 | { |
| 70 | static const struct { |
| 71 | size_t num; |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 72 | ossl_uintmax_t n; |
Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 73 | char *v; |
| 74 | } cases[] = { |
| 75 | { 1, 22, "a" }, { 2, 1021, "b" }, { 3, 3, "c" }, { 2, 22, NULL }, |
| 76 | { 2, 3, "d" }, { 3, 22, "e" }, { 3, 666, NULL }, { 4, 666, "f" }, |
| 77 | { 3, 3, NULL }, { 2, 22, NULL }, { 1, 666, NULL }, { 2, 64000, "g" }, |
| 78 | { 1, 1021, NULL }, { 0, 64000, NULL }, { 1, 23, "h" }, { 0, 23, NULL } |
| 79 | }; |
| 80 | SPARSE_ARRAY_OF(char) *sa = NULL; |
| 81 | size_t i; |
| 82 | int res = 0; |
| 83 | |
| 84 | if (!TEST_size_t_eq(ossl_sa_char_num(NULL), 0) |
| 85 | || !TEST_ptr(sa = ossl_sa_char_new()) |
| 86 | || !TEST_size_t_eq(ossl_sa_char_num(sa), 0)) |
| 87 | goto err; |
| 88 | for (i = 0; i < OSSL_NELEM(cases); i++) |
| 89 | if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v)) |
| 90 | || !TEST_size_t_eq(ossl_sa_char_num(sa), cases[i].num)) |
| 91 | goto err; |
| 92 | res = 1; |
| 93 | err: |
| 94 | ossl_sa_char_free(sa); |
| 95 | return res; |
| 96 | } |
| 97 | |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 98 | struct index_cases_st { |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 99 | ossl_uintmax_t n; |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 100 | char *v; |
| 101 | int del; |
| 102 | }; |
| 103 | |
| 104 | struct doall_st { |
| 105 | SPARSE_ARRAY_OF(char) *sa; |
| 106 | size_t num_cases; |
| 107 | const struct index_cases_st *cases; |
| 108 | int res; |
| 109 | int all; |
| 110 | }; |
| 111 | |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 112 | static void leaf_check_all(ossl_uintmax_t n, char *value, void *arg) |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 113 | { |
| 114 | struct doall_st *doall_data = (struct doall_st *)arg; |
| 115 | const struct index_cases_st *cases = doall_data->cases; |
| 116 | size_t i; |
| 117 | |
| 118 | doall_data->res = 0; |
| 119 | for (i = 0; i < doall_data->num_cases; i++) |
| 120 | if ((doall_data->all || !cases[i].del) |
| 121 | && n == cases[i].n && strcmp(value, cases[i].v) == 0) { |
| 122 | doall_data->res = 1; |
| 123 | return; |
| 124 | } |
Bernd Edlinger | 35f6fe7 | 2019-07-29 12:26:06 +0200 | [diff] [blame] | 125 | TEST_error("Index %ju with value %s not found", n, value); |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 126 | } |
| 127 | |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 128 | static void leaf_delete(ossl_uintmax_t n, char *value, void *arg) |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 129 | { |
| 130 | struct doall_st *doall_data = (struct doall_st *)arg; |
| 131 | const struct index_cases_st *cases = doall_data->cases; |
| 132 | size_t i; |
| 133 | |
| 134 | doall_data->res = 0; |
| 135 | for (i = 0; i < doall_data->num_cases; i++) |
| 136 | if (n == cases[i].n && strcmp(value, cases[i].v) == 0) { |
| 137 | doall_data->res = 1; |
| 138 | ossl_sa_char_set(doall_data->sa, n, NULL); |
| 139 | return; |
| 140 | } |
Bernd Edlinger | 35f6fe7 | 2019-07-29 12:26:06 +0200 | [diff] [blame] | 141 | TEST_error("Index %ju with value %s not found", n, value); |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int test_sparse_array_doall(void) |
| 145 | { |
| 146 | static const struct index_cases_st cases[] = { |
| 147 | { 22, "A", 1 }, { 1021, "b", 0 }, { 3, "c", 0 }, { INT_MAX, "d", 1 }, |
Pauli | 8ab53b1 | 2019-03-06 13:50:54 +1000 | [diff] [blame] | 148 | { (ossl_uintmax_t)-1, "H", 0 }, { (ossl_uintmax_t)-2, "i", 1 }, |
| 149 | { 666666666, "s", 1 }, { 1234567890, "t", 0 }, |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 150 | }; |
| 151 | struct doall_st doall_data; |
| 152 | size_t i; |
| 153 | SPARSE_ARRAY_OF(char) *sa = NULL; |
| 154 | int res = 0; |
| 155 | |
| 156 | if (!TEST_ptr(sa = ossl_sa_char_new())) |
| 157 | goto err; |
| 158 | doall_data.num_cases = OSSL_NELEM(cases); |
| 159 | doall_data.cases = cases; |
| 160 | doall_data.all = 1; |
| 161 | doall_data.sa = NULL; |
| 162 | for (i = 0; i < OSSL_NELEM(cases); i++) |
| 163 | if (!TEST_true(ossl_sa_char_set(sa, cases[i].n, cases[i].v))) { |
| 164 | TEST_note("failed at iteration %zu", i + 1); |
| 165 | goto err; |
| 166 | } |
Pauli | 1bdbdaf | 2018-11-16 11:44:30 +1000 | [diff] [blame] | 167 | |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 168 | ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data); |
| 169 | if (doall_data.res == 0) { |
| 170 | TEST_info("while checking all elements"); |
| 171 | goto err; |
| 172 | } |
| 173 | doall_data.all = 0; |
| 174 | doall_data.sa = sa; |
| 175 | ossl_sa_char_doall_arg(sa, &leaf_delete, &doall_data); |
| 176 | if (doall_data.res == 0) { |
| 177 | TEST_info("while deleting selected elements"); |
| 178 | goto err; |
| 179 | } |
| 180 | ossl_sa_char_doall_arg(sa, &leaf_check_all, &doall_data); |
| 181 | if (doall_data.res == 0) { |
| 182 | TEST_info("while checking for deleted elements"); |
| 183 | goto err; |
| 184 | } |
| 185 | res = 1; |
| 186 | |
| 187 | err: |
| 188 | ossl_sa_char_free(sa); |
| 189 | return res; |
| 190 | } |
| 191 | |
Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 192 | int setup_tests(void) |
| 193 | { |
| 194 | ADD_TEST(test_sparse_array); |
| 195 | ADD_TEST(test_sparse_array_num); |
Pauli | 008b4ff | 2019-02-14 08:13:58 +1000 | [diff] [blame] | 196 | ADD_TEST(test_sparse_array_doall); |
Pauli | a40f0f6 | 2019-01-24 12:15:54 +1000 | [diff] [blame] | 197 | return 1; |
| 198 | } |