test/counter_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 07 Apr 2009 14:50:20 +0100
changeset 620 37216ca5b9c6
parent 463 88ed40ad0d4f
permissions -rw-r--r--
Merge
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@463
     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@605
    21
#include <sstream>
kpeter@605
    22
kpeter@605
    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@605
    29
  std::stringstream s1, s2, s3;
kpeter@605
    30
  {
kpeter@605
    31
    Counter op("Bubble Sort - Operations: ", s1);
kpeter@605
    32
    Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@605
    33
    Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@605
    34
    for (int i = v.size()-1; i > 0; --i) {
kpeter@605
    35
      for (int j = 0; j < i; ++j) {
kpeter@605
    36
        if (v[j] > v[j+1]) {
kpeter@605
    37
          T tmp = v[j];
kpeter@605
    38
          v[j] = v[j+1];
kpeter@605
    39
          v[j+1] = tmp;
kpeter@605
    40
          as += 3;
kpeter@605
    41
        }
kpeter@605
    42
        ++co;
kpeter@171
    43
      }
kpeter@171
    44
    }
kpeter@171
    45
  }
kpeter@605
    46
  check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
kpeter@605
    47
  check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
kpeter@605
    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@605
    53
  std::stringstream s1, s2, s3;
kpeter@605
    54
  {
kpeter@605
    55
    Counter op("Insertion Sort - Operations: ", s1);
kpeter@605
    56
    Counter::SubCounter as(op, "Assignments: ", s2);
kpeter@605
    57
    Counter::SubCounter co(op, "Comparisons: ", s3);
kpeter@605
    58
    for (int i = 1; i < int(v.size()); ++i) {
kpeter@605
    59
      T value = v[i];
kpeter@605
    60
      ++as;
kpeter@605
    61
      int j = i;
kpeter@605
    62
      while (j > 0 && v[j-1] > value) {
kpeter@605
    63
        v[j] = v[j-1];
kpeter@605
    64
        --j;
kpeter@605
    65
        ++co; ++as;
kpeter@605
    66
      }
kpeter@605
    67
      v[j] = value;
kpeter@605
    68
      ++as;
kpeter@171
    69
    }
kpeter@171
    70
  }
kpeter@605
    71
  check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
kpeter@605
    72
  check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
kpeter@605
    73
  check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
kpeter@171
    74
}
kpeter@171
    75
kpeter@171
    76
template <typename MyCounter>
kpeter@605
    77
void counterTest(bool output) {
kpeter@605
    78
  std::stringstream s1, s2, s3;
kpeter@605
    79
  {
kpeter@605
    80
    MyCounter c("Main Counter: ", s1);
kpeter@605
    81
    c++;
kpeter@605
    82
    typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
kpeter@605
    83
    d++;
kpeter@605
    84
    typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
kpeter@605
    85
    e++;
kpeter@605
    86
    d+=3;
kpeter@605
    87
    c-=4;
kpeter@605
    88
    e-=2;
kpeter@605
    89
    c.reset(2);
kpeter@605
    90
    c.reset();
kpeter@605
    91
  }
kpeter@605
    92
  if (output) {
kpeter@605
    93
    check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
kpeter@605
    94
    check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
kpeter@605
    95
    check(s3.str() == "", "Wrong NoSubCounter");
kpeter@605
    96
  } else {
kpeter@605
    97
    check(s1.str() == "", "Wrong NoCounter");
kpeter@605
    98
    check(s2.str() == "", "Wrong SubCounter");
kpeter@605
    99
    check(s3.str() == "", "Wrong NoSubCounter");
kpeter@605
   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@605
   110
  counterTest<Counter>(true);
kpeter@605
   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
}