summaryrefslogtreecommitdiff
path: root/tests/threads/atomic.c
blob: 4d04a777a65e6806b5a5bfe6b0b44756091ce922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "clar_libgit2.h"

void test_threads_atomic__atomic32_set(void)
{
	git_atomic32 v = {0};
	git_atomic32_set(&v, 1);
	cl_assert_equal_i(v.val, 1);
}

void test_threads_atomic__atomic32_get(void)
{
	git_atomic32 v = {1};
	cl_assert_equal_i(git_atomic32_get(&v), 1);
}

void test_threads_atomic__atomic32_inc(void)
{
	git_atomic32 v = {0};
	cl_assert_equal_i(git_atomic32_inc(&v), 1);
	cl_assert_equal_i(v.val, 1);
}

void test_threads_atomic__atomic32_add(void)
{
	git_atomic32 v = {0};
	cl_assert_equal_i(git_atomic32_add(&v, 1), 1);
	cl_assert_equal_i(v.val, 1);
}

void test_threads_atomic__atomic32_dec(void)
{
	git_atomic32 v = {1};
	cl_assert_equal_i(git_atomic32_dec(&v), 0);
	cl_assert_equal_i(v.val, 0);
}

void test_threads_atomic__atomic64_set(void)
{
#ifndef GIT_ARCH_64
	cl_skip();
#else
	git_atomic64 v = {0};
	git_atomic64_set(&v, 1);
	cl_assert_equal_i(v.val, 1);
#endif
}

void test_threads_atomic__atomic64_get(void)
{
#ifndef GIT_ARCH_64
	cl_skip();
#else
	git_atomic64 v = {1};
	cl_assert_equal_i(git_atomic64_get(&v), 1);
#endif
}

void test_threads_atomic__atomic64_add(void)
{
#ifndef GIT_ARCH_64
	cl_skip();
#else
	git_atomic64 v = {0};
	cl_assert_equal_i(git_atomic64_add(&v, 1), 1);
	cl_assert_equal_i(v.val, 1);
#endif
}

void test_threads_atomic__cas_pointer(void)
{
	int *value = NULL;
	int newvalue1 = 1, newvalue2 = 2;

	/* value is updated */
	cl_assert_equal_p(git_atomic_compare_and_swap(&value, NULL, &newvalue1), NULL);
	cl_assert_equal_p(value, &newvalue1);

	/* value is not updated */
	cl_assert_equal_p(git_atomic_compare_and_swap(&value, NULL, &newvalue2), &newvalue1);
	cl_assert_equal_p(value, &newvalue1);
}

void test_threads_atomic__cas_intptr(void)
{
	intptr_t value = 0;
	intptr_t oldvalue;
	intptr_t newvalue;

	/* value is updated */
	oldvalue = 0;
	newvalue = 1;
	cl_assert_equal_i((intptr_t)git_atomic_compare_and_swap(&value, (void *)oldvalue, (void *)newvalue), 0);
	cl_assert_equal_i(value, 1);

	/* value is not updated */
	oldvalue = 0;
	newvalue = 2;
	cl_assert_equal_i((intptr_t)git_atomic_compare_and_swap(&value, (void *)oldvalue, (void *)newvalue), 1);
	cl_assert_equal_i(value, 1);
}

void test_threads_atomic__swap(void)
{
	int *value = NULL;
	int newvalue = 1;

	cl_assert_equal_p(git_atomic_swap(value, &newvalue), NULL);
	cl_assert_equal_p(value, &newvalue);

	cl_assert_equal_p(git_atomic_swap(value, NULL), &newvalue);
	cl_assert_equal_p(value, NULL);
}

void test_threads_atomic__load_ptr(void)
{
	int value = 1;
	int *ptr = &value;
	cl_assert_equal_p(git_atomic_load(ptr), &value);
}

void test_threads_atomic__load_intptr(void)
{
	intptr_t value = 1;
	cl_assert_equal_i((intptr_t)git_atomic_load(value), 1);
}