summaryrefslogtreecommitdiffstats
path: root/test/list_test.c
blob: db288d1d08c025b4569756a22230721522eefe6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>

#include <openssl/opensslconf.h>
#include <openssl/err.h>
#include <openssl/crypto.h>

#include "internal/list.h"
#include "internal/nelem.h"
#include "testutil.h"

typedef struct testl_st TESTL;
struct testl_st {
    int n;
    OSSL_LIST_MEMBER(fizz, TESTL);
    OSSL_LIST_MEMBER(buzz, TESTL);
};

DEFINE_LIST_OF(fizz, TESTL);
DEFINE_LIST_OF(buzz, TESTL);

static int test_fizzbuzz(void)
{
    OSSL_LIST(fizz) a;
    OSSL_LIST(buzz) b;
    TESTL elem[20];
    const int nelem = OSSL_NELEM(elem);
    int i, na = 0, nb = 0;

    ossl_list_fizz_init(&a);
    ossl_list_buzz_init(&b);

    if (!TEST_true(ossl_list_fizz_is_empty(&a)))
        return 0;

    for (i = 1; i < nelem; i++) {
        ossl_list_fizz_init_elem(elem + i);
        ossl_list_buzz_init_elem(elem + i);
        elem[i].n = i;
        if (i % 3 == 0) {
            ossl_list_fizz_insert_tail(&a, elem + i);
            na++;
        }
        if (i % 5 == 0) {
            ossl_list_buzz_insert_head(&b, elem + i);
            nb++;
        }
    }

    if (!TEST_false(ossl_list_fizz_is_empty(&a))
            || !TEST_size_t_eq(ossl_list_fizz_num(&a), na)
            || !TEST_size_t_eq(ossl_list_buzz_num(&b), nb)
            || !TEST_ptr(ossl_list_fizz_head(&a))
            || !TEST_ptr(ossl_list_fizz_tail(&a))
            || !TEST_ptr(ossl_list_buzz_head(&b))
            || !TEST_ptr(ossl_list_buzz_tail(&b))
            || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 3)
            || !TEST_int_eq(ossl_list_fizz_tail(&a)->n, na * 3)
            || !TEST_int_eq(ossl_list_buzz_head(&b)->n, nb * 5)
            || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 5))
        return 0;
    ossl_list_fizz_remove(&a, ossl_list_fizz_head(&a));
    ossl_list_buzz_remove(&b, ossl_list_buzz_tail(&b));
    if (!TEST_size_t_eq(ossl_list_fizz_num(&a), --na)
            || !TEST_size_t_eq(ossl_list_buzz_num(&b), --nb)
            || !TEST_ptr(ossl_list_fizz_head(&a))
            || !TEST_ptr(ossl_list_buzz_tail(&b))
            || !TEST_int_eq(ossl_list_fizz_head(&a)->n, 6)
            || !TEST_int_eq(ossl_list_buzz_tail(&b)->n, 10)
            || !TEST_ptr(ossl_list_fizz_next(ossl_list_fizz_head(&a)))
            || !TEST_ptr(ossl_list_fizz_prev(ossl_list_fizz_tail(&a)))
            || !TEST_int_eq(ossl_list_fizz_next(ossl_list_fizz_head(&a))->n, 9)
            || !TEST_int_eq(ossl_list_fizz_prev(ossl_list_fizz_tail(&a))->n, 15))
        return 0;
    return 1;
}

typedef struct int_st INTL;
struct int_st {
    int n;
    OSSL_LIST_MEMBER(int, INTL);
};

DEFINE_LIST_OF(int, INTL);

static int test_insert(void)
{
    INTL *c, *d;
    OSSL_LIST(int) l;
    INTL elem[20];
    size_t i;
    int n = 1;

    ossl_list_int_init(&l);
    for (i = 0; i < OSSL_NELEM(elem); i++) {
        ossl_list_int_init_elem(elem + i);
        elem[i].n = i;
    }

    /* Check various insert options - head, tail, middle */
    ossl_list_int_insert_head(&l, elem + 3);                /* 3 */
    ossl_list_int_insert_tail(&l, elem + 6);                /* 3 6 */
    ossl_list_int_insert_before(&l, elem + 6, elem + 5);    /* 3 5 6 */
    ossl_list_int_insert_before(&l, elem + 3, elem + 1);    /* 1 3 5 6 */
    ossl_list_int_insert_after(&l, elem + 1, elem + 2);     /* 1 2 3 5 6 */
    ossl_list_int_insert_after(&l, elem + 6, elem + 7);     /* 1 2 3 5 6 7 */
    ossl_list_int_insert_after(&l, elem + 3, elem + 4);     /* 1 2 3 4 5 6 7 */
    if (!TEST_size_t_eq(ossl_list_int_num(&l), 7))
        return 0;
    c = ossl_list_int_head(&l);
    d = ossl_list_int_tail(&l);
    while (c != NULL && d != NULL) {
        if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 8 - n))
            return 0;
        c = ossl_list_int_next(c);
        d = ossl_list_int_prev(d);
        n++;
    }
    if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
        return 0;

    /* Check removing head, tail and middle */
    ossl_list_int_remove(&l, elem + 1);                     /* 2 3 4 5 6 7 */
    ossl_list_int_remove(&l, elem + 6);                     /* 2 3 4 5 7 */
    ossl_list_int_remove(&l, elem + 7);                     /* 2 3 4 5 */
    n = 2;
    c = ossl_list_int_head(&l);
    d = ossl_list_int_tail(&l);
    while (c != NULL && d != NULL) {
        if (!TEST_int_eq(c->n, n) || !TEST_int_eq(d->n, 7 - n))
            return 0;
        c = ossl_list_int_next(c);
        d = ossl_list_int_prev(d);
        n++;
    }
    if (!TEST_ptr_null(c) || !TEST_ptr_null(d))
        return 0;

    /* Check removing the head of a two element list works */
    ossl_list_int_remove(&l, elem + 2);                     /* 3 4 5 */
    ossl_list_int_remove(&l, elem + 4);                     /* 3 5 */
    ossl_list_int_remove(&l, elem + 3);                     /* 5 */
    if (!TEST_ptr(ossl_list_int_head(&l))
            || !TEST_ptr(ossl_list_int_tail(&l))
            || !TEST_int_eq(ossl_list_int_head(&l)->n, 5)
            || !TEST_int_eq(ossl_list_int_tail(&l)->n, 5))
        return 0;

    /* Check removing the tail of a two element list works */
    ossl_list_int_insert_head(&l, elem);                    /* 0 5 */
    ossl_list_int_remove(&l, elem + 5);                     /* 0 */
    if (!TEST_ptr(ossl_list_int_head(&l))
            || !TEST_ptr(ossl_list_int_tail(&l))
            || !TEST_int_eq(ossl_list_int_head(&l)->n, 0)
            || !TEST_int_eq(ossl_list_int_tail(&l)->n, 0))
        return 0;

    /* Check removing the only element works */
    ossl_list_int_remove(&l, elem);
    if (!TEST_ptr_null(ossl_list_int_head(&l))
            || !TEST_ptr_null(ossl_list_int_tail(&l)))
        return 0;
    return 1;
}

int setup_tests(void)
{
    ADD_TEST(test_fizzbuzz);
    ADD_TEST(test_insert);
    return 1;
}