alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@25: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. alpar@25: * alpar@440: * Copyright (C) 2003-2009 alpar@25: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@25: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@25: * alpar@25: * Permission to use, modify and distribute this software is granted alpar@25: * provided that this copyright notice appears in all copies. For alpar@25: * precise terms see the accompanying LICENSE file. alpar@25: * alpar@25: * This software is provided "AS IS" with no warranty of any kind, alpar@25: * express or implied, and with no claim as to its suitability for any alpar@25: * purpose. alpar@25: * alpar@25: */ alpar@25: alpar@25: #include alpar@25: #include alpar@25: alpar@25: #include alpar@25: #include alpar@25: #include kpeter@684: #include alpar@25: alpar@25: #include "test_tools.h" alpar@25: alpar@25: using namespace lemon; alpar@25: using namespace lemon::concepts; alpar@25: alpar@25: struct A {}; alpar@25: inline bool operator<(A, A) { return true; } alpar@25: struct B {}; alpar@25: kpeter@94: class C { kpeter@94: int x; kpeter@94: public: kpeter@94: C(int _x) : x(_x) {} kpeter@94: }; kpeter@94: alpar@25: class F { alpar@25: public: alpar@25: typedef A argument_type; alpar@25: typedef B result_type; alpar@25: kpeter@80: B operator()(const A&) const { return B(); } kpeter@80: private: kpeter@80: F& operator=(const F&); alpar@25: }; alpar@25: kpeter@80: int func(A) { return 3; } alpar@25: kpeter@80: int binc(int a, B) { return a+1; } alpar@25: kpeter@80: typedef ReadMap DoubleMap; kpeter@80: typedef ReadWriteMap DoubleWriteMap; kpeter@80: typedef ReferenceMap DoubleRefMap; alpar@25: kpeter@80: typedef ReadMap BoolMap; alpar@25: typedef ReadWriteMap BoolWriteMap; kpeter@80: typedef ReferenceMap BoolRefMap; alpar@25: alpar@25: int main() kpeter@80: { kpeter@80: // Map concepts alpar@25: checkConcept, ReadMap >(); kpeter@94: checkConcept, ReadMap >(); alpar@25: checkConcept, WriteMap >(); kpeter@94: checkConcept, WriteMap >(); alpar@25: checkConcept, ReadWriteMap >(); kpeter@94: checkConcept, ReadWriteMap >(); alpar@25: checkConcept, ReferenceMap >(); kpeter@94: checkConcept, ReferenceMap >(); alpar@25: kpeter@80: // NullMap kpeter@80: { kpeter@80: checkConcept, NullMap >(); kpeter@80: NullMap map1; kpeter@80: NullMap map2 = map1; alpar@792: ignore_unused_variable_warning(map2); kpeter@80: map1 = nullMap(); kpeter@80: } kpeter@80: kpeter@80: // ConstMap kpeter@80: { kpeter@80: checkConcept, ConstMap >(); kpeter@123: checkConcept, ConstMap >(); kpeter@80: ConstMap map1; deba@136: ConstMap map2 = B(); kpeter@80: ConstMap map3 = map1; alpar@792: ignore_unused_variable_warning(map2,map3); alpar@792: kpeter@80: map1 = constMap(B()); kpeter@123: map1 = constMap(); kpeter@80: map1.setAll(B()); kpeter@123: ConstMap map4(C(1)); kpeter@123: ConstMap map5 = map4; alpar@792: ignore_unused_variable_warning(map5); alpar@792: kpeter@123: map4 = constMap(C(2)); kpeter@123: map4.setAll(C(3)); kpeter@82: kpeter@80: checkConcept, ConstMap >(); kpeter@80: check(constMap(10)[A()] == 10, "Something is wrong with ConstMap"); kpeter@80: kpeter@80: checkConcept, ConstMap > >(); kpeter@123: ConstMap > map6; kpeter@123: ConstMap > map7 = map6; kpeter@123: map6 = constMap(); kpeter@123: map7 = constMap >(); alpar@210: check(map6[A()] == 10 && map7[A()] == 10, alpar@210: "Something is wrong with ConstMap"); kpeter@80: } kpeter@80: kpeter@80: // IdentityMap kpeter@80: { kpeter@80: checkConcept, IdentityMap >(); kpeter@80: IdentityMap map1; kpeter@80: IdentityMap map2 = map1; alpar@792: ignore_unused_variable_warning(map2); alpar@792: kpeter@80: map1 = identityMap(); kpeter@82: kpeter@80: checkConcept, IdentityMap >(); alpar@210: check(identityMap()[1.0] == 1.0 && alpar@210: identityMap()[3.14] == 3.14, kpeter@80: "Something is wrong with IdentityMap"); kpeter@80: } kpeter@80: kpeter@80: // RangeMap kpeter@80: { kpeter@80: checkConcept, RangeMap >(); kpeter@80: RangeMap map1; kpeter@80: RangeMap map2(10); kpeter@80: RangeMap map3(10,B()); kpeter@80: RangeMap map4 = map1; kpeter@80: RangeMap map5 = rangeMap(); kpeter@80: RangeMap map6 = rangeMap(10); kpeter@80: RangeMap map7 = rangeMap(10,B()); kpeter@80: kpeter@80: checkConcept< ReferenceMap, kpeter@80: RangeMap >(); kpeter@80: std::vector v(10, 0); kpeter@80: v[5] = 100; kpeter@80: RangeMap map8(v); kpeter@80: RangeMap map9 = rangeMap(v); kpeter@80: check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, kpeter@80: "Something is wrong with RangeMap"); kpeter@80: } kpeter@80: kpeter@80: // SparseMap kpeter@80: { kpeter@80: checkConcept, SparseMap >(); kpeter@80: SparseMap map1; deba@136: SparseMap map2 = B(); kpeter@80: SparseMap map3 = sparseMap(); kpeter@80: SparseMap map4 = sparseMap(B()); kpeter@80: kpeter@80: checkConcept< ReferenceMap, kpeter@80: SparseMap >(); kpeter@80: std::map m; kpeter@80: SparseMap map5(m); kpeter@80: SparseMap map6(m,10); kpeter@80: SparseMap map7 = sparseMap(m); kpeter@80: SparseMap map8 = sparseMap(m,10); kpeter@80: alpar@210: check(map5[1.0] == 0 && map5[3.14] == 0 && alpar@210: map6[1.0] == 10 && map6[3.14] == 10, kpeter@80: "Something is wrong with SparseMap"); kpeter@80: map5[1.0] = map6[3.14] = 100; alpar@210: check(map5[1.0] == 100 && map5[3.14] == 0 && alpar@210: map6[1.0] == 10 && map6[3.14] == 100, kpeter@80: "Something is wrong with SparseMap"); kpeter@80: } kpeter@80: kpeter@80: // ComposeMap kpeter@80: { kpeter@80: typedef ComposeMap > CompMap; kpeter@80: checkConcept, CompMap>(); alpar@477: CompMap map1 = CompMap(DoubleMap(),ReadMap()); alpar@792: ignore_unused_variable_warning(map1); kpeter@80: CompMap map2 = composeMap(DoubleMap(), ReadMap()); alpar@792: ignore_unused_variable_warning(map2); kpeter@82: kpeter@80: SparseMap m1(false); m1[3.14] = true; kpeter@80: RangeMap m2(2); m2[0] = 3.0; m2[1] = 3.14; alpar@210: check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], alpar@210: "Something is wrong with ComposeMap") kpeter@80: } kpeter@80: kpeter@80: // CombineMap kpeter@80: { kpeter@80: typedef CombineMap > CombMap; kpeter@80: checkConcept, CombMap>(); alpar@477: CombMap map1 = CombMap(DoubleMap(), DoubleMap()); alpar@792: ignore_unused_variable_warning(map1); kpeter@80: CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus()); alpar@792: ignore_unused_variable_warning(map2); kpeter@80: kpeter@80: check(combineMap(constMap(), identityMap(), &binc)[B()] == 3, kpeter@80: "Something is wrong with CombineMap"); kpeter@80: } kpeter@80: kpeter@80: // FunctorToMap, MapToFunctor kpeter@80: { kpeter@80: checkConcept, FunctorToMap >(); kpeter@80: checkConcept, FunctorToMap >(); kpeter@80: FunctorToMap map1; alpar@477: FunctorToMap map2 = FunctorToMap(F()); alpar@792: ignore_unused_variable_warning(map2); alpar@792: kpeter@80: B b = functorToMap(F())[A()]; alpar@792: ignore_unused_variable_warning(b); kpeter@80: kpeter@80: checkConcept, MapToFunctor > >(); alpar@792: MapToFunctor > map = alpar@792: MapToFunctor >(ReadMap()); alpar@792: ignore_unused_variable_warning(map); kpeter@80: alpar@210: check(functorToMap(&func)[A()] == 3, alpar@210: "Something is wrong with FunctorToMap"); alpar@210: check(mapToFunctor(constMap(2))(A()) == 2, alpar@210: "Something is wrong with MapToFunctor"); alpar@210: check(mapToFunctor(functorToMap(&func))(A()) == 3 && alpar@210: mapToFunctor(functorToMap(&func))[A()] == 3, kpeter@80: "Something is wrong with FunctorToMap or MapToFunctor"); kpeter@80: check(functorToMap(mapToFunctor(constMap(2)))[A()] == 2, kpeter@80: "Something is wrong with FunctorToMap or MapToFunctor"); kpeter@80: } kpeter@80: kpeter@80: // ConvertMap kpeter@80: { alpar@210: checkConcept, alpar@210: ConvertMap, double> >(); kpeter@80: ConvertMap, int> map1(rangeMap(1, true)); alpar@792: ignore_unused_variable_warning(map1); kpeter@80: ConvertMap, int> map2 = convertMap(rangeMap(2, false)); alpar@792: ignore_unused_variable_warning(map2); alpar@792: kpeter@80: } kpeter@80: kpeter@80: // ForkMap kpeter@80: { kpeter@80: checkConcept >(); kpeter@82: kpeter@80: typedef RangeMap RM; kpeter@80: typedef SparseMap SM; kpeter@80: RM m1(10, -1); kpeter@80: SM m2(-1); kpeter@80: checkConcept, ForkMap >(); kpeter@80: checkConcept, ForkMap >(); kpeter@80: ForkMap map1(m1,m2); kpeter@80: ForkMap map2 = forkMap(m2,m1); kpeter@80: map2.set(5, 10); alpar@210: check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && alpar@210: m2[5] == 10 && map2[1] == -1 && map2[5] == 10, kpeter@80: "Something is wrong with ForkMap"); kpeter@80: } kpeter@82: kpeter@80: // Arithmetic maps: kpeter@80: // - AddMap, SubMap, MulMap, DivMap kpeter@80: // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap kpeter@80: // - NegMap, NegWriteMap, AbsMap kpeter@80: { kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@82: kpeter@80: ConstMap c1(1.0), c2(3.14); kpeter@80: IdentityMap im; kpeter@80: ConvertMap, double> id(im); alpar@210: check(addMap(c1,id)[0] == 1.0 && addMap(c1,id)[10] == 11.0, alpar@210: "Something is wrong with AddMap"); alpar@210: check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0, alpar@210: "Something is wrong with SubMap"); alpar@210: check(mulMap(id,c2)[0] == 0 && mulMap(id,c2)[2] == 6.28, alpar@210: "Something is wrong with MulMap"); alpar@210: check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2] == 1.57, alpar@210: "Something is wrong with DivMap"); kpeter@82: kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); alpar@25: kpeter@80: check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0, kpeter@80: "Something is wrong with ShiftMap"); alpar@210: check(shiftWriteMap(id, 2.0)[1] == 3.0 && alpar@210: shiftWriteMap(id, 2.0)[10] == 12.0, kpeter@80: "Something is wrong with ShiftWriteMap"); kpeter@80: check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0, kpeter@80: "Something is wrong with ScaleMap"); alpar@210: check(scaleWriteMap(id, 2.0)[1] == 2.0 && alpar@210: scaleWriteMap(id, 2.0)[10] == 20.0, kpeter@80: "Something is wrong with ScaleWriteMap"); kpeter@80: check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0, kpeter@80: "Something is wrong with NegMap"); kpeter@80: check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0, kpeter@80: "Something is wrong with NegWriteMap"); kpeter@80: check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0, kpeter@80: "Something is wrong with AbsMap"); kpeter@80: } kpeter@82: kpeter@82: // Logical maps: kpeter@82: // - TrueMap, FalseMap kpeter@82: // - AndMap, OrMap kpeter@82: // - NotMap, NotWriteMap kpeter@82: // - EqualMap, LessMap kpeter@80: { kpeter@82: checkConcept >(); kpeter@82: checkConcept >(); kpeter@82: checkConcept >(); kpeter@82: checkConcept >(); kpeter@80: checkConcept >(); kpeter@80: checkConcept >(); kpeter@82: checkConcept >(); kpeter@82: checkConcept >(); kpeter@82: kpeter@82: TrueMap tm; kpeter@82: FalseMap fm; kpeter@80: RangeMap rm(2); kpeter@80: rm[0] = true; rm[1] = false; alpar@210: check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && alpar@210: !andMap(fm,rm)[0] && !andMap(fm,rm)[1], kpeter@82: "Something is wrong with AndMap"); alpar@210: check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && alpar@210: orMap(fm,rm)[0] && !orMap(fm,rm)[1], kpeter@82: "Something is wrong with OrMap"); alpar@210: check(!notMap(rm)[0] && notMap(rm)[1], alpar@210: "Something is wrong with NotMap"); alpar@210: check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], alpar@210: "Something is wrong with NotWriteMap"); kpeter@82: kpeter@82: ConstMap cm(2.0); kpeter@82: IdentityMap im; kpeter@82: ConvertMap, double> id(im); kpeter@82: check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3], kpeter@82: "Something is wrong with LessMap"); kpeter@82: check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3], kpeter@82: "Something is wrong with EqualMap"); kpeter@80: } alpar@209: kpeter@167: // LoggerBoolMap kpeter@159: { kpeter@159: typedef std::vector vec; kpeter@159: vec v1; kpeter@159: vec v2(10); alpar@210: LoggerBoolMap > alpar@210: map1(std::back_inserter(v1)); kpeter@167: LoggerBoolMap map2(v2.begin()); kpeter@159: map1.set(10, false); kpeter@159: map1.set(20, true); map2.set(20, true); kpeter@159: map1.set(30, false); map2.set(40, false); kpeter@159: map1.set(50, true); map2.set(50, true); kpeter@159: map1.set(60, true); map2.set(60, true); kpeter@159: check(v1.size() == 3 && v2.size() == 10 && alpar@210: v1[0]==20 && v1[1]==50 && v1[2]==60 && alpar@210: v2[0]==20 && v2[1]==50 && v2[2]==60, kpeter@167: "Something is wrong with LoggerBoolMap"); alpar@209: kpeter@159: int i = 0; kpeter@167: for ( LoggerBoolMap::Iterator it = map2.begin(); kpeter@159: it != map2.end(); ++it ) kpeter@167: check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); kpeter@159: } kpeter@684: kpeter@684: // CrossRefMap kpeter@684: { kpeter@684: typedef ListDigraph Graph; kpeter@684: DIGRAPH_TYPEDEFS(Graph); kpeter@684: kpeter@684: checkConcept, kpeter@684: CrossRefMap >(); kpeter@684: kpeter@684: Graph gr; kpeter@684: typedef CrossRefMap CRMap; kpeter@684: typedef CRMap::ValueIterator ValueIt; kpeter@684: CRMap map(gr); kpeter@684: kpeter@684: Node n0 = gr.addNode(); kpeter@684: Node n1 = gr.addNode(); kpeter@684: Node n2 = gr.addNode(); kpeter@684: kpeter@684: map.set(n0, 'A'); kpeter@684: map.set(n1, 'B'); kpeter@684: map.set(n2, 'C'); kpeter@684: map.set(n2, 'A'); kpeter@684: map.set(n0, 'C'); kpeter@684: kpeter@684: check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', kpeter@684: "Wrong CrossRefMap"); kpeter@684: check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap"); kpeter@684: check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap"); kpeter@684: check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap"); kpeter@684: kpeter@684: ValueIt it = map.beginValue(); kpeter@684: check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && kpeter@684: it == map.endValue(), "Wrong value iterator"); kpeter@684: } alpar@25: alpar@25: return 0; alpar@25: }