alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@119:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@119:  *
alpar@440:  * Copyright (C) 2003-2009
alpar@119:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@119:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@119:  *
alpar@119:  * Permission to use, modify and distribute this software is granted
alpar@119:  * provided that this copyright notice appears in all copies. For
alpar@119:  * precise terms see the accompanying LICENSE file.
alpar@119:  *
alpar@119:  * This software is provided "AS IS" with no warranty of any kind,
alpar@119:  * express or implied, and with no claim as to its suitability for any
alpar@119:  * purpose.
alpar@119:  *
alpar@119:  */
alpar@119: 
alpar@119: #include <lemon/counter.h>
kpeter@171: #include <vector>
kpeter@558: #include <sstream>
kpeter@558: 
kpeter@558: #include "test/test_tools.h"
alpar@119: 
kpeter@171: using namespace lemon;
alpar@119: 
kpeter@171: template <typename T>
kpeter@171: void bubbleSort(std::vector<T>& v) {
kpeter@558:   std::stringstream s1, s2, s3;
kpeter@558:   {
kpeter@558:     Counter op("Bubble Sort - Operations: ", s1);
kpeter@558:     Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@558:     Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@558:     for (int i = v.size()-1; i > 0; --i) {
kpeter@558:       for (int j = 0; j < i; ++j) {
kpeter@558:         if (v[j] > v[j+1]) {
kpeter@558:           T tmp = v[j];
kpeter@558:           v[j] = v[j+1];
kpeter@558:           v[j+1] = tmp;
kpeter@558:           as += 3;
kpeter@558:         }
kpeter@558:         ++co;
kpeter@171:       }
kpeter@171:     }
kpeter@171:   }
kpeter@558:   check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
kpeter@558:   check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
kpeter@558:   check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
kpeter@171: }
alpar@119: 
kpeter@171: template <typename T>
kpeter@171: void insertionSort(std::vector<T>& v) {
kpeter@558:   std::stringstream s1, s2, s3;
kpeter@558:   {
kpeter@558:     Counter op("Insertion Sort - Operations: ", s1);
kpeter@558:     Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@558:     Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@558:     for (int i = 1; i < int(v.size()); ++i) {
kpeter@558:       T value = v[i];
kpeter@558:       ++as;
kpeter@558:       int j = i;
kpeter@558:       while (j > 0 && v[j-1] > value) {
kpeter@558:         v[j] = v[j-1];
kpeter@558:         --j;
kpeter@558:         ++co; ++as;
kpeter@558:       }
kpeter@558:       v[j] = value;
kpeter@558:       ++as;
kpeter@171:     }
kpeter@171:   }
kpeter@558:   check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
kpeter@558:   check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
kpeter@558:   check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
kpeter@171: }
kpeter@171: 
kpeter@171: template <typename MyCounter>
kpeter@558: void counterTest(bool output) {
kpeter@558:   std::stringstream s1, s2, s3;
kpeter@558:   {
kpeter@558:     MyCounter c("Main Counter: ", s1);
kpeter@558:     c++;
kpeter@558:     typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
kpeter@558:     d++;
kpeter@558:     typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
kpeter@558:     e++;
kpeter@558:     d+=3;
kpeter@558:     c-=4;
kpeter@558:     e-=2;
kpeter@558:     c.reset(2);
kpeter@558:     c.reset();
kpeter@558:   }
kpeter@558:   if (output) {
kpeter@558:     check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
kpeter@558:     check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
kpeter@558:     check(s3.str() == "", "Wrong NoSubCounter");
kpeter@558:   } else {
kpeter@558:     check(s1.str() == "", "Wrong NoCounter");
kpeter@558:     check(s2.str() == "", "Wrong SubCounter");
kpeter@558:     check(s3.str() == "", "Wrong NoSubCounter");
kpeter@558:   }
kpeter@171: }
kpeter@171: 
kpeter@171: void init(std::vector<int>& v) {
kpeter@171:   v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100;
alpar@209:   v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70;
alpar@119: }
alpar@119: 
alpar@119: int main()
alpar@119: {
kpeter@558:   counterTest<Counter>(true);
kpeter@558:   counterTest<NoCounter>(false);
alpar@209: 
kpeter@171:   std::vector<int> x(10);
kpeter@171:   init(x); bubbleSort(x);
kpeter@171:   init(x); insertionSort(x);
alpar@119: 
alpar@119:   return 0;
alpar@119: }