-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.c
191 lines (147 loc) · 5.05 KB
/
main.c
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
181
182
183
184
185
186
187
188
189
190
191
/**
* @file main.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#define EVENT_BIT_0 0x1
#define EVENT_BIT_1 0x2
#define EVENT_BIT_2 0x4
#define EVENT_BIT_3 0x8
static int test_read_write(struct harness_t *harness)
{
uint32_t mask;
struct event_t event;
BTASSERT(event_init(&event) == 0);
/* Write two events. */
mask = (EVENT_BIT_1 | EVENT_BIT_0);
BTASSERT(chan_write(&event, &mask, sizeof(mask)) == 4);
BTASSERT(chan_size(&event) == 1);
/* Read first event. */
mask = EVENT_BIT_0;
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == EVENT_BIT_0);
/* Read second event. */
mask = (EVENT_BIT_1 | EVENT_BIT_0);
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == EVENT_BIT_1);
/* Write second and third events. */
mask = (EVENT_BIT_2 | EVENT_BIT_1);
BTASSERT(chan_write(&event, &mask, sizeof(mask)) == 4);
/* Write first event. */
mask = EVENT_BIT_0;
BTASSERT(chan_write(&event, &mask, sizeof(mask)) == 4);
/* Read first and second events. */
mask = (EVENT_BIT_1 | EVENT_BIT_0);
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == (EVENT_BIT_1 | EVENT_BIT_0));
/* Read third events. */
mask = EVENT_BIT_2;
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == EVENT_BIT_2);
BTASSERT(chan_size(&event) == 0);
return (0);
}
static int test_poll(struct harness_t *harness)
{
uint32_t mask;
struct event_t event;
BTASSERT(event_init(&event) == 0);
mask = (EVENT_BIT_0);
BTASSERT(chan_write(&event, &mask, sizeof(mask)) == 4);
/* Poll the channel and make sure the event channel has an event
set.*/
BTASSERT(chan_poll(&event, NULL) == &event);
BTASSERT(chan_size(&event) == 1);
return (0);
}
static int test_poll_timeout(struct harness_t *harness)
{
struct event_t event;
struct time_t timeout;
BTASSERT(event_init(&event) == 0);
/* Poll the channel and make sure a timeout occured.*/
timeout.seconds = 0;
timeout.nanoseconds = 10000;
BTASSERT(chan_poll(&event, &timeout) == NULL);
BTASSERT(chan_size(&event) == 0);
return (0);
}
static int test_poll_list(struct harness_t *harness)
{
uint32_t mask;
struct event_t event;
struct event_t event_dummy;
struct chan_list_t list;
char workspace[64];
BTASSERT(event_init(&event) == 0);
BTASSERT(event_init(&event_dummy) == 0);
/* Use a list with one chan.*/
BTASSERT(chan_list_init(&list, workspace, sizeof(workspace)) == 0);
BTASSERT(chan_list_add(&list, &event) == 0);
BTASSERT(chan_list_add(&list, &event_dummy) == 0);
mask = (EVENT_BIT_3 | EVENT_BIT_2);
BTASSERT(chan_write(&event, &mask, sizeof(mask)) == 4);
/* Poll the list of channels and make sure the event channel has
events set.*/
BTASSERT(chan_list_poll(&list, NULL) == &event);
BTASSERT(chan_size(&event) == 1);
mask = EVENT_BIT_2;
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == EVENT_BIT_2);
BTASSERT(chan_size(&event) == 1);
mask = EVENT_BIT_3;
BTASSERT(chan_read(&event, &mask, sizeof(mask)) == 4);
BTASSERT(mask == EVENT_BIT_3);
BTASSERT(chan_size(&event) == 0);
return (0);
}
static int test_poll_list_timeout(struct harness_t *harness)
{
struct event_t event;
struct chan_list_t list;
char workspace[64];
struct time_t timeout;
BTASSERT(event_init(&event) == 0);
/* Use a list with one chan.*/
BTASSERT(chan_list_init(&list, workspace, sizeof(workspace)) == 0);
BTASSERT(chan_list_add(&list, &event) == 0);
/* Poll the list of channels and make sure an timeout occured. */
timeout.seconds = 0;
timeout.nanoseconds = 10000;
BTASSERT(chan_list_poll(&list, &timeout) == NULL);
/* Poll the list of channels and make sure an timeout occured. */
timeout.seconds = 0;
timeout.nanoseconds = 0;
BTASSERT(chan_list_poll(&list, &timeout) == NULL);
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_read_write, "test_read_write" },
{ test_poll, "test_poll" },
{ test_poll_timeout, "test_poll_timeout" },
{ test_poll_list, "test_poll_list" },
{ test_poll_list_timeout, "test_poll_list_timeout" },
{ NULL, NULL }
};
sys_start();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}