Skip to content

Commit 1498f74

Browse files
committed
ST: Add utest to check coroutine for cygwin64. #20
1 parent 39e2a04 commit 1498f74

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ endif
118118
ifeq ($(OS), CYGWIN64)
119119
EXTRA_OBJS = $(TARGETDIR)/md_cygwin64.o
120120
SFLAGS = -fPIC
121+
DSO_SUFFIX = dll
121122
LDFLAGS = -shared -soname=$(SONAME) -lc
122123
OTHER_FLAGS = -Wall
123124
DEFINES += -DMD_HAVE_SELECT

utest/st_utest.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,77 @@ VOID TEST(SampleTest, FastSampleInt64Test)
5353
EXPECT_EQ(8, (int)sizeof(int64_t));
5454
}
5555

56+
void* pfn_coroutine(void* /*arg*/)
57+
{
58+
st_usleep(0);
59+
return NULL;
60+
}
61+
62+
VOID TEST(SampleTest, StartCoroutine)
63+
{
64+
st_thread_t trd = st_thread_create(pfn_coroutine, NULL, 1, 0);
65+
EXPECT_TRUE(trd != NULL);
66+
67+
// Wait for joinable coroutine to quit.
68+
st_thread_join(trd, NULL);
69+
}
70+
71+
VOID TEST(SampleTest, StartCoroutineX3)
72+
{
73+
st_thread_t trd0 = st_thread_create(pfn_coroutine, NULL, 1, 0);
74+
st_thread_t trd1 = st_thread_create(pfn_coroutine, NULL, 1, 0);
75+
st_thread_t trd2 = st_thread_create(pfn_coroutine, NULL, 1, 0);
76+
EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);
77+
78+
// Wait for joinable coroutine to quit.
79+
st_thread_join(trd1, NULL);
80+
st_thread_join(trd2, NULL);
81+
st_thread_join(trd0, NULL);
82+
}
83+
84+
void* pfn_coroutine_add(void* arg)
85+
{
86+
int v = 0;
87+
int* pi = (int*)arg;
88+
89+
// Load the change of arg.
90+
while (v != *pi) {
91+
v = *pi;
92+
st_usleep(0);
93+
}
94+
95+
// Add with const.
96+
v += 100;
97+
*pi = v;
98+
99+
return NULL;
100+
}
101+
102+
VOID TEST(SampleTest, StartCoroutineAdd)
103+
{
104+
int v = 0;
105+
st_thread_t trd = st_thread_create(pfn_coroutine_add, &v, 1, 0);
106+
EXPECT_TRUE(trd != NULL);
107+
108+
// Wait for joinable coroutine to quit.
109+
st_thread_join(trd, NULL);
110+
111+
EXPECT_EQ(100, v);
112+
}
113+
114+
VOID TEST(SampleTest, StartCoroutineAddX3)
115+
{
116+
int v = 0;
117+
st_thread_t trd0 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
118+
st_thread_t trd1 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
119+
st_thread_t trd2 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
120+
EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);
121+
122+
// Wait for joinable coroutine to quit.
123+
st_thread_join(trd0, NULL);
124+
st_thread_join(trd1, NULL);
125+
st_thread_join(trd2, NULL);
126+
127+
EXPECT_EQ(300, v);
128+
}
129+

0 commit comments

Comments
 (0)