string: strerror_r() for windows.
[sliver-openvswitch.git] / tests / test-atomic.c
1 /*
2  * Copyright (c) 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "ovs-atomic.h"
20 #include "util.h"
21
22 #define TEST_ATOMIC_TYPE(ATOMIC_TYPE, BASE_TYPE)        \
23     {                                                   \
24         ATOMIC_TYPE x = ATOMIC_VAR_INIT(1);             \
25         BASE_TYPE value, orig;                          \
26                                                         \
27         atomic_read(&x, &value);                        \
28         ovs_assert(value == 1);                         \
29                                                         \
30         atomic_store(&x, 2);                            \
31         atomic_read(&x, &value);                        \
32         ovs_assert(value == 2);                         \
33                                                         \
34         atomic_init(&x, 3);                             \
35         atomic_read(&x, &value);                        \
36         ovs_assert(value == 3);                         \
37                                                         \
38         atomic_add(&x, 1, &orig);                       \
39         ovs_assert(orig == 3);                          \
40         atomic_read(&x, &value);                        \
41         ovs_assert(value == 4);                         \
42                                                         \
43         atomic_sub(&x, 2, &orig);                       \
44         ovs_assert(orig == 4);                          \
45         atomic_read(&x, &value);                        \
46         ovs_assert(value == 2);                         \
47                                                         \
48         atomic_or(&x, 6, &orig);                        \
49         ovs_assert(orig == 2);                          \
50         atomic_read(&x, &value);                        \
51         ovs_assert(value == 6);                         \
52                                                         \
53         atomic_and(&x, 10, &orig);                      \
54         ovs_assert(orig == 6);                          \
55         atomic_read(&x, &value);                        \
56         ovs_assert(value == 2);                         \
57                                                         \
58         atomic_xor(&x, 10, &orig);                      \
59         ovs_assert(orig == 2);                          \
60         atomic_read(&x, &value);                        \
61         ovs_assert(value == 8);                         \
62                                                         \
63         atomic_destroy(&x);                             \
64     }
65
66 static void
67 test_atomic_flag(void)
68 {
69     atomic_flag flag = ATOMIC_FLAG_INIT;
70     ovs_assert(atomic_flag_test_and_set(&flag) == false);
71     ovs_assert(atomic_flag_test_and_set(&flag) == true);
72     atomic_flag_clear(&flag);
73     ovs_assert(atomic_flag_test_and_set(&flag) == false);
74 }
75
76 int
77 main(void)
78 {
79     TEST_ATOMIC_TYPE(atomic_char, char);
80     TEST_ATOMIC_TYPE(atomic_uchar, unsigned char);
81     TEST_ATOMIC_TYPE(atomic_schar, signed char);
82     TEST_ATOMIC_TYPE(atomic_short, short);
83     TEST_ATOMIC_TYPE(atomic_ushort, unsigned short);
84     TEST_ATOMIC_TYPE(atomic_int, int);
85     TEST_ATOMIC_TYPE(atomic_uint, unsigned int);
86     TEST_ATOMIC_TYPE(atomic_long, long int);
87     TEST_ATOMIC_TYPE(atomic_ulong, unsigned long int);
88     TEST_ATOMIC_TYPE(atomic_llong, long long int);
89     TEST_ATOMIC_TYPE(atomic_ullong, unsigned long long int);
90     TEST_ATOMIC_TYPE(atomic_size_t, size_t);
91     TEST_ATOMIC_TYPE(atomic_ptrdiff_t, ptrdiff_t);
92     TEST_ATOMIC_TYPE(atomic_intmax_t, intmax_t);
93     TEST_ATOMIC_TYPE(atomic_uintmax_t, uintmax_t);
94     TEST_ATOMIC_TYPE(atomic_intptr_t, intptr_t);
95     TEST_ATOMIC_TYPE(atomic_uintptr_t, uintptr_t);
96     TEST_ATOMIC_TYPE(atomic_uint8_t, uint8_t);
97     TEST_ATOMIC_TYPE(atomic_int8_t, int8_t);
98     TEST_ATOMIC_TYPE(atomic_uint16_t, uint16_t);
99     TEST_ATOMIC_TYPE(atomic_int16_t, int16_t);
100     TEST_ATOMIC_TYPE(atomic_uint32_t, uint32_t);
101     TEST_ATOMIC_TYPE(atomic_int32_t, int32_t);
102     TEST_ATOMIC_TYPE(atomic_uint64_t, uint64_t);
103     TEST_ATOMIC_TYPE(atomic_int64_t, int64_t);
104
105     test_atomic_flag();
106
107     return 0;
108 }