WIP rework and adjust "Digxel" draft

This commit is contained in:
Fischlurch 2011-01-04 20:56:47 +01:00
parent 6638120ec2
commit b19fd1e634
3 changed files with 112 additions and 15 deletions

View file

@ -33,6 +33,10 @@ namespace lib {
namespace time {
Digxel::~Digxel () { }
// A hint for the compiler to emit VTables and magic stuff here
/** */

View file

@ -63,6 +63,7 @@
#define LIB_TIME_DIGXEL_H
#include "lib/error.hpp"
//#include "lib/symbol.hpp"
//#include <boost/operators.hpp>
#include <cstdlib> ////////////////////TODO
@ -72,8 +73,7 @@
namespace lib {
namespace time {
using std::string;
// using std::string;
/**
* A number element for building structured numeric displays.
@ -89,17 +89,77 @@ namespace time {
* @see lib::time::TCode
* @todo WIP-WIP-WIP
*/
template<class FMT>
class Digxel
{
public:
string describe() const;
virtual ~Digxel (); ///< this is an ABC
};
namespace digxel {
// using lib::Literal;
template<typename NUM>
struct PrintfFormatter
{
enum{ len = 6
, bufsiz = len+1
};
char printbuffer_[bufsiz];
static void
show (NUM val)
{
size_t space = std::snprintf (printbuffer_, bufsiz, "%5d", val);
REQUIRE (space <= bufsiz, "Digxel value exceeded available buffer size. "
"For showing %d, %d chars instead of just %d would be required."
, val, space, bufsiz);
}
};
template<typename NUM>
struct Formatter;
template<>
struct Formatter<int>
: PrintfFormatter<int>
{
enum{ len = 6
, bufsiz = len+1
};
};
/**
* The outward hull of a concrete Digxel implementation.
* Inheriting from the Digxel interface, it embodies a concrete
* Formatter specialised to yield the desired behaviour.
*
* @param TODO
* @todo WIP-WIP-WIP
*/
template< typename NUM
, class FMT = digxel::Formatter<NUM>
>
class Holder
: public Digxel
{
FMT buffer_;
NUM value_;
public:
Holder ()
: buffer_()
, value_()
{ }
};
}
}} // lib::time

View file

@ -55,6 +55,7 @@ namespace test{
const uint REPEAT = 40;
const uint RAND_RANGE = 100;
const uint RAND_DENOM = 3;
const uint TIMING_CNT = 10000;
inline double
randomFrac()
@ -63,7 +64,25 @@ namespace test{
arbitrary /= (1 + rand() % RAND_DENOM);
return arbitrary;
}
/* === special Digxel configuration for this test === */
double
limitingMutator (double value2set)
{
return (+1 < value2set) ? 1.0
: (-1 > value2set) ? -1.0
: value2set;
}
struct VerySpecialFormat
{
};
typedef digxel::Holder<double, VerySpecialFormat> TestDigxel;
}
@ -79,9 +98,14 @@ namespace test{
class Digxel_test : public Test
{
virtual void
run (Arg arg)
run (Arg)
{
checkSimpleUsage (ref);
checkSimpleUsage ();
checkMutation ();
verifyMutatorInfluence ();
checkCopy ();
checkDisplayOverrun ();
verifyDisplayCaching ();
}
@ -114,21 +138,30 @@ namespace test{
double arbitrary = randomFrac();
checksum += arbitrary;
digi.mutate (arbitrary); // invoke the mutation functor
digi = arbitrary; // invoke the mutation functor
CHECK (sum == checksum, "divergence after adding %f in iteration %d", arbitrary, i);
CHECK (0 == digi); // the value itself is not touched
CHECK (digi == abitrary);
}
}
void
checkDefaultMutator ()
verifyMutatorInfluence ()
{
TestDigxel digi;
// using the default mutator
CHECK (0 == digi);
digi.mutate(12.3);
digi = 12.3;
CHECK (12.3 == digi);
digi.mutator = limitingMutator;
CHECK (12.3 == digi);
digi = 12.3;
CHECK (1 == digi);
digi.setValueRaw(12.3);
CHECK (12.3 == digi);
}
@ -159,7 +192,7 @@ namespace test{
TestDigxel digi;
digi = 123456789.12345678;
string formatted (digi.show());
string formatted (digi.show()); // should trigger assertion
CHECK (formatted.length() <= digi.maxlen());
}