~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/.svn/text-base/atomic_unittest.c.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 
2
 *
 
3
 * unit tests for atomic operations.
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 */
 
21
 
 
22
 
 
23
#include <stdint.h>
 
24
#include <signal.h>
 
25
#include <stdlib.h>
 
26
#include <glib.h>
 
27
#include <check.h>
 
28
 
 
29
 
 
30
/* mock state */
 
31
 
 
32
 
 
33
/* mock functions for external references */
 
34
 
 
35
#define PGM_COMPILATION
 
36
#include "pgm/atomic.h"
 
37
 
 
38
 
 
39
/* target:
 
40
 *      uint32_t
 
41
 *      pgm_atomic_exchange_and_add32 (
 
42
 *              volatile uint32_t*      atomic,
 
43
 *              const uint32_t          val
 
44
 *      )
 
45
 */
 
46
 
 
47
START_TEST (test_int32_exchange_and_add_pass_001)
 
48
{
 
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");
 
54
}
 
55
END_TEST
 
56
 
 
57
/* target:
 
58
 *      void
 
59
 *      pgm_atomic_add32 (
 
60
 *              volatile uint32_t*      atomic,
 
61
 *              const uint32_t          val
 
62
 *      )
 
63
 */
 
64
 
 
65
START_TEST (test_int32_add_pass_001)
 
66
{
 
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");
 
72
}
 
73
END_TEST
 
74
 
 
75
/* ensure wrap around when casting uint32 */
 
76
START_TEST (test_int32_add_pass_002)
 
77
{
 
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");
 
85
}
 
86
END_TEST
 
87
 
 
88
/* target:
 
89
 *      uint32_t
 
90
 *      pgm_atomic_read32 (
 
91
 *              volatile uint32_t*      atomic
 
92
 *      )
 
93
 */
 
94
 
 
95
START_TEST (test_int32_get_pass_001)
 
96
{
 
97
        volatile uint32_t atomic = (uint32_t)-20;
 
98
        fail_unless ((uint32_t)-20 == pgm_atomic_read32 (&atomic), "read failed");
 
99
}
 
100
END_TEST
 
101
 
 
102
/* target:
 
103
 *      void
 
104
 *      pgm_atomic_int32_set (
 
105
 *              volatile int32_t*       atomic,
 
106
 *              const int32_t           val
 
107
 *      )
 
108
 */
 
109
 
 
110
START_TEST (test_int32_set_pass_001)
 
111
{
 
112
        volatile uint32_t atomic = (uint32_t)-20;
 
113
        pgm_atomic_write32 (&atomic, 5);
 
114
        fail_unless (5 == atomic, "write failed");
 
115
}
 
116
END_TEST
 
117
 
 
118
 
 
119
static
 
120
Suite*
 
121
make_test_suite (void)
 
122
{
 
123
        Suite* s;
 
124
 
 
125
        s = suite_create (__FILE__);
 
126
 
 
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);
 
130
 
 
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);
 
135
 
 
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);
 
139
 
 
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);
 
143
 
 
144
        return s;
 
145
}
 
146
 
 
147
static
 
148
Suite*
 
149
make_master_suite (void)
 
150
{
 
151
        Suite* s = suite_create ("Master");
 
152
        return s;
 
153
}
 
154
 
 
155
int
 
156
main (void)
 
157
{
 
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);
 
162
        srunner_free (sr);
 
163
        return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 
164
}
 
165
 
 
166
/* eof */