File: test_migr_stack.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (66 lines) | stat: -rw-r--r-- 2,240 bytes parent folder | download | duplicates (4)
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
55
56
57
58
59
60
61
62
63
64
65
66
/***************************************************************************
 *  tests/containers/test_migr_stack.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

//! \example containers/test_migr_stack.cpp
//! This is an example of how to use \c stxxl::STACK_GENERATOR class
//! to generate an \b migrating stack with critical size \c critical_size ,
//! external implementation \c normal_stack , \b four blocks per page,
//! block size \b 4096 bytes, and internal implementation
//! \c std::stack<int>

#include <stxxl/stack>

// forced instantiation
const unsigned critical_size = 8 * 4096;
template class stxxl::STACK_GENERATOR<size_t, stxxl::migrating, stxxl::normal, 4, 4096, std::stack<size_t>, critical_size>;

int main()
{
    typedef stxxl::STACK_GENERATOR<size_t, stxxl::migrating, stxxl::normal, 4, 4096, std::stack<size_t>, critical_size>::result migrating_stack_type;

    STXXL_MSG("Starting test.");

    migrating_stack_type my_stack;
    size_t test_size = 1 * 1024 * 1024 / sizeof(int);

    STXXL_MSG("Filling stack.");

    for (size_t i = 0; i < test_size; i++)
    {
        my_stack.push(i);
        STXXL_CHECK(my_stack.top() == i);
        STXXL_CHECK(my_stack.size() == i + 1);
        STXXL_CHECK((my_stack.size() >= critical_size) == my_stack.external());
    }

    STXXL_MSG("Testing swap.");
    // test swap
    migrating_stack_type my_stack2;
    std::swap(my_stack2, my_stack);
    std::swap(my_stack2, my_stack);

    STXXL_MSG("Removing elements from " <<
              (my_stack.external() ? "external" : "internal") << " stack");
    for (size_t i = test_size; i > 0; )
    {
        --i;
        STXXL_CHECK(my_stack.top() == i);
        STXXL_CHECK(my_stack.size() == i + 1);
        my_stack.pop();
        STXXL_CHECK(my_stack.size() == i);
        STXXL_CHECK(my_stack.external() == (test_size >= int(critical_size)));
    }

    STXXL_MSG("Test passed.");

    return 0;
}