Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2014 VMware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  29. /* Force assertions, even on debug builds. */
  30. #undef NDEBUG
  31.  
  32.  
  33. #include <stdint.h>
  34. #include <inttypes.h>
  35. #include <assert.h>
  36.  
  37. #include "u_atomic.h"
  38.  
  39. #ifdef _MSC_VER
  40. #pragma warning( disable : 28112 ) /* Accessing a local variable via an Interlocked function */
  41. #pragma warning( disable : 28113 ) /* A variable which is accessed via an Interlocked function must always be accessed via an Interlocked function */
  42. #endif
  43.  
  44.  
  45. /* Test only assignment-like operations, which are supported on all types */
  46. #define test_atomic_assign(type, ones) \
  47.    static void test_atomic_assign_##type (void) { \
  48.       type v, r; \
  49.       \
  50.       p_atomic_set(&v, ones); \
  51.       assert(v == ones && "p_atomic_set"); \
  52.       \
  53.       r = p_atomic_read(&v); \
  54.       assert(r == ones && "p_atomic_read"); \
  55.       \
  56.       v = ones; \
  57.       r = p_atomic_cmpxchg(&v, 0, 1); \
  58.       assert(v == ones && "p_atomic_cmpxchg"); \
  59.       assert(r == ones && "p_atomic_cmpxchg"); \
  60.       r = p_atomic_cmpxchg(&v, ones, 0); \
  61.       assert(v == 0 && "p_atomic_cmpxchg"); \
  62.       assert(r == ones && "p_atomic_cmpxchg"); \
  63.       \
  64.       (void) r; \
  65.    }
  66.  
  67.  
  68. /* Test arithmetic operations that are supported on 8 bits integer types */
  69. #define test_atomic_8bits(type, ones) \
  70.    test_atomic_assign(type, ones) \
  71.    \
  72.    static void test_atomic_8bits_##type (void) { \
  73.       type v, r; \
  74.       \
  75.       test_atomic_assign_##type(); \
  76.       \
  77.       v = 23; \
  78.       p_atomic_add(&v, 42); \
  79.       r = p_atomic_read(&v); \
  80.       assert(r == 65 && "p_atomic_add"); \
  81.       \
  82.       (void) r; \
  83.    }
  84.  
  85.  
  86. /* Test all operations */
  87. #define test_atomic(type, ones) \
  88.    test_atomic_8bits(type, ones) \
  89.    \
  90.    static void test_atomic_##type (void) { \
  91.       type v, r; \
  92.       bool b; \
  93.       \
  94.       test_atomic_8bits_##type(); \
  95.       \
  96.       v = 2; \
  97.       b = p_atomic_dec_zero(&v); \
  98.       assert(v == 1 && "p_atomic_dec_zero"); \
  99.       assert(b == false && "p_atomic_dec_zero"); \
  100.       b = p_atomic_dec_zero(&v); \
  101.       assert(v == 0 && "p_atomic_dec_zero"); \
  102.       assert(b == true && "p_atomic_dec_zero"); \
  103.       b = p_atomic_dec_zero(&v); \
  104.       assert(v == ones && "p_atomic_dec_zero"); \
  105.       assert(b == false && "p_atomic_dec_zero"); \
  106.       \
  107.       v = ones; \
  108.       p_atomic_inc(&v); \
  109.       assert(v == 0 && "p_atomic_inc"); \
  110.       \
  111.       v = ones; \
  112.       r = p_atomic_inc_return(&v); \
  113.       assert(v == 0 && "p_atomic_inc_return"); \
  114.       assert(r == v && "p_atomic_inc_return"); \
  115.       \
  116.       v = 0; \
  117.       p_atomic_dec(&v); \
  118.       assert(v == ones && "p_atomic_dec"); \
  119.       \
  120.       v = 0; \
  121.       r = p_atomic_dec_return(&v); \
  122.       assert(v == ones && "p_atomic_dec_return"); \
  123.       assert(r == v && "p_atomic_dec_return"); \
  124.       \
  125.       (void) r; \
  126.       (void) b; \
  127.    }
  128.  
  129.  
  130. test_atomic(int, -1)
  131. test_atomic(unsigned, ~0U)
  132.  
  133. test_atomic(int16_t, INT16_C(-1))
  134. test_atomic(uint16_t, UINT16_C(0xffff))
  135. test_atomic(int32_t, INT32_C(-1))
  136. test_atomic(uint32_t, UINT32_C(0xffffffff))
  137. test_atomic(int64_t, INT64_C(-1))
  138. test_atomic(uint64_t, UINT64_C(0xffffffffffffffff))
  139.  
  140. test_atomic_8bits(int8_t, INT8_C(-1))
  141. test_atomic_8bits(uint8_t, UINT8_C(0xff))
  142. test_atomic_assign(bool, true)
  143.  
  144. int
  145. main()
  146. {
  147.    test_atomic_int();
  148.    test_atomic_unsigned();
  149.  
  150.    test_atomic_int16_t();
  151.    test_atomic_uint16_t();
  152.    test_atomic_int32_t();
  153.    test_atomic_uint32_t();
  154.    test_atomic_int64_t();
  155.    test_atomic_uint64_t();
  156.  
  157.    test_atomic_8bits_int8_t();
  158.    test_atomic_8bits_uint8_t();
  159.    test_atomic_assign_bool();
  160.  
  161.    return 0;
  162. }
  163.