Format tests and examples as well

This commit is contained in:
Martchus 2022-01-30 21:32:08 +01:00
parent 1e393bf3fe
commit ec4a44e4de
10 changed files with 811 additions and 824 deletions

View File

@ -61,6 +61,16 @@ use_package(TARGET_NAME Catch2::Catch2 PACKAGE_NAME Catch2 LIBRARIES_VARIABLE "T
# find threading library (required by examples)
use_package(TARGET_NAME Threads::Threads PACKAGE_NAME Threads LIBRARIES_VARIABLE "TEST_LIBRARIES")
# add test/example files to EXCLUDED_FILES so they're formatted
set(TESTS basic typed)
set(EXAMPLES basic multi rel resize scale typed view)
foreach (TEST ${TESTS})
list(APPEND EXCLUDED_FILES "tests/${TEST}.cc")
endforeach ()
foreach (EXAMPLE ${EXAMPLES})
list(APPEND EXCLUDED_FILES "examples/${EXAMPLE}.cc")
endforeach ()
# include modules to apply configuration
include(BasicConfig)
include(WindowsResources)
@ -71,11 +81,9 @@ include(ConfigHeader)
# configure test targets
include(TestUtilities)
list(APPEND TEST_LIBRARIES ${META_TARGET_NAME})
set(TESTS basic typed)
foreach (TEST ${TESTS})
configure_test_target(TEST_NAME "${TEST}_tests" SRC_FILES "tests/${TEST}.cc" LIBRARIES "${TEST_LIBRARIES}")
endforeach ()
set(EXAMPLES basic multi rel resize scale typed view)
foreach (EXAMPLE ${EXAMPLES})
configure_test_target(TEST_NAME "${EXAMPLE}_example" SRC_FILES "examples/${EXAMPLE}.cc" LIBRARIES "${TEST_LIBRARIES}")
endforeach ()

View File

@ -9,8 +9,7 @@ void checkLMDB(MDBEnv* env, MDBDbi dbi)
MDBOutVal data;
if (!rotxn->get(dbi, "lmdb", data)) {
cout << "Outside RW transaction, found that lmdb = " << data.get<string_view>() << endl;
}
else
} else
cout << "Outside RW transaction, found nothing" << endl;
}
@ -26,8 +25,7 @@ int main()
MDBOutVal data;
if (!txn->get(dbi, "lmdb", data)) {
cout << "Within RW transaction, found that lmdb = " << data.get<string_view>() << endl;
}
else
} else
cout << "Found nothing" << endl;
std::thread elsewhere(checkLMDB, env.get(), dbi);

View File

@ -24,8 +24,7 @@ int main()
string_view v1;
if (!txn->get(dbi, "mdb", v1)) {
cout << v1 << endl;
}
else {
} else {
cout << "found nothing" << endl;
}
txn->commit();
@ -57,6 +56,4 @@ int main()
for (int rc = cursor.first(key, data); !rc; rc = cursor.next(key, data)) {
cout << key.get<string_view>() << " = " << data.get<string_view>() << endl;
}
}

View File

