atomic.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2017 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #include "../../test_slapd.h"
  9. void
  10. test_libslapd_counters_atomic_usage(void **state __attribute__((unused)))
  11. {
  12. Slapi_Counter *tc = slapi_counter_new();
  13. uint64_t value = 0;
  14. /* Check that it starts as 0 */
  15. value = slapi_counter_get_value(tc);
  16. assert_true(value == 0);
  17. /* Increment */
  18. slapi_counter_increment(tc);
  19. value = slapi_counter_get_value(tc);
  20. assert_true(value == 1);
  21. /* add */
  22. slapi_counter_add(tc, 100);
  23. value = slapi_counter_get_value(tc);
  24. assert_true(value == 101);
  25. /* set */
  26. slapi_counter_set_value(tc, 200);
  27. value = slapi_counter_get_value(tc);
  28. assert_true(value == 200);
  29. /* dec */
  30. slapi_counter_decrement(tc);
  31. value = slapi_counter_get_value(tc);
  32. assert_true(value == 199);
  33. /* sub */
  34. slapi_counter_subtract(tc, 99);
  35. value = slapi_counter_get_value(tc);
  36. assert_true(value == 100);
  37. /* init */
  38. slapi_counter_init(tc);
  39. value = slapi_counter_get_value(tc);
  40. assert_true(value == 0);
  41. slapi_counter_destroy(&tc);
  42. /* We could attempt a more complex thread test later? */
  43. }
  44. void
  45. test_libslapd_counters_atomic_overflow(void **state __attribute__((unused)))
  46. {
  47. Slapi_Counter *tc = slapi_counter_new();
  48. /* This is intmax ... */
  49. uint32_t value_32 = 0xFFFFFFFF;
  50. uint64_t value = 0;
  51. slapi_counter_set_value(tc, (uint64_t)value_32);
  52. value = slapi_counter_get_value(tc);
  53. assert_true(value == (uint64_t)value_32);
  54. slapi_counter_increment(tc);
  55. value = slapi_counter_get_value(tc);
  56. assert_true(value != 0);
  57. assert_true(value > (uint64_t)value_32);
  58. slapi_counter_destroy(&tc);
  59. }