Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame^] | 1 | /* $Id$ */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 2 | /*
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame^] | 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 4 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame^] | 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.
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 9 | *
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame^] | 10 | * This program is distributed in the hope that it will be useful,
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Benny Prijono | e0312a7 | 2005-11-18 00:16:43 +0000 | [diff] [blame^] | 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
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 18 | */
|
| 19 | #include "test.h"
|
| 20 | #include <pjlib.h>
|
| 21 |
|
| 22 | /**
|
| 23 | * \page page_pjlib_atomic_test Test: Atomic Variable
|
| 24 | *
|
| 25 | * This file provides implementation of \b atomic_test(). It tests the
|
| 26 | * functionality of the atomic variable API.
|
| 27 | *
|
| 28 | * \section atomic_test_sec Scope of the Test
|
| 29 | *
|
| 30 | * API tested:
|
| 31 | * - pj_atomic_create()
|
| 32 | * - pj_atomic_get()
|
| 33 | * - pj_atomic_inc()
|
| 34 | * - pj_atomic_dec()
|
| 35 | * - pj_atomic_set()
|
| 36 | * - pj_atomic_destroy()
|
| 37 | *
|
| 38 | *
|
| 39 | * This file is <b>pjlib-test/atomic.c</b>
|
| 40 | *
|
| 41 | * \include pjlib-test/atomic.c
|
| 42 | */
|
| 43 |
|
| 44 |
|
| 45 | #if INCLUDE_ATOMIC_TEST
|
| 46 |
|
| 47 | int atomic_test(void)
|
| 48 | {
|
| 49 | pj_pool_t *pool;
|
| 50 | pj_atomic_t *atomic_var;
|
| 51 | pj_status_t rc;
|
| 52 |
|
| 53 | pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
|
| 54 | if (!pool)
|
| 55 | return -10;
|
| 56 |
|
| 57 | /* create() */
|
| 58 | rc = pj_atomic_create(pool, 111, &atomic_var);
|
| 59 | if (rc != 0) {
|
| 60 | return -20;
|
| 61 | }
|
| 62 |
|
| 63 | /* get: check the value. */
|
| 64 | if (pj_atomic_get(atomic_var) != 111)
|
| 65 | return -30;
|
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame] | 66 |
|
| 67 | /* increment. */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 68 | pj_atomic_inc(atomic_var);
|
| 69 | if (pj_atomic_get(atomic_var) != 112)
|
| 70 | return -40;
|
| 71 |
|
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame] | 72 | /* decrement. */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 73 | pj_atomic_dec(atomic_var);
|
| 74 | if (pj_atomic_get(atomic_var) != 111)
|
| 75 | return -50;
|
| 76 |
|
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame] | 77 | /* set */
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 78 | pj_atomic_set(atomic_var, 211);
|
| 79 | if (pj_atomic_get(atomic_var) != 211)
|
| 80 | return -60;
|
Benny Prijono | a9946d5 | 2005-11-06 09:37:47 +0000 | [diff] [blame] | 81 |
|
| 82 | /* add */
|
| 83 | pj_atomic_add(atomic_var, 10);
|
| 84 | if (pj_atomic_get(atomic_var) != 221)
|
| 85 | return -60;
|
Benny Prijono | e722461 | 2005-11-13 19:40:44 +0000 | [diff] [blame] | 86 |
|
| 87 | /* check the value again. */
|
| 88 | if (pj_atomic_get(atomic_var) != 221)
|
| 89 | return -70;
|
| 90 |
|
| 91 | /* destroy */
|
| 92 | rc = pj_atomic_destroy(atomic_var);
|
| 93 | if (rc != 0)
|
| 94 | return -80;
|
| 95 |
|
| 96 | pj_pool_release(pool);
|
| 97 |
|
| 98 | return 0;
|
| 99 | }
|
| 100 |
|
| 101 |
|
| 102 | #else
|
| 103 | /* To prevent warning about "translation unit is empty"
|
| 104 | * when this test is disabled.
|
| 105 | */
|
| 106 | int dummy_atomic_test;
|
| 107 | #endif /* INCLUDE_ATOMIC_TEST */
|
| 108 |
|