@ -1,7 +1,7 @@
#include "../lmdb-safe.hh"
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <c++utilities/application/global.h>
@ -10,12 +10,10 @@
using namespace std;
using namespace LMDBSafe;
struct Record
{
struct Record {
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
template <class Archive> void serialize(Archive &ar, const unsigned int version)
{
CPP_UTILITIES_UNUSED(version)
ar &id &domain_id &name &type &ttl &content &enabled &auth;
@ -29,7 +27,6 @@ struct Record
std::string content;
bool enabled{ true };
bool auth{ true };
};
static unsigned int getMaxID(MDBRWTransaction &txn, MDBDbi &dbi)
@ -54,7 +51,6 @@ static void store(MDBRWTransaction& txn, MDBDbi& records, MDBDbi& domainidx, MDB
txn->put(nameidx, r.name, r.id);
}
int main(int argc, char **argv)
{
auto env = getMDBEnv("pdns", MDB_NOSUBDIR, 0600);
@ -99,7 +95,6 @@ int main(int argc, char** argv)
r.content = "ns1.powerdns.com";
store(txn, records, domainidx, nameidx, r);
r.id = ++maxid;
r.type = "A";
r.content = "1.2.3.4";
@ -115,7 +110,6 @@ int main(int argc, char** argv)
r.content = "letsencrypt.org";
store(txn, records, domainidx, nameidx, r);
r.id = ++maxid;
r.type = "AAAA";
r.name = "www." + domain;
@ -156,11 +150,9 @@ int main(int argc, char** argv)
boost::archive::binary_iarchive oi(istr, boost::archive::no_header);
oi >> test;
cout << "Record: " << test.name << " " << test.type << " " << test.ttl << " " << test.content << endl;
}
else {
} else {
cout << "Did not find anything for id " << id << endl;
}
++count;
}
}

View File

