[Lemon-commits] Peter Kovacs: Improve timer and counter tests (#...
Lemon HG
hg at lemon.cs.elte.hu
Sun Mar 29 09:47:42 CEST 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/f53d641aa967
changeset: 588:f53d641aa967
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Fri Mar 27 18:49:25 2009 +0100
description:
Improve timer and counter tests (#253)
- Do not print the output of counter_test.cc.
- Check the output of counter_test.cc.
- Shorten the running time of time_measure_test.cc.
diffstat:
test/counter_test.cc | 103 +++++++++++++++++++++------------
test/time_measure_test.cc | 10 +--
2 files changed, 69 insertions(+), 44 deletions(-)
diffs (163 lines):
diff --git a/test/counter_test.cc b/test/counter_test.cc
--- a/test/counter_test.cc
+++ b/test/counter_test.cc
@@ -18,59 +18,86 @@
#include <lemon/counter.h>
#include <vector>
+#include <sstream>
+
+#include "test/test_tools.h"
using namespace lemon;
template <typename T>
void bubbleSort(std::vector<T>& v) {
- Counter op("Bubble Sort - Operations: ");
- Counter::NoSubCounter as(op, "Assignments: ");
- Counter::NoSubCounter co(op, "Comparisons: ");
- for (int i = v.size()-1; i > 0; --i) {
- for (int j = 0; j < i; ++j) {
- if (v[j] > v[j+1]) {
- T tmp = v[j];
- v[j] = v[j+1];
- v[j+1] = tmp;
- as += 3;
+ std::stringstream s1, s2, s3;
+ {
+ Counter op("Bubble Sort - Operations: ", s1);
+ Counter::SubCounter as(op, "Assignments: ", s2);
+ Counter::SubCounter co(op, "Comparisons: ", s3);
+ for (int i = v.size()-1; i > 0; --i) {
+ for (int j = 0; j < i; ++j) {
+ if (v[j] > v[j+1]) {
+ T tmp = v[j];
+ v[j] = v[j+1];
+ v[j+1] = tmp;
+ as += 3;
+ }
+ ++co;
}
- ++co;
}
}
+ check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
+ check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
+ check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
}
template <typename T>
void insertionSort(std::vector<T>& v) {
- Counter op("Insertion Sort - Operations: ");
- Counter::NoSubCounter as(op, "Assignments: ");
- Counter::NoSubCounter co(op, "Comparisons: ");
- for (int i = 1; i < int(v.size()); ++i) {
- T value = v[i];
- ++as;
- int j = i;
- while (j > 0 && v[j-1] > value) {
- v[j] = v[j-1];
- --j;
- ++co; ++as;
+ std::stringstream s1, s2, s3;
+ {
+ Counter op("Insertion Sort - Operations: ", s1);
+ Counter::SubCounter as(op, "Assignments: ", s2);
+ Counter::SubCounter co(op, "Comparisons: ", s3);
+ for (int i = 1; i < int(v.size()); ++i) {
+ T value = v[i];
+ ++as;
+ int j = i;
+ while (j > 0 && v[j-1] > value) {
+ v[j] = v[j-1];
+ --j;
+ ++co; ++as;
+ }
+ v[j] = value;
+ ++as;
}
- v[j] = value;
- ++as;
}
+ check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
+ check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
+ check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
}
template <typename MyCounter>
-void counterTest() {
- MyCounter c("Main Counter: ");
- c++;
- typename MyCounter::SubCounter d(c, "SubCounter: ");
- d++;
- typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
- e++;
- d+=3;
- c-=4;
- e-=2;
- c.reset(2);
- c.reset();
+void counterTest(bool output) {
+ std::stringstream s1, s2, s3;
+ {
+ MyCounter c("Main Counter: ", s1);
+ c++;
+ typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
+ d++;
+ typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
+ e++;
+ d+=3;
+ c-=4;
+ e-=2;
+ c.reset(2);
+ c.reset();
+ }
+ if (output) {
+ check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
+ check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
+ check(s3.str() == "", "Wrong NoSubCounter");
+ } else {
+ check(s1.str() == "", "Wrong NoCounter");
+ check(s2.str() == "", "Wrong SubCounter");
+ check(s3.str() == "", "Wrong NoSubCounter");
+ }
}
void init(std::vector<int>& v) {
@@ -80,8 +107,8 @@
int main()
{
- counterTest<Counter>();
- counterTest<NoCounter>();
+ counterTest<Counter>(true);
+ counterTest<NoCounter>(false);
std::vector<int> x(10);
init(x); bubbleSort(x);
diff --git a/test/time_measure_test.cc b/test/time_measure_test.cc
--- a/test/time_measure_test.cc
+++ b/test/time_measure_test.cc
@@ -39,18 +39,16 @@
{
Timer T;
unsigned int n;
- for(n=0;T.realTime()<1.0;n++) ;
+ for(n=0;T.realTime()<0.1;n++) ;
std::cout << T << " (" << n << " time queries)\n";
- T.restart();
- while(T.realTime()<2.0) ;
- std::cout << T << '\n';
+
TimeStamp full;
TimeStamp t;
- t=runningTimeTest(f,1,&n,&full);
+ t=runningTimeTest(f,0.1,&n,&full);
std::cout << t << " (" << n << " tests)\n";
std::cout << "Total: " << full << "\n";
- t=runningTimeTest(g,1,&n,&full);
+ t=runningTimeTest(g,0.1,&n,&full);
std::cout << t << " (" << n << " tests)\n";
std::cout << "Total: " << full << "\n";
More information about the Lemon-commits
mailing list