Improve timer and counter tests (#253)
authorPeter Kovacs <kpeter@inf.elte.hu>
Fri, 27 Mar 2009 18:49:25 +0100
changeset 605f53d641aa967
parent 604 3bd0484f4cc2
child 607 49a39bae067c
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.
test/counter_test.cc
test/time_measure_test.cc
     1.1 --- a/test/counter_test.cc	Sat Mar 28 10:36:53 2009 +0000
     1.2 +++ b/test/counter_test.cc	Fri Mar 27 18:49:25 2009 +0100
     1.3 @@ -18,59 +18,86 @@
     1.4  
     1.5  #include <lemon/counter.h>
     1.6  #include <vector>
     1.7 +#include <sstream>
     1.8 +
     1.9 +#include "test/test_tools.h"
    1.10  
    1.11  using namespace lemon;
    1.12  
    1.13  template <typename T>
    1.14  void bubbleSort(std::vector<T>& v) {
    1.15 -  Counter op("Bubble Sort - Operations: ");
    1.16 -  Counter::NoSubCounter as(op, "Assignments: ");
    1.17 -  Counter::NoSubCounter co(op, "Comparisons: ");
    1.18 -  for (int i = v.size()-1; i > 0; --i) {
    1.19 -    for (int j = 0; j < i; ++j) {
    1.20 -      if (v[j] > v[j+1]) {
    1.21 -        T tmp = v[j];
    1.22 -        v[j] = v[j+1];
    1.23 -        v[j+1] = tmp;
    1.24 -        as += 3;
    1.25 +  std::stringstream s1, s2, s3;
    1.26 +  {
    1.27 +    Counter op("Bubble Sort - Operations: ", s1);
    1.28 +    Counter::SubCounter as(op, "Assignments: ", s2);
    1.29 +    Counter::SubCounter co(op, "Comparisons: ", s3);
    1.30 +    for (int i = v.size()-1; i > 0; --i) {
    1.31 +      for (int j = 0; j < i; ++j) {
    1.32 +        if (v[j] > v[j+1]) {
    1.33 +          T tmp = v[j];
    1.34 +          v[j] = v[j+1];
    1.35 +          v[j+1] = tmp;
    1.36 +          as += 3;
    1.37 +        }
    1.38 +        ++co;
    1.39        }
    1.40 -      ++co;
    1.41      }
    1.42    }
    1.43 +  check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
    1.44 +  check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
    1.45 +  check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
    1.46  }
    1.47  
    1.48  template <typename T>
    1.49  void insertionSort(std::vector<T>& v) {
    1.50 -  Counter op("Insertion Sort - Operations: ");
    1.51 -  Counter::NoSubCounter as(op, "Assignments: ");
    1.52 -  Counter::NoSubCounter co(op, "Comparisons: ");
    1.53 -  for (int i = 1; i < int(v.size()); ++i) {
    1.54 -    T value = v[i];
    1.55 -    ++as;
    1.56 -    int j = i;
    1.57 -    while (j > 0 && v[j-1] > value) {
    1.58 -      v[j] = v[j-1];
    1.59 -      --j;
    1.60 -      ++co; ++as;
    1.61 +  std::stringstream s1, s2, s3;
    1.62 +  {
    1.63 +    Counter op("Insertion Sort - Operations: ", s1);
    1.64 +    Counter::SubCounter as(op, "Assignments: ", s2);
    1.65 +    Counter::SubCounter co(op, "Comparisons: ", s3);
    1.66 +    for (int i = 1; i < int(v.size()); ++i) {
    1.67 +      T value = v[i];
    1.68 +      ++as;
    1.69 +      int j = i;
    1.70 +      while (j > 0 && v[j-1] > value) {
    1.71 +        v[j] = v[j-1];
    1.72 +        --j;
    1.73 +        ++co; ++as;
    1.74 +      }
    1.75 +      v[j] = value;
    1.76 +      ++as;
    1.77      }
    1.78 -    v[j] = value;
    1.79 -    ++as;
    1.80    }
    1.81 +  check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
    1.82 +  check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
    1.83 +  check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
    1.84  }
    1.85  
    1.86  template <typename MyCounter>
    1.87 -void counterTest() {
    1.88 -  MyCounter c("Main Counter: ");
    1.89 -  c++;
    1.90 -  typename MyCounter::SubCounter d(c, "SubCounter: ");
    1.91 -  d++;
    1.92 -  typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
    1.93 -  e++;
    1.94 -  d+=3;
    1.95 -  c-=4;
    1.96 -  e-=2;
    1.97 -  c.reset(2);
    1.98 -  c.reset();
    1.99 +void counterTest(bool output) {
   1.100 +  std::stringstream s1, s2, s3;
   1.101 +  {
   1.102 +    MyCounter c("Main Counter: ", s1);
   1.103 +    c++;
   1.104 +    typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
   1.105 +    d++;
   1.106 +    typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
   1.107 +    e++;
   1.108 +    d+=3;
   1.109 +    c-=4;
   1.110 +    e-=2;
   1.111 +    c.reset(2);
   1.112 +    c.reset();
   1.113 +  }
   1.114 +  if (output) {
   1.115 +    check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
   1.116 +    check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
   1.117 +    check(s3.str() == "", "Wrong NoSubCounter");
   1.118 +  } else {
   1.119 +    check(s1.str() == "", "Wrong NoCounter");
   1.120 +    check(s2.str() == "", "Wrong SubCounter");
   1.121 +    check(s3.str() == "", "Wrong NoSubCounter");
   1.122 +  }
   1.123  }
   1.124  
   1.125  void init(std::vector<int>& v) {
   1.126 @@ -80,8 +107,8 @@
   1.127  
   1.128  int main()
   1.129  {
   1.130 -  counterTest<Counter>();
   1.131 -  counterTest<NoCounter>();
   1.132 +  counterTest<Counter>(true);
   1.133 +  counterTest<NoCounter>(false);
   1.134  
   1.135    std::vector<int> x(10);
   1.136    init(x); bubbleSort(x);
     2.1 --- a/test/time_measure_test.cc	Sat Mar 28 10:36:53 2009 +0000
     2.2 +++ b/test/time_measure_test.cc	Fri Mar 27 18:49:25 2009 +0100
     2.3 @@ -39,18 +39,16 @@
     2.4  {
     2.5    Timer T;
     2.6    unsigned int n;
     2.7 -  for(n=0;T.realTime()<1.0;n++) ;
     2.8 +  for(n=0;T.realTime()<0.1;n++) ;
     2.9    std::cout << T << " (" << n << " time queries)\n";
    2.10 -  T.restart();
    2.11 -  while(T.realTime()<2.0) ;
    2.12 -  std::cout << T << '\n';
    2.13 +
    2.14    TimeStamp full;
    2.15    TimeStamp t;
    2.16 -  t=runningTimeTest(f,1,&n,&full);
    2.17 +  t=runningTimeTest(f,0.1,&n,&full);
    2.18    std::cout << t << " (" << n << " tests)\n";
    2.19    std::cout << "Total: " << full << "\n";
    2.20  
    2.21 -  t=runningTimeTest(g,1,&n,&full);
    2.22 +  t=runningTimeTest(g,0.1,&n,&full);
    2.23    std::cout << t << " (" << n << " tests)\n";
    2.24    std::cout << "Total: " << full << "\n";
    2.25