The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. //==============================================================================
  21. #if JUCE_UNIT_TESTS
  22. #include "../utilities/juce_UnitTest.h"
  23. #include "../memory/juce_Atomic.h"
  24. class AtomicTests : public UnitTest
  25. {
  26. public:
  27. AtomicTests() : UnitTest ("Atomics") {}
  28. void runTest()
  29. {
  30. beginTest ("Misc");
  31. char a1[7];
  32. expect (numElementsInArray(a1) == 7);
  33. int a2[3];
  34. expect (numElementsInArray(a2) == 3);
  35. expect (ByteOrder::swap ((uint16) 0x1122) == 0x2211);
  36. expect (ByteOrder::swap ((uint32) 0x11223344) == 0x44332211);
  37. expect (ByteOrder::swap ((uint64) literal64bit (0x1122334455667788)) == literal64bit (0x8877665544332211));
  38. beginTest ("Atomic int");
  39. AtomicTester <int>::testInteger (*this);
  40. beginTest ("Atomic unsigned int");
  41. AtomicTester <unsigned int>::testInteger (*this);
  42. beginTest ("Atomic int32");
  43. AtomicTester <int32>::testInteger (*this);
  44. beginTest ("Atomic uint32");
  45. AtomicTester <uint32>::testInteger (*this);
  46. beginTest ("Atomic long");
  47. AtomicTester <long>::testInteger (*this);
  48. beginTest ("Atomic void*");
  49. AtomicTester <void*>::testInteger (*this);
  50. beginTest ("Atomic int*");
  51. AtomicTester <int*>::testInteger (*this);
  52. beginTest ("Atomic float");
  53. AtomicTester <float>::testFloat (*this);
  54. #if ! JUCE_64BIT_ATOMICS_UNAVAILABLE // 64-bit intrinsics aren't available on some old platforms
  55. beginTest ("Atomic int64");
  56. AtomicTester <int64>::testInteger (*this);
  57. beginTest ("Atomic uint64");
  58. AtomicTester <uint64>::testInteger (*this);
  59. beginTest ("Atomic double");
  60. AtomicTester <double>::testFloat (*this);
  61. #endif
  62. }
  63. template <typename Type>
  64. class AtomicTester
  65. {
  66. public:
  67. AtomicTester() {}
  68. static void testInteger (UnitTest& test)
  69. {
  70. Atomic<Type> a, b;
  71. a.set ((Type) 10);
  72. test.expect (a.value == (Type) 10);
  73. test.expect (a.get() == (Type) 10);
  74. a += (Type) 15;
  75. test.expect (a.get() == (Type) 25);
  76. a.memoryBarrier();
  77. a -= (Type) 5;
  78. test.expect (a.get() == (Type) 20);
  79. test.expect (++a == (Type) 21);
  80. ++a;
  81. test.expect (--a == (Type) 21);
  82. test.expect (a.get() == (Type) 21);
  83. a.memoryBarrier();
  84. testFloat (test);
  85. }
  86. static void testFloat (UnitTest& test)
  87. {
  88. Atomic<Type> a, b;
  89. a = (Type) 21;
  90. a.memoryBarrier();
  91. /* These are some simple test cases to check the atomics - let me know
  92. if any of these assertions fail on your system!
  93. */
  94. test.expect (a.get() == (Type) 21);
  95. test.expect (a.compareAndSetValue ((Type) 100, (Type) 50) == (Type) 21);
  96. test.expect (a.get() == (Type) 21);
  97. test.expect (a.compareAndSetValue ((Type) 101, a.get()) == (Type) 21);
  98. test.expect (a.get() == (Type) 101);
  99. test.expect (! a.compareAndSetBool ((Type) 300, (Type) 200));
  100. test.expect (a.get() == (Type) 101);
  101. test.expect (a.compareAndSetBool ((Type) 200, a.get()));
  102. test.expect (a.get() == (Type) 200);
  103. test.expect (a.exchange ((Type) 300) == (Type) 200);
  104. test.expect (a.get() == (Type) 300);
  105. b = a;
  106. test.expect (b.get() == a.get());
  107. }
  108. };
  109. };
  110. static AtomicTests atomicUnitTests;
  111. #endif
  112. END_JUCE_NAMESPACE