1 | // $Id: target_test.cc 865 2007-09-10 19:41:04Z 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/trac/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/classifier/Target.h" |
---|
25 | |
---|
26 | #include <fstream> |
---|
27 | #include <iostream> |
---|
28 | #include <string> |
---|
29 | #include <vector> |
---|
30 | |
---|
31 | using namespace theplu::yat; |
---|
32 | |
---|
33 | int main(const int argc,const char* argv[]) |
---|
34 | { |
---|
35 | std::ostream* error; |
---|
36 | if (argc>1 && argv[1]==std::string("-v")) |
---|
37 | error = &std::cerr; |
---|
38 | else { |
---|
39 | error = new std::ofstream("/dev/null"); |
---|
40 | if (argc>1) |
---|
41 | std::cout << "target_test -v : for printing extra information\n"; |
---|
42 | } |
---|
43 | *error << "testing target" << std::endl; |
---|
44 | bool ok = true; |
---|
45 | |
---|
46 | // This test assumes classes are introduced in order of appearence |
---|
47 | std::vector<std::string> color(10,"default"); |
---|
48 | color[2]=color[7]="white"; |
---|
49 | color[4]=color[5]="black"; |
---|
50 | color[6]=color[3]="green"; |
---|
51 | color[8]=color[9]="red"; |
---|
52 | classifier::Target target3(color); |
---|
53 | std::vector<size_t> facit(10); |
---|
54 | facit[2]=facit[7]=1; |
---|
55 | facit[4]=facit[5]=3; |
---|
56 | facit[6]=facit[3]=2; |
---|
57 | facit[8]=facit[9]=4; |
---|
58 | |
---|
59 | for (size_t i=0; i<facit.size(); i++){ |
---|
60 | if (target3(i)!=facit[i]){ |
---|
61 | ok=false; |
---|
62 | *error << "target(" << i << ") is " << target3(0) << " expected " |
---|
63 | << facit[i] << std::endl; |
---|
64 | } |
---|
65 | } |
---|
66 | std::vector<size_t> sub; |
---|
67 | sub.push_back(2); |
---|
68 | sub.push_back(1); |
---|
69 | classifier::Target sub_target(target3,sub); |
---|
70 | if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){ |
---|
71 | ok = false; |
---|
72 | *error << "error in constructor Target(const Target&, std::vector<size_t>&)" |
---|
73 | << "\nclasses are not preserved" << std::endl; |
---|
74 | } |
---|
75 | if (target3.binary(sub[0])!=sub_target.binary(0) || |
---|
76 | target3.binary(sub[1])!=sub_target.binary(1)){ |
---|
77 | ok = false; |
---|
78 | *error << "error in constructor Target(const Target&, std::vector<size_t>&)" |
---|
79 | << "\nbinary classes are not preserved" << std::endl; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | std::vector<std::string> label(31,"negative"); |
---|
84 | for (size_t i=0; i<16; i++) |
---|
85 | label[i] = "positive"; |
---|
86 | label[20]="neither"; |
---|
87 | classifier::Target target(label); |
---|
88 | if (target.nof_classes()!=3){ |
---|
89 | ok=false; |
---|
90 | *error << "Expected number of classes to be 3" << std::endl; |
---|
91 | } |
---|
92 | if (target(0)!=0){ |
---|
93 | ok=false; |
---|
94 | *error << "Error: target(0)!=0" << std::endl; |
---|
95 | } |
---|
96 | if (target(20)!=2){ |
---|
97 | ok=false; |
---|
98 | *error << "Error: target(20)!=2" << std::endl; |
---|
99 | } |
---|
100 | if (!target.binary(0)){ |
---|
101 | ok=false; |
---|
102 | *error << "Error: target.binary(0) not true" << std::endl; |
---|
103 | } |
---|
104 | if (target.binary(20)){ |
---|
105 | ok=false; |
---|
106 | *error << "Error: target.binary(20) not false" << std::endl; |
---|
107 | } |
---|
108 | target.set_binary(2,true); |
---|
109 | if (!target.binary(20)){ |
---|
110 | ok=false; |
---|
111 | *error << "Error: target.binary(20) not true" << std::endl; |
---|
112 | } |
---|
113 | std::ifstream is("data/rank_target.txt"); |
---|
114 | classifier::Target target2(is); |
---|
115 | is.close(); |
---|
116 | if (target2.size()!=6){ |
---|
117 | ok=false; |
---|
118 | *error << "ERROR: size of target is " << target2.size() << " expected 6." |
---|
119 | << std::endl; |
---|
120 | } |
---|
121 | |
---|
122 | if (ok) |
---|
123 | return 0; |
---|
124 | return -1; |
---|
125 | } |
---|