add tester

This commit is contained in:
2025-04-23 23:54:09 +08:00
parent f012b549d2
commit db88c4381a
3 changed files with 153 additions and 9 deletions

View File

@@ -3,7 +3,82 @@
"cdev.h": "c",
"module.h": "c",
"types.h": "c",
"init.h": "c"
"init.h": "c",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"format": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stdfloat": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"text_encoding": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"csignal": "cpp",
"syncstream": "cpp"
},
"C_Cpp.default.compilerPath": "/usr/bin/clang++"
}

View File

@@ -1,10 +1,9 @@
obj-m += wendy.o
OUT_DIR := build
.PHONY: all kern_mod install uninstall clean reader writer
.PHONY: all kern_mod install uninstall clean reader writer tester dirprepare
all: kern_mod
@true
all: kern_mod reader writer tester
kern_mod: wendy.c
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) MO=$(PWD)/build/kern
@@ -15,14 +14,18 @@ install:
uninstall:
sudo rmmod wendy
reader:
mkdir -p $(OUT_DIR)
reader: dirprepare
gcc pipe_read.c -o $(OUT_DIR)/pread
writer:
mkdir -p $(OUT_DIR)
writer: dirprepare
gcc pipe_write.c -o $(OUT_DIR)/pwrite
tester: dirprepare
g++ --std=c++20 pipe_tester.cpp -o $(OUT_DIR)/tester
dirprepare:
mkdir -p $(OUT_DIR)
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
rm -rf $(OUT_DIR)

View File

@@ -1,5 +1,71 @@
#include <iostream>
#include <thread>
#include <fstream>
#include <chrono>
#include <syncstream>
void writer() {
std::osyncstream(std::cout) << "Writer started" << std::endl;
char *buffer = new char[8192];
for (int i = 0; i < 8192; ++i) {
buffer[i] = static_cast<char>((i % 95) + 32); // Printable range
}
FILE *fl = fopen("/dev/wendy_in", "w");
if (!fl) {
std::osyncstream(std::cerr) << "Cannot open wendy_in" << std::endl;
}
int wrote = 0;
wrote = fwrite(buffer, sizeof(char), 8192, fl);
if (wrote < 0) {
std::osyncstream(std::cerr) << "Write error: " << wrote << std::endl;
}
std::osyncstream(std::cout) << "Wrote " << wrote << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
wrote = fwrite(buffer, sizeof(char), 8192, fl);
if (wrote < 0) {
std::osyncstream(std::cerr) << "2nd write error: " << wrote << std::endl;
}
std::osyncstream(std::cout) << "2nd wrote " << wrote << std::endl;
}
void reader() {
std::osyncstream(std::cout) << "Reader started" << std::endl;
FILE *fl = fopen("/dev/wendy_out", "r");
if (!fl) {
std::osyncstream(std::cerr) << "Cannot open wendy_out" << std::endl;
return;
}
char *buffer = new char[257];
int read = 0;
int total_read = 0;
do {
read = fread(buffer, sizeof(char), 100, fl);
if (read <= 0) {
break;
}
total_read += read;
std::osyncstream(std::cout) << "Read " << read << "bytes, content: " << buffer << ", total " << total_read << std::endl;
} while (1);
std::osyncstream(std::cout) << "Read failed: read = " << read << std::endl;
}
int main() {
std::thread reader_thread(reader);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::thread writer_thread(writer);
if (reader_thread.joinable()) {
reader_thread.join();
}
if (writer_thread.joinable()) {
writer_thread.join();
}
return 0;
}