diff --git a/test/counter_test.cc b/test/counter_test.cc --- a/test/counter_test.cc +++ b/test/counter_test.cc @@ -17,50 +17,75 @@ */ #include +#include -///\file \brief Test cases for time_measure.h -/// -///\todo To be extended +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; + } + ++co; + } + } +} -int fibonacci(int f) -{ - static lemon::Counter count("Fibonacci steps: "); - count++; - if(f<1) return 0; - else if(f==1) return 1; - else return fibonacci(f-1)+fibonacci(f-2); +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; + } + v[j] = value; + ++as; + } +} + +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 init(std::vector& v) { + v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100; + v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70; } int main() { - fibonacci(10); + counterTest(); + counterTest(); - { - typedef lemon::Counter MyCounter; - MyCounter c("Main counter: "); - c++; - c++; - MyCounter::SubCounter d(c,"Subcounter: "); - d++; - d++; - MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: "); - e++; - e++; - } - - { - typedef lemon::NoCounter MyCounter; - MyCounter c("Main counter: "); - c++; - c++; - MyCounter::SubCounter d(c,"Subcounter: "); - d++; - d++; - MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: "); - e++; - e++; - } + std::vector x(10); + init(x); bubbleSort(x); + init(x); insertionSort(x); return 0; }