Stxxl  1.4.0
containers/test_queue.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_queue.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2005 Roman Dementiev <dementiev@ira.uka.de>
00007  *  Copyright (C) 2009 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 #include <queue>
00015 #include <stxxl/queue>
00016 
00017 typedef unsigned my_type;
00018 
00019 template <class q1type, class q2type>
00020 void check(const q1type & q1, const q2type & q2)
00021 {
00022     assert(q1.empty() == q2.empty());
00023     assert(q1.size() == q2.size());
00024     if (!q1.empty())
00025     {
00026         if (q1.front() != q2.front() || q1.back() != q2.back())
00027             STXXL_MSG(q1.size() << ": (" << q1.front() << ", " << q1.back() << ") (" << q2.front() << ", " << q2.back() << ")" << (q1.front() == q2.front() ? "" : " FRONT"));
00028         assert(q1.front() == q2.front());
00029         assert(q1.back() == q2.back());
00030     }
00031 }
00032 
00033 int main()
00034 {
00035 #if 1
00036     //stxxl::set_seed(424648671);  // works fine with a single disk, fails with two disks
00037     STXXL_MSG("SEED=" << stxxl::get_next_seed());
00038     stxxl::srandom_number32(stxxl::get_next_seed());
00039     stxxl::random_number32_r rnd;
00040 #else
00041     //stxxl::ran32State = 1028675152; // fails with two disks
00042     STXXL_MSG("ran32State=" << stxxl::ran32State);
00043     stxxl::random_number32 rnd;
00044 #endif
00045 
00046     unsigned cnt;
00047     STXXL_MSG("Elements in a block: " << stxxl::queue<my_type>::block_type::size);
00048 
00049     // FIXME: can this be raised to recommended (3, 2) without breaking "Testing special case 4" or any other tests?
00050     stxxl::queue<my_type> xqueue(2, 2, -1);
00051     std::queue<my_type> squeue;
00052     check(xqueue, squeue);
00053 
00054     STXXL_MSG("Testing special case 4");
00055     cnt = stxxl::queue<my_type>::block_type::size;
00056     while (cnt--)
00057     {
00058         my_type val = rnd();
00059         xqueue.push(val);
00060         squeue.push(val);
00061         check(xqueue, squeue);
00062     }
00063     cnt = stxxl::queue<my_type>::block_type::size;
00064     while (cnt--)
00065     {
00066         xqueue.pop();
00067         squeue.pop();
00068         check(xqueue, squeue);
00069     }
00070     STXXL_MSG("Testing other cases ");
00071     cnt = 100 * stxxl::queue<my_type>::block_type::size;
00072     while (cnt--)
00073     {
00074         if ((cnt % 1000000) == 0)
00075             STXXL_MSG("Operations left: " << cnt << " queue size " << squeue.size());
00076 
00077         int rndtmp = rnd() % 3;
00078         if (rndtmp > 0 || squeue.empty())
00079         {
00080             my_type val = rnd();
00081             xqueue.push(val);
00082             squeue.push(val);
00083             check(xqueue, squeue);
00084         }
00085         else
00086         {
00087             xqueue.pop();
00088             squeue.pop();
00089             check(xqueue, squeue);
00090         }
00091     }
00092     while (!squeue.empty())
00093     {
00094         if ((cnt++ % 1000000) == 0)
00095             STXXL_MSG("Operations: " << cnt << " queue size " << squeue.size());
00096 
00097         int rndtmp = rnd() % 4;
00098         if (rndtmp >= 3)
00099         {
00100             my_type val = rnd();
00101             xqueue.push(val);
00102             squeue.push(val);
00103             check(xqueue, squeue);
00104         }
00105         else
00106         {
00107             xqueue.pop();
00108             squeue.pop();
00109             check(xqueue, squeue);
00110         }
00111     }
00112     cnt = 10 * stxxl::queue<my_type>::block_type::size;
00113     while (cnt--)
00114     {
00115         if ((cnt % 1000000) == 0)
00116             STXXL_MSG("Operations left: " << cnt << " queue size " << squeue.size());
00117 
00118         int rndtmp = rnd() % 3;
00119         if (rndtmp > 0 || squeue.empty())
00120         {
00121             my_type val = rnd();
00122             xqueue.push(val);
00123             squeue.push(val);
00124             check(xqueue, squeue);
00125         }
00126         else
00127         {
00128             xqueue.pop();
00129             squeue.pop();
00130             check(xqueue, squeue);
00131         }
00132     }
00133     typedef stxxl::queue<my_type>::block_type block_type;
00134     stxxl::read_write_pool<block_type> pool(5, 5);
00135     stxxl::queue<my_type> xqueue1(pool, -1);
00136     std::queue<my_type> squeue1;
00137 
00138     cnt = 10 * stxxl::queue<my_type>::block_type::size;
00139     while (cnt--)
00140     {
00141         if ((cnt % 1000000) == 0)
00142             STXXL_MSG("Operations left: " << cnt << " queue size " << squeue1.size());
00143 
00144         int rndtmp = rnd() % 3;
00145         if (rndtmp > 0 || squeue1.empty())
00146         {
00147             my_type val = rnd();
00148             xqueue1.push(val);
00149             squeue1.push(val);
00150             check(xqueue1, squeue1);
00151         }
00152         else
00153         {
00154             xqueue1.pop();
00155             squeue1.pop();
00156             check(xqueue1, squeue1);
00157         }
00158     }
00159 
00160     {
00161         // test proper destruction of a single-block queue
00162         stxxl::queue<int> q;
00163         q.push(42);
00164     }
00165 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines