test/counter_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 20 Aug 2009 22:41:40 +0200
branch1.1
changeset 688 841e1fd5e2b3
parent 440 88ed40ad0d4f
permissions -rw-r--r--
Merge bugfix #302
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@119
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@119
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@119
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@119
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@119
     8
 *
alpar@119
     9
 * Permission to use, modify and distribute this software is granted
alpar@119
    10
 * provided that this copyright notice appears in all copies. For
alpar@119
    11
 * precise terms see the accompanying LICENSE file.
alpar@119
    12
 *
alpar@119
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@119
    14
 * express or implied, and with no claim as to its suitability for any
alpar@119
    15
 * purpose.
alpar@119
    16
 *
alpar@119
    17
 */
alpar@119
    18
alpar@119
    19
#include <lemon/counter.h>
kpeter@171
    20
#include <vector>
kpeter@549
    21
#include <sstream>
kpeter@549
    22
kpeter@549
    23
#include "test/test_tools.h"
alpar@119
    24
kpeter@171
    25
using namespace lemon;
alpar@119
    26
kpeter@171
    27
template <typename T>
kpeter@171
    28
void bubbleSort(std::vector<T>& v) {
kpeter@549
    29
  std::stringstream s1, s2, s3;
kpeter@549
    30
  {
kpeter@549
    31
    Counter op("Bubble Sort - Operations: ", s1);
kpeter@549
    32
    Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@549
    33
    Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@549
    34
    for (int i = v.size()-1; i > 0; --i) {
kpeter@549
    35
      for (int j = 0; j < i; ++j) {
kpeter@549
    36
        if (v[j] > v[j+1]) {
kpeter@549
    37
          T tmp = v[j];
kpeter@549
    38
          v[j] = v[j+1];
kpeter@549
    39
          v[j+1] = tmp;
kpeter@549
    40
          as += 3;
kpeter@549
    41
        }
kpeter@549
    42
        ++co;
kpeter@171
    43
      }
kpeter@171
    44
    }
kpeter@171
    45
  }
kpeter@549
    46
  check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
kpeter@549
    47
  check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
kpeter@549
    48
  check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
kpeter@171
    49
}
alpar@119
    50
kpeter@171
    51
template <typename T>
kpeter@171
    52
void insertionSort(std::vector<T>& v) {
kpeter@549
    53
  std::stringstream s1, s2, s3;
kpeter@549
    54
  {
kpeter@549
    55
    Counter op("Insertion Sort - Operations: ", s1);
kpeter@549
    56
    Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@549
    57
    Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@549
    58
    for (int i = 1; i < int(v.size()); ++i) {
kpeter@549
    59
      T value = v[i];
kpeter@549
    60
      ++as;
kpeter@549
    61
      int j = i;
kpeter@549
    62
      while (j > 0 && v[j-1] > value) {
kpeter@549
    63
        v[j] = v[j-1];
kpeter@549
    64
        --j;
kpeter@549
    65
        ++co; ++as;
kpeter@549
    66
      }
kpeter@549
    67
      v[j] = value;
kpeter@549
    68
      ++as;
kpeter@171
    69
    }
kpeter@171
    70
  }
kpeter@549
    71
  check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
kpeter@549
    72
  check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
kpeter@549
    73
  check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
kpeter@171
    74
}
kpeter@171
    75
kpeter@171
    76
template <typename MyCounter>
kpeter@549
    77
void counterTest(bool output) {
kpeter@549
    78
  std::stringstream s1, s2, s3;
kpeter@549
    79
  {
kpeter@549
    80
    MyCounter c("Main Counter: ", s1);
kpeter@549
    81
    c++;
kpeter@549
    82
    typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
kpeter@549
    83
    d++;
kpeter@549
    84
    typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
kpeter@549
    85
    e++;
kpeter@549
    86
    d+=3;
kpeter@549
    87
    c-=4;
kpeter@549
    88
    e-=2;
kpeter@549
    89
    c.reset(2);
kpeter@549
    90
    c.reset();
kpeter@549
    91
  }
kpeter@549
    92
  if (output) {
kpeter@549
    93
    check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
kpeter@549
    94
    check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
kpeter@549
    95
    check(s3.str() == "", "Wrong NoSubCounter");
kpeter@549
    96
  } else {
kpeter@549
    97
    check(s1.str() == "", "Wrong NoCounter");
kpeter@549
    98
    check(s2.str() == "", "Wrong SubCounter");
kpeter@549
    99
    check(s3.str() == "", "Wrong NoSubCounter");
kpeter@549
   100
  }
kpeter@171
   101
}
kpeter@171
   102
kpeter@171
   103
void init(std::vector<int>& v) {
kpeter@171
   104
  v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100;
alpar@209
   105
  v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70;
alpar@119
   106
}
alpar@119
   107
alpar@119
   108
int main()
alpar@119
   109
{
kpeter@549
   110
  counterTest<Counter>(true);
kpeter@549
   111
  counterTest<NoCounter>(false);
alpar@209
   112
kpeter@171
   113
  std::vector<int> x(10);
kpeter@171
   114
  init(x); bubbleSort(x);
kpeter@171
   115
  init(x); insertionSort(x);
alpar@119
   116
alpar@119
   117
  return 0;
alpar@119
   118
}