Stxxl  1.4.0
containers/test_stack.cpp
Go to the documentation of this file.
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  *  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 //! \example containers/test_stack.cpp
00015 //! This is an example of how to use \c stxxl::STACK_GENERATOR class
00016 //! to generate an \b external stack type
00017 //! with \c stxxl::grow_shrink_stack implementation, \b four blocks per page,
00018 //! block size \b 4096 bytes
00019 
00020 #include <stxxl/stack>
00021 
00022 
00023 template <typename stack_type>
00024 void test_lvalue_correctness(stack_type & stack, int a, int b)
00025 {
00026     int i;
00027     assert(stack.empty());
00028     for (i = 0; i < a; ++i)
00029         stack.push(i);
00030     for (i = 0; i < b; ++i)
00031         stack.push(i);
00032     for (i = 0; i < b; ++i)
00033         stack.pop();
00034     stack.top() = 0xbeeff00d;
00035     for (i = 0; i < b; ++i)
00036         stack.push(i);
00037     for (i = 0; i < b; ++i)
00038         stack.pop();
00039     if ((stack.top() != int(0xbeeff00d))) {
00040         STXXL_ERRMSG("STACK MISMATCH AFTER top() LVALUE MODIFICATION (0x" << std::hex << stack.top() << " != 0xbeeff00d)");
00041         assert(stack.top() == int(0xbeeff00d));
00042     }
00043     for (i = 0; i < a; ++i)
00044         stack.pop();
00045 }
00046 
00047 
00048 template <typename stack_type>
00049 void simple_test(stack_type & my_stack, int test_size)
00050 {
00051     int i;
00052 
00053     for (i = 0; i < test_size; i++)
00054     {
00055         my_stack.push(i);
00056         assert(my_stack.top() == i);
00057         assert(my_stack.size() == i + 1);
00058     }
00059 
00060     for (i = test_size - 1; i >= 0; i--)
00061     {
00062         assert(my_stack.top() == i);
00063         my_stack.pop();
00064         assert(my_stack.size() == i);
00065     }
00066 
00067     for (i = 0; i < test_size; i++)
00068     {
00069         my_stack.push(i);
00070         assert(my_stack.top() == i);
00071         assert(my_stack.size() == i + 1);
00072     }
00073 
00074     // test swap
00075     stack_type s2;
00076     std::swap(s2, my_stack);
00077     std::swap(s2, my_stack);
00078 
00079     for (i = test_size - 1; i >= 0; i--)
00080     {
00081         assert(my_stack.top() == i);
00082         my_stack.pop();
00083         assert(my_stack.size() == i);
00084     }
00085 
00086     std::stack<int> int_stack;
00087 
00088     for (i = 0; i < test_size; i++)
00089     {
00090         int_stack.push(i);
00091         assert(int_stack.top() == i);
00092         assert(int(int_stack.size()) == i + 1);
00093     }
00094 
00095     stack_type my_stack1(int_stack);
00096 
00097     for (i = test_size - 1; i >= 0; i--)
00098     {
00099         assert(my_stack1.top() == i);
00100         my_stack1.pop();
00101         assert(my_stack1.size() == i);
00102     }
00103 
00104     STXXL_MSG("Test 1 passed.");
00105 
00106     test_lvalue_correctness(my_stack, 4 * 4096 / 4 * 2, 4 * 4096 / 4 * 2 * 20);
00107 }
00108 
00109 int main(int argc, char * argv[])
00110 {
00111     typedef stxxl::STACK_GENERATOR<int, stxxl::external, stxxl::normal, 4, 4096>::result ext_normal_stack_type;
00112     typedef stxxl::STACK_GENERATOR<int, stxxl::migrating, stxxl::normal, 4, 4096>::result ext_migrating_stack_type;
00113     typedef stxxl::STACK_GENERATOR<int, stxxl::external, stxxl::grow_shrink, 4, 4096>::result ext_stack_type;
00114     typedef stxxl::STACK_GENERATOR<int, stxxl::external, stxxl::grow_shrink2, 1, 4096>::result ext_stack_type2;
00115 
00116     if (argc < 2)
00117     {
00118         STXXL_MSG("Usage: " << argv[0] << " test_size_in_pages");
00119         return -1;
00120     }
00121     {
00122         ext_normal_stack_type my_stack;
00123         simple_test(my_stack, atoi(argv[1]) * 4 * 4096 / sizeof(int));
00124     }
00125     {
00126         ext_migrating_stack_type my_stack;
00127         //simple_test(my_stack, atoi(argv[1]) * 4 * 4096 / sizeof(int));
00128     }
00129     {
00130         ext_stack_type my_stack;
00131         simple_test(my_stack, atoi(argv[1]) * 4 * 4096 / sizeof(int));
00132     }
00133     {
00134         // prefetch/write pool with 10 blocks prefetching and 10 block write cache (> D is recommended)
00135         stxxl::read_write_pool<ext_stack_type2::block_type> pool(10, 10);
00136         // create a stack that does not prefetch (level of prefetch aggressiveness 0)
00137         ext_stack_type2 my_stack(pool, 0);
00138         int test_size = atoi(argv[1]) * 4 * 4096 / sizeof(int), i;
00139 
00140         for (i = 0; i < test_size; i++)
00141         {
00142             my_stack.push(i);
00143             assert(my_stack.top() == i);
00144             assert(my_stack.size() == i + 1);
00145         }
00146         my_stack.set_prefetch_aggr(10);
00147         for (i = test_size - 1; i >= 0; i--)
00148         {
00149             assert(my_stack.top() == i);
00150             my_stack.pop();
00151             assert(my_stack.size() == i);
00152         }
00153 
00154         for (i = 0; i < test_size; i++)
00155         {
00156             my_stack.push(i);
00157             assert(my_stack.top() == i);
00158             assert(my_stack.size() == i + 1);
00159         }
00160 
00161         // test swap
00162         ext_stack_type2 s2(pool, 0);
00163         std::swap(s2, my_stack);
00164         std::swap(s2, my_stack);
00165 
00166         for (i = test_size - 1; i >= 0; i--)
00167         {
00168             assert(my_stack.top() == i);
00169             my_stack.pop();
00170             assert(my_stack.size() == i);
00171         }
00172 
00173         STXXL_MSG("Test 2 passed.");
00174 
00175         test_lvalue_correctness(my_stack, 4 * 4096 / 4 * 2, 4 * 4096 / 4 * 2 * 20);
00176     }
00177 
00178     return 0;
00179 }
00180 
00181 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines