2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
15 #include <openssl/crypto.h>
16 #include <internal/nelem.h>
18 #include "internal/sparse_array.h"
21 /* The macros below generate unused functions which error out one of the clang
22 * builds. We disable this check here.
25 #pragma clang diagnostic ignored "-Wunused-function"
28 DEFINE_SPARSE_ARRAY_OF(char);
30 static int test_sparse_array(void)
36 { 22, "a" }, { 0, "z" }, { 1, "b" }, { 290, "c" },
37 { INT_MAX, "m" }, { 6666666, "d" }, { (size_t)-1, "H" },
40 SPARSE_ARRAY_OF(char) *sa;
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)))
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);
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);
64 ossl_sa_char_free(sa);
68 static int test_sparse_array_num(void)
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 }
80 SPARSE_ARRAY_OF(char) *sa = NULL;
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))
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))
94 ossl_sa_char_free(sa);
100 ADD_TEST(test_sparse_array);
101 ADD_TEST(test_sparse_array_num);