Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / tests / test-atomic.c
1 /*
2  * Copyright (c) 2013, 2014 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 #include "ovstest.h"
22
23 #define TEST_ATOMIC_TYPE(ATOMIC_TYPE, BASE_TYPE)        \
24     {                                                   \
25         ATOMIC_TYPE x = ATOMIC_VAR_INIT(1);             \
26         BASE_TYPE value, orig;                          \
27                                                         \
28         atomic_read(&x, &value);                        \
29         ovs_assert(value == 1);                         \
30                                                         \
31         atomic_store(&x, 2);                            \
32         atomic_read(&x, &value);                        \
33         ovs_assert(value == 2);                         \
34                                                         \
35         atomic_init(&x, 3);                             \
36         atomic_read(&x, &value);                        \
37         ovs_assert(value == 3);                         \
38                                                         \
39         atomic_add(&x, 1, &orig);                       \
40         ovs_assert(orig == 3);                          \
41         atomic_read(&x, &value);                        \
42         ovs_assert(value == 4);                         \
43                                                         \
44         atomic_sub(&x, 2, &orig);                       \
45         ovs_assert(orig == 4);                          \
46         atomic_read(&x, &value);                        \
47         ovs_assert(value == 2);                         \
48                                                         \
49         atomic_or(&x, 6, &orig);                        \
50         ovs_assert(orig == 2);                          \
51         atomic_read(&x, &value);                        \
52         ovs_assert(value == 6);                         \
53                                                         \
54         atomic_and(&x, 10, &orig);                      \
55         ovs_assert(orig == 6);                          \
56         atomic_read(&x, &value);                        \
57         ovs_assert(value == 2);                         \
58                                                         \
59         atomic_xor(&x, 10, &orig);                      \
60         ovs_assert(orig == 2);                          \
61         atomic_read(&x, &value);                        \
62         ovs_assert(value == 8);                         \
63     }
64
65 static void
66 test_atomic_flag(void)
67 {
68     atomic_flag flag = ATOMIC_FLAG_INIT;
69     ovs_assert(atomic_flag_test_and_set(&flag) == false);
70     ovs_assert(atomic_flag_test_and_set(&flag) == true);
71     atomic_flag_clear(&flag);
72     ovs_assert(atomic_flag_test_and_set(&flag) == false);
73 }
74
75
76 static void
77 test_atomic_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
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
108 OVSTEST_REGISTER("test-atomic", test_atomic_main);