stxxl::STACK_GENERATOR
class to generate an external stack type with stxxl::grow_shrink_stack
implementation, four blocks per page, block size 4096 bytes
00001 /*************************************************************************** 00002 * containers/test_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 00018 00019 #include <stxxl/stack> 00020 00021 00022 int main(int argc, char * argv[]) 00023 { 00024 typedef stxxl::STACK_GENERATOR<int, stxxl::external, stxxl::grow_shrink, 4, 4096>::result ext_stack_type; 00025 typedef stxxl::STACK_GENERATOR<int, stxxl::external, stxxl::grow_shrink2, 1, 4096>::result ext_stack_type2; 00026 00027 if (argc < 2) 00028 { 00029 STXXL_MSG("Usage: " << argv[0] << " test_size_in_pages"); 00030 return -1; 00031 } 00032 { 00033 ext_stack_type my_stack; 00034 int test_size = atoi(argv[1]) * 4 * 4096 / sizeof(int), i; 00035 00036 for (i = 0; i < test_size; i++) 00037 { 00038 my_stack.push(i); 00039 assert(my_stack.top() == i); 00040 assert(my_stack.size() == i + 1); 00041 } 00042 00043 for (i = test_size - 1; i >= 0; i--) 00044 { 00045 assert(my_stack.top() == i); 00046 my_stack.pop(); 00047 assert(my_stack.size() == i); 00048 } 00049 00050 for (i = 0; i < test_size; i++) 00051 { 00052 my_stack.push(i); 00053 assert(my_stack.top() == i); 00054 assert(my_stack.size() == i + 1); 00055 } 00056 00057 // test swap 00058 ext_stack_type s2; 00059 std::swap(s2, my_stack); 00060 std::swap(s2, my_stack); 00061 00062 for (i = test_size - 1; i >= 0; i--) 00063 { 00064 assert(my_stack.top() == i); 00065 my_stack.pop(); 00066 assert(my_stack.size() == i); 00067 } 00068 00069 std::stack<int> int_stack; 00070 00071 for (i = 0; i < test_size; i++) 00072 { 00073 int_stack.push(i); 00074 assert(int_stack.top() == i); 00075 assert(int(int_stack.size()) == i + 1); 00076 } 00077 00078 ext_stack_type my_stack1(int_stack); 00079 00080 for (i = test_size - 1; i >= 0; i--) 00081 { 00082 assert(my_stack1.top() == i); 00083 my_stack1.pop(); 00084 assert(my_stack1.size() == i); 00085 } 00086 00087 STXXL_MSG("Test 1 passed."); 00088 } 00089 { 00090 // prefetch pool with 10 blocks (> D is recommended) 00091 stxxl::prefetch_pool<ext_stack_type2::block_type> p_pool(10); 00092 // write pool with 10 blocks (> D is recommended) 00093 stxxl::write_pool<ext_stack_type2::block_type> w_pool(10); 00094 // create a stack that does not prefetch (level of prefetch aggressiveness 0) 00095 ext_stack_type2 my_stack(p_pool, w_pool, 0); 00096 int test_size = atoi(argv[1]) * 4 * 4096 / sizeof(int), i; 00097 00098 for (i = 0; i < test_size; i++) 00099 { 00100 my_stack.push(i); 00101 assert(my_stack.top() == i); 00102 assert(my_stack.size() == i + 1); 00103 } 00104 my_stack.set_prefetch_aggr(10); 00105 for (i = test_size - 1; i >= 0; i--) 00106 { 00107 assert(my_stack.top() == i); 00108 my_stack.pop(); 00109 assert(my_stack.size() == i); 00110 } 00111 00112 for (i = 0; i < test_size; i++) 00113 { 00114 my_stack.push(i); 00115 assert(my_stack.top() == i); 00116 assert(my_stack.size() == i + 1); 00117 } 00118 00119 // test swap 00120 ext_stack_type2 s2(p_pool, w_pool, 0); 00121 std::swap(s2, my_stack); 00122 std::swap(s2, my_stack); 00123 00124 for (i = test_size - 1; i >= 0; i--) 00125 { 00126 assert(my_stack.top() == i); 00127 my_stack.pop(); 00128 assert(my_stack.size() == i); 00129 } 00130 00131 STXXL_MSG("Test 2 passed."); 00132 } 00133 00134 return 0; 00135 }