nrf_atomic_sanity_check.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_ATOMIC_SANITY_CHECK_H__
  41. #define NRF_ATOMIC_SANITY_CHECK_H__
  42. #include "nrf_atomic.h"
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @brief Quick sanity check of nrf_atomic API
  48. * */
  49. static inline void nrf_atomic_sanity_check(void)
  50. {
  51. #if defined(DEBUG_NRF) || defined(DEBUG_NRF_USER)
  52. nrf_atomic_u32_t val;
  53. nrf_atomic_u32_t flag;
  54. /*Fetch version tests*/
  55. val = 0;
  56. ASSERT(nrf_atomic_u32_store_fetch(&val, 10) == 0);
  57. ASSERT(nrf_atomic_u32_store_fetch(&val, 0) == 10);
  58. val = 0;
  59. ASSERT(nrf_atomic_u32_or_fetch(&val, 1 << 16) == 0);
  60. ASSERT(nrf_atomic_u32_or_fetch(&val, 1 << 5) == ((1 << 16)));
  61. ASSERT(nrf_atomic_u32_or_fetch(&val, 1 << 5) == ((1 << 16) | (1 << 5)));
  62. ASSERT(nrf_atomic_u32_or_fetch(&val, 0) == ((1 << 16) | (1 << 5)));
  63. ASSERT(nrf_atomic_u32_or_fetch(&val, 0xFFFFFFFF) == ((1 << 16) | (1 << 5)));
  64. ASSERT(nrf_atomic_u32_or_fetch(&val, 0xFFFFFFFF) == (0xFFFFFFFF));
  65. val = 0xFFFFFFFF;
  66. ASSERT(nrf_atomic_u32_and_fetch(&val, ~(1 << 16)) == 0xFFFFFFFF);
  67. ASSERT(nrf_atomic_u32_and_fetch(&val, ~(1 << 5)) == (0xFFFFFFFF & ~((1 << 16))));
  68. ASSERT(nrf_atomic_u32_and_fetch(&val, 0) == (0xFFFFFFFF & ~(((1 << 16) | (1 << 5)))));
  69. ASSERT(nrf_atomic_u32_and_fetch(&val, 0xFFFFFFFF) == (0));
  70. val = 0;
  71. ASSERT(nrf_atomic_u32_xor_fetch(&val, (1 << 16)) == 0);
  72. ASSERT(nrf_atomic_u32_xor_fetch(&val, (1 << 5)) == ((1 << 16)));
  73. ASSERT(nrf_atomic_u32_xor_fetch(&val, 0) == ((1 << 16) | (1 << 5)));
  74. ASSERT(nrf_atomic_u32_xor_fetch(&val, (1 << 16) | (1 << 5)) == ((1 << 16) | (1 << 5)));
  75. ASSERT(nrf_atomic_u32_xor_fetch(&val, 0) == (0));
  76. val = 0;
  77. ASSERT(nrf_atomic_u32_add_fetch(&val, 100) == 0);
  78. ASSERT(nrf_atomic_u32_add_fetch(&val, 100) == 100);
  79. ASSERT(nrf_atomic_u32_add_fetch(&val, 1 << 24) == 200);
  80. ASSERT(nrf_atomic_u32_add_fetch(&val, 0) == (200 + (1 << 24)));
  81. ASSERT(nrf_atomic_u32_add_fetch(&val, 0xFFFFFFFF) == (200 + (1 << 24)));
  82. ASSERT(nrf_atomic_u32_add_fetch(&val, 0) == (200 - 1 + (1 << 24)));
  83. val = 1000;
  84. ASSERT(nrf_atomic_u32_sub_fetch(&val, 100) == 1000);
  85. ASSERT(nrf_atomic_u32_sub_fetch(&val, 100) == 900);
  86. ASSERT(nrf_atomic_u32_sub_fetch(&val, 0) == 800);
  87. ASSERT(nrf_atomic_u32_sub_fetch(&val, 0xFFFFFFFF) == 800);
  88. ASSERT(nrf_atomic_u32_sub_fetch(&val, 0) == 801);
  89. flag = 0;
  90. ASSERT(nrf_atomic_flag_set_fetch(&flag) == 0);
  91. ASSERT(nrf_atomic_flag_set_fetch(&flag) == 1);
  92. ASSERT(nrf_atomic_flag_clear_fetch(&flag) == 1);
  93. ASSERT(nrf_atomic_flag_clear_fetch(&flag) == 0);
  94. /*No fetch version tests*/
  95. val = 0;
  96. ASSERT(nrf_atomic_u32_store(&val, 10) == 10);
  97. ASSERT(nrf_atomic_u32_store(&val, 0) == 0);
  98. val = 0;
  99. ASSERT(nrf_atomic_u32_or(&val, 1 << 16) == 1 << 16);
  100. ASSERT(nrf_atomic_u32_or(&val, 1 << 5) == ((1 << 16) | (1 << 5)));
  101. ASSERT(nrf_atomic_u32_or(&val, 1 << 5) == ((1 << 16) | (1 << 5)));
  102. ASSERT(nrf_atomic_u32_or(&val, 0) == ((1 << 16) | (1 << 5)));
  103. ASSERT(nrf_atomic_u32_or(&val, 0xFFFFFFFF) == 0xFFFFFFFF);
  104. val = 0xFFFFFFFF;
  105. ASSERT(nrf_atomic_u32_and(&val, ~(1 << 16)) == (0xFFFFFFFF & ~((1 << 16))));
  106. ASSERT(nrf_atomic_u32_and(&val, ~(1 << 5)) == (0xFFFFFFFF & ~(((1 << 16) | (1 << 5)))));
  107. ASSERT(nrf_atomic_u32_and(&val, 0) == 0);
  108. val = 0;
  109. ASSERT(nrf_atomic_u32_xor(&val, (1 << 16)) == ((1 << 16)));
  110. ASSERT(nrf_atomic_u32_xor(&val, (1 << 5)) == ((1 << 16) | (1 << 5)));
  111. ASSERT(nrf_atomic_u32_xor(&val, 0) == ((1 << 16) | (1 << 5)));
  112. ASSERT(nrf_atomic_u32_xor(&val, (1 << 16) | (1 << 5)) == 0);
  113. val = 0;
  114. ASSERT(nrf_atomic_u32_add(&val, 100) == 100);
  115. ASSERT(nrf_atomic_u32_add(&val, 100) == 200);
  116. ASSERT(nrf_atomic_u32_add(&val, 1 << 24) == (200 + (1 << 24)));
  117. ASSERT(nrf_atomic_u32_add(&val, 0) == (200 + (1 << 24)));
  118. ASSERT(nrf_atomic_u32_add(&val, 0xFFFFFFFF) == (200 - 1 + (1 << 24)));
  119. val = 1000;
  120. ASSERT(nrf_atomic_u32_sub(&val, 100) == 900);
  121. ASSERT(nrf_atomic_u32_sub(&val, 100) == 800);
  122. ASSERT(nrf_atomic_u32_sub(&val, 0) == 800);
  123. ASSERT(nrf_atomic_u32_sub(&val, 0xFFFFFFFF) == 801);
  124. flag = 0;
  125. ASSERT(nrf_atomic_flag_set(&flag) == 1);
  126. ASSERT(nrf_atomic_flag_set(&flag) == 1);
  127. ASSERT(nrf_atomic_flag_clear(&flag) == 0);
  128. ASSERT(nrf_atomic_flag_clear(&flag) == 0);
  129. #endif
  130. }
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif /* NRF_ATOMIC_SANITY_CHECK_H__ */