interlocked.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #include <boost/detail/interlocked.hpp>
  15. #include <windows.h> // include to test there are no conflicts with Windows SDK
  16. #include <boost/cstdint.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. void test_int32()
  19. {
  20. boost::int32_t n = 0, r = 0;
  21. r = BOOST_INTERLOCKED_INCREMENT(&n);
  22. BOOST_TEST_EQ(n, 1);
  23. BOOST_TEST_EQ(r, 1);
  24. r = BOOST_INTERLOCKED_DECREMENT(&n);
  25. BOOST_TEST_EQ(n, 0);
  26. BOOST_TEST_EQ(r, 0);
  27. r = BOOST_INTERLOCKED_COMPARE_EXCHANGE(&n, 2, 0);
  28. BOOST_TEST_EQ(n, 2);
  29. BOOST_TEST_EQ(r, 0);
  30. r = BOOST_INTERLOCKED_EXCHANGE(&n, 3);
  31. BOOST_TEST_EQ(n, 3);
  32. BOOST_TEST_EQ(r, 2);
  33. r = BOOST_INTERLOCKED_EXCHANGE_ADD(&n, 10);
  34. BOOST_TEST_EQ(n, 13);
  35. BOOST_TEST_EQ(r, 3);
  36. }
  37. void test_pointer()
  38. {
  39. void* p = 0;
  40. void* q = 0;
  41. q = BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&p, (void*)&q, (void*)0);
  42. BOOST_TEST_EQ(p, (void*)&q);
  43. BOOST_TEST_EQ(q, (void*)0);
  44. q = BOOST_INTERLOCKED_EXCHANGE_POINTER(&p, (void*)0);
  45. BOOST_TEST_EQ(p, (void*)0);
  46. BOOST_TEST_EQ(q, (void*)&q);
  47. }
  48. int main()
  49. {
  50. test_int32();
  51. test_pointer();
  52. return boost::report_errors();
  53. }