test: 新增 tests 单元测试文件夹

This commit is contained in:
2025-10-01 02:37:31 +00:00
parent 648f72cb60
commit 67dff96d60
7 changed files with 1 additions and 1 deletions
-6
View File
@@ -1,6 +0,0 @@
#include <iostream>
int main()
{
puts("Hello world!");
return 0;
}
-20
View File
@@ -1,20 +0,0 @@
#include <http/http_response.hpp>
#include <string>
#include <iostream>
int main()
{
std::string body = "Hello HTTP!";
auto builder = ouc_server::http::HttpResponse::create();
auto res =
builder.body(body)
.header("Content-Length", std::to_string(body.size()))
.header("Content-Type", "text/plain")
.build();
printf("%s\n", res.to_string().c_str());
return 0;
}
-71
View File
@@ -1,71 +0,0 @@
#include <utils/mpmc_queue.hpp>
#include <thread>
#include <vector>
#include <iostream>
#include <atomic>
#include <cassert>
#include <chrono>
constexpr size_t PRODUCER_COUNT = 4;
constexpr size_t CONSUMER_COUNT = 4;
constexpr size_t ITEMS_PER_PRODUCER = 100;
int main()
{
pjh_std::MPMCQueue<int> queue(1024);
// 生产者线程
std::vector<std::thread> producers;
for (size_t i = 0; i < PRODUCER_COUNT; ++i)
{
producers.emplace_back(
[i, &queue]()
{
for (size_t j = 0; j < ITEMS_PER_PRODUCER; ++j) {
// 非阻塞 push,失败就重试
while (!queue.push(static_cast<int>(i * ITEMS_PER_PRODUCER + j))) {
std::this_thread::yield();
}
} });
}
// 消费者线程
std::vector<std::thread> consumers;
std::atomic<size_t> consumed{0};
for (size_t i = 0; i < CONSUMER_COUNT; ++i)
{
consumers.emplace_back(
[&queue, &consumed]()
{
while (true) {
auto val = queue.pop();
if (val) {
consumed.fetch_add(1, std::memory_order_relaxed);
} else {
// 队列空,检查是否已经完成
if (consumed.load(std::memory_order_relaxed) >= PRODUCER_COUNT * ITEMS_PER_PRODUCER)
break;
std::this_thread::yield();
}
} });
}
for (auto &t : producers)
if (t.joinable())
t.join();
for (auto &t : consumers)
if (t.joinable())
t.join();
std::cout << "Total produced: " << PRODUCER_COUNT * ITEMS_PER_PRODUCER << "\n";
std::cout << "Total consumed: " << consumed.load() << "\n";
if (consumed.load() == PRODUCER_COUNT * ITEMS_PER_PRODUCER)
std::cout << "Test passed.\n";
else
std::cout << "Test failed.\n";
return 0;
}
-52
View File
@@ -1,52 +0,0 @@
#include <server/tcp_server.hpp>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <string.h>
int main()
{
using namespace ouc_server::server;
using namespace ouc_server::ouc_socket;
TCPServer server;
if (!server.start("127.0.0.1", 8080))
{
std::cerr << "Failed to start server\n";
return 1;
}
puts("Start Listening!");
server.on_connection(
[](TCPSocket &client)
{ std::cout << "New client connected, fd=" << client.get_fd() << "\n"; });
server.on_message(
[&](TCPSocket &client, const std::string &msg)
{
if (!memcmp("exit", msg.c_str(), 4))
{
server.remove_fd(client);
return;
}
std::cout << "Received: " << msg;
std::string ret_str("Echo: " + msg);
size_t count = client.send(ret_str.c_str());
while (count < ret_str.size())
count += client.send(ret_str.c_str() + count);
});
server.on_close(
[](TCPSocket &client)
{ std::cout << "Client disconnected, fd=" << client.get_fd() << "\n"; });
while (true)
{
server.loop();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
-55
View File
@@ -1,55 +0,0 @@
#include <socket/tcp_socket.hpp>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
using namespace ouc_server::ouc_socket;
auto server = TCPSocket::create();
if (!server.bind("127.0.0.1", 8080))
{
perror("socket");
return 1;
}
if (!server.listen())
{
perror("listen");
return 1;
}
puts("Server listening on port 8080...");
auto client = server.accept();
while (client.get_fd() < 0)
{
client = server.accept();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
puts("Client conntected");
while (true)
{
char buf[1024];
ssize_t n = client.recv(buf, sizeof(buf));
if (n <= 0)
continue;
if (!memcmp("exit", buf, 4))
break;
std::string data(buf, n);
std::cout << "Receive data: " << data;
// client.send("Receive!: ");
// client.send(buf);
}
client.close();
server.close();
}
-21
View File
@@ -1,21 +0,0 @@
#include <utils/thread_pool.hpp>
int main()
{
using namespace ouc_server::utils;
ThreadPool pool(4);
auto f1 = pool.sumbit(
[]()
{ puts("Hello world from thread pool!"); });
auto f2 = pool.sumbit(
[](int a, int b)
{
return a + b;
},
1, 2);
f1.get();
printf("f2 result: %d\n", f2.get());
}