atomic.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "prio.h"
  6. #include "prprf.h"
  7. #include "pratom.h"
  8. /*
  9. * TODO: create a macro to generate the six lines of code that are repeated
  10. * for every test. Also rewrite the statement
  11. * result = result | ((EXPRESSION) ? 0 : 1);
  12. * as
  13. * result |= !(EXPRESSION);
  14. */
  15. int main(int argc, char **argv)
  16. {
  17. PRInt32 rv, oldval, test, result = 0;
  18. PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
  19. /***********************/
  20. /* Test the functions. */
  21. /***********************/
  22. oldval = test = -2;
  23. rv = PR_AtomicIncrement(&test);
  24. result = result | ((rv == -1) ? 0 : 1);
  25. PR_fprintf(
  26. output, "PR_AtomicIncrement(%d) == %d: %s\n",
  27. oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
  28. oldval = test;
  29. rv = PR_AtomicIncrement(&test);
  30. result = result | ((rv == 0) ? 0 : 1);
  31. PR_fprintf(
  32. output, "PR_AtomicIncrement(%d) == %d: %s\n",
  33. oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
  34. oldval = test;
  35. rv = PR_AtomicIncrement(&test);
  36. result = result | ((rv == 1) ? 0 : 1);
  37. PR_fprintf(
  38. output, "PR_AtomicIncrement(%d) == %d: %s\n",
  39. oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
  40. oldval = test = -2;
  41. rv = PR_AtomicAdd(&test,1);
  42. result = result | ((rv == -1) ? 0 : 1);
  43. PR_fprintf(
  44. output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
  45. oldval, 1, rv, (rv == -1) ? "PASSED" : "FAILED");
  46. oldval = test;
  47. rv = PR_AtomicAdd(&test, 4);
  48. result = result | ((rv == 3) ? 0 : 1);
  49. PR_fprintf(
  50. output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
  51. oldval, 4, rv, (rv == 3) ? "PASSED" : "FAILED");
  52. oldval = test;
  53. rv = PR_AtomicAdd(&test, -6);
  54. result = result | ((rv == -3) ? 0 : 1);
  55. PR_fprintf(
  56. output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
  57. oldval, -6, rv, (rv == -3) ? "PASSED" : "FAILED");
  58. oldval = test = 2;
  59. rv = PR_AtomicDecrement(&test);
  60. result = result | ((rv == 1) ? 0 : 1);
  61. PR_fprintf(
  62. output, "PR_AtomicDecrement(%d) == %d: %s\n",
  63. oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
  64. oldval = test;
  65. rv = PR_AtomicDecrement(&test);
  66. result = result | ((rv == 0) ? 0 : 1);
  67. PR_fprintf(
  68. output, "PR_AtomicDecrement(%d) == %d: %s\n",
  69. oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
  70. oldval = test;
  71. rv = PR_AtomicDecrement(&test);
  72. result = result | ((rv == -1) ? 0 : 1);
  73. PR_fprintf(
  74. output, "PR_AtomicDecrement(%d) == %d: %s\n",
  75. oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
  76. /* set to a different value */
  77. oldval = test = -2;
  78. rv = PR_AtomicSet(&test, 2);
  79. result = result | (((rv == -2) && (test == 2)) ? 0 : 1);
  80. PR_fprintf(
  81. output, "PR_AtomicSet(%d, %d) == %d: %s\n",
  82. oldval, 2, rv, ((rv == -2) && (test == 2)) ? "PASSED" : "FAILED");
  83. /* set to the same value */
  84. oldval = test = -2;
  85. rv = PR_AtomicSet(&test, -2);
  86. result = result | (((rv == -2) && (test == -2)) ? 0 : 1);
  87. PR_fprintf(
  88. output, "PR_AtomicSet(%d, %d) == %d: %s\n",
  89. oldval, -2, rv, ((rv == -2) && (test == -2)) ? "PASSED" : "FAILED");
  90. /***********************/
  91. /* Test the macros. */
  92. /***********************/
  93. oldval = test = -2;
  94. rv = PR_ATOMIC_INCREMENT(&test);
  95. result = result | ((rv == -1) ? 0 : 1);
  96. PR_fprintf(
  97. output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
  98. oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
  99. oldval = test;
  100. rv = PR_ATOMIC_INCREMENT(&test);
  101. result = result | ((rv == 0) ? 0 : 1);
  102. PR_fprintf(
  103. output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
  104. oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
  105. oldval = test;
  106. rv = PR_ATOMIC_INCREMENT(&test);
  107. result = result | ((rv == 1) ? 0 : 1);
  108. PR_fprintf(
  109. output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
  110. oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
  111. oldval = test = -2;
  112. rv = PR_ATOMIC_ADD(&test,1);
  113. result = result | ((rv == -1) ? 0 : 1);
  114. PR_fprintf(
  115. output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
  116. oldval, 1, rv, (rv == -1) ? "PASSED" : "FAILED");
  117. oldval = test;
  118. rv = PR_ATOMIC_ADD(&test, 4);
  119. result = result | ((rv == 3) ? 0 : 1);
  120. PR_fprintf(
  121. output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
  122. oldval, 4, rv, (rv == 3) ? "PASSED" : "FAILED");
  123. oldval = test;
  124. rv = PR_ATOMIC_ADD(&test, -6);
  125. result = result | ((rv == -3) ? 0 : 1);
  126. PR_fprintf(
  127. output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
  128. oldval, -6, rv, (rv == -3) ? "PASSED" : "FAILED");
  129. oldval = test = 2;
  130. rv = PR_ATOMIC_DECREMENT(&test);
  131. result = result | ((rv == 1) ? 0 : 1);
  132. PR_fprintf(
  133. output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
  134. oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
  135. oldval = test;
  136. rv = PR_ATOMIC_DECREMENT(&test);
  137. result = result | ((rv == 0) ? 0 : 1);
  138. PR_fprintf(
  139. output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
  140. oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
  141. oldval = test;
  142. rv = PR_ATOMIC_DECREMENT(&test);
  143. result = result | ((rv == -1) ? 0 : 1);
  144. PR_fprintf(
  145. output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
  146. oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
  147. /* set to a different value */
  148. oldval = test = -2;
  149. rv = PR_ATOMIC_SET(&test, 2);
  150. result = result | (((rv == -2) && (test == 2)) ? 0 : 1);
  151. PR_fprintf(
  152. output, "PR_ATOMIC_SET(%d, %d) == %d: %s\n",
  153. oldval, 2, rv, ((rv == -2) && (test == 2)) ? "PASSED" : "FAILED");
  154. /* set to the same value */
  155. oldval = test = -2;
  156. rv = PR_ATOMIC_SET(&test, -2);
  157. result = result | (((rv == -2) && (test == -2)) ? 0 : 1);
  158. PR_fprintf(
  159. output, "PR_ATOMIC_SET(%d, %d) == %d: %s\n",
  160. oldval, -2, rv, ((rv == -2) && (test == -2)) ? "PASSED" : "FAILED");
  161. PR_fprintf(
  162. output, "Atomic operations test %s\n",
  163. (result == 0) ? "PASSED" : "FAILED");
  164. return result;
  165. } /* main */
  166. /* atomic.c */