aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/storageutil/functortest.cpp
blob: 0fa1eeaaa8a1363163d9827b65b34758c4b4de98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <cppunit/extensions/HelperMacros.h>
#include <list>
#include <string>
#include <algorithm>
#include <vespa/storage/storageutil/functor.h>

class Functor_Test : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(Functor_Test);
  CPPUNIT_TEST(testReplace);
  CPPUNIT_TEST(testDeletePointer);
  CPPUNIT_TEST_SUITE_END();

public:

protected:
  void testReplace();
  void testDeletePointer();
};

using namespace storage;
using namespace std;

CPPUNIT_TEST_SUITE_REGISTRATION(Functor_Test);

void Functor_Test::testReplace()
{
    string source("this.is.a.string.with.many.dots.");
    for_each(source.begin(), source.end(), Functor::Replace<char>('.', '_'));
    CPPUNIT_ASSERT_EQUAL(string("this_is_a_string_with_many_dots_"), source);
}

namespace {

    static int instanceCounter = 0;

    class TestClass {
    public:
        TestClass() { instanceCounter++; }
        ~TestClass() { instanceCounter--; }
    };
}

void Functor_Test::testDeletePointer()
{
    list<TestClass*> mylist;
    mylist.push_back(new TestClass());
    mylist.push_back(new TestClass());
    mylist.push_back(new TestClass());
    CPPUNIT_ASSERT_EQUAL(3, instanceCounter);
    for_each(mylist.begin(), mylist.end(), Functor::DeletePointer());
    CPPUNIT_ASSERT_EQUAL(0, instanceCounter);
}