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