X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=tests%2Funit%2Ftest_uatomic.c;h=d9c70e0ac912747d5a68fcf7ee90872b0e25a3e8;hb=HEAD;hp=804ce7bd8a79b1945f9fcd9ea5098d3f34a7a971;hpb=f5ab766ee2c8300cb00ca5878b1cb464f960a66d;p=urcu.git diff --git a/tests/unit/test_uatomic.c b/tests/unit/test_uatomic.c index 804ce7b..d9c70e0 100644 --- a/tests/unit/test_uatomic.c +++ b/tests/unit/test_uatomic.c @@ -1,37 +1,26 @@ -/* - * test_uatomic.c - * - * Userspace RCU library - test atomic operations - * - * Copyright February 2009 - Mathieu Desnoyers - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ +// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers +// +// SPDX-License-Identifier: GPL-2.0-or-later #include -#include #include +#include "tap.h" + +#define NR_TESTS 17 + +#define BYTE_PER_LONG (sizeof(unsigned long) / sizeof(unsigned char)) +#define SHORT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned short)) +#define INT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned int)) + struct testvals { #ifdef UATOMIC_HAS_ATOMIC_BYTE - unsigned char c; + unsigned char c[BYTE_PER_LONG]; #endif #ifdef UATOMIC_HAS_ATOMIC_SHORT - unsigned short s; + unsigned short s[SHORT_PER_LONG]; #endif - unsigned int i; + unsigned int i[INT_PER_LONG]; unsigned long l; }; @@ -42,50 +31,86 @@ do { \ __typeof__(*(ptr)) v; \ \ uatomic_add(ptr, 10); \ - assert(uatomic_read(ptr) == 10); \ - uatomic_add(ptr, -11UL); \ - assert(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ - v = uatomic_cmpxchg(ptr, -1UL, 22); \ - assert(uatomic_read(ptr) == 22); \ - assert(v == (__typeof__(*(ptr)))-1UL); \ - v = uatomic_cmpxchg(ptr, 33, 44); \ - assert(uatomic_read(ptr) == 22); \ - assert(v == 22); \ - v = uatomic_xchg(ptr, 55); \ - assert(uatomic_read(ptr) == 55); \ - assert(v == 22); \ + ok1(uatomic_read(ptr) == 10); \ + \ + uatomic_add(ptr, -11UL); \ + ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ + \ + v = uatomic_cmpxchg(ptr, -1UL, 22); \ + ok1(uatomic_read(ptr) == 22); \ + ok1(v == (__typeof__(*(ptr)))-1UL); \ + \ + v = uatomic_cmpxchg(ptr, 33, 44); \ + ok1(uatomic_read(ptr) == 22); \ + ok1(v == 22); \ + \ + v = uatomic_xchg(ptr, 55); \ + ok1(uatomic_read(ptr) == 55); \ + ok1(v == 22); \ + \ uatomic_set(ptr, 22); \ uatomic_inc(ptr); \ - assert(uatomic_read(ptr) == 23); \ + ok1(uatomic_read(ptr) == 23); \ + \ uatomic_dec(ptr); \ - assert(uatomic_read(ptr) == 22); \ + ok1(uatomic_read(ptr) == 22); \ + \ v = uatomic_add_return(ptr, 74); \ - assert(v == 96); \ - assert(uatomic_read(ptr) == 96); \ + ok1(v == 96); \ + ok1(uatomic_read(ptr) == 96); \ + \ uatomic_or(ptr, 58); \ - assert(uatomic_read(ptr) == 122); \ + ok1(uatomic_read(ptr) == 122); \ + \ v = uatomic_sub_return(ptr, 1); \ - assert(v == 121); \ + ok1(v == 121); \ + \ uatomic_sub(ptr, (unsigned int) 2); \ - assert(uatomic_read(ptr) == 119); \ + ok1(uatomic_read(ptr) == 119); \ + \ uatomic_inc(ptr); \ uatomic_inc(ptr); \ - assert(uatomic_read(ptr) == 121); \ + ok1(uatomic_read(ptr) == 121); \ + \ uatomic_and(ptr, 129); \ - assert(uatomic_read(ptr) == 1); \ + ok1(uatomic_read(ptr) == 1); \ + \ } while (0) -int main(int argc, char **argv) +int main(void) { + int nr_run = INT_PER_LONG + 1; + unsigned long i; + +#ifdef UATOMIC_HAS_ATOMIC_BYTE + nr_run += BYTE_PER_LONG; +#endif +#ifdef UATOMIC_HAS_ATOMIC_SHORT + nr_run += SHORT_PER_LONG; +#endif + + plan_tests(nr_run * NR_TESTS); #ifdef UATOMIC_HAS_ATOMIC_BYTE - do_test(&vals.c); + for (i = 0; i < BYTE_PER_LONG; i++) { + diag("Test atomic ops on byte with %lu byte offset from long alignment", + i); + do_test(&vals.c[i]); + } #endif #ifdef UATOMIC_HAS_ATOMIC_SHORT - do_test(&vals.s); + for (i = 0; i < SHORT_PER_LONG; i++) { + diag("Test atomic ops on short with %lu byte offset from long alignment", + i * sizeof(unsigned short)); + do_test(&vals.s[i]); + } #endif - do_test(&vals.i); + for (i = 0; i < INT_PER_LONG; i++) { + diag("Test atomic ops on int with %lu byte offset from long alignment", + i * sizeof(unsigned int)); + do_test(&vals.i[i]); + } + diag("Test atomic ops on long"); do_test(&vals.l); - printf("Atomic ops test OK\n"); - return 0; + return exit_status(); }