1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
3
* unit tests for atomic operations.
5
* Copyright (c) 2010 Miru Limited.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
/* mock functions for external references */
35
#define PGM_COMPILATION
36
#include "pgm/atomic.h"
41
* pgm_atomic_exchange_and_add32 (
42
* volatile uint32_t* atomic,
47
START_TEST (test_int32_exchange_and_add_pass_001)
49
volatile uint32_t atomic = 0;
50
fail_unless (0 == pgm_atomic_exchange_and_add32 (&atomic, 5), "xadd failed");
51
fail_unless (5 == atomic, "xadd failed");
52
fail_unless (5 == pgm_atomic_exchange_and_add32 (&atomic, (uint32_t)-10), "xadd failed");
53
fail_unless ((uint32_t)-5 == atomic, "xadd failed");
60
* volatile uint32_t* atomic,
65
START_TEST (test_int32_add_pass_001)
67
volatile uint32_t atomic = (uint32_t)-5;
68
pgm_atomic_add32 (&atomic, 20);
69
fail_unless (15 == atomic, "add failed");
70
pgm_atomic_add32 (&atomic, (uint32_t)-35);
71
fail_unless ((uint32_t)-20 == atomic, "add failed");
75
/* ensure wrap around when casting uint32 */
76
START_TEST (test_int32_add_pass_002)
78
volatile uint32_t atomic = 0;
79
pgm_atomic_add32 (&atomic, UINT32_MAX/2);
80
fail_unless ((UINT32_MAX/2) == atomic, "add failed");
81
pgm_atomic_add32 (&atomic, UINT32_MAX - (UINT32_MAX/2));
82
fail_unless (UINT32_MAX == atomic, "add failed");
83
pgm_atomic_add32 (&atomic, 1);
84
fail_unless (0 == atomic, "add failed");
91
* volatile uint32_t* atomic
95
START_TEST (test_int32_get_pass_001)
97
volatile uint32_t atomic = (uint32_t)-20;
98
fail_unless ((uint32_t)-20 == pgm_atomic_read32 (&atomic), "read failed");
104
* pgm_atomic_int32_set (
105
* volatile int32_t* atomic,
110
START_TEST (test_int32_set_pass_001)
112
volatile uint32_t atomic = (uint32_t)-20;
113
pgm_atomic_write32 (&atomic, 5);
114
fail_unless (5 == atomic, "write failed");
121
make_test_suite (void)
125
s = suite_create (__FILE__);
127
TCase* tc_exchange_and_add = tcase_create ("exchange-and-add");
128
suite_add_tcase (s, tc_exchange_and_add);
129
tcase_add_test (tc_exchange_and_add, test_int32_exchange_and_add_pass_001);
131
TCase* tc_add = tcase_create ("add");
132
suite_add_tcase (s, tc_add);
133
tcase_add_test (tc_add, test_int32_add_pass_001);
134
tcase_add_test (tc_add, test_int32_add_pass_002);
136
TCase* tc_get = tcase_create ("get");
137
suite_add_tcase (s, tc_get);
138
tcase_add_test (tc_get, test_int32_get_pass_001);
140
TCase* tc_set = tcase_create ("set");
141
suite_add_tcase (s, tc_set);
142
tcase_add_test (tc_set, test_int32_set_pass_001);
149
make_master_suite (void)
151
Suite* s = suite_create ("Master");
158
SRunner* sr = srunner_create (make_master_suite ());
159
srunner_add_suite (sr, make_test_suite ());
160
srunner_run_all (sr, CK_ENV);
161
int number_failed = srunner_ntests_failed (sr);
163
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;