File: test_set_ops.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (134 lines) | stat: -rw-r--r-- 3,719 bytes parent folder | download | duplicates (11)
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
/* Boost.MultiIndex test for standard set operations.
 *
 * Copyright 2003-2022 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/multi_index for library home page.
 */

#include "test_set_ops.hpp"

#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <algorithm>
#include <vector>
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include <boost/detail/lightweight_test.hpp>

using namespace boost::multi_index;

struct type1{};

struct type2
{
private:
  operator type1()const{return type1();}
};

struct type3
{
  operator type1()const{return type1();}
};

struct less_type12
{
  bool operator()(type1,type1)const{return false;}
  bool operator()(type1,type2)const{return false;}
  bool operator()(type2,type1)const{return false;}
};

bool less_type1_f(type1,type1){return false;}

struct hash_type12
{
  std::size_t operator()(type1)const{return 0;}
  std::size_t operator()(type2)const{return 0;}
};

struct eq_type12
{
  bool operator()(type1,type1)const{return true;}
  bool operator()(type1,type2)const{return true;}
  bool operator()(type2,type1)const{return true;}
};

void test_set_ops()
{
  employee_set               es;
  employee_set_by_name&      i1=get<by_name>(es);
  const employee_set_by_age& i2=get<age>(es);
  employee_set_by_ssn&       i4=get<ssn>(es);

  es.insert(employee(0,"Joe",31,1123));
  es.insert(employee(1,"Robert",27,5601));
  es.insert(employee(2,"John",40,7889));
  es.insert(employee(3,"Albert",20,9012));
  es.insert(employee(4,"John",57,1002));

  BOOST_TEST(i1.find("John")->name=="John");
  BOOST_TEST(i2.find(41)==i2.end());
  BOOST_TEST(i4.find(5601)->name=="Robert");

  BOOST_TEST(i1.count("John")==2);
  BOOST_TEST(i2.count(20)==1);
  BOOST_TEST(es.count(employee(10,"",-1,0))==0);
  BOOST_TEST(i4.count(7881)==0);

  BOOST_TEST(i1.contains("John"));
  BOOST_TEST(!es.contains(employee(10,"",-1,0)));
  BOOST_TEST(!i4.contains(7881));

  BOOST_TEST(
    std::distance(
      i2.lower_bound(31),
      i2.upper_bound(60))==3);

  std::pair<employee_set_by_name::iterator,employee_set_by_name::iterator> p=
    i1.equal_range("John");
  BOOST_TEST(std::distance(p.first,p.second)==2);

  p=i1.equal_range("Serena");
  BOOST_TEST(p.first==i1.end()&&p.second==i1.end());

  std::pair<employee_set_by_age::iterator,employee_set_by_age::iterator> p2=
    i2.equal_range(30);
  BOOST_TEST(p2.first==p2.second&&p2.first->age==31);

  /* check promotion detection plays nice with private conversion */

  multi_index_container<
    type1,
    indexed_by<
      ordered_unique<identity<type1>,less_type12>,
      hashed_unique<identity<type1>,hash_type12,eq_type12>
    >
  > c;
  c.insert(type1());

  BOOST_TEST(c.find(type2())==c.begin());
  BOOST_TEST(c.count(type2())==1);
  BOOST_TEST(c.contains(type2()));
  BOOST_TEST(c.lower_bound(type2())==c.begin());
  BOOST_TEST(c.upper_bound(type2())==c.end());
  BOOST_TEST(c.equal_range(type2())==std::make_pair(c.begin(),c.end()));

  BOOST_TEST(c.get<1>().find(type2())==c.get<1>().begin());
  BOOST_TEST(c.get<1>().count(type2())==1);
  BOOST_TEST(c.get<1>().contains(type2()));
  BOOST_TEST(c.get<1>().equal_range(type2())==
             std::make_pair(c.get<1>().begin(),c.get<1>().end()));

  /* check promotion detection does not break with functions */

  multi_index_container<
    type1,
    indexed_by<
      ordered_unique<identity<type1>,bool(*)(type1,type1)>
    >
  > c2(boost::make_tuple(boost::make_tuple(identity<type1>(),&less_type1_f)));
  c2.insert(type1());

  BOOST_TEST(c2.find(type3())==c2.begin());
}