source: trunk/test/target_test.cc @ 1437

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

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: target_test.cc 1437 2008-08-25 17:55:00Z 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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Suite.h"
26
27#include "yat/classifier/Target.h"
28#include "yat/utility/Index.h"
29
30#include <fstream>
31#include <iostream>
32#include <string>
33#include <vector>
34
35using namespace theplu::yat;
36
37int main( int argc, char* argv[])
38{ 
39  test::Suite suite(argc, argv);
40  suite.err() << "testing target" << std::endl;
41
42  // This test assumes classes are introduced in order of appearence
43  std::vector<std::string> color(10,"default");
44  color[2]=color[7]="white";
45  color[4]=color[5]="black";
46  color[6]=color[3]="green";
47  color[8]=color[9]="red";
48  classifier::Target target3(color);
49  std::vector<size_t> facit(10);
50  facit[2]=facit[7]=1;
51  facit[4]=facit[5]=3;
52  facit[6]=facit[3]=2;
53  facit[8]=facit[9]=4;
54 
55  for (size_t i=0; i<facit.size(); i++){
56    if (target3(i)!=facit[i]){
57      suite.add(false);
58      suite.err() << "target(" << i << ") is " << target3(0) << " expected " 
59             << facit[i] << std::endl;
60    }
61  }
62  std::vector<size_t> sub;
63  sub.push_back(2);
64  sub.push_back(1);
65  classifier::Target sub_target(target3,utility::Index(sub));
66  if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){
67    suite.add(false);
68    suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)"
69           << "\nclasses are not preserved" << std::endl;
70  }
71  if (target3.binary(sub[0])!=sub_target.binary(0) || 
72      target3.binary(sub[1])!=sub_target.binary(1)){
73    suite.add(false);
74    suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)"
75           << "\nbinary classes are not preserved" << std::endl;
76  }
77
78
79  std::vector<std::string> label(31,"negative");
80  for (size_t i=0; i<16; i++) 
81    label[i] = "positive";
82  label[20]="neither";
83  classifier::Target target(label);
84  if (target.nof_classes()!=3){
85    suite.add(false);
86    suite.err() << "Expected number of classes to be 3" << std::endl;
87  }
88  if (target(0)!=0){
89    suite.add(false);
90    suite.err() << "Error: target(0)!=0" << std::endl;
91  }
92  if (target(20)!=2){
93    suite.add(false);
94    suite.err() << "Error: target(20)!=2" << std::endl;
95  }
96  if (!target.binary(0)){
97    suite.add(false);
98    suite.err() << "Error: target.binary(0) not true" << std::endl;
99  }   
100  if (target.binary(20)){
101    suite.add(false);
102    suite.err() << "Error: target.binary(20) not false" << std::endl;
103  }   
104  target.set_binary(2,true);
105  if (!target.binary(20)){
106    suite.add(false);
107    suite.err() << "Error: target.binary(20) not true" << std::endl;
108  }   
109  std::ifstream is(test::filename("data/rank_target.txt").c_str());
110  classifier::Target target2(is);
111  is.close();
112  if (target2.size()!=6){
113    suite.add(false);
114    suite.err() << "ERROR: size of target is " << target2.size() 
115                << " expected 6."  << std::endl;
116  }   
117
118  suite.return_value();
119}
Note: See TracBrowser for help on using the repository browser.