2023-11-20 16:38:55 +01:00
|
|
|
/*
|
|
|
|
|
RandomDraw(Test) - verify the component builder for random selected values
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2023, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
/** @file random-draw-test.cpp
|
|
|
|
|
** unit test \ref RandomDraw_test
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
//#include "lib/test/test-helper.hpp"
|
|
|
|
|
#include "lib/random-draw.hpp"
|
2023-11-20 18:44:51 +01:00
|
|
|
#include "lib/test/diagnostic-output.hpp"////////////////////TODO
|
2023-11-20 16:38:55 +01:00
|
|
|
//#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
//#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
// using util::isSameObject;
|
|
|
|
|
// using std::rand;
|
|
|
|
|
|
|
|
|
|
// namespace error = lumiera::error;
|
|
|
|
|
// using error::LUMIERA_ERROR_FATAL;
|
|
|
|
|
// using error::LUMIERA_ERROR_STATE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
// const Literal THE_END = "all dead and hero got the girl";
|
|
|
|
|
|
2023-11-20 21:05:18 +01:00
|
|
|
struct SymmetricFive
|
|
|
|
|
: function<Limited<int, 5,-5>(size_t)>
|
|
|
|
|
{
|
|
|
|
|
static size_t defaultSrc (size_t hash) { return hash; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal helper to expose the signature `size_t(size_t)`
|
|
|
|
|
* by wrapping a given lambda or functor.
|
|
|
|
|
*/
|
|
|
|
|
template<class SIG>
|
|
|
|
|
struct Adaptor
|
|
|
|
|
{
|
|
|
|
|
static_assert (not sizeof(SIG), "Unable to adapt given functor.");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename RES>
|
|
|
|
|
struct Adaptor<RES(size_t)>
|
|
|
|
|
{
|
|
|
|
|
template<typename FUN>
|
|
|
|
|
static decltype(auto)
|
|
|
|
|
build (FUN&& fun)
|
|
|
|
|
{
|
|
|
|
|
return std::forward<FUN>(fun);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename RES>
|
|
|
|
|
struct Adaptor<RES(void)>
|
|
|
|
|
{
|
|
|
|
|
template<typename FUN>
|
|
|
|
|
static auto
|
|
|
|
|
build (FUN&& fun)
|
|
|
|
|
{
|
|
|
|
|
return [functor=std::forward<FUN>(fun)]
|
|
|
|
|
(size_t)
|
|
|
|
|
{
|
|
|
|
|
return functor();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
2023-11-20 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-20 21:05:18 +01:00
|
|
|
|
|
|
|
|
using Draw = RandomDraw<SymmetricFive>;
|
2023-11-20 16:38:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************//**
|
|
|
|
|
* @test Verify a flexible builder for random-value generators; using a config template,
|
|
|
|
|
* these can be outfitted to use a suitable source of randomness and to produce
|
|
|
|
|
* values from a desired target type and limited range.
|
|
|
|
|
* - TODO
|
|
|
|
|
* @see result.hpp
|
|
|
|
|
* @see lib::ThreadJoinable usage example
|
|
|
|
|
*/
|
|
|
|
|
class RandomDraw_test
|
|
|
|
|
: public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
simpleUse();
|
|
|
|
|
|
2023-11-20 21:05:18 +01:00
|
|
|
verify_policy();
|
2023-11-20 16:38:55 +01:00
|
|
|
verify_numerics();
|
2023-11-20 18:44:51 +01:00
|
|
|
verify_buildProfile();
|
2023-11-20 16:38:55 +01:00
|
|
|
verify_dynamicChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test TODO demonstrate a basic usage scenario
|
|
|
|
|
* @todo WIP 11/23 🔁 define ⟶ implement
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
simpleUse()
|
|
|
|
|
{
|
2023-11-20 18:44:51 +01:00
|
|
|
auto draw = Draw().probability(0.5);
|
2023-11-20 21:05:18 +01:00
|
|
|
SHOW_EXPR (int(draw(0) ));
|
|
|
|
|
SHOW_EXPR (int(draw(127)));
|
|
|
|
|
SHOW_EXPR (int(draw(128)));
|
|
|
|
|
SHOW_EXPR (int(draw(141)));
|
|
|
|
|
SHOW_EXPR (int(draw(255)));
|
|
|
|
|
SHOW_EXPR (int(draw(256)));
|
|
|
|
|
// CHECK (draw(0) == 0);
|
|
|
|
|
// CHECK (draw(127) == 0);
|
|
|
|
|
// CHECK (draw(128) == 1);
|
|
|
|
|
// CHECK (draw(141) == 2);
|
|
|
|
|
// CHECK (draw(255) ==10);
|
|
|
|
|
// CHECK (draw(256) == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test TODO verify configuration through policy template
|
|
|
|
|
* @todo WIP 11/23 🔁 define ⟶ implement
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verify_policy()
|
|
|
|
|
{
|
|
|
|
|
// auto d1 = RandomDraw<random_draw::LimitedRandomGenerate<5>>().probability(1.0);
|
|
|
|
|
//SHOW_EXPR (uint(d1()))
|
2023-11-20 16:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test TODO verify random number transformations
|
|
|
|
|
* @todo WIP 11/23 🔁 define ⟶ implement
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verify_numerics()
|
|
|
|
|
{
|
|
|
|
|
UNIMPLEMENTED ("verify random number transformations");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-20 18:44:51 +01:00
|
|
|
/** @test TODO verify the Builder-API to define the profile of result values.
|
|
|
|
|
* @todo WIP 11/23 🔁 define ⟶ implement
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verify_buildProfile()
|
|
|
|
|
{
|
|
|
|
|
UNIMPLEMENTED ("verify random number profile configuration");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-20 16:38:55 +01:00
|
|
|
/** @test TODO change the generation profile dynamically
|
|
|
|
|
* @todo WIP 11/23 🔁 define ⟶ implement
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verify_dynamicChange()
|
|
|
|
|
{
|
|
|
|
|
UNIMPLEMENTED ("change the generation profile dynamically");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (RandomDraw_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|