atomic.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "test.h"
  20. #include <pjlib.h>
  21. /**
  22. * \page page_pjlib_atomic_test Test: Atomic Variable
  23. *
  24. * This file provides implementation of \b atomic_test(). It tests the
  25. * functionality of the atomic variable API.
  26. *
  27. * \section atomic_test_sec Scope of the Test
  28. *
  29. * API tested:
  30. * - pj_atomic_create()
  31. * - pj_atomic_get()
  32. * - pj_atomic_inc()
  33. * - pj_atomic_dec()
  34. * - pj_atomic_set()
  35. * - pj_atomic_destroy()
  36. *
  37. *
  38. * This file is <b>pjlib-test/atomic.c</b>
  39. *
  40. * \include pjlib-test/atomic.c
  41. */
  42. #if INCLUDE_ATOMIC_TEST
  43. int atomic_test(void)
  44. {
  45. pj_pool_t *pool;
  46. pj_atomic_t *atomic_var;
  47. pj_status_t rc;
  48. pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
  49. if (!pool)
  50. return -10;
  51. /* create() */
  52. rc = pj_atomic_create(pool, 111, &atomic_var);
  53. if (rc != 0) {
  54. return -20;
  55. }
  56. /* get: check the value. */
  57. if (pj_atomic_get(atomic_var) != 111)
  58. return -30;
  59. /* increment. */
  60. pj_atomic_inc(atomic_var);
  61. if (pj_atomic_get(atomic_var) != 112)
  62. return -40;
  63. /* decrement. */
  64. pj_atomic_dec(atomic_var);
  65. if (pj_atomic_get(atomic_var) != 111)
  66. return -50;
  67. /* set */
  68. pj_atomic_set(atomic_var, 211);
  69. if (pj_atomic_get(atomic_var) != 211)
  70. return -60;
  71. /* add */
  72. pj_atomic_add(atomic_var, 10);
  73. if (pj_atomic_get(atomic_var) != 221)
  74. return -60;
  75. /* check the value again. */
  76. if (pj_atomic_get(atomic_var) != 221)
  77. return -70;
  78. /* destroy */
  79. rc = pj_atomic_destroy(atomic_var);
  80. if (rc != 0)
  81. return -80;
  82. pj_pool_release(pool);
  83. return 0;
  84. }
  85. #else
  86. /* To prevent warning about "translation unit is empty"
  87. * when this test is disabled.
  88. */
  89. int dummy_atomic_test;
  90. #endif /* INCLUDE_ATOMIC_TEST */