1.1 --- a/test/counter_test.cc Fri Nov 13 12:33:33 2009 +0100
1.2 +++ b/test/counter_test.cc Thu Dec 10 17:05:35 2009 +0100
1.3 @@ -2,7 +2,7 @@
1.4 *
1.5 * This file is a part of LEMON, a generic C++ optimization library.
1.6 *
1.7 - * Copyright (C) 2003-2008
1.8 + * Copyright (C) 2003-2009
1.9 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 *
1.12 @@ -18,59 +18,86 @@
1.13
1.14 #include <lemon/counter.h>
1.15 #include <vector>
1.16 +#include <sstream>
1.17 +
1.18 +#include "test/test_tools.h"
1.19
1.20 using namespace lemon;
1.21
1.22 template <typename T>
1.23 void bubbleSort(std::vector<T>& v) {
1.24 - Counter op("Bubble Sort - Operations: ");
1.25 - Counter::NoSubCounter as(op, "Assignments: ");
1.26 - Counter::NoSubCounter co(op, "Comparisons: ");
1.27 - for (int i = v.size()-1; i > 0; --i) {
1.28 - for (int j = 0; j < i; ++j) {
1.29 - if (v[j] > v[j+1]) {
1.30 - T tmp = v[j];
1.31 - v[j] = v[j+1];
1.32 - v[j+1] = tmp;
1.33 - as += 3;
1.34 + std::stringstream s1, s2, s3;
1.35 + {
1.36 + Counter op("Bubble Sort - Operations: ", s1);
1.37 + Counter::SubCounter as(op, "Assignments: ", s2);
1.38 + Counter::SubCounter co(op, "Comparisons: ", s3);
1.39 + for (int i = v.size()-1; i > 0; --i) {
1.40 + for (int j = 0; j < i; ++j) {
1.41 + if (v[j] > v[j+1]) {
1.42 + T tmp = v[j];
1.43 + v[j] = v[j+1];
1.44 + v[j+1] = tmp;
1.45 + as += 3;
1.46 + }
1.47 + ++co;
1.48 }
1.49 - ++co;
1.50 }
1.51 }
1.52 + check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter");
1.53 + check(s2.str() == "Assignments: 57\n", "Wrong subcounter");
1.54 + check(s3.str() == "Comparisons: 45\n", "Wrong subcounter");
1.55 }
1.56
1.57 template <typename T>
1.58 void insertionSort(std::vector<T>& v) {
1.59 - Counter op("Insertion Sort - Operations: ");
1.60 - Counter::NoSubCounter as(op, "Assignments: ");
1.61 - Counter::NoSubCounter co(op, "Comparisons: ");
1.62 - for (int i = 1; i < int(v.size()); ++i) {
1.63 - T value = v[i];
1.64 - ++as;
1.65 - int j = i;
1.66 - while (j > 0 && v[j-1] > value) {
1.67 - v[j] = v[j-1];
1.68 - --j;
1.69 - ++co; ++as;
1.70 + std::stringstream s1, s2, s3;
1.71 + {
1.72 + Counter op("Insertion Sort - Operations: ", s1);
1.73 + Counter::SubCounter as(op, "Assignments: ", s2);
1.74 + Counter::SubCounter co(op, "Comparisons: ", s3);
1.75 + for (int i = 1; i < int(v.size()); ++i) {
1.76 + T value = v[i];
1.77 + ++as;
1.78 + int j = i;
1.79 + while (j > 0 && v[j-1] > value) {
1.80 + v[j] = v[j-1];
1.81 + --j;
1.82 + ++co; ++as;
1.83 + }
1.84 + v[j] = value;
1.85 + ++as;
1.86 }
1.87 - v[j] = value;
1.88 - ++as;
1.89 }
1.90 + check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter");
1.91 + check(s2.str() == "Assignments: 37\n", "Wrong subcounter");
1.92 + check(s3.str() == "Comparisons: 19\n", "Wrong subcounter");
1.93 }
1.94
1.95 template <typename MyCounter>
1.96 -void counterTest() {
1.97 - MyCounter c("Main Counter: ");
1.98 - c++;
1.99 - typename MyCounter::SubCounter d(c, "SubCounter: ");
1.100 - d++;
1.101 - typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ");
1.102 - e++;
1.103 - d+=3;
1.104 - c-=4;
1.105 - e-=2;
1.106 - c.reset(2);
1.107 - c.reset();
1.108 +void counterTest(bool output) {
1.109 + std::stringstream s1, s2, s3;
1.110 + {
1.111 + MyCounter c("Main Counter: ", s1);
1.112 + c++;
1.113 + typename MyCounter::SubCounter d(c, "SubCounter: ", s2);
1.114 + d++;
1.115 + typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3);
1.116 + e++;
1.117 + d+=3;
1.118 + c-=4;
1.119 + e-=2;
1.120 + c.reset(2);
1.121 + c.reset();
1.122 + }
1.123 + if (output) {
1.124 + check(s1.str() == "Main Counter: 3\n", "Wrong Counter");
1.125 + check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter");
1.126 + check(s3.str() == "", "Wrong NoSubCounter");
1.127 + } else {
1.128 + check(s1.str() == "", "Wrong NoCounter");
1.129 + check(s2.str() == "", "Wrong SubCounter");
1.130 + check(s3.str() == "", "Wrong NoSubCounter");
1.131 + }
1.132 }
1.133
1.134 void init(std::vector<int>& v) {
1.135 @@ -80,8 +107,8 @@
1.136
1.137 int main()
1.138 {
1.139 - counterTest<Counter>();
1.140 - counterTest<NoCounter>();
1.141 + counterTest<Counter>(true);
1.142 + counterTest<NoCounter>(false);
1.143
1.144 std::vector<int> x(10);
1.145 init(x); bubbleSort(x);