Replace individual test binaries with unified test runner.
- Removed standalone test executables (`test_undo`, `test_buffer_save`, `test_buffer_open_nonexistent_save`, etc.). - Introduced `kte_tests` as a unified test runner. - Migrated existing tests to a new minimal, reusable framework in `tests/Test.h`. - Updated `CMakeLists.txt` to build a single `kte_tests` executable. - Simplified dependencies, reducing the need for ncurses/GUI in test builds.
This commit is contained in:
63
tests/Test.h
Normal file
63
tests/Test.h
Normal file
@@ -0,0 +1,63 @@
|
||||
// Minimal header-only unit test framework for kte
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
|
||||
namespace ktet {
|
||||
|
||||
struct TestCase {
|
||||
std::string name;
|
||||
std::function<void()> fn;
|
||||
};
|
||||
|
||||
inline std::vector<TestCase>& registry() {
|
||||
static std::vector<TestCase> r;
|
||||
return r;
|
||||
}
|
||||
|
||||
struct Registrar {
|
||||
Registrar(const char* name, std::function<void()> fn) {
|
||||
registry().push_back(TestCase{std::string(name), std::move(fn)});
|
||||
}
|
||||
};
|
||||
|
||||
// Assertions
|
||||
struct AssertionFailure {
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
inline void expect(bool cond, const char* expr, const char* file, int line) {
|
||||
if (!cond) {
|
||||
std::cerr << file << ":" << line << ": EXPECT failed: " << expr << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
inline void assert_true(bool cond, const char* expr, const char* file, int line) {
|
||||
if (!cond) {
|
||||
throw AssertionFailure{std::string(file) + ":" + std::to_string(line) + ": ASSERT failed: " + expr};
|
||||
}
|
||||
}
|
||||
|
||||
template<typename A, typename B>
|
||||
inline void assert_eq_impl(const A& a, const B& b, const char* ea, const char* eb, const char* file, int line) {
|
||||
if (!(a == b)) {
|
||||
std::ostringstream oss;
|
||||
oss << file << ":" << line << ": ASSERT_EQ failed: " << ea << " == " << eb;
|
||||
throw AssertionFailure{oss.str()};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ktet
|
||||
|
||||
#define TEST(name) \
|
||||
static void name(); \
|
||||
static ::ktet::Registrar _reg_##name(#name, &name); \
|
||||
static void name()
|
||||
|
||||
#define EXPECT_TRUE(x) ::ktet::expect((x), #x, __FILE__, __LINE__)
|
||||
#define ASSERT_TRUE(x) ::ktet::assert_true((x), #x, __FILE__, __LINE__)
|
||||
#define ASSERT_EQ(a,b) ::ktet::assert_eq_impl((a),(b), #a, #b, __FILE__, __LINE__)
|
||||
33
tests/TestRunner.cc
Normal file
33
tests/TestRunner.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "Test.h"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
int main() {
|
||||
using namespace std::chrono;
|
||||
auto ® = ktet::registry();
|
||||
std::cout << "kte unit tests: " << reg.size() << " test(s)\n";
|
||||
int failed = 0;
|
||||
auto t0 = steady_clock::now();
|
||||
for (const auto &tc : reg) {
|
||||
auto ts = steady_clock::now();
|
||||
try {
|
||||
tc.fn();
|
||||
auto te = steady_clock::now();
|
||||
auto ms = duration_cast<milliseconds>(te - ts).count();
|
||||
std::cout << "[ OK ] " << tc.name << " (" << ms << " ms)\n";
|
||||
} catch (const ktet::AssertionFailure &e) {
|
||||
++failed;
|
||||
std::cerr << "[FAIL] " << tc.name << " -> " << e.msg << "\n";
|
||||
} catch (const std::exception &e) {
|
||||
++failed;
|
||||
std::cerr << "[EXCP] " << tc.name << " -> " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
++failed;
|
||||
std::cerr << "[EXCP] " << tc.name << " -> unknown exception\n";
|
||||
}
|
||||
}
|
||||
auto t1 = steady_clock::now();
|
||||
auto total_ms = duration_cast<milliseconds>(t1 - t0).count();
|
||||
std::cout << "Done in " << total_ms << " ms. Failures: " << failed << "\n";
|
||||
return failed == 0 ? 0 : 1;
|
||||
}
|
||||
79
tests/test_buffer_io.cc
Normal file
79
tests/test_buffer_io.cc
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "Test.h"
|
||||
#include <fstream>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include "Buffer.h"
|
||||
|
||||
static std::string read_all(const std::string &path) {
|
||||
std::ifstream in(path, std::ios::binary);
|
||||
return std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
TEST(Buffer_SaveAs_and_Save_new_file) {
|
||||
const std::string path = "./.kte_ut_buffer_io_1.tmp";
|
||||
std::remove(path.c_str());
|
||||
|
||||
Buffer b;
|
||||
// insert two lines
|
||||
b.insert_text(0, 0, std::string("Hello, world!\n"));
|
||||
b.insert_text(1, 0, std::string("Second line\n"));
|
||||
|
||||
std::string err;
|
||||
ASSERT_TRUE(b.SaveAs(path, err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
|
||||
// append another line then Save()
|
||||
b.insert_text(2, 0, std::string("Third\n"));
|
||||
b.SetDirty(true);
|
||||
ASSERT_TRUE(b.Save(err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
|
||||
std::string got = read_all(path);
|
||||
ASSERT_EQ(got, std::string("Hello, world!\nSecond line\nThird\n"));
|
||||
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(Buffer_Save_after_Open_existing) {
|
||||
const std::string path = "./.kte_ut_buffer_io_2.tmp";
|
||||
std::remove(path.c_str());
|
||||
{
|
||||
std::ofstream out(path, std::ios::binary);
|
||||
out << "abc\n123\n";
|
||||
}
|
||||
|
||||
Buffer b;
|
||||
std::string err;
|
||||
ASSERT_TRUE(b.OpenFromFile(path, err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
|
||||
b.insert_text(2, 0, std::string("tail\n"));
|
||||
b.SetDirty(true);
|
||||
ASSERT_TRUE(b.Save(err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
|
||||
std::string got = read_all(path);
|
||||
ASSERT_EQ(got, std::string("abc\n123\ntail\n"));
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
|
||||
TEST(Buffer_Open_nonexistent_then_SaveAs) {
|
||||
const std::string path = "./.kte_ut_buffer_io_3.tmp";
|
||||
std::remove(path.c_str());
|
||||
|
||||
Buffer b;
|
||||
std::string err;
|
||||
ASSERT_TRUE(b.OpenFromFile(path, err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
ASSERT_EQ(b.IsFileBacked(), false);
|
||||
|
||||
b.insert_text(0, 0, std::string("hello, world"));
|
||||
b.insert_text(0, 12, std::string("\n"));
|
||||
b.SetDirty(true);
|
||||
ASSERT_TRUE(b.SaveAs(path, err));
|
||||
ASSERT_EQ(err.empty(), true);
|
||||
|
||||
std::string got = read_all(path);
|
||||
ASSERT_EQ(got, std::string("hello, world\n"));
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
49
tests/test_piece_table.cc
Normal file
49
tests/test_piece_table.cc
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "Test.h"
|
||||
#include "PieceTable.h"
|
||||
#include <string>
|
||||
|
||||
TEST(PieceTable_Insert_Delete_LineCount) {
|
||||
PieceTable pt;
|
||||
// start empty
|
||||
ASSERT_EQ(pt.Size(), (std::size_t)0);
|
||||
ASSERT_EQ(pt.LineCount(), (std::size_t)1); // empty buffer has 1 logical line
|
||||
|
||||
// Insert some text with newlines
|
||||
const char *t = "abc\n123\nxyz"; // last line without trailing NL
|
||||
pt.Insert(0, t, 11);
|
||||
ASSERT_EQ(pt.Size(), (std::size_t)11);
|
||||
ASSERT_EQ(pt.LineCount(), (std::size_t)3);
|
||||
|
||||
// Check get line
|
||||
ASSERT_EQ(pt.GetLine(0), std::string("abc"));
|
||||
ASSERT_EQ(pt.GetLine(1), std::string("123"));
|
||||
ASSERT_EQ(pt.GetLine(2), std::string("xyz"));
|
||||
|
||||
// Delete middle line entirely including its trailing NL
|
||||
auto r = pt.GetLineRange(1); // [start,end) points to start of line 1 to start of line 2
|
||||
pt.Delete(r.first, r.second - r.first);
|
||||
ASSERT_EQ(pt.LineCount(), (std::size_t)2);
|
||||
ASSERT_EQ(pt.GetLine(0), std::string("abc"));
|
||||
ASSERT_EQ(pt.GetLine(1), std::string("xyz"));
|
||||
}
|
||||
|
||||
TEST(PieceTable_LineCol_Conversions) {
|
||||
PieceTable pt;
|
||||
std::string s = "hello\nworld\n"; // two lines with trailing NL
|
||||
pt.Insert(0, s.data(), s.size());
|
||||
|
||||
// Byte offsets of starts
|
||||
auto off0 = pt.LineColToByteOffset(0, 0);
|
||||
auto off1 = pt.LineColToByteOffset(1, 0);
|
||||
auto off2 = pt.LineColToByteOffset(2, 0); // EOF
|
||||
ASSERT_EQ(off0, (std::size_t)0);
|
||||
ASSERT_EQ(off1, (std::size_t)6); // "hello\n"
|
||||
ASSERT_EQ(off2, pt.Size());
|
||||
|
||||
auto lc0 = pt.ByteOffsetToLineCol(0);
|
||||
auto lc1 = pt.ByteOffsetToLineCol(6);
|
||||
ASSERT_EQ(lc0.first, (std::size_t)0);
|
||||
ASSERT_EQ(lc0.second, (std::size_t)0);
|
||||
ASSERT_EQ(lc1.first, (std::size_t)1);
|
||||
ASSERT_EQ(lc1.second, (std::size_t)0);
|
||||
}
|
||||
36
tests/test_search.cc
Normal file
36
tests/test_search.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "Test.h"
|
||||
#include "OptimizedSearch.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static std::vector<std::size_t> ref_find_all(const std::string &text, const std::string &pat) {
|
||||
std::vector<std::size_t> res;
|
||||
if (pat.empty()) return res;
|
||||
std::size_t from = 0;
|
||||
while (true) {
|
||||
auto p = text.find(pat, from);
|
||||
if (p == std::string::npos) break;
|
||||
res.push_back(p);
|
||||
from = p + pat.size();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
TEST(OptimizedSearch_basic_cases) {
|
||||
OptimizedSearch os;
|
||||
struct Case { std::string text; std::string pat; } cases[] = {
|
||||
{"", ""},
|
||||
{"", "a"},
|
||||
{"a", ""},
|
||||
{"a", "a"},
|
||||
{"aaaaa", "aa"},
|
||||
{"hello world", "world"},
|
||||
{"abcabcabc", "abc"},
|
||||
{"the quick brown fox", "fox"},
|
||||
};
|
||||
for (auto &c : cases) {
|
||||
auto got = os.find_all(c.text, c.pat, 0);
|
||||
auto ref = ref_find_all(c.text, c.pat);
|
||||
ASSERT_EQ(got, ref);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user