source: trunk/test/matrix_lookup_weighted_test.cc @ 1797

Last change on this file since 1797 was 1797, checked in by Peter, 14 years ago

updating copyright statements

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