libamxc  1.10.3
C Generic Data Containers
test_amxc_astack.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <setjmp.h>
57 #include <stdarg.h>
58 #include <cmocka.h>
59 #include <amxc/amxc_astack.h>
60 
61 #include "test_amxc_astack.h"
62 
63 #include <amxc/amxc_macros.h>
65  // passing NULL pointers should not lead to segfault
66  assert_int_equal(amxc_astack_new(NULL), -1);
67  amxc_astack_delete(NULL, NULL);
68 }
69 
71  amxc_astack_t* astack = NULL;
72  assert_int_equal(amxc_astack_new(&astack), 0);
73  amxc_astack_delete(&astack, NULL);
74  assert_ptr_equal(astack, NULL);
75 }
76 
78  // passing NULL pointers should not lead to segfault
79  assert_int_equal(amxc_astack_init(NULL), -1);
80  amxc_astack_clean(NULL, NULL);
81 }
82 
84  amxc_astack_t astack;
85 
86  assert_int_equal(amxc_astack_init(&astack), 0);
87  amxc_astack_clean(&astack, NULL);
88 }
89 
90 void amxc_astack_push_check(UNUSED void** state) {
91  amxc_astack_t astack;
92  char data[] = "abcdefg";
93 
94  assert_int_equal(amxc_astack_init(&astack), 0);
95  assert_int_equal(amxc_astack_size(&astack), 0);
96  assert_int_equal(amxc_astack_is_empty(&astack), true);
97 
98  assert_ptr_not_equal(amxc_astack_push(&astack, &data[0]), NULL);
99  assert_ptr_not_equal(amxc_astack_push(&astack, &data[1]), NULL);
100  assert_ptr_not_equal(amxc_astack_push(&astack, &data[2]), NULL);
101  assert_ptr_not_equal(amxc_astack_push(&astack, &data[3]), NULL);
102 
103  assert_int_equal(amxc_astack_size(&astack), 4);
104  assert_int_equal(amxc_astack_is_empty(&astack), false);
105 
106  amxc_astack_clean(&astack, NULL);
107 }
108 
109 void amxc_astack_pop_check(UNUSED void** state) {
110  amxc_astack_t astack;
111  char data[] = "abcdefg";
112 
113  assert_int_equal(amxc_astack_init(&astack), 0);
114 
115  assert_ptr_not_equal(amxc_astack_push(&astack, &data[0]), NULL);
116  assert_ptr_not_equal(amxc_astack_push(&astack, &data[1]), NULL);
117  assert_ptr_not_equal(amxc_astack_push(&astack, &data[2]), NULL);
118  assert_ptr_not_equal(amxc_astack_push(&astack, &data[3]), NULL);
119 
120  assert_ptr_equal(amxc_astack_pop(&astack), &data[3]);
121  assert_ptr_equal(amxc_astack_pop(&astack), &data[2]);
122  assert_ptr_equal(amxc_astack_pop(&astack), &data[1]);
123  assert_ptr_equal(amxc_astack_pop(&astack), &data[0]);
124 
125  assert_int_equal(amxc_astack_size(&astack), 0);
126  assert_int_equal(amxc_astack_is_empty(&astack), true);
127 
128  assert_ptr_equal(amxc_astack_pop(&astack), NULL);
129 
130  amxc_astack_clean(&astack, NULL);
131 }
Ambiorix array stack API header file.
#define UNUSED
Definition: amxc_macros.h:70
AMXC_INLINE void amxc_astack_clean(amxc_astack_t *const astack, amxc_astack_it_delete_t func)
Removes all items from the array stack.
Definition: amxc_astack.h:205
AMXC_INLINE bool amxc_astack_is_empty(const amxc_astack_t *const astack)
Checks that the array stack is empty.
Definition: amxc_astack.h:286
AMXC_INLINE int amxc_astack_init(amxc_astack_t *const astack)
Initializes an array stack.
Definition: amxc_astack.h:188
AMXC_INLINE void amxc_astack_delete(amxc_astack_t **astack, amxc_astack_it_delete_t func)
Frees the previously allocated array stack.
Definition: amxc_astack.h:161
AMXC_INLINE size_t amxc_astack_size(const amxc_astack_t *const astack)
Calculate the number of items on the stack, expressed in number of items.
Definition: amxc_astack.h:270
AMXC_INLINE int amxc_astack_new(amxc_astack_t **astack)
Allocates an array stack.
Definition: amxc_astack.h:137
AMXC_INLINE void * amxc_astack_pop(amxc_astack_t *const astack)
Removes the last added data from the stack.
Definition: amxc_astack.h:239
AMXC_INLINE amxc_astack_it_t * amxc_astack_push(amxc_astack_t *const astack, void *data)
Adds an item to the array stack.
Definition: amxc_astack.h:223
The array structure.
Definition: amxc_array.h:162
char data[]
void amxc_astack_init_clean_null_check(UNUSED void **state)
void amxc_astack_push_check(UNUSED void **state)
void amxc_astack_init_clean_check(UNUSED void **state)
void amxc_astack_new_delete_check(UNUSED void **state)
void amxc_astack_pop_check(UNUSED void **state)
void amxc_astack_new_delete_null_check(UNUSED void **state)