source: trunk/test/matrix_lookup_weighted_test.cc @ 1619

Last change on this file since 1619 was 1587, checked in by Peter, 13 years ago

closes #396

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: matrix_lookup_weighted_test.cc 1587 2008-10-17 15:31:42Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/utility/MatrixWeighted.h"
26#include "yat/classifier/MatrixLookupWeighted.h"
27
28#include <fstream>
29#include <iostream>
30#include <vector>
31
32using namespace theplu::yat;
33
34utility::MatrixWeighted matrix_weighted(size_t n);
35bool compare(const classifier::MatrixLookupWeighted&,
36             const utility::MatrixWeighted,
37             const utility::Index& row_index,
38             const utility::Index& col_index,
39             test::Suite& suite);
40
41int main(int argc, char* argv[])
42{
43  using namespace theplu::yat::classifier;
44
45  test::Suite suite(argc, argv);
46
47  suite.err() << "\nTesting MatrixLookupWeighted" << std::endl;
48  suite.err() << "  Testing\n" 
49              << "  MatrixLookupWeighted(const utility::MatrixWeighted&,\n"
50              << "                       const utility::Index& rows,\n"
51              << "                       const utility::Index& columns); "
52              << "...";
53  utility::MatrixWeighted mw = matrix_weighted(10);
54  std::vector<size_t> index;
55  index.push_back(1);
56  index.push_back(5);
57  index.push_back(3);
58  utility::Index row_index(5);
59  utility::Index column_index(index);
60  MatrixLookupWeighted mlw(mw, row_index, column_index);
61  if (compare(mlw, mw, row_index, column_index, suite))
62    suite.err() << "ok.\n";
63  else
64    suite.err() << "failed.\n";
65
66
67  suite.err() << "  Testing\n" 
68              << "  MatrixLookupWeighted(const utility::MatrixWeighted&); "
69              << "...";
70  MatrixLookupWeighted mlw2(mw);
71  if (compare(mlw2, mw, utility::Index(mw.rows()), utility::Index(mw.columns()), 
72              suite))
73    suite.err() << "ok.\n";
74  else
75    suite.err() << "failed.\n";
76
77  suite.err() << "  Testing\n" 
78              << "  MatrixLookupWeighted(const MatrixLookupWeighted&); "
79              << "...";
80  MatrixLookupWeighted mlw3(mlw);
81  if (compare(mlw3, mw, row_index, column_index, suite))
82    suite.err() << "ok.\n";
83  else
84    suite.err() << "failed.\n";
85
86  return suite.return_value();
87}
88
89bool compare(const classifier::MatrixLookupWeighted& mlw,
90             const utility::MatrixWeighted mw,
91             const utility::Index& row_index,
92             const utility::Index& column_index,
93             test::Suite& suite)
94{
95  bool ok=true;
96  ok &= (mlw.rows() == row_index.size());
97  ok &= (mlw.columns() == column_index.size());
98  for (size_t i=0; i<mlw.rows() && ok; ++i) {
99    for (size_t j=0; j<mlw.columns(); ++j) {
100      ok &= (mlw(i,j) == mw(row_index[i], column_index[j]));
101    }
102  }
103  suite.add(ok);
104  return ok;
105}
106
107
108utility::MatrixWeighted matrix_weighted(size_t n)
109{
110  utility::MatrixWeighted res(n,n);
111  for (size_t i=0;i<n;i++)
112    for (size_t j=0;j<n;j++)
113      res(i,j).data()=10*i+j;
114  return res;
115}
116
117
Note: See TracBrowser for help on using the repository browser.