Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Shumilov <[email protected]>
  • Loading branch information
PetrShumilov committed Oct 27, 2024
1 parent ab0b951 commit def1651
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
3 changes: 2 additions & 1 deletion common/common-tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ prepend(COMMON_TESTS_SOURCES ${COMMON_DIR}/
smart_ptrs/tagged-ptr-test.cpp
type_traits/list_of_types_test.cpp
wrappers/span-test.cpp
wrappers/string_view-test.cpp)
wrappers/string_view-test.cpp
ucontext/ucontext-portable-test.cpp)

prepare_cross_platform_libs(COMMON_TESTS_LIBS zstd)
set(COMMON_TESTS_LIBS vk::common_src vk::net_src vk::binlog_src vk::unicode ${COMMON_TESTS_LIBS} ${EPOLL_SHIM_LIB} OpenSSL::Crypto z)
Expand Down
65 changes: 65 additions & 0 deletions common/ucontext/ucontext-portable-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2020 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include <gtest/gtest.h>
#include "common/ucontext/ucontext-portable.h"

uint8_t ctx1_stack[8192];
uint8_t ctx2_stack[8192];

ucontext_t_portable cur_ctx;
ucontext_t_portable ctx1;
ucontext_t_portable ctx2;
ucontext_t_portable ctx3;
volatile int finished = 0;

void ctx1_entrypoint(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7) {
ASSERT_EQ(a0, 0);
ASSERT_EQ(a1, 1);
ASSERT_EQ(a2, 2);
ASSERT_EQ(a3, 3);
ASSERT_EQ(a4, 4);
ASSERT_EQ(a5, 5);
ASSERT_EQ(a6, 6);
ASSERT_EQ(a7, 7);
ASSERT_EQ(swapcontext_portable(&ctx1, &ctx2), 0);
finished = 1;
}

void ctx2_entrypoint() {
ASSERT_EQ(swapcontext_portable(&ctx2, &ctx1), 0);
}

/*
Current context -> swap to context 2 -> run entrypoint of context2 ->
-> swap to context 1 -> run entrypoint of context 2 -> swap before `finished` is set to context 2 ->
-> run trampoline of context 2 and swap to context 1 -> continue run entrypoint of context 1 and set `finished` to 1 ->
-> run trampoline of context 1 and swap to initial context.
*/
TEST(ucontext_portable, swapcontext) {
ASSERT_EQ(getcontext_portable(&ctx1), 0);
set_context_stack_ptr_portable(ctx1, ctx1_stack);
set_context_stack_size_portable(ctx1, sizeof(ctx1_stack));
set_context_link_portable(ctx1, &cur_ctx);
makecontext_portable(&ctx1, reinterpret_cast<void(*)()>(ctx1_entrypoint), 8, 0, 1, 2, 3, 4, 5, 6, 7);

ASSERT_EQ(getcontext_portable(&ctx2), 0);
set_context_stack_ptr_portable(ctx2, ctx2_stack);
set_context_stack_size_portable(ctx2, sizeof(ctx2_stack));
set_context_link_portable(ctx2, &ctx1);
makecontext_portable(&ctx2, reinterpret_cast<void(*)()>(ctx2_entrypoint), 0);

ASSERT_EQ(swapcontext_portable(&cur_ctx, &ctx2), 0);
ASSERT_EQ(finished, 1);
}

volatile int context_set_cnt = 0;
TEST(ucontext_portable, get_and_setcontext) {
ASSERT_EQ(getcontext_portable(&ctx3), 0);
context_set_cnt = context_set_cnt + 1; // Due to `++` and `+=` ops with volatile are deprecated
if (context_set_cnt < 10) {
setcontext_portable(&ctx3);
}
ASSERT_EQ(context_set_cnt, 10);
}

0 comments on commit def1651

Please sign in to comment.