1 1.1 isaki /* $NetBSD: t_atomic_swap.c,v 1.1 2019/02/17 12:24:17 isaki Exp $ */ 2 1.1 isaki 3 1.1 isaki /* 4 1.1 isaki * Copyright (C) 2019 Tetsuya Isaki. All rights reserved. 5 1.1 isaki * 6 1.1 isaki * Redistribution and use in source and binary forms, with or without 7 1.1 isaki * modification, are permitted provided that the following conditions 8 1.1 isaki * are met: 9 1.1 isaki * 1. Redistributions of source code must retain the above copyright 10 1.1 isaki * notice, this list of conditions and the following disclaimer. 11 1.1 isaki * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 isaki * notice, this list of conditions and the following disclaimer in the 13 1.1 isaki * documentation and/or other materials provided with the distribution. 14 1.1 isaki * 15 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 isaki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 isaki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 isaki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 isaki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 1.1 isaki * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 1.1 isaki * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 1.1 isaki * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 1.1 isaki * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 isaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 isaki * SUCH DAMAGE. 26 1.1 isaki */ 27 1.1 isaki 28 1.1 isaki #include <sys/cdefs.h> 29 1.1 isaki __RCSID("$NetBSD: t_atomic_swap.c,v 1.1 2019/02/17 12:24:17 isaki Exp $"); 30 1.1 isaki 31 1.1 isaki #include <atf-c.h> 32 1.1 isaki #include <inttypes.h> 33 1.1 isaki #include <sys/atomic.h> 34 1.1 isaki 35 1.1 isaki /* 36 1.1 isaki * These tests don't examine the atomicity. 37 1.1 isaki */ 38 1.1 isaki 39 1.1 isaki #define OLDVAL (0x1122334455667788UL) 40 1.1 isaki #define NEWVAL (0x8090a0b0c0d0e0f0UL) 41 1.1 isaki 42 1.1 isaki /* 43 1.1 isaki * atomic_swap_*() 44 1.1 isaki */ 45 1.1 isaki #define atf_swap(NAME, TYPE, FMT) \ 46 1.1 isaki ATF_TC(NAME); \ 47 1.1 isaki ATF_TC_HEAD(NAME, tc) \ 48 1.1 isaki { \ 49 1.1 isaki atf_tc_set_md_var(tc, "descr", #NAME); \ 50 1.1 isaki } \ 51 1.1 isaki ATF_TC_BODY(NAME, tc) \ 52 1.1 isaki { \ 53 1.1 isaki volatile TYPE val; \ 54 1.1 isaki TYPE newval; \ 55 1.1 isaki TYPE expval; \ 56 1.1 isaki TYPE expres; \ 57 1.1 isaki TYPE res; \ 58 1.1 isaki val = (TYPE)OLDVAL; \ 59 1.1 isaki newval = (TYPE)NEWVAL; \ 60 1.1 isaki expval = (TYPE)NEWVAL; \ 61 1.1 isaki expres = (TYPE)OLDVAL; \ 62 1.1 isaki res = NAME(&val, newval); \ 63 1.1 isaki ATF_REQUIRE_MSG(val == expval, \ 64 1.1 isaki "val expects " FMT " but " FMT, expval, val); \ 65 1.1 isaki ATF_REQUIRE_MSG(res == expres, \ 66 1.1 isaki "res expects " FMT " but " FMT, expres, res); \ 67 1.1 isaki } 68 1.1 isaki 69 1.1 isaki atf_swap(atomic_swap_32, uint32_t, "0x%" PRIx32); 70 1.1 isaki atf_swap(atomic_swap_uint, unsigned int, "0x%x"); 71 1.1 isaki atf_swap(atomic_swap_ulong, unsigned long, "0x%lx"); 72 1.1 isaki atf_swap(atomic_swap_ptr, void *, "%p"); 73 1.1 isaki #if defined(__HAVE_ATOMIC64_OPS) 74 1.1 isaki atf_swap(atomic_swap_64, uint64_t, "0x%" PRIx64); 75 1.1 isaki #endif 76 1.1 isaki 77 1.1 isaki ATF_TP_ADD_TCS(tp) 78 1.1 isaki { 79 1.1 isaki ATF_TP_ADD_TC(tp, atomic_swap_32); 80 1.1 isaki ATF_TP_ADD_TC(tp, atomic_swap_uint); 81 1.1 isaki ATF_TP_ADD_TC(tp, atomic_swap_ulong); 82 1.1 isaki ATF_TP_ADD_TC(tp, atomic_swap_ptr); 83 1.1 isaki #if defined(__HAVE_ATOMIC64_OPS) 84 1.1 isaki ATF_TP_ADD_TC(tp, atomic_swap_64); 85 1.1 isaki #endif 86 1.1 isaki 87 1.1 isaki return atf_no_error(); 88 1.1 isaki } 89