Added silcatomic.h for atomic operations.
[silc.git] / lib / silcutil / tests / test_silcatomic.c
1 /* atomic operation tests */
2
3 #include "silc.h"
4 #include "silcatomic.h"
5
6 int main(int argc, char **argv)
7 {
8   SilcBool success = FALSE;
9   SilcAtomic ref;
10   SilcUInt8 ret8;
11   SilcUInt16 ret16;
12   SilcUInt32 ret32;
13
14   if (argc > 1 && !strcmp(argv[1], "-d")) {
15     silc_log_debug(TRUE);
16     silc_log_debug_hexdump(TRUE);
17     silc_log_set_debug_string("*atomic*");
18   }
19
20   silc_atomic_init(&ref, 1);
21
22   ret8 = silc_atomic_add_int(&ref, 7);
23   SILC_LOG_DEBUG(("ref8: 1 + 7 = %d (8)", ret8));
24   ret8 = silc_atomic_add_int(&ref, 3);
25   SILC_LOG_DEBUG(("ref8: 8 + 3 = %d (11)", ret8));
26   ret8 = silc_atomic_sub_int(&ref, 10);
27   SILC_LOG_DEBUG(("ref8: 11 - 10 = %d (1)", ret8));
28
29   ret16 = silc_atomic_add_int(&ref, 1);
30   SILC_LOG_DEBUG(("ref16: 1 + 1 = %d (2)", ret16));
31   ret16 = silc_atomic_add_int(&ref, 31020);
32   SILC_LOG_DEBUG(("ref16: 2 + 31020 = %d (31022)", ret16));
33   ret16 = silc_atomic_add_int(&ref, 34000);
34   SILC_LOG_DEBUG(("ref16: 31022 + 34000 = %d (65022)", ret16));
35   ret16 = silc_atomic_sub_int(&ref, 0);
36   SILC_LOG_DEBUG(("ref16: 65022 - 0 = %d (65022)", ret16));
37   ret16 = silc_atomic_sub_int(&ref, 0xffff);
38   SILC_LOG_DEBUG(("ref16: 65022 - 0xffff = %d (65023) (underflow)", ret16));
39
40   SILC_LOG_DEBUG(("Current value: %d (-513)", silc_atomic_get_int(&ref)));
41
42   silc_atomic_uninit(&ref);
43
44   success = TRUE;
45
46  err:
47   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
48   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
49
50   return success;
51 }