Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * containers/test_migr_stack.cpp 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2003 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * 00008 * Distributed under the Boost Software License, Version 1.0. 00009 * (See accompanying file LICENSE_1_0.txt or copy at 00010 * http://www.boost.org/LICENSE_1_0.txt) 00011 **************************************************************************/ 00012 00013 //! \example containers/test_migr_stack.cpp 00014 //! This is an example of how to use \c stxxl::STACK_GENERATOR class 00015 //! to generate an \b migrating stack with critical size \c critical_size , 00016 //! external implementation \c normal_stack , \b four blocks per page, 00017 //! block size \b 4096 bytes, and internal implementation 00018 //! \c std::stack<int> 00019 00020 #include <stxxl/stack> 00021 00022 00023 int main() 00024 { 00025 const unsigned critical_size = 8 * 4096; 00026 typedef stxxl::STACK_GENERATOR<int, stxxl::migrating, stxxl::normal, 4, 4096, std::stack<int>, critical_size>::result migrating_stack_type; 00027 00028 STXXL_MSG("Starting test."); 00029 00030 migrating_stack_type my_stack; 00031 int test_size = 1 * 1024 * 1024 / sizeof(int), i; 00032 00033 STXXL_MSG("Filling stack."); 00034 00035 for (i = 0; i < test_size; i++) 00036 { 00037 my_stack.push(i); 00038 assert(my_stack.top() == i); 00039 assert(my_stack.size() == i + 1); 00040 assert((my_stack.size() >= critical_size) == my_stack.external()); 00041 } 00042 00043 STXXL_MSG("Testing swap."); 00044 // test swap 00045 migrating_stack_type my_stack2; 00046 std::swap(my_stack2, my_stack); 00047 std::swap(my_stack2, my_stack); 00048 00049 STXXL_MSG("Removing elements from " << 00050 (my_stack.external() ? "external" : "internal") << " stack"); 00051 for (i = test_size - 1; i >= 0; i--) 00052 { 00053 assert(my_stack.top() == i); 00054 assert(my_stack.size() == i + 1); 00055 my_stack.pop(); 00056 assert(my_stack.size() == i); 00057 assert(my_stack.external() == (test_size >= int(critical_size))); 00058 } 00059 00060 STXXL_MSG("Test passed."); 00061 00062 return 0; 00063 }