diff options
| author | Amlal <amlal@nekernel.org> | 2025-08-12 11:53:13 +0200 |
|---|---|---|
| committer | Amlal <amlal@nekernel.org> | 2025-08-12 11:53:24 +0200 |
| commit | 54e32a8a3535d99d104775687af0731b0be3b129 (patch) | |
| tree | b43e378237a724699b785ae21ca63374c510564c /dev | |
| parent | 2450c6579543ae94b3dcb2af4893cfc6ecd0fcef (diff) | |
wip: work in progress network tests using GTest. Reworked print module too.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/lib/fix/parser.hpp | 2 | ||||
| -rw-r--r-- | dev/lib/io/print.hpp | 24 | ||||
| -rw-r--r-- | dev/lib/net/network.hpp | 22 | ||||
| -rw-r--r-- | dev/lib/net/url.hpp | 8 | ||||
| -rw-r--r-- | dev/tests/fix_basic/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | dev/tests/fix_basic/fix_test.cc | 2 | ||||
| -rw-r--r-- | dev/tests/network_basic/CMakeLists.txt | 23 | ||||
| -rw-r--r-- | dev/tests/network_basic/net_test.cc | 36 | ||||
| -rw-r--r-- | dev/tests/tracked_ptr_basic/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | dev/tests/tracked_ptr_leak/CMakeLists.txt | 2 |
10 files changed, 107 insertions, 16 deletions
diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp index 1095e8d..0f2287e 100644 --- a/dev/lib/fix/parser.hpp +++ b/dev/lib/fix/parser.hpp @@ -13,7 +13,7 @@ #include <utility> #include <string> #include <vector> - +#include <cstdint> #include <sys/types.h> #include <unistd.h> #include <signal.h> diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index e326845..36192c4 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -8,20 +8,34 @@ #ifndef _SNU_PRINT_HPP #define _SNU_PRINT_HPP +#include <initializer_list> #include <iostream> +#include <ostream> namespace snu::io -{ +{ + template <typename T> + inline void printv(T fmt) noexcept + { + std::cout << fmt; + } + + template <typename T> + inline void printv(std::initializer_list<T> fmt) noexcept + { + std::cout << fmt; + } + template <typename... T> - inline void print(T... fmt) + inline void print(T... fmt) noexcept { - std::cout << std::move(fmt...); + (printv(fmt), ...); } template <typename... T> - inline void println(T... fmt) + inline void println(T... fmt) noexcept { - std::cout << std::move(fmt...); + print(static_cast<T>(fmt)...); std::cout << std::endl; } } // namespace snu::io diff --git a/dev/lib/net/network.hpp b/dev/lib/net/network.hpp index a58cb1f..67f3740 100644 --- a/dev/lib/net/network.hpp +++ b/dev/lib/net/network.hpp @@ -11,7 +11,7 @@ #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> - +#include <string> #include <utility> #include <cstddef> @@ -38,7 +38,7 @@ namespace snu::net static constexpr auto local_address_ip6 = "127.0.0.1"; static constexpr auto local_address_ip4 = "::1"; - static constexpr auto backlog_count = 18U; + static constexpr auto backlog_count = 18U; socket_type fd_{}; @@ -58,7 +58,7 @@ namespace snu::net if (!len) return false; - auto ret = ::recv(fd_, out, len, MSG_WAITALL); + auto ret = ::recv(fd_, out, len, 0); return ret > 0; } @@ -79,12 +79,22 @@ namespace snu::net return ret > 0; } + template <typename ptr_type> + bool transmit(std::basic_string<ptr_type> out) noexcept + { + if (out.empty()) + return false; + + auto ret = ::send(fd_, out.data(), out.size(), 0); + + return ret > 0; + } + template <int32_t af, int32_t kind, int32_t ip_proto, int32_t port> bool construct(const char* addr = basic_modem::local_address_ip4, const bool& is_server = false) noexcept { - static_assert(af != 0, "AF is zero"); - static_assert(kind != 0, "Kind is zero"); - static_assert(ip_proto != 0, "IPProto is zero"); + static_assert(af != 0, "Address family is zero"); + static_assert(kind != 0, "Type is zero"); fd_ = ::socket(af, kind, ip_proto); diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index 5dc0d03..9a28f9b 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -18,10 +18,15 @@ namespace snu::net template <typename char_type> class basic_url; + template <typename char_type> + class basic_url_traits; + /// @brief Basic URL parser container. template <typename char_type> class basic_url final { + friend basic_url_traits<char_type>; + std::basic_stringstream<char_type> ss_{}; public: @@ -33,6 +38,9 @@ namespace snu::net basic_url& operator/=(const std::basic_string<char_type>& in) { + if (in.empty()) + return *this; + ss_ += in; return *this; } diff --git a/dev/tests/fix_basic/CMakeLists.txt b/dev/tests/fix_basic/CMakeLists.txt index 3a2542d..f792277 100644 --- a/dev/tests/fix_basic/CMakeLists.txt +++ b/dev/tests/fix_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/fix_basic/fix_test.cc b/dev/tests/fix_basic/fix_test.cc index 1ce128f..bd04d07 100644 --- a/dev/tests/fix_basic/fix_test.cc +++ b/dev/tests/fix_basic/fix_test.cc @@ -13,6 +13,6 @@ TEST(FIXTest, BasicFIXUsage) snu::fix::basic_visitor<char> basic_visitor; snu::fix::basic_range_data<char> fix = basic_visitor.visit("8=FIX.4.2|9=65|35=A|49=SERVER|56=CLIENT|34=177|52=20090107-18:15:16|98=0|108=30|10=062|"); - EXPECT_EQ(fix.magic_, "FIX.4.2"); + EXPECT_EQ(fix.magic_, snu::fix::detail::begin_fix()); EXPECT_TRUE(fix.is_valid()); }
\ No newline at end of file diff --git a/dev/tests/network_basic/CMakeLists.txt b/dev/tests/network_basic/CMakeLists.txt new file mode 100644 index 0000000..9e0fc6b --- /dev/null +++ b/dev/tests/network_basic/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10) +project(SOCLTests) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable(NetworkTestBasic net_test.cc) +target_link_libraries(NetworkTestBasic gtest_main) + +set_property(TARGET NetworkTestBasic PROPERTY CXX_STANDARD 20) +target_include_directories(NetworkTestBasic PUBLIC ../../) + +include(GoogleTest) +gtest_discover_tests(NetworkTestBasic) diff --git a/dev/tests/network_basic/net_test.cc b/dev/tests/network_basic/net_test.cc new file mode 100644 index 0000000..72bcda0 --- /dev/null +++ b/dev/tests/network_basic/net_test.cc @@ -0,0 +1,36 @@ +/* + * File: tests/tracked_ptr_test.cc + * Purpose: Custom smart pointer unit tests in C++ + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp. + */ + +#include <lib/net/network.hpp> +#include <lib/io/print.hpp> +#include <cstring> +#include <gtest/gtest.h> + +TEST(NetworkTest, BasicNetworkUsage) +{ + + snu::net::basic_modem<char> modem; + modem.construct<AF_INET, SOCK_STREAM, IPPROTO_IP, 80>(snu::net::basic_modem<char>::local_address_ip4, true); + + snu::net::basic_modem<char> modem_cl; + modem.construct<AF_INET, SOCK_STREAM, IPPROTO_IP, 80>(snu::net::basic_modem<char>::local_address_ip4, false); + + EXPECT_TRUE(modem_cl.is_valid()); + EXPECT_TRUE(modem.is_valid()); + + std::basic_string<char> buf_dst = "HELLO, NET!"; + char* buf = new char[buf_dst.size()]; + + modem_cl.transmit(buf_dst); + modem.receive<char*>(buf, buf_dst.size()); + + snu::io::println<>(buf); + snu::io::println<>(buf_dst); + + delete[] buf; + buf = nullptr; +}
\ No newline at end of file diff --git a/dev/tests/tracked_ptr_basic/CMakeLists.txt b/dev/tests/tracked_ptr_basic/CMakeLists.txt index 97f7204..9100e9a 100644 --- a/dev/tests/tracked_ptr_basic/CMakeLists.txt +++ b/dev/tests/tracked_ptr_basic/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( diff --git a/dev/tests/tracked_ptr_leak/CMakeLists.txt b/dev/tests/tracked_ptr_leak/CMakeLists.txt index 17df8f1..625a1dc 100644 --- a/dev/tests/tracked_ptr_leak/CMakeLists.txt +++ b/dev/tests/tracked_ptr_leak/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(TrackedPtrTests) +project(SOCLTests) include(FetchContent) FetchContent_Declare( |
