COIN-OR::LEMON - Graph Library

Changeset 171:02f4d5d9bfd7 in lemon for test/counter_test.cc


Ignore:
Timestamp:
06/15/08 22:05:23 (16 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Children:
172:c94a80f38d7f, 173:b026e9779b28, 175:4eb8900a865c
Phase:
public
Message:

Improve and redesign test programs + unify their output (ticket #25)

  • Move graph related utilities form test_tools.h to graph_test.h.
  • Move the contents of graph_utils_test.h to graph_utils_test.cc.
  • Rename map_test.h -> graph_maps_test.h.
  • Rename digraph_test.h -> graph_test.h.
  • Many improvements in the following files:
    • digraph_test.cc
    • graph_test.cc
    • graph_test.h
    • graph_maps_test.h
    • graph_utils_test.cc
    • bfs_test.cc
    • dfs_test.cc
    • counter_test.cc
  • Test programs print messages only if it really seems necessary.
  • Remove \file commands form .cc test files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/counter_test.cc

    r119 r171  
    1818
    1919#include <lemon/counter.h>
     20#include <vector>
    2021
    21 ///\file \brief Test cases for time_measure.h
    22 ///
    23 ///\todo To be extended
     22using namespace lemon;
    2423
     24template <typename T>
     25void bubbleSort(std::vector<T>& v) {
     26  Counter op("Bubble Sort - Operations: ");
     27  Counter::NoSubCounter as(op, "Assignments: ");
     28  Counter::NoSubCounter co(op, "Comparisons: ");
     29  for (int i = v.size()-1; i > 0; --i) {
     30    for (int j = 0; j < i; ++j) {
     31      if (v[j] > v[j+1]) {
     32        T tmp = v[j];
     33        v[j] = v[j+1];
     34        v[j+1] = tmp;
     35        as += 3;
     36      }
     37      ++co;
     38    }
     39  }
     40}
    2541
    26 int fibonacci(int f)
    27 {
    28   static lemon::Counter count("Fibonacci steps: ");
    29   count++;
    30   if(f<1) return 0;
    31   else if(f==1) return 1;
    32   else return fibonacci(f-1)+fibonacci(f-2);
     42template <typename T>
     43void insertionSort(std::vector<T>& v) {
     44  Counter op("Insertion Sort - Operations: ");
     45  Counter::NoSubCounter as(op, "Assignments: ");
     46  Counter::NoSubCounter co(op, "Comparisons: ");
     47  for (int i = 1; i < int(v.size()); ++i) {
     48    T value = v[i];
     49    ++as;
     50    int j = i;
     51    while (j > 0 && v[j-1] > value) {
     52      v[j] = v[j-1];
     53      --j;
     54      ++co; ++as;
     55    }
     56    v[j] = value;
     57    ++as;
     58  }
     59}
     60
     61template <typename MyCounter>
     62void counterTest() {
     63  MyCounter c("Main Counter: ");
     64  c++;
     65  typename MyCounter::SubCounter d(c, "SubCounter: ");
     66  d++;
     67  typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
     68  e++;
     69  d+=3;
     70  c-=4;
     71  e-=2;
     72  c.reset(2);
     73  c.reset();
     74}
     75
     76void init(std::vector<int>& v) {
     77  v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100;
     78  v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70;
    3379}
    3480
    3581int main()
    3682{
    37   fibonacci(10);
     83  counterTest<Counter>();
     84  counterTest<NoCounter>();
    3885 
    39   { 
    40     typedef lemon::Counter MyCounter;
    41     MyCounter c("Main counter: ");
    42     c++;
    43     c++;
    44     MyCounter::SubCounter d(c,"Subcounter: ");
    45     d++;
    46     d++;
    47     MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: ");
    48     e++;
    49     e++;
    50   }
    51  
    52   {
    53     typedef lemon::NoCounter MyCounter;
    54     MyCounter c("Main counter: ");
    55     c++;
    56     c++;
    57     MyCounter::SubCounter d(c,"Subcounter: ");
    58     d++;
    59     d++;
    60     MyCounter::SubCounter::SubCounter e(d,"SubSubCounter: ");
    61     e++;
    62     e++;
    63   }
     86  std::vector<int> x(10);
     87  init(x); bubbleSort(x);
     88  init(x); insertionSort(x);
    6489
    6590  return 0;
Note: See TracChangeset for help on using the changeset viewer.