File: test.C

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (175 lines) | stat: -rw-r--r-- 4,743 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Prefs.h"
#include "RangeList.h"
#include "stringutil.h"
#include <iostream>
#include <vector> 
#include <map>
#include <string>

using namespace std; 

//====================================================
int TestKey(Preferences &P, string key, string value){
  if (P.GetValue(key) != value){
    cerr << "Tests failed because P.GetValue("<<key<<") returned \"" <<P.GetValue(key)<<"\" instead of the expected value \""<<value<<"\""<<endl;
    return 4; 
  }
  return 0;
}

//====================================================
int TestPrefs(Preferences &P) {
  
  cerr << "save to file..."<<endl;
  string label = P.GetLabel();
  string key1("key1"), key2("key2");
  string value1(P.GetValue(key1)), value2(P.GetValue(key2));    
  string filename = P.GetFilename();
  try {
    P.SaveToFile();
  } 
  catch (string err){
    cerr << err <<endl;
    return 1;
  }
  cerr << "reset..."<<endl;
  P.Reset();
  cerr << "ReadFromFile..."<<endl;
  P.SetFile(filename);
  P.SetLabel(label);
  try {
    P.ReadFromFile();
  } 
  catch (string err){
    cerr << err <<endl;
    return 2;
  }
  int err = TestKey(P, key1, value1);
  if (!err)
    err = TestKey(P, key2, value2);
  return err; 
}

//====================================================
void TestPrefs(void){
  int err = 0; 
  try {
    string key1("key1"), key2("key2");
    
    //test prefrerences
    Preferences P("testprefs.txt");  
    P.SetLabel("This is the label");
    P.SetValue(key1, "Blah");
    P.SetValue(key2, "Lots of words");
    err = TestPrefs(P);
    if (!err){
      cout << "Round 2..." << endl;
      P.Reset();
      P.SetFile("testprefs.txt");
      P.SetLabel("New label");
      P.SetValue(key1, "Blah2");
      P.SetValue(key2, "True love lasts forever");
      err = TestPrefs(P);
    }
    P.SetLabel("new label");
    
    cout << "Testing RangeList" << endl; 
    RangeList rlist("testrange.txt", 1);
    rlist.Sort();
    rlist.Unique();
    cout << "unsorted:" << endl; 
    rlist.Print();
    cout << "sorted: " << endl; 
    rlist.Print();

    if (!rlist.InRange(43))
      throw string("43 should be in range but isn't!");
    if (rlist.InRange(7))
      throw string("7 is not supposed to be in range!");

    cout << " testing environment capture... " << endl; 
    P.ReadFromEnvironment(); 
    std::map<std::string, std::string> thePrefs = P.GetPrefsMap(); 
    std::map<std::string, std::string>::iterator currentpos = thePrefs.begin(), endpos = thePrefs.end(); 
    while (currentpos != endpos) {
      cout << currentpos->first << ": " << currentpos->second << endl; 
      ++currentpos; 
    }
  }
  catch (string errs) {
    cerr << "Error while testing: " << endl;
    cerr << errs << endl; 
    err = 1;
  }

  if (!err) 
    cout << "Tests succeeded" << endl;   
  else
    cout << "Test failed." << endl;
  
  return ; 

}


//====================================================
void RunTest(int testnum, vector<string> &tests, vector<string> &expectedResults) {
  string resultString; 
  vector<string> result = Split(tests[testnum]); 
  vector<string>::iterator pos = result.begin(), endpos = result.end(); 
  while (pos != endpos) {
    resultString = "("; 
    vector<string>::iterator respos = result.begin(), resendpos = result.end(); 
    while (respos != resendpos) {
      if (respos != result.begin()) {
        resultString += ", "; 
      }
      resultString += *respos; 
      respos++;
    }
    resultString += ")"; 
    if (resultString == expectedResults[testnum]) {
      cerr << "SUCCESS: "; 
    } else {
      cerr << "FAILED: "; 
    }
    cerr << "result of test: expected "<<expectedResults[testnum] << " and got " <<resultString << endl; 
    ++pos;
  }
}
  
//====================================================
void TestStrings(void) {

  //-------------------
#define ADDTEST(test, expected) \
  tests.push_back(test); \
  expectedResults.push_back(expected) 
  //-------------------

  vector<string> tests, expectedResults; 
  ADDTEST("", "()"); 
  ADDTEST(" ", "()"); 
  ADDTEST(" test", "(test)"); 
  ADDTEST("test ", "(test)"); 
  ADDTEST("test1 test2", "(test1, test2)"); 
  ADDTEST(" test1 test2", "(test1, test2)"); 
  ADDTEST(" test1 test2 ", "(test1, test2)"); 
  ADDTEST("test1 test2 ", "(test1, test2)"); 
  ADDTEST("test1  test2 ", "(test1, test2)"); 
  ADDTEST("test1  test2 test3", "(test1, test2, test3)"); 
  ADDTEST("   ", "()"); 
  vector<string>::iterator pos = tests.begin(), endpos = tests.end(); 
  unsigned int testnum = 0; 
  while (testnum < tests.size()) {
    RunTest(testnum, tests, expectedResults); 
    ++testnum; 
  }
  return; 
}

//====================================================
int main(int argc, char *argv[]) {
  TestPrefs(); 
  TestStrings(); 
}