event: Add a simple test
[platform/kernel/u-boot.git] / test / common / event.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Unit tests for event handling
4  *
5  * Copyright 2021 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <event.h>
12 #include <test/common.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15
16 struct test_state {
17         struct udevice *dev;
18         int val;
19 };
20
21 static int h_adder(void *ctx, struct event *event)
22 {
23         struct event_data_test *data = &event->data.test;
24         struct test_state *test_state = ctx;
25
26         test_state->val += data->signal;
27
28         return 0;
29 }
30
31 static int test_event_base(struct unit_test_state *uts)
32 {
33         struct test_state state;
34         int signal;
35
36         state.val = 12;
37         ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
38
39         signal = 17;
40
41         /* Check that the handler is called */
42         ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
43         ut_asserteq(12 + 17, state.val);
44
45         return 0;
46 }
47 COMMON_TEST(test_event_base, 0);