From ba4ff3cd42972f0e0d7b3a24c30686078292d889 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 15 Sep 2022 14:31:33 +0200 Subject: [PATCH] libZRCola: Add test Signed-off-by: Simon Rozman --- lib/libZRCola/build/Makefile | 10 ++- lib/libZRCola/src/.gitignore | 7 +- lib/libZRCola/test/.gitignore | 2 + lib/libZRCola/test/test.cpp | 129 ++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 lib/libZRCola/test/.gitignore create mode 100644 lib/libZRCola/test/test.cpp diff --git a/lib/libZRCola/build/Makefile b/lib/libZRCola/build/Makefile index 9e482ef..2518572 100644 --- a/lib/libZRCola/build/Makefile +++ b/lib/libZRCola/build/Makefile @@ -17,9 +17,17 @@ all: ../lib/libZRCola.a ../lib/libZRCola.a: ../src/pch.h.gch $(OBJS) $(AR) $(ARFLAGS) $@ $(OBJS) +.PHONY: test +test: ../test/test + ../test/test + +../test/test: ../lib/libZRCola.a + $(CXX) $(CPPFLAGS) -I../../stdex/include -I../include $(CXXFLAGS) -L../lib -o $@ ../test/test.cpp -lstdc++ -lZRCola + .PHONY: clean clean: - -rm -r ../src/*.h.gch ../src/*.cpp.o ../lib/libZRCola.a + -rm -r ../src/*.{d,gch,o} ../lib/libZRCola.a ../test/*.d ../test/test include ../../../include/targets.mak -include $(DEPS) +-include ../test/test.d diff --git a/lib/libZRCola/src/.gitignore b/lib/libZRCola/src/.gitignore index 9e482be..ec27f82 100644 --- a/lib/libZRCola/src/.gitignore +++ b/lib/libZRCola/src/.gitignore @@ -1,4 +1,3 @@ -/*.h.gch -/*.h.d -/*.cpp.d -/*.cpp.o +/*.d +/*.gch +/*.o diff --git a/lib/libZRCola/test/.gitignore b/lib/libZRCola/test/.gitignore new file mode 100644 index 0000000..773e2d6 --- /dev/null +++ b/lib/libZRCola/test/.gitignore @@ -0,0 +1,2 @@ +/*.d +/test diff --git a/lib/libZRCola/test/test.cpp b/lib/libZRCola/test/test.cpp new file mode 100644 index 0000000..f6c211d --- /dev/null +++ b/lib/libZRCola/test/test.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include + +using namespace std; +using namespace ZRCola; + +translation_db t_db; +transet_db ts_db; +transeq_db tsq_db; +langchar_db lc_db; +language_db lang_db; +character_db chr_db; +chrcat_db cc_db; +chrtag_db ct_db; +tagname_db tn_db; +highlight_db h_db; + +static void load_database() +{ + fstream dat("../../../output/data/ZRCola.zrcdb", ios_base::in | ios_base::binary); + if (!dat.good()) + throw runtime_error("ZRCola.zrcdb not found or cannot be opened."); + + if (!stdex::idrec::find(dat, ZRCOLA_DB_ID, sizeof(recordid_t))) + throw runtime_error("ZRCola.zrcdb is not a valid ZRCola database."); + + recordsize_t size; + dat.read((char*)&size, sizeof(recordsize_t)); + if (dat.good()) { + bool has_translation_data = false; + + for (;;) { + recordid_t id; + if (!stdex::idrec::read_id(dat, id, size)) break; + + if (id == translation_rec::id()) { + dat >> translation_rec(t_db); + if (dat.good()) { + has_translation_data = true; + } else { + cerr << "Error reading translation data from ZRCola.zrcdb.\n"; + t_db.clear(); + } + } else if (id == transet_rec::id()) { + dat >> transet_rec(ts_db); + if (!dat.good()) { + cerr << "Error reading translation set data from ZRCola.zrcdb.\n"; + ts_db.clear(); + } + } else if (id == transeq_rec::id()) { + dat >> transeq_rec(tsq_db); + if (!dat.good()) { + cerr << "Error reading translation sequence data from ZRCola.zrcdb.\n"; + tsq_db.clear(); + } + } else if (id == langchar_rec::id()) { + dat >> langchar_rec(lc_db); + if (!dat.good()) { + cerr << "Error reading language character data from ZRCola.zrcdb.\n"; + lc_db.clear(); + } + } else if (id == language_rec::id()) { + dat >> language_rec(lang_db); + if (!dat.good()) { + cerr << "Error reading language character data from ZRCola.zrcdb.\n"; + lang_db.clear(); + } + } else if (id == character_rec::id()) { + dat >> character_rec(chr_db); + if (!dat.good()) { + cerr << "Error reading character data from ZRCola.zrcdb.\n"; + chr_db.clear(); + } + } else if (id == chrcat_rec::id()) { + dat >> chrcat_rec(cc_db); + if (!dat.good()) { + cerr << "Error reading character category data from ZRCola.zrcdb.\n"; + cc_db.clear(); + } + } else if (id == chrtag_rec::id()) { + dat >> chrtag_rec(ct_db); + if (!dat.good()) { + cerr << "Error reading character tag data from ZRCola.zrcdb.\n"; + ct_db.clear(); + } + } else if (id == tagname_rec::id()) { + dat >> tagname_rec(tn_db); + if (!dat.good()) { + cerr << "Error reading tag name data from ZRCola.zrcdb.\n"; + tn_db.clear(); + } + } else if (id == highlight_rec::id()) { + dat >> highlight_rec(h_db); + if (!dat.good()) { + cerr << "Error reading highlight data from ZRCola.zrcdb.\n"; + h_db.clear(); + } + } else + stdex::idrec::ignore(dat); + } + + if (!has_translation_data) + throw runtime_error("ZRCola.zrcdb has no translation data."); + } +} + +int main() +{ + try { + load_database(); + + u16string output; + vector map; + t_db.Translate(ZRCOLA_TRANSETID_DEFAULT, u"", -1, output, &map); + if (!output.empty()) throw runtime_error("Empty string translated to nonempty output."); + if (!map.empty()) throw runtime_error("Empty string translation produced non-empty map."); + + t_db.Translate(ZRCOLA_TRANSETID_DEFAULT, u"To je test.", -1, output, &map); + if (output != u"T  ťᵉⓢṭ.") throw runtime_error("Unexpected translation."); + + cout << "Passed\n"; + return 0; + } catch (exception &ex) { + cerr << typeid(ex).name() << ": " << ex.what() << endl; + return 1; + } +}