source: trunk/test/index_test.cc @ 1106

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

Fixes #316 - Index class

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: index_test.cc 1106 2008-02-19 04:28:42Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/utility/Index.h"
25#include "yat/utility/SmartPtr.h"
26
27#include <fstream>
28#include <iostream>
29#include <vector>
30
31int main(const int argc,const char* argv[])
32{ 
33  using namespace theplu::yat;
34  using utility::Index;
35
36  std::ostream* error;
37  if (argc>1 && argv[1]==std::string("-v"))
38    error = &std::cerr;
39  else {
40    error = new std::ofstream("/dev/null");
41    if (argc>1)
42      std::cout << "index_test -v : for printing extra information\n";
43  }
44  bool ok = true;
45  *error << "testing Index" << std::endl;
46
47  Index index;
48  if (index.size()){
49    ok = false;
50    *error << "Default constructor failed\n";
51  }
52
53  Index index2(10);
54  if (index2.size()!=10){
55    ok = false;
56    *error << "Constructor Index(10) failed\n";
57  }
58  for (size_t i=0; i<index2.size(); ++i)
59    if (index2[i]!=i){
60      ok = false;
61      *error << "index[" << i << "]=" << index2[i] << " expected " << i
62             << "\n";
63    }
64
65  std::vector<size_t> vec;
66  vec.push_back(1);
67  vec.push_back(3);
68  vec.push_back(5);
69  utility::SmartPtr<const std::vector<size_t> > 
70    sp(new const std::vector<size_t>(vec));
71  Index index3(sp);
72  if (index3.size()!=vec.size()){
73    ok = false;
74    *error << "Constructor from SmartPtr failed\n";
75  }
76  for (size_t i=0; i<index3.size(); ++i)
77    if (index3[i]!=vec[i]){
78      ok = false;
79      *error << "index[" << i << "]=" << index3[i] << " expected " << vec[i] 
80             << "\n";
81    }
82
83  *error << "  testing creation of sub index\n";
84  Index index4(index2, index3);
85  if (index4.size()!=index3.size()){
86    ok = false;
87    *error << "failed\n";
88  }
89  for (size_t i=0; i<index4.size(); ++i)
90    if (index4[i]!=index2[index3[i]]){
91      ok = false;
92      *error << "index[" << i << "]=" << index4[i] << " expected " 
93             << index2[index3[i]] << "\n";
94    }
95
96  Index index5(index4);
97  if (index5.size()!=index4.size()){
98    ok = false;
99    *error << "failed\n";
100  }
101  for (size_t i=0; i<index5.size(); ++i)
102    if (index5[i]!=index4[i]){
103      ok = false;
104      *error << "index[" << i << "]=" << index5[i] << " expected " 
105             << index4[i] << "\n";
106    }
107   
108
109  if (ok)
110    *error << "Ok\n";
111  else
112    *error << "Test failed\n";
113
114  if (error!=&std::cerr)
115    delete error;
116
117  if (ok)
118    return 0;
119  return -1;
120}
Note: See TracBrowser for help on using the repository browser.