2 #define BOOST_TEST_MODULE (ParameterSetRegistry_t) 3 #include "boost/test/unit_test.hpp" 8 #include "hep_concurrency/simultaneous_function_spawner.h" 16 #include <unordered_map> 20 using namespace fhicl;
23 using namespace string_literals;
27 BOOST_AUTO_TEST_SUITE(ParameterSetRegistry_t)
32 unordered_map<ParameterSetID, ParameterSet, detail::HashParameterSetID>;
33 static_assert(is_same_v<ctype, ParameterSetRegistry::collection_type>);
34 static_assert(is_same_v<ParameterSetID, ParameterSetRegistry::key_type>);
35 static_assert(is_same_v<ParameterSet, ParameterSetRegistry::mapped_type>);
36 static_assert(is_same_v<pair<ParameterSetID const, ParameterSet>,
38 static_assert(is_same_v<size_t, ParameterSetRegistry::size_type>);
40 is_same_v<ctype::const_iterator, ParameterSetRegistry::const_iterator>);
48 "x: 5 y: { a: \"oops\" b: 9 } z: { c: \"Looooong striiiiiing.\" }");
60 vector<ParameterSet> v1;
63 {
"a1: 4 b1: 7.0 c1: [ 5, 4, 3 ]",
64 "a2: [ oh, my, stars ]",
65 "a3: { x: { y: [ now, is, the, time, for, all, good, men ] } }"}) {
69 vector<ParameterSetRegistry::value_type> v2;
70 string const f{
"filler"};
73 v2.emplace_back(
p.id(),
move(
p));
94 char* errMsg =
nullptr;
96 "BEGIN TRANSACTION; DROP TABLE IF EXISTS ParameterSets;" 97 "CREATE TABLE ParameterSets(ID PRIMARY KEY, PSetBlob); COMMIT;",
104 vector<pair<string, bool>> testData{
105 {
"x: [ 1, 3, 5, 7 ]",
false},
106 {
"a2: [ oh, my, stars ]",
true},
107 {
"y1: \"Oh, home on the range\" y2: \"Where the deer and the antelope " 110 vector<pair<ParameterSet, bool>> v1(testData.size());
113 vector<function<void()>> tasks;
115 [&v1, &tasks](
size_t const i,
auto const&
p) {
117 tasks.push_back([&
entry, &
p] {
119 entry = make_pair(pset,
p.second);
122 simultaneous_function_spawner sfs{tasks};
126 std::recursive_mutex
m{};
127 auto insert_into_db = [&db, &
m](
auto const& pr) {
140 "INSERT INTO ParameterSets(ID, PSetBlob) VALUES(?, ?);",
145 auto const& pset = pr.first;
146 bool const inRegistry{pr.second};
147 string const id{pset.id().to_string()};
149 sqlite3_bind_text(oStmt, 1,
id.c_str(),
id.
size() + 1, SQLITE_STATIC);
152 string const psBlob{pset.to_compact_string()};
153 auto const rc2 = sqlite3_bind_text(
154 oStmt, 2, psBlob.c_str(), psBlob.size() + 1, SQLITE_STATIC);
158 std::lock_guard sentry{
m};
159 assert(sqlite3_step(oStmt) == SQLITE_DONE);
161 auto const rc3 = sqlite3_finalize(oStmt);
166 vector<function<void()>> tasks;
168 v1, back_inserter(tasks), [insert_into_db](
auto const& pr) {
169 return [insert_into_db, pr] { insert_into_db(pr); };
171 simultaneous_function_spawner sfs{tasks};
180 std::recursive_mutex
m{};
181 auto read_from_registry = [&expected_size, &
m](
auto const&
p) {
182 auto const&
id =
p.first.id();
186 std::lock_guard sentry{
m};
193 std::lock_guard sentry{
m};
203 std::lock_guard sentry{
m};
208 vector<function<void()>> tasks;
210 v1, back_inserter(tasks), [read_from_registry](
auto const&
p) {
211 return [read_from_registry,
p] { read_from_registry(
p); };
213 simultaneous_function_spawner sfs{tasks};
227 "SELECT 1 from sqlite_master where type='table' and name='ParameterSets';",
235 sqlite3_finalize(stmt);
237 db,
"SELECT COUNT(*) from ParameterSets;", -1, &stmt,
nullptr);
240 sqlite3_finalize(stmt);
244 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(Typedefs)
static ParameterSetID const & put(ParameterSet const &ps)
static ParameterSet make(intermediate_table const &tbl)
void for_all_with_index(FwdCont &, Func)
static collection_type const & get() noexcept
struct sqlite3_stmt sqlite3_stmt
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
auto transform_all(Container &, OutputIt, UnaryOp)
static bool has(ParameterSetID const &id)
void throwOnSQLiteFailure(int rc, char *msg=nullptr)
typename collection_type::value_type value_type
static void exportTo(sqlite3 *db)
static void importFrom(sqlite3 *db)