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