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

Last change on this file since 4064 was 4064, checked in by Peter, 13 months ago

first functioning version (tests pass) of Kendall class using the Ranking class. Split out the code in separate header and source files. Ranking class is still slow (linear) so Kendall::score is still quadratic. refs #710

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1// $Id: ranking.cc 4064 2021-08-05 05:36:32Z 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#define YAT_DEBUG_RANKING 1
25
26#include "yat/utility/Ranking.h"
27#include "yat/utility/utility.h"
28
29#include <algorithm>
30
31using namespace theplu::yat;
32
33void test1(test::Suite& suite)
34{
35  suite.out() << "Constructor(0)\n";
36  utility::Ranking<double> ranking;
37  {
38    suite.out() << "Constructor(1)\n";
39    utility::Ranking<double, std::less<double> > ranking2(ranking.compare());
40    test::avoid_compiler_warning(ranking2);
41    suite.out() << "Constructor(0)\n";
42    utility::Ranking<double, std::greater<double> > ranking3;
43    test::avoid_compiler_warning(ranking3);
44    std::greater<double> compare;
45    suite.out() << "Constructor(1)\n";
46    utility::Ranking<double, std::greater<double> > ranking4(compare);
47    test::avoid_compiler_warning(ranking4);
48  }
49
50  suite.out() << "::insert\n";
51  ranking.insert(0);
52  suite.out() << "::insert\n";
53  ranking.insert(1);
54  suite.out() << "::insert\n";
55  ranking.insert(2);
56  suite.out() << "::insert\n";
57  ranking.insert(1);
58  if (ranking.size() != 4) {
59    suite.add(false);
60    suite.err() << "error: incorrect size: " << ranking.size() << "\n";
61  }
62  suite.out() << "distance\n";
63  int dist = std::distance(ranking.begin(), ranking.end());
64  if (dist != 4) {
65    suite.add(false);
66    suite.err() << "error: incorrect distance: " << dist << "\n";
67  }
68  int cdist = std::distance(ranking.cbegin(), ranking.cend());
69  if (cdist != 4) {
70    suite.add(false);
71    suite.err() << "error: incorrect const distance: " << cdist << "\n";
72  }
73  int rdist = std::distance(ranking.rbegin(), ranking.rend());
74  if (rdist != 4) {
75    suite.add(false);
76    suite.err() << "error: incorrect reverse distance: " << rdist << "\n";
77  }
78  int crdist = std::distance(ranking.crbegin(), ranking.crend());
79  if (crdist != 4) {
80    suite.add(false);
81    suite.err() << "error: incorrect const reverse distance: "
82                << crdist << "\n";
83  }
84
85  // try to find lower bound for a large value
86  utility::Ranking<double>::iterator lower = ranking.lower_bound(10000);
87  if (lower != ranking.end()) {
88    suite.add(false);
89    suite.err() << "ranking.lower_bound(10000) expected ranking.end()\n";
90  }
91  lower = ranking.lower_bound(1);
92  if (*lower != 1) {
93    suite.add(false);
94    suite.err() << "error: *lower returned: " << *lower << "\n";
95  }
96
97  // try to find upper bound for a large value
98  utility::Ranking<double>::iterator upper = ranking.upper_bound(10000);
99  if (upper != ranking.end()) {
100    suite.add(false);
101    suite.err() << "ranking.upper_bound(10000) expected ranking.end()\n";
102  }
103  upper = ranking.upper_bound(1);
104  if (*upper != 2) {
105    suite.add(false);
106    suite.err() << "error: *upper returned: " << *upper << "\n";
107  }
108
109  dist = std::distance(lower, upper);
110  if (dist != 2) {
111    suite.add(false);
112    suite.err() << "error: distance(lower, upper): " << dist << "\n";
113  }
114
115  std::vector<int> vec(10);
116  ranking.insert(vec.begin(), vec.end());
117}
118
119
120void test2(test::Suite& suite)
121{
122  suite.out() << "=== " << __func__ << " ===\n";
123  // mimick how Ranking is used in Kendall::score
124  utility::Ranking<double> ranking;
125  auto lower = ranking.lower_bound(2);
126  int r = ranking.ranking(lower);
127  suite.out() << "ranking: " << r << "\n";
128  if (r != 0) {
129    suite.add(false);
130    suite.err() << "error: incorrect; expected 0\n";
131  }
132  ranking.insert(lower, 2);
133
134  lower = ranking.lower_bound(1);
135  r = ranking.ranking(lower);
136  suite.out() << "ranking: " << r << "\n";
137  if (r != 0) {
138    suite.add(false);
139    suite.err() << "error: incorrect; expected 0\n";
140  }
141  ranking.insert(lower, 1);
142
143  lower = ranking.lower_bound(3);
144  if (lower != ranking.cend()) {
145    suite.add(false);
146    suite.err() << "error: expected ranking.lower_bound(3) to return end()\n";
147  }
148  suite.out() << "values in ranking object:\n";
149  for (auto it=ranking.cbegin(); it!=ranking.cend(); ++it)
150    suite.out() << "value: " << *it << "\n";
151  size_t n = ranking.size();
152  if (n != 2) {
153    suite.add(false);
154    suite.err() << "::size() returns " << n << "; expected 2\n";
155  }
156  n = std::distance(ranking.cbegin(), ranking.cend());
157  if (n!=2) {
158    suite.add(false);
159    suite.err() << "distance(begin, end) returns " << n << "; expected 2\n";
160  }
161
162  r = ranking.ranking(lower);
163  suite.out() << "ranking: " << r << "\n";
164  if (r != 2) {
165    suite.add(false);
166    suite.err() << "error: incorrect; expected 2\n";
167  }
168}
169
170
171int main(int argc, char* argv[])
172{
173  test::Suite suite(argc, argv);
174  try {
175    test1(suite);
176    test2(suite);
177  }
178  catch (std::exception& e) {
179    suite.add(false);
180    suite.err() << "error: exception caught: what(): ";
181    utility::print_what(e, suite.err());
182  }
183  return suite.return_value();
184}
Note: See TracBrowser for help on using the repository browser.