source: trunk/test/target_test.cc @ 1619

Last change on this file since 1619 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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