2011-01-04 03:50:33 +01:00
|
|
|
/*
|
|
|
|
|
Digxel(Test) - cover behaviour of a generic number-element holder
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2011, 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.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2011-01-06 01:35:22 +01:00
|
|
|
#include "lib/time/display.hpp"
|
2011-01-04 03:50:33 +01:00
|
|
|
#include "lib/time/digxel.hpp"
|
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
#include <iostream>
|
2011-01-04 03:50:33 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
2011-01-06 03:45:29 +01:00
|
|
|
using lumiera::error::LUMIERA_ERROR_ASSERTION;
|
2011-01-04 03:50:33 +01:00
|
|
|
using util::isSameObject;
|
|
|
|
|
using std::rand;
|
2011-01-06 01:35:22 +01:00
|
|
|
using std::cout;
|
2011-01-04 03:50:33 +01:00
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace time{
|
|
|
|
|
namespace test{
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
namespace { // Test data and setup
|
2011-01-04 03:50:33 +01:00
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
const uint REPEAT = 40;
|
2011-01-04 03:50:33 +01:00
|
|
|
const uint RAND_RANGE = 100;
|
|
|
|
|
const uint RAND_DENOM = 3;
|
2011-01-06 01:35:22 +01:00
|
|
|
const uint TIMING_CNT = 10000000;
|
2011-01-04 03:50:33 +01:00
|
|
|
|
|
|
|
|
inline double
|
|
|
|
|
randomFrac()
|
|
|
|
|
{
|
|
|
|
|
double arbitrary = (rand() % RAND_RANGE);
|
|
|
|
|
arbitrary /= (1 + rand() % RAND_DENOM);
|
|
|
|
|
return arbitrary;
|
|
|
|
|
}
|
2011-01-04 20:56:47 +01:00
|
|
|
|
2011-01-05 05:14:28 +01:00
|
|
|
inline uint
|
|
|
|
|
isOdd (uint i)
|
|
|
|
|
{
|
|
|
|
|
return i % 2;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
|
2011-01-05 05:14:28 +01:00
|
|
|
double sum(0),
|
|
|
|
|
checksum(0);
|
2011-01-06 01:35:22 +01:00
|
|
|
|
2011-01-05 05:14:28 +01:00
|
|
|
double
|
|
|
|
|
sideeffectSum (double val)
|
|
|
|
|
{
|
|
|
|
|
sum += val;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2011-01-04 20:56:47 +01:00
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
|
2011-01-04 20:56:47 +01:00
|
|
|
/* === special Digxel configuration for this test === */
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
|
limitingMutator (double value2set)
|
|
|
|
|
{
|
|
|
|
|
return (+1 < value2set) ? 1.0
|
|
|
|
|
: (-1 > value2set) ? -1.0
|
|
|
|
|
: value2set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct VerySpecialFormat
|
2011-01-05 05:14:28 +01:00
|
|
|
: digxel::PrintfFormatter<double, 11>
|
2011-01-04 20:56:47 +01:00
|
|
|
{
|
2011-01-06 01:35:22 +01:00
|
|
|
VerySpecialFormat() : digxel::PrintfFormatter<double,11>("##%+5.1f ##") { }
|
2011-01-04 20:56:47 +01:00
|
|
|
};
|
|
|
|
|
|
2011-01-05 05:14:28 +01:00
|
|
|
typedef Digxel<double, VerySpecialFormat> TestDigxel;
|
2011-01-06 01:35:22 +01:00
|
|
|
|
|
|
|
|
}//(End)Test setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-04 03:50:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* @test verify correct behaviour of an display "Digxel":
|
|
|
|
|
* A self-contained numeric element to support building displays.
|
|
|
|
|
* - build a Digxel
|
|
|
|
|
* - set a value
|
|
|
|
|
* - retrieve formatted display
|
2011-01-06 01:35:22 +01:00
|
|
|
* - performing side-effects from the setter-functor
|
|
|
|
|
* - formatted value caching
|
2011-01-04 03:50:33 +01:00
|
|
|
*/
|
|
|
|
|
class Digxel_test : public Test
|
|
|
|
|
{
|
|
|
|
|
virtual void
|
2011-01-04 20:56:47 +01:00
|
|
|
run (Arg)
|
2011-01-04 03:50:33 +01:00
|
|
|
{
|
2011-01-04 20:56:47 +01:00
|
|
|
checkSimpleUsage ();
|
|
|
|
|
checkMutation ();
|
|
|
|
|
verifyMutatorInfluence ();
|
2011-01-06 01:35:22 +01:00
|
|
|
verifyComparisons ();
|
2011-01-04 20:56:47 +01:00
|
|
|
checkCopy ();
|
|
|
|
|
checkDisplayOverrun ();
|
|
|
|
|
verifyDisplayCaching ();
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
checkSimpleUsage ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel digi;
|
|
|
|
|
CHECK (0 == digi);
|
2011-01-06 01:35:22 +01:00
|
|
|
CHECK ("## +0.0 ##" == string(digi));
|
|
|
|
|
cout << "empty____" << digi << endl;
|
2011-01-04 03:50:33 +01:00
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
digi = -88.77;
|
|
|
|
|
CHECK (-88.77 == digi);
|
|
|
|
|
CHECK ("##-88.8 ##" == string(digi));
|
|
|
|
|
cout << "value____" << digi << endl;
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
checkMutation ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel digi;
|
|
|
|
|
|
|
|
|
|
// configure what the Digxel does on "mutation"
|
2011-01-05 05:14:28 +01:00
|
|
|
digi.mutator = sideeffectSum;
|
2011-01-04 03:50:33 +01:00
|
|
|
|
|
|
|
|
CHECK (0 == digi);
|
2011-01-06 01:35:22 +01:00
|
|
|
sum = checksum = 0;
|
2011-01-04 03:50:33 +01:00
|
|
|
for (uint i=0; i < REPEAT; ++i)
|
|
|
|
|
{
|
|
|
|
|
double arbitrary = randomFrac();
|
2011-01-06 01:35:22 +01:00
|
|
|
checksum += arbitrary; // for verification
|
|
|
|
|
//
|
|
|
|
|
digi = arbitrary; //...causes invocation of mutation functor
|
2011-01-04 03:50:33 +01:00
|
|
|
|
|
|
|
|
CHECK (sum == checksum, "divergence after adding %f in iteration %d", arbitrary, i);
|
2011-01-05 01:05:13 +01:00
|
|
|
CHECK (digi == arbitrary);
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
2011-01-06 01:35:22 +01:00
|
|
|
CHECK (0 < sum);
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-01-04 20:56:47 +01:00
|
|
|
verifyMutatorInfluence ()
|
2011-01-04 03:50:33 +01:00
|
|
|
{
|
|
|
|
|
TestDigxel digi;
|
|
|
|
|
|
2011-01-04 20:56:47 +01:00
|
|
|
// using the default mutator
|
2011-01-04 03:50:33 +01:00
|
|
|
CHECK (0 == digi);
|
2011-01-04 20:56:47 +01:00
|
|
|
digi = 12.3;
|
|
|
|
|
CHECK (12.3 == digi);
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
// a special mutator to limit the value
|
2011-01-04 20:56:47 +01:00
|
|
|
digi.mutator = limitingMutator;
|
|
|
|
|
CHECK (12.3 == digi);
|
|
|
|
|
digi = 12.3;
|
|
|
|
|
CHECK (1 == digi);
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
digi = 0.5;
|
|
|
|
|
CHECK (0.5 == digi);
|
|
|
|
|
digi = -0.678;
|
|
|
|
|
CHECK (-0.678 == digi);
|
|
|
|
|
digi = -9.1011;
|
|
|
|
|
CHECK (-1 == digi);
|
|
|
|
|
|
|
|
|
|
digi.setValueRaw(12.3); // bypassing mutator
|
2011-01-04 03:50:33 +01:00
|
|
|
CHECK (12.3 == digi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
void
|
|
|
|
|
verifyComparisons ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel d1;
|
|
|
|
|
TestDigxel d2;
|
|
|
|
|
|
|
|
|
|
CHECK (d1 == d2);
|
|
|
|
|
|
|
|
|
|
double someValue = randomFrac();
|
|
|
|
|
d1 = someValue;
|
|
|
|
|
|
|
|
|
|
CHECK (d1 == someValue);
|
|
|
|
|
CHECK (d1 != d2);
|
|
|
|
|
CHECK (d2 != d1);
|
|
|
|
|
|
|
|
|
|
d2 = d1 + 22;
|
|
|
|
|
CHECK (d1 < d2);
|
|
|
|
|
CHECK (d1 <= d2);
|
|
|
|
|
|
|
|
|
|
CHECK (!(d1 > d2));
|
|
|
|
|
CHECK (!(d1 >= d2));
|
|
|
|
|
CHECK (!(d1 == d2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-04 03:50:33 +01:00
|
|
|
void
|
|
|
|
|
checkCopy ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel d1;
|
|
|
|
|
|
|
|
|
|
double someValue = randomFrac();
|
|
|
|
|
|
|
|
|
|
d1 = someValue;
|
|
|
|
|
CHECK (d1 == someValue);
|
|
|
|
|
|
2011-01-05 05:14:28 +01:00
|
|
|
TestDigxel d2(d1);
|
2011-01-04 03:50:33 +01:00
|
|
|
CHECK (d2 == someValue);
|
|
|
|
|
CHECK (!isSameObject (d1, d2));
|
|
|
|
|
|
|
|
|
|
d1 = randomFrac();
|
|
|
|
|
CHECK (d1 != d2);
|
|
|
|
|
CHECK (d2 == someValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
/** @test Digxel should be protected
|
|
|
|
|
* against display buffer overrun */
|
2011-01-04 03:50:33 +01:00
|
|
|
void
|
|
|
|
|
checkDisplayOverrun ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel digi;
|
|
|
|
|
digi = 123456789.12345678;
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
string formatted;
|
|
|
|
|
#if false ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
|
|
|
|
|
VERIFY_ERROR (ASSERTION, formatted = digi.show() ); // should trigger assertion
|
2011-01-06 03:45:29 +01:00
|
|
|
formatted = digi.show(); // second time doesn't reformat
|
2011-01-06 01:35:22 +01:00
|
|
|
#endif ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
|
|
|
|
|
|
2011-01-04 03:50:33 +01:00
|
|
|
CHECK (formatted.length() <= digi.maxlen());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
/** @test verify caching of formatted values.
|
|
|
|
|
* Digxel avoids reformatting unchanged values;
|
|
|
|
|
* to verify the effectivity of this measure, we
|
|
|
|
|
* take some timings.
|
|
|
|
|
* @warning the results of such tests could be unreliable,
|
|
|
|
|
* but in this case here I saw a significant difference,
|
|
|
|
|
* with values of 0.5sec / 0.8sec */
|
2011-01-04 03:50:33 +01:00
|
|
|
void
|
|
|
|
|
verifyDisplayCaching ()
|
|
|
|
|
{
|
|
|
|
|
TestDigxel digi;
|
|
|
|
|
digi = 1;
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
clock_t start(0), stop(0);
|
2011-01-04 03:50:33 +01:00
|
|
|
start = clock();
|
|
|
|
|
for (uint i=0; i < TIMING_CNT; ++i)
|
|
|
|
|
{
|
|
|
|
|
digi = 1;
|
2011-01-05 05:14:28 +01:00
|
|
|
isOdd (i);
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
stop = clock();
|
|
|
|
|
uint without_reformatting = stop - start;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
start = clock();
|
|
|
|
|
for (uint i=0; i < TIMING_CNT; ++i)
|
|
|
|
|
{
|
2011-01-05 05:14:28 +01:00
|
|
|
digi = isOdd (i);
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
stop = clock();
|
|
|
|
|
uint with_reformatting = stop - start;
|
|
|
|
|
|
2011-01-06 01:35:22 +01:00
|
|
|
cout << "without reformatting = "<< double(without_reformatting)/CLOCKS_PER_SEC <<"sec"<< endl;
|
|
|
|
|
cout << "with reformatting = "<< double(with_reformatting )/CLOCKS_PER_SEC <<"sec"<< endl;
|
|
|
|
|
|
|
|
|
|
CHECK (without_reformatting < with_reformatting);
|
2011-01-04 03:50:33 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (Digxel_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::time::test
|