source: trunk/test/target_test.cc @ 509

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

added test for target
redesign crossSplitter
added two class function in Target

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1// $Id: target_test.cc 509 2006-02-18 13:47:32Z 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<std::string> label(31,"negative");
46  for (size_t i=0; i<16; i++) 
47    label[i] = "positive";
48  label[20]="neither";
49  classifier::Target target(label);
50  if (target.nof_classes()!=3){
51    ok=false;
52    *error << "Expected number of classes to be 3" << std::endl;
53  }
54  if (target(0)!=0){
55    ok=false;
56    *error << "Error: target(0)!=0" << std::endl;
57  }
58  if (target(20)!=2){
59    ok=false;
60    *error << "Error: target(20)!=2" << std::endl;
61  }
62  if (!target.one(0)){
63    ok=false;
64    *error << "Error: target.one(0) not true" << std::endl;
65  }   
66  if (target.one(20)){
67    ok=false;
68    *error << "Error: target.one(20) not false" << std::endl;
69  }   
70  target.set_one(2);
71  if (!target.one(20)){
72    ok=false;
73    *error << "Error: target.one(20) not true" << std::endl;
74  }   
75  std::ifstream is("data/rank_target.txt");
76  classifier::Target target2(is);
77  is.close();
78  if (target2.size()!=6){
79    ok=false;
80    *error << "ERROR: size of target is " << target2.size() << " expected 6." 
81           << std::endl;
82  }   
83
84  if (ok)
85    return 0;
86  return -1;
87}
Note: See TracBrowser for help on using the repository browser.