libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
bogo_sort.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include "core/bogo_sort.hpp"
25 
26 #include <algorithm>
27 #include <vector>
28 
29 using namespace libjmmcg;
30 
31 typedef std::vector<int> colln_t;
32 
33 BOOST_AUTO_TEST_SUITE(bogo_sort_tests)
34 
35 BOOST_AUTO_TEST_CASE(empty)
36 {
37  colln_t c;
38  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
39 }
40 
41 BOOST_AUTO_TEST_CASE(sort_one)
42 {
43  colln_t c{1};
44  colln_t const c_cpy(c);
45  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
46  BOOST_CHECK(c==c_cpy);
47 }
48 
49 BOOST_AUTO_TEST_CASE(sort_two_sorted)
50 {
51  colln_t c{1, 2};
52  colln_t const c_cpy(c);
53  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
54  BOOST_CHECK(c==c_cpy);
55 }
56 
57 BOOST_AUTO_TEST_CASE(sort_two_reversed)
58 {
59  colln_t c{2, 1};
60  colln_t c_cpy(c);
61  std::sort(c_cpy.begin(), c_cpy.end());
62  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
63  BOOST_CHECK(c==c_cpy);
64 }
65 
66 BOOST_AUTO_TEST_CASE(sort_same)
67 {
68  colln_t c{1, 1, 1, 1};
69  colln_t const c_cpy(c);
70  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
71  BOOST_CHECK(c==c_cpy);
72 }
73 
74 BOOST_AUTO_TEST_CASE(sort_sorted)
75 {
76  colln_t c{1, 2, 3, 4};
77  colln_t const c_cpy(c);
78  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
79  BOOST_CHECK(c==c_cpy);
80 }
81 
82 BOOST_AUTO_TEST_CASE(sort_reversed)
83 {
84  colln_t c{4, 3, 2, 1};
85  colln_t c_cpy(c);
86  std::sort(c_cpy.begin(), c_cpy.end());
87  BOOST_CHECK_NO_THROW(bogo_sort(c.begin(), c.end(), std::less<colln_t::value_type>()));
88  BOOST_CHECK(c==c_cpy);
89 }
90 
91 BOOST_AUTO_TEST_SUITE_END()