ovs-atomic: Delete atomic, atomic_flag, ovs_refcount destroy functions.
[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
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
64 static void
65 test_atomic_flag(void)
66 {
67     atomic_flag flag = ATOMIC_FLAG_INIT;
68     ovs_assert(atomic_flag_test_and_set(&flag) == false);
69     ovs_assert(atomic_flag_test_and_set(&flag) == true);
70     atomic_flag_clear(&flag);
71     ovs_assert(atomic_flag_test_and_set(&flag) == false);
72 }
73
74 int
75 main(void)
76 {
77     TEST_ATOMIC_TYPE(atomic_char, char);
78     TEST_ATOMIC_TYPE(atomic_uchar, unsigned char);
79     TEST_ATOMIC_TYPE(atomic_schar, signed char);
80     TEST_ATOMIC_TYPE(atomic_short, short);
81     TEST_ATOMIC_TYPE(atomic_ushort, unsigned short);
82     TEST_ATOMIC_TYPE(atomic_int, int);
83     TEST_ATOMIC_TYPE(atomic_uint, unsigned int);
84     TEST_ATOMIC_TYPE(atomic_long, long int);
85     TEST_ATOMIC_TYPE(atomic_ulong, unsigned long int);
86     TEST_ATOMIC_TYPE(atomic_llong, long long int);
87     TEST_ATOMIC_TYPE(atomic_ullong, unsigned long long int);
88     TEST_ATOMIC_TYPE(atomic_size_t, size_t);
89     TEST_ATOMIC_TYPE(atomic_ptrdiff_t, ptrdiff_t);
90     TEST_ATOMIC_TYPE(atomic_intmax_t, intmax_t);
91     TEST_ATOMIC_TYPE(atomic_uintmax_t, uintmax_t);
92     TEST_ATOMIC_TYPE(atomic_intptr_t, intptr_t);
93     TEST_ATOMIC_TYPE(atomic_uintptr_t, uintptr_t);
94     TEST_ATOMIC_TYPE(atomic_uint8_t, uint8_t);
95     TEST_ATOMIC_TYPE(atomic_int8_t, int8_t);
96     TEST_ATOMIC_TYPE(atomic_uint16_t, uint16_t);
97     TEST_ATOMIC_TYPE(atomic_int16_t, int16_t);
98     TEST_ATOMIC_TYPE(atomic_uint32_t, uint32_t);
99     TEST_ATOMIC_TYPE(atomic_int32_t, int32_t);
100     TEST_ATOMIC_TYPE(atomic_uint64_t, uint64_t);
101     TEST_ATOMIC_TYPE(atomic_int64_t, int64_t);
102
103     test_atomic_flag();
104
105     return 0;
106 }