@ -5,8 +5,7 @@
using namespace std;
using namespace LMDBSafe;
template <typename First, typename Second>
struct Pair {
template <typename First, typename Second> struct Pair {
First first;
Second second;
};
@ -22,9 +21,7 @@ int main(int argc, char**)
rwtxn->put(main, "counter", "1234");
rwtxn->put(main, MDBInVal::fromStruct(Pair<int, int>{ 12, 13 }), "hoi dan 12,13");
rwtxn->put(main, MDBInVal::fromStruct(Pair<int, int>{14,15}),
MDBInVal::fromStruct(Pair<int, int>{20,23}));
rwtxn->put(main, MDBInVal::fromStruct(Pair<int, int>{ 14, 15 }), MDBInVal::fromStruct(Pair<int, int>{ 20, 23 }));
MDBOutVal out;
if (!rwtxn->get(main, MDBInVal::fromStruct(Pair<int, int>{ 12, 13 }), out))
@ -35,18 +32,15 @@ int main(int argc, char**)
if (!rwtxn->get(main, MDBInVal::fromStruct(Pair<int, int>{ 14, 15 }), out)) {
auto res = out.get_struct<Pair<int, int>>();
cout << "Got: " << res.first << ", " << res.second << endl;
}
else
} else
cout << "Got nothing!1" << endl;
rwtxn->put(main, 12.12, 7.3);
if (!rwtxn->get(main, 12.12, out)) {
cout << "Got: " << out.get<double>() << endl;
}
else
} else
cout << "Got nothing!1" << endl;
rwtxn->commit();
return 0;
@ -59,20 +53,17 @@ int main(int argc, char**)
cout << data.get<string>() << endl;
cout << data.get<string_view>() << endl;
struct Bert
{
struct Bert {
uint16_t x, y;
};
auto b = data.get_struct<Bert>();
cout << b.x << " " << b.y << endl;
cout << data.get<unsigned long>() << endl;
}
else
} else
cout << "Didn't find it" << endl;
exit(1);
}
}
else {
} else {
size_t size = 1ULL * 4096 * 244140ULL;
for (unsigned int n = 0;; ++n) {
if (!(n % 16384)) {

View File

@ -5,9 +5,9 @@
using namespace std;
using namespace LMDBSafe;
struct MDBVal
{
MDBVal(unsigned int v) : d_v(v)
struct MDBVal {
MDBVal(unsigned int v)
: d_v(v)
{
d_mdbval.mv_size = sizeof(d_v);
d_mdbval.mv_data = &d_v;
@ -20,7 +20,6 @@ struct MDBVal
MDB_val d_mdbval;
};
int main(int argc, char **argv)
{
auto env = getMDBEnv("./database", MDB_NOSUBDIR, 0600);
@ -31,7 +30,8 @@ int main(int argc, char** argv)
if (argc > 1)
limit = CppUtilities::stringToNumber<unsigned int>(argv[1]);
cout<<"Counting records.. "; cout.flush();
cout << "Counting records.. ";
cout.flush();
auto cursor = txn->getCursor(dbi);
MDBOutVal key, data;
int count = 0;
@ -43,16 +43,19 @@ int main(int argc, char** argv)
}
cout << "Have " << count << "!" << endl;
cout<<"Clearing records.. "; cout.flush();
cout << "Clearing records.. ";
cout.flush();
mdb_drop(*txn, dbi, 0); // clear records
cout << "Done!" << endl;
cout << "Adding "<<limit<<" values .. "; cout.flush();
cout << "Adding " << limit << " values .. ";
cout.flush();
for (unsigned long n = 0; n < limit; ++n) {
txn->put(dbi, n, n, MDB_APPEND);
}
cout << "Done!" << endl;
cout <<"Calling commit.. "; cout.flush();
cout << "Calling commit.. ";
cout.flush();
txn->commit();
cout << "Done!" << endl;
}

View File

@ -10,8 +10,7 @@
using namespace std;
using namespace LMDBSafe;
struct DNSResourceRecord
{
struct DNSResourceRecord {
string qname; // index
uint16_t qtype{ 0 };
uint32_t domain_id{ 0 }; // index
@ -21,21 +20,18 @@ struct DNSResourceRecord
bool auth{ true };
};
struct DomainInfo
{
struct DomainInfo {
string qname;
string master;
};
template<class Archive>
void serialize(Archive & ar, DomainInfo& g, const unsigned int version)
template <class Archive> void serialize(Archive &ar, DomainInfo &g, const unsigned int version)
{
CPP_UTILITIES_UNUSED(version)
ar &g.qname &g.master;
}
template<class Archive>
void serialize(Archive & ar, DNSResourceRecord& g, const unsigned int version)
template <class Archive> void serialize(Archive &ar, DNSResourceRecord &g, const unsigned int version)
{
CPP_UTILITIES_UNUSED(version)
ar &g.qtype;
@ -47,9 +43,7 @@ void serialize(Archive & ar, DNSResourceRecord& g, const unsigned int version)
ar &g.auth;
}
struct compound
{
struct compound {
std::string operator()(const DNSResourceRecord &rr)
{
std::string ret;
@ -62,30 +56,23 @@ struct compound
int main()
{
TypedDBI<DNSResourceRecord,
index_on<DNSResourceRecord, string, &DNSResourceRecord::qname>,
index_on<DNSResourceRecord, uint32_t, &DNSResourceRecord::domain_id>,
index_on_function<DNSResourceRecord, string, compound>
> tdbi(getMDBEnv("./typed.lmdb", MDB_NOSUBDIR, 0600), "records");
TypedDBI<DomainInfo,
index_on<DomainInfo, string, &DomainInfo::qname>
> tdomains(tdbi.getEnv(), "domains");
TypedDBI<DNSResourceRecord, index_on<DNSResourceRecord, string, &DNSResourceRecord::qname>,
index_on<DNSResourceRecord, uint32_t, &DNSResourceRecord::domain_id>, index_on_function<DNSResourceRecord, string, compound>>
tdbi(getMDBEnv("./typed.lmdb", MDB_NOSUBDIR, 0600), "records");
TypedDBI<DomainInfo, index_on<DomainInfo, string, &DomainInfo::qname>> tdomains(tdbi.getEnv(), "domains");
{
auto rotxn = tdbi.getROTransaction();
DNSResourceRecord rr0;
if (rotxn.get(2, rr0)) {
cout << "id 2, found " << rr0.qname << endl;
}
else {
} else {
cout << "Did not find id 2" << endl;
}
cout << "Iterating over name 'powerdns.com': " << endl;
auto range = rotxn.equal_range<0>("powerdns.com");
for(auto iter = std::move(range.first); iter != range.second; ++iter)
{
for (auto iter = std::move(range.first); iter != range.second; ++iter) {
cout << iter->qname << " " << iter->qtype << " " << iter->content << endl;
}
cout << "Currently have " << rotxn.size() << " entries" << endl;
@ -111,53 +98,76 @@ int main()
domtxn.put(di, 11);
DNSResourceRecord rr;
rr.domain_id=11; rr.qtype = 5; rr.ttl = 3600; rr.qname = "www.powerdns.com"; rr.ordername = "www";
rr.domain_id = 11;
rr.qtype = 5;
rr.ttl = 3600;
rr.qname = "www.powerdns.com";
rr.ordername = "www";
rr.content = "powerdns.com";
auto id = txn.put(rr);
cout << "Puted as id " << id << endl;
rr.qname = "powerdns.com"; rr.qtype = 1; rr.ordername=""; rr.content = "1.2.3.4";
rr.qname = "powerdns.com";
rr.qtype = 1;
rr.ordername = "";
rr.content = "1.2.3.4";
id = txn.put(rr);
cout << "Puted as id " << id << endl;
rr.qtype = 2; rr.content = "ns1.powerdns.com"; rr.ordername = "ns1";
rr.qtype = 2;
rr.content = "ns1.powerdns.com";
rr.ordername = "ns1";
id = txn.put(rr);
cout << "Puted as id " << id << endl;
rr.content = "ns2.powerdns.com"; rr.ordername = "ns2"; id = txn.put(rr);
rr.content = "ns2.powerdns.com";
rr.ordername = "ns2";
id = txn.put(rr);
cout << "Puted as id " << id << endl;
DomainInfo di2{ "ds9a.nl", "ns1.powerdns.com" };
domtxn.put(di, 10);
rr.qname = "www.ds9a.nl"; rr.domain_id = 10; rr.content = "1.2.3.4"; rr.qtype = 1;
rr.qname = "www.ds9a.nl";
rr.domain_id = 10;
rr.content = "1.2.3.4";
rr.qtype = 1;
rr.ordername = "www";
txn.put(rr);
rr.qname = "ds9a.nl"; rr.content = "ns1.ds9a.nl bert.ds9a.nl 1"; rr.qtype = 6;
rr.qname = "ds9a.nl";
rr.content = "ns1.ds9a.nl bert.ds9a.nl 1";
rr.qtype = 6;
rr.ordername = "";
txn.put(rr);
rr.qname = "ds9a.nl"; rr.content = "25 ns1.ds9a.nl"; rr.qtype = 15;
rr.qname = "ds9a.nl";
rr.content = "25 ns1.ds9a.nl";
rr.qtype = 15;
txn.put(rr);
rr.qname = "ns1.ds9a.nl"; rr.content = "1.2.3.4"; rr.qtype = 1;
rr.qname = "ns1.ds9a.nl";
rr.content = "1.2.3.4";
rr.qtype = 1;
rr.ordername = "ns1";
txn.put(rr);
rr.qname = "ns1.ds9a.nl"; rr.content = "::1"; rr.qtype = 26;
rr.qname = "ns1.ds9a.nl";
rr.content = "::1";
rr.qtype = 26;
txn.put(rr);
rr.qname = "ns2.ds9a.nl"; rr.content = "1.2.3.4"; rr.qtype = 1;
rr.qname = "ns2.ds9a.nl";
rr.content = "1.2.3.4";
rr.qtype = 1;
rr.ordername = "ns2";
txn.put(rr);
rr.qname = "ns2.ds9a.nl"; rr.content = "::1"; rr.qtype = 26;
rr.qname = "ns2.ds9a.nl";
rr.content = "::1";
rr.qtype = 26;
txn.put(rr);
DNSResourceRecord rr2;
id = txn.get<0>("www.powerdns.com", rr2);
@ -171,7 +181,6 @@ int main()
id = txn.get<0>("powerdns.com", rr3);
cout << id << endl;
cout << "Going to iterate over everything, ordered by name!" << endl;
for (auto iter = txn.begin<0>(); iter != txn.end(); ++iter) {
cout << iter.getID() << ": " << iter->qname << " " << iter->qtype << " " << iter->content << endl;
@ -219,9 +228,7 @@ int main()
DNSResourceRecord change;
txn.get(1, change);
cout << "1.auth: " << change.auth << endl;
txn.modify(1, [](DNSResourceRecord& record) {
record.auth = false;
});
txn.modify(1, [](DNSResourceRecord &record) { record.auth = false; });
txn.get(1, change);
cout << "1.auth: " << change.auth << endl;
txn.del(1);

View File

@ -21,7 +21,6 @@ void countDB(MDBEnv& env, MDBROTransaction& txn, const std::string& dbname)
cout << ": " << val.get<std::string>();
cout << "\n";
++count;
}
cout << count << endl;
}
@ -42,6 +41,4 @@ int main(int argc, char** argv)
cout << key.get<string>() << endl;
countDB(env, txn, key.get<string>());
} while (!cursor.get(key, val, MDB_NEXT));
}

View File

@ -10,7 +10,8 @@
using namespace std;
using namespace LMDBSafe;
TEST_CASE("Most basic tests", "[mostbasic]") {
TEST_CASE("Most basic tests", "[mostbasic]")
{
unlink("./tests");
MDBEnv env("./tests", MDB_NOSUBDIR, 0600);
@ -33,7 +34,8 @@ TEST_CASE("Most basic tests", "[mostbasic]") {
REQUIRE(rotxn->get(main, "lmdb", out) == MDB_NOTFOUND);
}
TEST_CASE("Range tests", "[range]") {
TEST_CASE("Range tests", "[range]")
{
unlink("./tests");
MDBEnv env("./tests", MDB_NOSUBDIR, 0600);
@ -92,7 +94,6 @@ TEST_CASE("Range tests", "[range]") {
REQUIRE(cursor.lower_bound("kees", key, val) == MDB_NOTFOUND);
}
}
TEST_CASE("moving transactions")
@ -203,7 +204,6 @@ TEST_CASE("nested RW transactions", "[transactions]")
CHECK(main_txn->get(main, "bertt", dummy) == MDB_NOTFOUND);
}
TEST_CASE("nesting RW -> RO", "[transactions]")
{
unlink("./tests");
@ -289,10 +289,7 @@ TEST_CASE("try to nest twice", "[transactions]")
sub_txn->del(main, "berthubert", "lmdb");
CHECK(sub_txn->get(main, "berthubert", dummy) == MDB_NOTFOUND);
CHECK_THROWS_AS(
main_txn->getRWTransaction(),
std::runtime_error
);
CHECK_THROWS_AS(main_txn->getRWTransaction(), std::runtime_error);
}
}

View File

@ -13,27 +13,24 @@
using namespace std;
using namespace LMDBSafe;
struct Member
{
struct Member {
std::string firstName;
std::string lastName;
time_t enrolled;
};
template<class Archive>
void serialize(Archive & ar, Member& g, const unsigned int version)
template <class Archive> void serialize(Archive &ar, Member &g, const unsigned int version)
{
CPP_UTILITIES_UNUSED(version)
ar &g.firstName &g.lastName &g.enrolled;
}
TEST_CASE("Basic typed tests", "[basictyped]") {
TEST_CASE("Basic typed tests", "[basictyped]")
{
unlink("./tests-typed");
typedef TypedDBI<Member,
index_on<Member, string, &Member::firstName>,
index_on<Member, string, &Member::lastName>,
index_on<Member, time_t, &Member::enrolled>
> tmembers_t;
typedef TypedDBI<Member, index_on<Member, string, &Member::firstName>, index_on<Member, string, &Member::lastName>,
index_on<Member, time_t, &Member::enrolled>>
tmembers_t;
auto tmembers = tmembers_t(getMDBEnv("./tests-typed", MDB_CREATE | MDB_NOSUBDIR, 0600), "members");