libamxp  1.4.0
Patterns C Implementation
test_signal_slots_recursive.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 <stdio.h>
57 #include <stdarg.h>
58 #include <stddef.h>
59 #include <setjmp.h>
60 #include <fcntl.h>
61 #include <unistd.h>
62 #include <cmocka.h>
63 
64 #include <amxc/amxc_macros.h>
65 #include <amxc/amxc_variant.h>
66 #include <amxc/amxc_lqueue.h>
67 
68 #include <amxp/amxp_signal.h>
69 #include <amxp/amxp_slot.h>
70 #include <amxp_signal_priv.h>
71 
73 
74 static amxp_signal_t* sig1 = NULL;
75 static amxp_signal_t* sig2 = NULL;
76 static amxp_signal_mngr_t* sigmngr = NULL;
77 
78 static void test_slot_delete_sigmngr(UNUSED const char* const sig_name,
79  UNUSED const amxc_var_t* const data,
80  UNUSED void* const priv) {
81  if(sig1 != NULL) {
83  }
84 }
85 
86 static void test_slot_delete_signal(UNUSED const char* const sig_name,
87  UNUSED const amxc_var_t* const data,
88  UNUSED void* const priv) {
90 }
91 
92 static void test_slot1(UNUSED const char* const sig_name,
93  UNUSED const amxc_var_t* const data,
94  UNUSED void* const priv) {
95  printf("Test slot 1\n");
96  amxp_sigmngr_trigger_signal(NULL, "test:signal2", NULL);
97 }
98 
99 static void test_slot_disconnect(UNUSED const char* const sig_name,
100  UNUSED const amxc_var_t* const data,
101  UNUSED void* const priv) {
102  printf("Test slot disconnect\n");
104 }
105 
106 static void test_slot2(UNUSED const char* const sig_name,
107  UNUSED const amxc_var_t* const data,
108  UNUSED void* const priv) {
109  printf("Test slot 2\n");
110 }
111 
113  assert_int_equal(amxp_signal_new(NULL, &sig1, "test:signal1"), 0);
114  assert_int_equal(amxp_signal_new(NULL, &sig2, "test:signal2"), 0);
115 
116  assert_int_equal(amxp_slot_connect(NULL, "test:signal1", NULL, test_slot1, NULL), 0);
117  assert_int_equal(amxp_slot_connect(NULL, "test:signal2", NULL, test_slot_delete_sigmngr, NULL), 0);
118 
119  amxp_sigmngr_trigger_signal(NULL, "test:signal1", NULL);
120 
121  amxp_sigmngr_trigger_signal(NULL, "test:signal1", NULL);
122  amxp_sigmngr_trigger_signal(NULL, "test:signal2", NULL);
123 
124  // sig1 and sig2 will be deleted due to clean-up of global sigmngr
125  // caused by deleting the global sigmngr
126  sig1 = NULL;
127  sig2 = NULL;
128 }
129 
131  assert_int_equal(amxp_sigmngr_new(&sigmngr), 0);
132  assert_int_equal(amxp_signal_new(sigmngr, &sig1, "test:signal1"), 0);
133  assert_int_equal(amxp_signal_new(NULL, &sig2, "test:signal2"), 0);
134 
135  assert_int_equal(amxp_slot_connect(sigmngr, "test:signal1", NULL, test_slot1, NULL), 0);
136  assert_int_equal(amxp_slot_connect(NULL, "test:signal2", NULL, test_slot_delete_sigmngr, NULL), 0);
137 
138  amxp_sigmngr_trigger_signal(sigmngr, "test:signal1", NULL);
139  sigmngr = NULL; // deleted due to trigger of slot 2 which deletes sigmngr of sig1
140  sig1 = NULL; // deleted due to trigger of slot 2 which deletes sigmngr of sig1
141 
142  amxp_sigmngr_trigger_signal(NULL, "test:signal2", NULL);
143 
145 }
146 
148  assert_int_equal(amxp_sigmngr_new(&sigmngr), 0);
149  assert_int_equal(amxp_signal_new(sigmngr, &sig1, "test:signal1"), 0);
150  assert_int_equal(amxp_signal_new(NULL, &sig2, "test:signal2"), 0);
151 
152  assert_int_equal(amxp_slot_connect(sigmngr, "test:signal1", NULL, test_slot1, NULL), 0);
153  assert_int_equal(amxp_slot_connect(sigmngr, "test:signal1", NULL, test_slot2, NULL), 0);
154  assert_int_equal(amxp_slot_connect(NULL, "test:signal2", NULL, test_slot_delete_signal, NULL), 0);
155 
156  amxp_sigmngr_trigger_signal(sigmngr, "test:signal1", NULL);
157 
160 }
161 
163  assert_int_equal(amxp_sigmngr_new(&sigmngr), 0);
164  assert_int_equal(amxp_signal_new(sigmngr, &sig1, "test:signal1"), 0);
165  assert_int_equal(amxp_signal_new(NULL, &sig2, "test:signal2"), 0);
166 
167  assert_int_equal(amxp_slot_connect(sigmngr, "test:signal1", NULL, test_slot_disconnect, NULL), 0);
168  assert_int_equal(amxp_slot_connect(sigmngr, "test:signal1", NULL, test_slot1, NULL), 0);
169  assert_int_equal(amxp_slot_connect(NULL, "test:signal2", NULL, test_slot_delete_signal, NULL), 0);
170 
171  amxp_sigmngr_trigger_signal(sigmngr, "test:signal1", NULL);
172 
175 }
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
#define UNUSED
Definition: main.c:68
int amxp_sigmngr_new(amxp_signal_mngr_t **sig_mngr)
Constructor function, creates a new signal manager instance.
Definition: amxp_signal.c:330
void amxp_sigmngr_trigger_signal(amxp_signal_mngr_t *const sig_mngr, const char *name, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:492
int amxp_sigmngr_delete(amxp_signal_mngr_t **sig_mngr)
Destructor function, deletes a signal manager instance.
Definition: amxp_signal.c:349
int amxp_signal_new(amxp_signal_mngr_t *sig_mngr, amxp_signal_t **signal, const char *name)
Constructor function, creates a new signal.
Definition: amxp_signal.c:620
int amxp_signal_delete(amxp_signal_t **signal)
Destructor function, deletes a signal.
Definition: amxp_signal.c:669
void amxp_slot_disconnect_all(amxp_slot_fn_t fn)
Disconnects a slot from all signals it was connected to.
Definition: amxp_slot.c:459
int amxp_slot_connect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to a named signal of a signal manager.
Definition: amxp_slot.c:300
Structure containing the signal manager information.
Definition: amxp_signal.h:103
Structure containing the signal information.
Definition: amxp_signal.h:119
amxp_signal_mngr_t * mngr
Definition: amxp_signal.h:123
void test_signal_slot_disconnect(UNUSED void **state)
static void test_slot_disconnect(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static amxp_signal_mngr_t * sigmngr
static void test_slot_delete_signal(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static void test_slot1(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
void test_signal_recursive_delete_global_sigmngr(UNUSED void **state)
static amxp_signal_t * sig2
void test_signal_recursive_delete_sigmngr(UNUSED void **state)
static amxp_signal_t * sig1
void test_signal_recursive_delete_signal(UNUSED void **state)
static void test_slot_delete_sigmngr(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
static void test_slot2(UNUSED const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)