| 1 | /* |
| 2 | * test_uatomic.c |
| 3 | * |
| 4 | * Userspace RCU library - test atomic operations |
| 5 | * |
| 6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <urcu/uatomic.h> |
| 25 | |
| 26 | #include "tap.h" |
| 27 | |
| 28 | #define NR_TESTS 17 |
| 29 | |
| 30 | struct testvals { |
| 31 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 32 | unsigned char c; |
| 33 | #endif |
| 34 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 35 | unsigned short s; |
| 36 | #endif |
| 37 | unsigned int i; |
| 38 | unsigned long l; |
| 39 | }; |
| 40 | |
| 41 | static struct testvals vals; |
| 42 | |
| 43 | #define do_test(ptr) \ |
| 44 | do { \ |
| 45 | __typeof__(*(ptr)) v; \ |
| 46 | \ |
| 47 | uatomic_add(ptr, 10); \ |
| 48 | ok1(uatomic_read(ptr) == 10); \ |
| 49 | \ |
| 50 | uatomic_add(ptr, -11UL); \ |
| 51 | ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ |
| 52 | \ |
| 53 | v = uatomic_cmpxchg(ptr, -1UL, 22); \ |
| 54 | ok1(uatomic_read(ptr) == 22); \ |
| 55 | ok1(v == (__typeof__(*(ptr)))-1UL); \ |
| 56 | \ |
| 57 | v = uatomic_cmpxchg(ptr, 33, 44); \ |
| 58 | ok1(uatomic_read(ptr) == 22); \ |
| 59 | ok1(v == 22); \ |
| 60 | \ |
| 61 | v = uatomic_xchg(ptr, 55); \ |
| 62 | ok1(uatomic_read(ptr) == 55); \ |
| 63 | ok1(v == 22); \ |
| 64 | \ |
| 65 | uatomic_set(ptr, 22); \ |
| 66 | uatomic_inc(ptr); \ |
| 67 | ok1(uatomic_read(ptr) == 23); \ |
| 68 | \ |
| 69 | uatomic_dec(ptr); \ |
| 70 | ok1(uatomic_read(ptr) == 22); \ |
| 71 | \ |
| 72 | v = uatomic_add_return(ptr, 74); \ |
| 73 | ok1(v == 96); \ |
| 74 | ok1(uatomic_read(ptr) == 96); \ |
| 75 | \ |
| 76 | uatomic_or(ptr, 58); \ |
| 77 | ok1(uatomic_read(ptr) == 122); \ |
| 78 | \ |
| 79 | v = uatomic_sub_return(ptr, 1); \ |
| 80 | ok1(v == 121); \ |
| 81 | \ |
| 82 | uatomic_sub(ptr, (unsigned int) 2); \ |
| 83 | ok1(uatomic_read(ptr) == 119); \ |
| 84 | \ |
| 85 | uatomic_inc(ptr); \ |
| 86 | uatomic_inc(ptr); \ |
| 87 | ok1(uatomic_read(ptr) == 121); \ |
| 88 | \ |
| 89 | uatomic_and(ptr, 129); \ |
| 90 | ok1(uatomic_read(ptr) == 1); \ |
| 91 | \ |
| 92 | } while (0) |
| 93 | |
| 94 | int main(int argc, char **argv) |
| 95 | { |
| 96 | int nr_run = 2; |
| 97 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 98 | nr_run += 1; |
| 99 | #endif |
| 100 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 101 | nr_run += 1; |
| 102 | #endif |
| 103 | |
| 104 | plan_tests(nr_run * NR_TESTS); |
| 105 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 106 | diag("Test atomic ops on byte"); |
| 107 | do_test(&vals.c); |
| 108 | #endif |
| 109 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 110 | diag("Test atomic ops on short"); |
| 111 | do_test(&vals.s); |
| 112 | #endif |
| 113 | diag("Test atomic ops on int"); |
| 114 | do_test(&vals.i); |
| 115 | diag("Test atomic ops on long"); |
| 116 | do_test(&vals.l); |
| 117 | |
| 118 | return exit_status(); |
| 119 | } |