alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@119: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. alpar@119: * alpar@463: * Copyright (C) 2003-2009 alpar@119: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@119: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@119: * alpar@119: * Permission to use, modify and distribute this software is granted alpar@119: * provided that this copyright notice appears in all copies. For alpar@119: * precise terms see the accompanying LICENSE file. alpar@119: * alpar@119: * This software is provided "AS IS" with no warranty of any kind, alpar@119: * express or implied, and with no claim as to its suitability for any alpar@119: * purpose. alpar@119: * alpar@119: */ alpar@119: alpar@119: #include kpeter@171: #include kpeter@605: #include kpeter@605: kpeter@605: #include "test/test_tools.h" alpar@119: kpeter@171: using namespace lemon; alpar@119: kpeter@171: template kpeter@171: void bubbleSort(std::vector& v) { kpeter@605: std::stringstream s1, s2, s3; kpeter@605: { kpeter@605: Counter op("Bubble Sort - Operations: ", s1); kpeter@605: Counter::SubCounter as(op, "Assignments: ", s2); kpeter@605: Counter::SubCounter co(op, "Comparisons: ", s3); kpeter@605: for (int i = v.size()-1; i > 0; --i) { kpeter@605: for (int j = 0; j < i; ++j) { kpeter@605: if (v[j] > v[j+1]) { kpeter@605: T tmp = v[j]; kpeter@605: v[j] = v[j+1]; kpeter@605: v[j+1] = tmp; kpeter@605: as += 3; kpeter@605: } kpeter@605: ++co; kpeter@171: } kpeter@171: } kpeter@171: } kpeter@605: check(s1.str() == "Bubble Sort - Operations: 102\n", "Wrong counter"); kpeter@605: check(s2.str() == "Assignments: 57\n", "Wrong subcounter"); kpeter@605: check(s3.str() == "Comparisons: 45\n", "Wrong subcounter"); kpeter@171: } alpar@119: kpeter@171: template kpeter@171: void insertionSort(std::vector& v) { kpeter@605: std::stringstream s1, s2, s3; kpeter@605: { kpeter@605: Counter op("Insertion Sort - Operations: ", s1); kpeter@605: Counter::SubCounter as(op, "Assignments: ", s2); kpeter@605: Counter::SubCounter co(op, "Comparisons: ", s3); kpeter@605: for (int i = 1; i < int(v.size()); ++i) { kpeter@605: T value = v[i]; kpeter@605: ++as; kpeter@605: int j = i; kpeter@605: while (j > 0 && v[j-1] > value) { kpeter@605: v[j] = v[j-1]; kpeter@605: --j; kpeter@605: ++co; ++as; kpeter@605: } kpeter@605: v[j] = value; kpeter@605: ++as; kpeter@171: } kpeter@171: } kpeter@605: check(s1.str() == "Insertion Sort - Operations: 56\n", "Wrong counter"); kpeter@605: check(s2.str() == "Assignments: 37\n", "Wrong subcounter"); kpeter@605: check(s3.str() == "Comparisons: 19\n", "Wrong subcounter"); kpeter@171: } kpeter@171: kpeter@171: template kpeter@605: void counterTest(bool output) { kpeter@605: std::stringstream s1, s2, s3; kpeter@605: { kpeter@605: MyCounter c("Main Counter: ", s1); kpeter@605: c++; kpeter@605: typename MyCounter::SubCounter d(c, "SubCounter: ", s2); kpeter@605: d++; kpeter@605: typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: ", s3); kpeter@605: e++; kpeter@605: d+=3; kpeter@605: c-=4; kpeter@605: e-=2; kpeter@605: c.reset(2); kpeter@605: c.reset(); kpeter@605: } kpeter@605: if (output) { kpeter@605: check(s1.str() == "Main Counter: 3\n", "Wrong Counter"); kpeter@605: check(s2.str() == "SubCounter: 3\n", "Wrong SubCounter"); kpeter@605: check(s3.str() == "", "Wrong NoSubCounter"); kpeter@605: } else { kpeter@605: check(s1.str() == "", "Wrong NoCounter"); kpeter@605: check(s2.str() == "", "Wrong SubCounter"); kpeter@605: check(s3.str() == "", "Wrong NoSubCounter"); kpeter@605: } kpeter@171: } kpeter@171: kpeter@171: void init(std::vector& v) { kpeter@171: v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100; alpar@209: v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70; alpar@119: } alpar@119: alpar@119: int main() alpar@119: { kpeter@605: counterTest(true); kpeter@605: counterTest(false); alpar@209: kpeter@171: std::vector x(10); kpeter@171: init(x); bubbleSort(x); kpeter@171: init(x); insertionSort(x); alpar@119: alpar@119: return 0; alpar@119: }