test/counter_test.cc
 changeset 171 02f4d5d9bfd7 parent 119 82a2639a05bb child 209 765619b7cbb2
```     1.1 --- a/test/counter_test.cc	Sun Jun 15 22:03:33 2008 +0200
1.2 +++ b/test/counter_test.cc	Sun Jun 15 22:05:23 2008 +0200
1.3 @@ -17,50 +17,75 @@
1.4   */
1.5
1.6  #include <lemon/counter.h>
1.7 +#include <vector>
1.8
1.9 -///\file \brief Test cases for time_measure.h
1.10 -///
1.11 -///\todo To be extended
1.12 +using namespace lemon;
1.13
1.14 +template <typename T>
1.15 +void bubbleSort(std::vector<T>& v) {
1.16 +  Counter op("Bubble Sort - Operations: ");
1.17 +  Counter::NoSubCounter as(op, "Assignments: ");
1.18 +  Counter::NoSubCounter co(op, "Comparisons: ");
1.19 +  for (int i = v.size()-1; i > 0; --i) {
1.20 +    for (int j = 0; j < i; ++j) {
1.21 +      if (v[j] > v[j+1]) {
1.22 +        T tmp = v[j];
1.23 +        v[j] = v[j+1];
1.24 +        v[j+1] = tmp;
1.25 +        as += 3;
1.26 +      }
1.27 +      ++co;
1.28 +    }
1.29 +  }
1.30 +}
1.31
1.32 -int fibonacci(int f)
1.33 -{
1.34 -  static lemon::Counter count("Fibonacci steps: ");
1.35 -  count++;
1.36 -  if(f<1) return 0;
1.37 -  else if(f==1) return 1;
1.38 -  else return fibonacci(f-1)+fibonacci(f-2);
1.39 +template <typename T>
1.40 +void insertionSort(std::vector<T>& v) {
1.41 +  Counter op("Insertion Sort - Operations: ");
1.42 +  Counter::NoSubCounter as(op, "Assignments: ");
1.43 +  Counter::NoSubCounter co(op, "Comparisons: ");
1.44 +  for (int i = 1; i < int(v.size()); ++i) {
1.45 +    T value = v[i];
1.46 +    ++as;
1.47 +    int j = i;
1.48 +    while (j > 0 && v[j-1] > value) {
1.49 +      v[j] = v[j-1];
1.50 +      --j;
1.51 +      ++co; ++as;
1.52 +    }
1.53 +    v[j] = value;
1.54 +    ++as;
1.55 +  }
1.56 +}
1.57 +
1.58 +template <typename MyCounter>
1.59 +void counterTest() {
1.60 +  MyCounter c("Main Counter: ");
1.61 +  c++;
1.62 +  typename MyCounter::SubCounter d(c, "SubCounter: ");
1.63 +  d++;
1.64 +  typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
1.65 +  e++;
1.66 +  d+=3;
1.67 +  c-=4;
1.68 +  e-=2;
1.69 +  c.reset(2);
1.70 +  c.reset();
1.71 +}
1.72 +
1.73 +void init(std::vector<int>& v) {
1.74 +  v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100;
1.75 +  v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70;
1.76  }
1.77
1.78  int main()
1.79  {
1.80 -  fibonacci(10);
1.81 +  counterTest<Counter>();
1.82 +  counterTest<NoCounter>();
1.83
1.84 -  {
1.85 -    typedef lemon::Counter MyCounter;
1.86 -    MyCounter c("Main counter: ");
1.87 -    c++;
1.88 -    c++;
1.89 -    MyCounter::SubCounter d(c,"Subcounter: ");
1.90 -    d++;
1.91 -    d++;
1.92 -    MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: ");
1.93 -    e++;
1.94 -    e++;
1.95 -  }
1.96 -
1.97 -  {
1.98 -    typedef lemon::NoCounter MyCounter;
1.99 -    MyCounter c("Main counter: ");
1.100 -    c++;
1.101 -    c++;
1.102 -    MyCounter::SubCounter d(c,"Subcounter: ");
1.103 -    d++;
1.104 -    d++;
1.105 -    MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: ");
1.106 -    e++;
1.107 -    e++;
1.108 -  }
1.109 +  std::vector<int> x(10);
1.110 +  init(x); bubbleSort(x);
1.111 +  init(x); insertionSort(x);
1.112
1.113    return 0;
1.114  }
```