use boost::random to ensure distinct test frame contents

This commit is contained in:
Fischlurch 2011-11-01 03:11:43 +01:00
parent 20777cca8a
commit db3ea6638d
4 changed files with 44 additions and 80 deletions

View file

@ -20,53 +20,31 @@
// 1/11 - integer floor and wrap operation(s)
// 1/11 - how to fetch the path of the own executable -- at least under Linux?
// 10/11 - simple demo using a pointer and a struct
// 11/11 - using the boost random number generator(s)
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/random/linear_congruential.hpp>
using boost::lexical_cast;
using std::cout;
/**
* custom datastructure
* holding a constant char array with "hey"
*/
struct MyStruct
{
char data_[3];
const int length_;
MyStruct()
: length_(3)
{
const char *tmp = "hey";
for (int i=0; i<length_; ++i)
data_[i] = *(tmp+i);
}
};
// define a global variable holding a MyStruct
MyStruct theStruct;
void
printMyStruct(MyStruct* myPointer)
{
for (int i=0; i < myPointer->length_; ++i)
cout << myPointer->data_[i];
cout << "\n";
}
using std::endl;
int
main (int, char**) //(int argc, char* argv[])
main (int cnt, char* argv[])
{
printMyStruct (&theStruct);
int32_t seed = (2 == cnt)? lexical_cast<int32_t> (argv[1]) : 42;
boost::rand48 ranGen(seed);
cout << "seed = "<< seed << endl;
for (uint i=0; i< 100; ++i)
cout << ranGen() % CHAR_MAX <<"__";
cout << "\n.gulp.\n";

View file

@ -2,7 +2,7 @@ TESTING "Component Test Suite: Render Engine parts" ./test-components --group=en
PLANNED "Test support: dummy frames" TestFrame_test <<END
TEST "Test support: dummy frames" TestFrame_test <<END
return: 0
END

View file

@ -24,25 +24,30 @@
#include "proc/engine/testframe.hpp"
#include "lib/error.hpp"
#include <boost/random/linear_congruential.hpp>
#include <boost/scoped_ptr.hpp>
#include <limits.h>
#include <cstring>
#include <vector>
using std::memcpy;
using std::vector;
namespace engine {
namespace test {
using std::vector;
using std::memcpy;
typedef boost::rand48 PseudoRandom;
namespace error = lumiera::error;
namespace { // hidden local support facilities....
/** @internal helper for generating unique test frames.
* The "discriminator" is used as a stepping size when
* This "discriminator" is used as a random seed when
* filling the test frame data buffers. It is generated
* to be different on adjacent frames of the same series,
* as well as to differ to all near by neighbouring channels.
@ -50,10 +55,10 @@ namespace test {
* @param family the channel this frame belongs to
*/
uint64_t
generateDiscriminator(uint seq, uint family)
generateDistinction(uint seq, uint family)
{
// random offset, but fixed per executable run
static uint base(10 + rand() % 90);
static uint base(10 + rand() % 990);
// use the family as stepping
return (seq+1) * (base+family);
@ -128,6 +133,7 @@ namespace test {
boost::scoped_ptr<TestFrames> testFrames;
TestFrame&
accessTestFrame (uint seqNr, uint chanNr)
{
@ -162,6 +168,7 @@ namespace test {
/* ===== TestFrame class ===== */
TestFrame::~TestFrame()
{
@ -170,16 +177,16 @@ namespace test {
TestFrame::TestFrame(uint seq, uint family)
: discriminator_(generateDiscriminator(seq,family))
: distinction_(generateDistinction (seq,family))
, stage_(CREATED)
{
ASSERT (0 < discriminator_);
ASSERT (0 < distinction_);
buildData();
}
TestFrame::TestFrame (TestFrame const& o)
: discriminator_(o.discriminator_)
: distinction_(o.distinction_)
, stage_(CREATED)
{
memcpy (data_, o.data_, BUFFSIZ);
@ -192,7 +199,7 @@ namespace test {
throw new error::Logic ("target TestFrame is already dead");
if (this != &o)
{
discriminator_ = o.discriminator_;
distinction_ = o.distinction_;
stage_ = CREATED;
memcpy (data_, o.data_, BUFFSIZ);
}
@ -242,9 +249,9 @@ namespace test {
bool
TestFrame::verifyData() const
{
char c(0);
PseudoRandom gen(distinction_);
for (uint i=0; i<BUFFSIZ; ++i)
if (data_[i] != (c=generate(c)))
if (data_[i] != (gen() % CHAR_MAX))
return false;
return true;
}
@ -252,15 +259,9 @@ namespace test {
void
TestFrame::buildData()
{
char c(0);
PseudoRandom gen(distinction_);
for (uint i=0; i<BUFFSIZ; ++i)
data_[i] = (c=generate(c));
}
char
TestFrame::generate (char prev) const
{
return (discriminator_ + prev) % CHAR_MAX;
data_[i] = (gen() % CHAR_MAX);
}

View file

@ -25,23 +25,14 @@
#define PROC_ENGINE_TESTFRAME_H
//#include "lib/time/timevalue.hpp"
//#include <string>
#include <cstdlib>
#include <stdint.h>
//using std::tr1::shared_ptr;
//using std::string;
namespace engine {
namespace test {
//class TestPlacement;
/**
* Mock data frame for simulated rendering.
* A test frame can be created and placed instead of a real data frame.
@ -52,12 +43,13 @@ namespace test {
*
* Each TestFrame is automatically filled with pseudo random data;
* multiple frames are arranged in sequences and channels, causing the random data
* to be reproducible yet different in each frame. The lifecycle of each TestFrame
* is tracked and marked in an embedded state field. Moreover, the contents of
* the data block can be verified, because the sequence of bytes is reproducible,
* to be reproducible yet different within each frame. TestFrame's lifecycle is
* tracked and marked in an embedded state field. Moreover, the contents of the
* data block can be verified, because the sequence of bytes is reproducible,
* based on the channel and sequence number of the test frame.
*
* @todo WIP-WIP-WIP 9/11
* @see TestFrame_test
* @see OutputSlotProtocol_test
*
*/
class TestFrame
@ -68,7 +60,7 @@ namespace test {
static const size_t BUFFSIZ = 1024;
uint64_t discriminator_;
uint64_t distinction_;
StageOfLife stage_;
char data_[BUFFSIZ];
@ -79,7 +71,7 @@ namespace test {
TestFrame (TestFrame const&);
TestFrame& operator= (TestFrame const&);
/** Helper to verify a given memory location holds
/** Helper to verify that a given memory location holds
* an active TestFrame instance (created, not yet destroyed)
* @return true if the TestFrame datastructure is intact and
* marked as still alive.
@ -101,9 +93,7 @@ namespace test {
private:
bool contentEquals (TestFrame const& o) const;
bool verifyData() const;
char generate (char) const;
bool verifyData() const;
void buildData ();
};
@ -126,10 +116,5 @@ namespace test {
/* == some test data to check == */
// extern const lib::time::Duration LENGTH_TestClip;
}} // namespace engine::test
#endif