~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/MemoryPoolTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MemoryPoolTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/MemoryPoolTest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "MemoryPoolTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/MemoryPool.h"
 
37
#include <vector>
 
38
 
 
39
 
 
40
using Poco::MemoryPool;
 
41
 
 
42
 
 
43
MemoryPoolTest::MemoryPoolTest(const std::string& name): CppUnit::TestCase(name)
 
44
{
 
45
}
 
46
 
 
47
 
 
48
MemoryPoolTest::~MemoryPoolTest()
 
49
{
 
50
}
 
51
 
 
52
 
 
53
void MemoryPoolTest::testMemoryPool()
 
54
{
 
55
        MemoryPool pool1(100, 0, 10);
 
56
        
 
57
        assert (pool1.blockSize() == 100);
 
58
        assert (pool1.allocated() == 0);
 
59
        assert (pool1.available() == 0);
 
60
        
 
61
        std::vector<void*> ptrs;
 
62
        for (int i = 0; i < 10; ++i)
 
63
        {
 
64
                ptrs.push_back(pool1.get());
 
65
                assert (pool1.allocated() == i + 1);
 
66
                assert (pool1.available() == 0);
 
67
        }
 
68
        
 
69
        try
 
70
        {
 
71
                pool1.get();
 
72
                fail("pool exhausted - must throw exception");
 
73
        }
 
74
        catch (Poco::OutOfMemoryException&)
 
75
        {
 
76
        }
 
77
        
 
78
        int av = 0;
 
79
        for (std::vector<void*>::iterator it = ptrs.begin(); it != ptrs.end(); ++it)
 
80
        {
 
81
                pool1.release(*it);
 
82
                ++av;
 
83
                assert (pool1.available() == av);
 
84
        }
 
85
        
 
86
        MemoryPool pool2(32, 5, 10);
 
87
        assert (pool2.available() == 5);
 
88
        assert (pool2.blockSize() == 32);
 
89
        assert (pool2.allocated() == 5);
 
90
}
 
91
 
 
92
 
 
93
void MemoryPoolTest::setUp()
 
94
{
 
95
}
 
96
 
 
97
 
 
98
void MemoryPoolTest::tearDown()
 
99
{
 
100
}
 
101
 
 
102
 
 
103
CppUnit::Test* MemoryPoolTest::suite()
 
104
{
 
105
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MemoryPoolTest");
 
106
 
 
107
        CppUnit_addTest(pSuite, MemoryPoolTest, testMemoryPool);
 
108
 
 
109
        return pSuite;
 
110
}