LUMIERA.clone/tests/library/fun-hash-dispatch-test.cpp
Ichthyostega abeca98233 Invocation: Analysis regarding dispatch of information functions for Nodes
The choice to rely on strictly typed functor bindings for the Node operation
bears the danger to produce ''template bloat'' — it would be dangerous to add
further functions to the Port-API naïvely; espeically simple information functions
will likely not depend on the full type information.

A remedy to explore would be to exploit properties marked into the Port's `ProcID`
as key for a dispatcher hashtable; assuming that the `NodeBuilder` will be responsible
for registering the corresponding implementation functions, such a solution could even
be somewhat type-safe, as long as the semantics of the ProcID are maintained correctly.
2025-01-11 00:20:36 +01:00

101 lines
2.7 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
FunHashDispatch(Test) - verify dispatching of opaque functions keyed by hash-ID
Copyright (C)
2025, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file fun-hash-dispatch-test.cpp
** unit test \ref FunHashDispatch_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/fun-hash-dispatch.hpp"
#include "lib/format-obj.hpp"
#include "lib/test/diagnostic-output.hpp"////////////////////TODO
//#include "lib/util.hpp"
//#include <cstdlib>
#include <string>
namespace lib {
namespace test{
// using util::isSameAdr;
// using std::rand;
using util::toString;
using std::string;
// namespace error = lumiera::error;
// using error::LUMIERA_ERROR_FATAL;
// using error::LUMIERA_ERROR_STATE;
namespace {
// #define Type(_EXPR_) lib::test::showType<decltype(_EXPR_)>()
}
/***********************************************************************************//**
* @test Verify generic helper to provide a hash-based function dispatch table.
* @see fun-hash-dispatch.hpp
* @see NodeMeta_test
*/
class FunHashDispatch_test
: public Test
{
void
run (Arg)
{
FunHashDispatch<string(int)> dispatch;
auto one = [](int i) { return toString(i); };
auto two = [](int i) { return string(size_t(i),'*'); };
auto* res = dispatch.enrol (1, one);
CHECK (res == one);
CHECK (dispatch.contains(1));
SHOW_EXPR(dispatch.select(1)(42));
CHECK (dispatch.select(1)(42) == "42"_expect);
dispatch.enrol (2, two);
CHECK (dispatch.contains(1));
CHECK (dispatch.contains(2));
SHOW_EXPR(dispatch.select(1)(5));
CHECK (dispatch.select(1)(5) == "5"_expect);
SHOW_EXPR(dispatch.select(2)(5));
CHECK (dispatch.select(2)(5) == "*****"_expect);
res = dispatch.enrol (1,two);
CHECK (res == one);
res = dispatch.enrol (3,two);
CHECK (res == two);
CHECK (dispatch.select(3)(2) == "**"_expect);
VERIFY_FAIL ("Expect function for given hash"
, dispatch.select(5) );
}
};
/** Register this test class... */
LAUNCHER (FunHashDispatch_test, "unit common");
}} // namespace lib::test