Skip to content

Commit

Permalink
ST: Add utest to check coroutine for cygwin64. #20
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 28, 2021
1 parent 39e2a04 commit 1498f74
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ endif
ifeq ($(OS), CYGWIN64)
EXTRA_OBJS = $(TARGETDIR)/md_cygwin64.o
SFLAGS = -fPIC
DSO_SUFFIX = dll
LDFLAGS = -shared -soname=$(SONAME) -lc
OTHER_FLAGS = -Wall
DEFINES += -DMD_HAVE_SELECT
Expand Down
74 changes: 74 additions & 0 deletions utest/st_utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,77 @@ VOID TEST(SampleTest, FastSampleInt64Test)
EXPECT_EQ(8, (int)sizeof(int64_t));
}

void* pfn_coroutine(void* /*arg*/)
{
st_usleep(0);
return NULL;
}

VOID TEST(SampleTest, StartCoroutine)
{
st_thread_t trd = st_thread_create(pfn_coroutine, NULL, 1, 0);
EXPECT_TRUE(trd != NULL);

// Wait for joinable coroutine to quit.
st_thread_join(trd, NULL);
}

VOID TEST(SampleTest, StartCoroutineX3)
{
st_thread_t trd0 = st_thread_create(pfn_coroutine, NULL, 1, 0);
st_thread_t trd1 = st_thread_create(pfn_coroutine, NULL, 1, 0);
st_thread_t trd2 = st_thread_create(pfn_coroutine, NULL, 1, 0);
EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);

// Wait for joinable coroutine to quit.
st_thread_join(trd1, NULL);
st_thread_join(trd2, NULL);
st_thread_join(trd0, NULL);
}

void* pfn_coroutine_add(void* arg)
{
int v = 0;
int* pi = (int*)arg;

// Load the change of arg.
while (v != *pi) {
v = *pi;
st_usleep(0);
}

// Add with const.
v += 100;
*pi = v;

return NULL;
}

VOID TEST(SampleTest, StartCoroutineAdd)
{
int v = 0;
st_thread_t trd = st_thread_create(pfn_coroutine_add, &v, 1, 0);
EXPECT_TRUE(trd != NULL);

// Wait for joinable coroutine to quit.
st_thread_join(trd, NULL);

EXPECT_EQ(100, v);
}

VOID TEST(SampleTest, StartCoroutineAddX3)
{
int v = 0;
st_thread_t trd0 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
st_thread_t trd1 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
st_thread_t trd2 = st_thread_create(pfn_coroutine_add, &v, 1, 0);
EXPECT_TRUE(trd0 != NULL && trd1 != NULL && trd2 != NULL);

// Wait for joinable coroutine to quit.
st_thread_join(trd0, NULL);
st_thread_join(trd1, NULL);
st_thread_join(trd2, NULL);

EXPECT_EQ(300, v);
}

0 comments on commit 1498f74

Please sign in to comment.