source: branches/kendall-score/test/ranking.cc @ 4037

Last change on this file since 4037 was 4037, checked in by Peter, 19 months ago

skeleton for new Ranking container. refs #710.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1// $Id: ranking.cc 4037 2021-01-25 04:08:28Z peter $
2
3/*
4  Copyright (C) 2021 Peter Johansson
5
6  The yat library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 3 of the
9  License, or (at your option) any later version.
10
11  The yat library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with yat. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <config.h>
21
22#include "Suite.h"
23
24#include "yat/utility/Ranking.h"
25#include "yat/utility/utility.h"
26
27#include <algorithm>
28
29using namespace theplu::yat;
30
31void test1(test::Suite& suite)
32{
33  suite.out() << "Constructor(0)\n";
34  utility::Ranking<double> ranking;
35  {
36    suite.out() << "Constructor(1)\n";
37    utility::Ranking<double, std::less<double> > ranking2(ranking.compare());
38    test::avoid_compiler_warning(ranking2);
39    suite.out() << "Constructor(0)\n";
40    utility::Ranking<double, std::greater<double> > ranking3;
41    test::avoid_compiler_warning(ranking3);
42    std::greater<double> compare;
43    suite.out() << "Constructor(1)\n";
44    utility::Ranking<double, std::greater<double> > ranking4(compare);
45    test::avoid_compiler_warning(ranking4);
46  }
47
48  suite.out() << "::insert\n";
49  ranking.insert(0);
50  suite.out() << "::insert\n";
51  ranking.insert(1);
52  suite.out() << "::insert\n";
53  ranking.insert(2);
54  suite.out() << "::insert\n";
55  ranking.insert(1);
56  if (ranking.size() != 4) {
57    suite.add(false);
58    suite.err() << "error: incorrect size: " << ranking.size() << "\n";
59  }
60  suite.out() << "distance\n";
61  int dist = std::distance(ranking.begin(), ranking.end());
62  if (dist != 4) {
63    suite.add(false);
64    suite.err() << "error: incorrect distance: " << dist << "\n";
65  }
66  int cdist = std::distance(ranking.cbegin(), ranking.cend());
67  if (cdist != 4) {
68    suite.add(false);
69    suite.err() << "error: incorrect const distance: " << cdist << "\n";
70  }
71  int rdist = std::distance(ranking.rbegin(), ranking.rend());
72  if (rdist != 4) {
73    suite.add(false);
74    suite.err() << "error: incorrect reverse distance: " << rdist << "\n";
75  }
76  int crdist = std::distance(ranking.crbegin(), ranking.crend());
77  if (crdist != 4) {
78    suite.add(false);
79    suite.err() << "error: incorrect const reverse distance: "
80                << crdist << "\n";
81  }
82
83  utility::Ranking<double>::iterator lower = ranking.lower_bound(1);
84  if (*lower != 1) {
85    suite.add(false);
86    suite.err() << "error: *lower returned: " << *lower << "\n";
87  }
88  utility::Ranking<double>::iterator upper = ranking.upper_bound(1);
89  if (*upper != 2) {
90    suite.add(false);
91    suite.err() << "error: *upper returned: " << *upper << "\n";
92  }
93  dist = std::distance(lower, upper);
94  if (dist != 2) {
95    suite.add(false);
96    suite.err() << "error: distance(lower, upper): " << dist << "\n";
97  }
98
99  std::vector<int> vec(10);
100  ranking.insert(vec.begin(), vec.end());
101}
102
103
104void test2(test::Suite& suite)
105{
106  // mimick Ranking is used Kendall::score
107  utility::Ranking<double> ranking;
108  auto lower = ranking.lower_bound(2);
109  int r = ranking.ranking(lower);
110  suite.out() << "ranking: " << r << "\n";
111  if (r != 0) {
112    suite.add(false);
113    suite.err() << "error: incorrect; expected 0\n";
114  }
115  ranking.insert(lower, 2);
116
117  lower = ranking.lower_bound(1);
118  r = ranking.ranking(lower);
119  suite.out() << "ranking: " << r << "\n";
120  if (r != 0) {
121    suite.add(false);
122    suite.err() << "error: incorrect; expected 0\n";
123  }
124  ranking.insert(lower, 1);
125
126  lower = ranking.lower_bound(3);
127  r = ranking.ranking(lower);
128  suite.out() << "ranking: " << r << "\n";
129  if (r != 2) {
130    suite.add(false);
131    suite.err() << "error: incorrect; expected 2\n";
132  }
133}
134
135
136int main(int argc, char* argv[])
137{
138  test::Suite suite(argc, argv);
139  try {
140    test1(suite);
141    test2(suite);
142  }
143  catch (std::exception& e) {
144    suite.add(false);
145    suite.err() << "error: exception caught: what(): ";
146    utility::print_what(e, suite.err());
147  }
148  return suite.return_value();
149}
Note: See TracBrowser for help on using the repository browser.