• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *             Copyright Andrey Semashev 2019.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   interlocked.cpp
9  * \author Andrey Semashev
10  * \date   10.07.2019
11  *
12  * \brief  This file contains test for interlocked.hpp
13  */
14 
15 #include <boost/detail/interlocked.hpp>
16 #include <windows.h> // include to test there are no conflicts with Windows SDK
17 #include <boost/cstdint.hpp>
18 #include <boost/core/lightweight_test.hpp>
19 
test_int32()20 void test_int32()
21 {
22     boost::int32_t n = 0, r = 0;
23 
24     r = BOOST_INTERLOCKED_INCREMENT(&n);
25     BOOST_TEST_EQ(n, 1);
26     BOOST_TEST_EQ(r, 1);
27     r = BOOST_INTERLOCKED_DECREMENT(&n);
28     BOOST_TEST_EQ(n, 0);
29     BOOST_TEST_EQ(r, 0);
30     r = BOOST_INTERLOCKED_COMPARE_EXCHANGE(&n, 2, 0);
31     BOOST_TEST_EQ(n, 2);
32     BOOST_TEST_EQ(r, 0);
33     r = BOOST_INTERLOCKED_EXCHANGE(&n, 3);
34     BOOST_TEST_EQ(n, 3);
35     BOOST_TEST_EQ(r, 2);
36     r = BOOST_INTERLOCKED_EXCHANGE_ADD(&n, 10);
37     BOOST_TEST_EQ(n, 13);
38     BOOST_TEST_EQ(r, 3);
39 }
40 
test_pointer()41 void test_pointer()
42 {
43     void* p = 0;
44     void* q = 0;
45 
46     q = BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&p, (void*)&q, (void*)0);
47     BOOST_TEST_EQ(p, (void*)&q);
48     BOOST_TEST_EQ(q, (void*)0);
49     q = BOOST_INTERLOCKED_EXCHANGE_POINTER(&p, (void*)0);
50     BOOST_TEST_EQ(p, (void*)0);
51     BOOST_TEST_EQ(q, (void*)&q);
52 }
53 
main()54 int main()
55 {
56     test_int32();
57     test_pointer();
58 
59     return boost::report_errors();
60 }
61