From def1651da2417a7909410c5055faa2ee231bb016 Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Fri, 25 Oct 2024 19:52:06 +0300
Subject: [PATCH] Add tests
Signed-off-by: Petr Shumilov
---
common/common-tests.cmake | 3 +-
common/ucontext/ucontext-portable-test.cpp | 65 ++++++++++++++++++++++
2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/common/common-tests.cmake b/common/common-tests.cmake
index ae0985dea2..08fdd17ae3 100644
--- a/common/common-tests.cmake
+++ b/common/common-tests.cmake
@@ -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)
diff --git a/common/ucontext/ucontext-portable-test.cpp b/common/ucontext/ucontext-portable-test.cpp
index e69de29bb2..e3f49b3c6c 100644
--- a/common/ucontext/ucontext-portable-test.cpp
+++ b/common/ucontext/ucontext-portable-test.cpp
@@ -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
+#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(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(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);
+}
\ No newline at end of file