source: trunk/test/target_test.cc @ 521

Last change on this file since 521 was 521, checked in by Peter, 16 years ago

extended target and ensemble tests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: target_test.cc 521 2006-02-22 15:41:55Z peter $
2
3#include <c++_tools/classifier/Target.h>
4
5#include <fstream>
6#include <iostream>
7#include <string>
8#include <vector>
9
10using namespace theplu;
11
12int main(const int argc,const char* argv[])
13{ 
14  std::ostream* error;
15  if (argc>1 && argv[1]==std::string("-v"))
16    error = &std::cerr;
17  else {
18    error = new std::ofstream("/dev/null");
19    if (argc>1)
20      std::cout << "target_test -v : for printing extra information\n";
21  }
22  *error << "testing target" << std::endl;
23  bool ok = true;
24
25  // This test assumes classes are introduced in order of appearence
26  std::vector<std::string> color(10,"default");
27  color[2]=color[7]="white";
28  color[4]=color[5]="black";
29  color[6]=color[3]="green";
30  color[8]=color[9]="red";
31  classifier::Target target3(color);
32  std::vector<size_t> facit(10);
33  facit[2]=facit[7]=1;
34  facit[4]=facit[5]=3;
35  facit[6]=facit[3]=2;
36  facit[8]=facit[9]=4;
37 
38  for (size_t i=0; i<facit.size(); i++){
39    if (target3(i)!=facit[i]){
40      ok=false;
41      *error << "target(" << i << ") is " << target3(0) << " expected " 
42             << facit[i] << std::endl;
43    }
44  }
45  std::vector<size_t> sub;
46  sub.push_back(2);
47  sub.push_back(1);
48  classifier::Target sub_target(target3,sub);
49  if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){
50    ok = false;
51    *error << "error in constructor Target(const Target&, std::vector<size_t>&)"
52           << "\nclasses are not preserved" << std::endl;
53  }
54  if (target3.binary(sub[0])!=sub_target.binary(0) || 
55      target3.binary(sub[1])!=sub_target.binary(1)){
56    ok = false;
57    *error << "error in constructor Target(const Target&, std::vector<size_t>&)"
58           << "\nbinary classes are not preserved" << std::endl;
59  }
60
61
62  std::vector<std::string> label(31,"negative");
63  for (size_t i=0; i<16; i++) 
64    label[i] = "positive";
65  label[20]="neither";
66  classifier::Target target(label);
67  if (target.nof_classes()!=3){
68    ok=false;
69    *error << "Expected number of classes to be 3" << std::endl;
70  }
71  if (target(0)!=0){
72    ok=false;
73    *error << "Error: target(0)!=0" << std::endl;
74  }
75  if (target(20)!=2){
76    ok=false;
77    *error << "Error: target(20)!=2" << std::endl;
78  }
79  if (!target.binary(0)){
80    ok=false;
81    *error << "Error: target.binary(0) not true" << std::endl;
82  }   
83  if (target.binary(20)){
84    ok=false;
85    *error << "Error: target.binary(20) not false" << std::endl;
86  }   
87  target.set_binary(2,true);
88  if (!target.binary(20)){
89    ok=false;
90    *error << "Error: target.binary(20) not true" << std::endl;
91  }   
92  std::ifstream is("data/rank_target.txt");
93  classifier::Target target2(is);
94  is.close();
95  if (target2.size()!=6){
96    ok=false;
97    *error << "ERROR: size of target is " << target2.size() << " expected 6." 
98           << std::endl;
99  }   
100
101  if (ok)
102    return 0;
103  return -1;
104}
Note: See TracBrowser for help on using the repository browser.