PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 7ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/unordered/test/unordered/equality_tests.cpp

http://hadesmem.googlecode.com/
C++ | 171 lines | 141 code | 25 blank | 5 comment | 18 complexity | 032bb974fea3013536736a546d27e06e MD5 | raw file
  1
  2// Copyright 2008-2009 Daniel James.
  3// Distributed under the Boost Software License, Version 1.0. (See accompanying
  4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5
  6#include "../helpers/prefix.hpp"
  7
  8#include <boost/unordered_set.hpp>
  9#include <boost/unordered_map.hpp>
 10#include <boost/preprocessor/seq.hpp>
 11#include <list>
 12#include "../helpers/test.hpp"
 13
 14namespace equality_tests
 15{
 16    struct mod_compare
 17    {
 18        bool alt_hash_;
 19
 20        explicit mod_compare(bool alt_hash = false) : alt_hash_(alt_hash) {}
 21
 22        bool operator()(int x, int y) const
 23        {
 24            return x % 1000 == y % 1000;
 25        }
 26
 27        int operator()(int x) const
 28        {
 29            return alt_hash_ ? x % 250 : (x + 5) % 250;
 30        }
 31    };
 32
 33#define UNORDERED_EQUALITY_SET_TEST(seq1, op, seq2)                         \
 34    {                                                                       \
 35        boost::unordered_set<int, mod_compare, mod_compare> set1, set2;     \
 36        BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set1, seq1)             \
 37        BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set2, seq2)             \
 38        BOOST_TEST(set1 op set2);                                           \
 39    }
 40
 41#define UNORDERED_EQUALITY_MULTISET_TEST(seq1, op, seq2)                    \
 42    {                                                                       \
 43        boost::unordered_multiset<int, mod_compare, mod_compare>            \
 44            set1, set2;                                                     \
 45        BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set1, seq1)             \
 46        BOOST_PP_SEQ_FOR_EACH(UNORDERED_SET_INSERT, set2, seq2)             \
 47        BOOST_TEST(set1 op set2);                                           \
 48    }
 49
 50#define UNORDERED_EQUALITY_MAP_TEST(seq1, op, seq2)                         \
 51    {                                                                       \
 52        boost::unordered_map<int, int, mod_compare, mod_compare>            \
 53            map1, map2;                                                     \
 54        BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1)             \
 55        BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2)             \
 56        BOOST_TEST(map1 op map2);                                           \
 57    }
 58
 59#define UNORDERED_EQUALITY_MULTIMAP_TEST(seq1, op, seq2)                    \
 60    {                                                                       \
 61        boost::unordered_multimap<int, int, mod_compare, mod_compare>       \
 62            map1, map2;                                                     \
 63        BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map1, seq1)             \
 64        BOOST_PP_SEQ_FOR_EACH(UNORDERED_MAP_INSERT, map2, seq2)             \
 65        BOOST_TEST(map1 op map2);                                           \
 66    }
 67
 68#define UNORDERED_SET_INSERT(r, set, item) set.insert(item);
 69#define UNORDERED_MAP_INSERT(r, map, item) \
 70    map.insert(std::pair<int const, int> BOOST_PP_SEQ_TO_TUPLE(item));
 71
 72    UNORDERED_AUTO_TEST(equality_size_tests)
 73    {
 74        boost::unordered_set<int> x1, x2;
 75        BOOST_TEST(x1 == x2);
 76        BOOST_TEST(!(x1 != x2));
 77
 78        x1.insert(1);
 79        BOOST_TEST(x1 != x2);
 80        BOOST_TEST(!(x1 == x2));
 81        BOOST_TEST(x2 != x1);
 82        BOOST_TEST(!(x2 == x1));
 83        
 84        x2.insert(1);
 85        BOOST_TEST(x1 == x2);
 86        BOOST_TEST(!(x1 != x2));
 87        
 88        x2.insert(2);
 89        BOOST_TEST(x1 != x2);
 90        BOOST_TEST(!(x1 == x2));
 91        BOOST_TEST(x2 != x1);
 92        BOOST_TEST(!(x2 == x1));
 93    }
 94    
 95    UNORDERED_AUTO_TEST(equality_key_value_tests)
 96    {
 97        UNORDERED_EQUALITY_MULTISET_TEST((1), !=, (2))
 98        UNORDERED_EQUALITY_SET_TEST((2), ==, (2))
 99        UNORDERED_EQUALITY_MAP_TEST(((1)(1))((2)(1)), !=, ((1)(1))((3)(1)))
100    }
101    
102    UNORDERED_AUTO_TEST(equality_collision_test)
103    {
104        UNORDERED_EQUALITY_MULTISET_TEST(
105            (1), !=, (501))
106        UNORDERED_EQUALITY_MULTISET_TEST(
107            (1)(251), !=, (1)(501))
108        UNORDERED_EQUALITY_MULTIMAP_TEST(
109            ((251)(1))((1)(1)), !=, ((501)(1))((1)(1)))
110        UNORDERED_EQUALITY_MULTISET_TEST(
111            (1)(501), ==, (1)(501))
112        UNORDERED_EQUALITY_SET_TEST(
113            (1)(501), ==, (501)(1))
114    }
115
116    UNORDERED_AUTO_TEST(equality_group_size_test)
117    {
118        UNORDERED_EQUALITY_MULTISET_TEST(
119            (10)(20)(20), !=, (10)(10)(20))
120        UNORDERED_EQUALITY_MULTIMAP_TEST(
121            ((10)(1))((20)(1))((20)(1)), !=,
122            ((10)(1))((20)(1))((10)(1)))
123        UNORDERED_EQUALITY_MULTIMAP_TEST(
124            ((20)(1))((10)(1))((10)(1)), ==,
125            ((10)(1))((20)(1))((10)(1)))
126    }
127    
128    UNORDERED_AUTO_TEST(equality_map_value_test)
129    {
130        UNORDERED_EQUALITY_MAP_TEST(
131            ((1)(1)), !=, ((1)(2)))
132        UNORDERED_EQUALITY_MAP_TEST(
133            ((1)(1)), ==, ((1)(1)))
134        UNORDERED_EQUALITY_MULTIMAP_TEST(
135            ((1)(1)), !=, ((1)(2)))
136        UNORDERED_EQUALITY_MULTIMAP_TEST(
137            ((1)(1))((1)(1)), !=, ((1)(1))((1)(2)))
138        UNORDERED_EQUALITY_MULTIMAP_TEST(
139            ((1)(2))((1)(1)), !=, ((1)(1))((1)(2)))
140    }
141
142    UNORDERED_AUTO_TEST(equality_predicate_test)
143    {
144        UNORDERED_EQUALITY_SET_TEST(
145            (1), ==, (1001))
146        UNORDERED_EQUALITY_MAP_TEST(
147            ((1)(2))((1001)(1)), ==, ((1001)(2))((1)(1)))
148    }
149
150    // Test that equality still works when the two containers have
151    // different hash functions but the same equality predicate.
152
153    UNORDERED_AUTO_TEST(equality_different_hash_test)
154    {
155        typedef boost::unordered_set<int, mod_compare, mod_compare> set;
156        set set1(0, mod_compare(false), mod_compare(false));
157        set set2(0, mod_compare(true), mod_compare(true));
158        BOOST_TEST(set1 == set2);
159        set1.insert(1); set2.insert(2);
160        BOOST_TEST(set1 != set2);
161        set1.insert(2); set2.insert(1);
162        BOOST_TEST(set1 == set2);
163        set1.insert(10); set2.insert(20);
164        BOOST_TEST(set1 != set2);
165        set1.insert(20); set2.insert(10);
166        BOOST_TEST(set1 == set2);
167    }
168
169}
170
171RUN_TESTS()