1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/* Boost.Flyweight basic test template.
*
* Copyright 2006-2024 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* See http://www.boost.org/libs/flyweight for library home page.
*/
#ifndef BOOST_FLYWEIGHT_TEST_BASIC_TEMPLATE_HPP
#define BOOST_FLYWEIGHT_TEST_BASIC_TEMPLATE_HPP
#if defined(_MSC_VER)
#pragma once
#endif
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <boost/core/lightweight_test.hpp>
#include <boost/flyweight/key_value.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/utility/value_init.hpp>
#include <string>
#include <sstream>
#include "heavy_objects.hpp"
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
#include <utility>
#endif
#if !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT)
#include <boost/functional/hash.hpp>
#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
#include <functional>
#endif
#endif
#define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
template<typename Flyweight,typename ForwardIterator>
void test_basic_template(
ForwardIterator first,ForwardIterator last
BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight))
{
typedef typename Flyweight::value_type value_type;
ForwardIterator it;
for(it=first;it!=last;++it){
/* construct/copy/destroy */
Flyweight f1(*it);
Flyweight f2;
Flyweight c1(f1);
const Flyweight c2(static_cast<const Flyweight&>(f2));
value_type v1(*it);
boost::value_initialized<value_type> v2;
BOOST_TEST(f1.get_key()==*it);
BOOST_TEST((f1==f2)==(f1.get()==v2.data()));
BOOST_TEST(f1==c1);
BOOST_TEST(f2==c2);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
Flyweight cr1(std::move(c1));
Flyweight cr2(std::move(c2));
BOOST_TEST(f1==cr1);
BOOST_TEST(f2==cr2);
#endif
#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
/* testcase for https://svn.boost.org/trac/boost/ticket/10439 */
Flyweight f3={};
BOOST_TEST(f3==f2);
#endif
f1=((void)0,f1); /* self assignment warning */
BOOST_TEST(f1==f1);
c1=f2;
BOOST_TEST(c1==f2);
c1=f1;
BOOST_TEST(c1==f1);
/* convertibility to underlying type */
BOOST_TEST(f1.get()==v1);
BOOST_TEST(*f1==v1);
/* identity of reference */
BOOST_TEST(&f1.get()==&c1.get());
BOOST_TEST(&(*f1)==&c1.get());
BOOST_TEST(f1.operator->()==&c1.get());
/* modifiers */
f1.swap(f1);
BOOST_TEST(f1==c1);
f1.swap(f2);
BOOST_TEST(f1==c2);
BOOST_TEST(f2==c1);
boost::flyweights::swap(f1,f2);
BOOST_TEST(f1==c1);
BOOST_TEST(f2==c2);
/* specialized algorithms */
std::ostringstream oss1;
oss1<<f1;
std::ostringstream oss2;
oss2<<f1.get();
BOOST_TEST(oss1.str()==oss2.str());
#if !defined(BOOST_FLYWEIGHT_DISABLE_HASH_SUPPORT)
/* hash support */
BOOST_TEST(boost::hash<Flyweight>()(f1)==boost::hash<Flyweight>()(c1));
BOOST_TEST(boost::hash<Flyweight>()(f1)==
boost::hash<const value_type*>()(&f1.get()));
#if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
BOOST_TEST(std::hash<Flyweight>()(f1)==std::hash<Flyweight>()(c1));
BOOST_TEST(std::hash<Flyweight>()(f1)==
std::hash<const value_type*>()(&f1.get()));
#endif
#endif
}
}
template<typename Flyweight,typename ForwardIterator>
void test_basic_with_assign_template(
ForwardIterator first,ForwardIterator last
BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight))
{
typedef typename Flyweight::value_type value_type;
ForwardIterator it;
test_basic_template<Flyweight>(first,last);
for(it=first;it!=last;++it){
/* value construction */
value_type v(*it);
Flyweight f1(v);
Flyweight f2(f1.get());
BOOST_TEST(f1.get()==v);
BOOST_TEST(f2.get()==v);
BOOST_TEST(f1==f2);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
value_type v1(v);
const value_type v2(v);
Flyweight fr1(std::move(v1));
Flyweight fr2(std::move(v2));
BOOST_TEST(fr1==v);
BOOST_TEST(fr2==v);
#endif
/* value assignment */
Flyweight f3,f4;
f3=v;
f4=f1.get();
BOOST_TEST(f2.get()==v);
BOOST_TEST(f3.get()==v);
BOOST_TEST(f2==f3);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
Flyweight fr3;
fr3=value_type(*it);
BOOST_TEST(fr3.get()==value_type(*it));
#endif
/* specialized algorithms */
std::ostringstream oss1;
oss1<<f1;
std::istringstream iss1(oss1.str());
Flyweight f5;
iss1>>f5;
BOOST_TEST(f5==f1);
}
}
template<
typename Flyweight1,typename Flyweight2,
typename ForwardIterator1,typename ForwardIterator2
>
void test_basic_comparison_template(
ForwardIterator1 first1,ForwardIterator1 last1,
ForwardIterator2 first2
BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight1)
BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight2))
{
typedef typename Flyweight1::value_type value_type1;
typedef typename Flyweight2::value_type value_type2;
for(;first1!=last1;++first1,++first2){
value_type1 v1=value_type1(*first1);
value_type2 v2=value_type2(*first2);
Flyweight1 f1(v1);
Flyweight2 f2(v2);
BOOST_TEST((f1==f2)==(f1.get()==v2));
BOOST_TEST((f1< f2)==(f1.get()< v2));
BOOST_TEST((f1!=f2)==(f1.get()!=v2));
BOOST_TEST((f1> f2)==(f1.get()> v2));
BOOST_TEST((f1>=f2)==(f1.get()>=v2));
BOOST_TEST((f1<=f2)==(f1.get()<=v2));
#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
BOOST_TEST((f1==v2)==(f1.get()==v2));
BOOST_TEST((f1< v2)==(f1.get()< v2));
BOOST_TEST((f1!=v2)==(f1.get()!=v2));
BOOST_TEST((f1> v2)==(f1.get()> v2));
BOOST_TEST((f1>=v2)==(f1.get()>=v2));
BOOST_TEST((f1<=v2)==(f1.get()<=v2));
BOOST_TEST((v1==f2)==(f1.get()==v2));
BOOST_TEST((v1< f2)==(f1.get()< v2));
BOOST_TEST((v1!=f2)==(f1.get()!=v2));
BOOST_TEST((v1> f2)==(f1.get()> v2));
BOOST_TEST((v1>=f2)==(f1.get()>=v2));
BOOST_TEST((v1<=f2)==(f1.get()<=v2));
#endif
}
}
template<typename FlyweightSpecifier>
void test_basic_template(BOOST_EXPLICIT_TEMPLATE_TYPE(FlyweightSpecifier))
{
typedef typename boost::mpl::apply1<
FlyweightSpecifier,int
>::type int_flyweight;
typedef typename boost::mpl::apply1<
FlyweightSpecifier,std::string
>::type string_flyweight;
typedef typename boost::mpl::apply1<
FlyweightSpecifier,char
>::type char_flyweight;
typedef typename boost::mpl::apply1<
FlyweightSpecifier,
boost::flyweights::key_value<std::string,texture,from_texture_to_string>
>::type texture_flyweight;
typedef typename boost::mpl::apply1<
FlyweightSpecifier,
boost::flyweights::key_value<int,factorization>
>::type factorization_flyweight;
int ints[]={0,1,1,0,1,2,3,4,3,4,0,0};
test_basic_with_assign_template<int_flyweight>(
&ints[0],&ints[0]+LENGTHOF(ints));
const char* words[]={"hello","boost","flyweight","boost","bye","c++","c++"};
test_basic_with_assign_template<string_flyweight>(
&words[0],&words[0]+LENGTHOF(words));
const char* textures[]={"wood","grass","sand","granite","terracotta"};
test_basic_with_assign_template<texture_flyweight>(
&textures[0],&textures[0]+LENGTHOF(textures));
int factorizations[]={1098,102387,90846,2223978};
test_basic_template<factorization_flyweight>(
&factorizations[0],&factorizations[0]+LENGTHOF(factorizations));
char chars[]={0,2,4,5,1,1,1,3,4,1,1,0};
test_basic_comparison_template<int_flyweight,char_flyweight>(
&ints[0],&ints[0]+LENGTHOF(ints),&chars[0]);
test_basic_comparison_template<string_flyweight,string_flyweight>(
&words[0],&words[0]+LENGTHOF(words)-1,&words[1]);
test_basic_comparison_template<texture_flyweight,texture_flyweight>(
&textures[0],&textures[0]+LENGTHOF(textures)-1,&textures[1]);
#if !defined(BOOST_NO_EXCEPTIONS)
typedef typename boost::mpl::apply1<
FlyweightSpecifier,
boost::flyweights::key_value<int,throwing_value,from_throwing_value_to_int>
>::type throwing_flyweight;
try{
throwing_flyweight fw(0);
}catch(const throwing_value_exception&){}
try{
throwing_flyweight fw=throwing_flyweight(throwing_value());
(void)fw;
}catch(const throwing_value_exception&){}
throwing_flyweight fw=throwing_flyweight(
throwing_value(false /* doesn't throw */));
(void)fw.get();
#endif
}
#undef LENGTHOF
#endif
|