# HG changeset patch # User Peter Kovacs # Date 1238176165 -3600 # Node ID f53d641aa967669a036ba891f018f71fe54a462b # Parent 3bd0484f4cc267ba274439b9b51d90c9aa5f252e 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. diff -r 3bd0484f4cc2 -r f53d641aa967 test/counter_test.cc --- a/test/counter_test.cc Sat Mar 28 10:36:53 2009 +0000 +++ b/test/counter_test.cc Fri Mar 27 18:49:25 2009 +0100 @@ -18,59 +18,86 @@ #include #include +#include + +#include "test/test_tools.h" using namespace lemon; template void bubbleSort(std::vector& 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 void insertionSort(std::vector& 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 -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& v) { @@ -80,8 +107,8 @@ int main() { - counterTest(); - counterTest(); + counterTest(true); + counterTest(false); std::vector x(10); init(x); bubbleSort(x); diff -r 3bd0484f4cc2 -r f53d641aa967 test/time_measure_test.cc --- a/test/time_measure_test.cc Sat Mar 28 10:36:53 2009 +0000 +++ b/test/time_measure_test.cc Fri Mar 27 18:49:25 2009 +0100 @@ -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";