source: trunk/test/target_test.cc @ 1251

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

make distcheck works - fixes ticket:217

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