Statistics
| Branch: | Revision:

root / test / pagerank_test.cpp @ 21:c131219e7a08

History | View | Annotate | Download (3.6 kB)

1
#include <string>
2
#include <iostream>
3
#include <fstream>
4
#include <vector>
5
#include <cmath>
6
#include <cstdlib>
7
#include <cstring>
8

    
9
#include <errno.h>
10
#include <dirent.h>
11

    
12
#include "table.h"
13

    
14
using namespace std;
15

    
16
const double EPSILON = 0.0001;
17

    
18
void error(const char *p,const char *p2) {
19
    cerr << p <<  ' ' << p2 <<  '\n';
20
    exit(1);
21
}
22

    
23
void error(const string p,const string p2) {
24
    cerr << p <<  ' ' << p2 <<  '\n';
25
    exit(1);
26
}
27

    
28
int main(int argc, char *argv[]) {
29

    
30
    Table t;
31
    bool numeric = false;
32

    
33
    if (argc != 2 && argc != 3) {
34
        cerr << "Usage: pagerank_test [-n] <test_suite>" << endl;
35
        exit(1);
36
    }
37
    
38
    if (argc == 3) {
39
        if (!strcmp(argv[1], "-n")) {
40
            numeric = true;
41
        } else {
42
            cerr << "Usage: pagerank_test [-n] <test_suite>" << endl;
43
            exit(1);
44
        }
45
    }
46

    
47
    string tests_filename = argv[argc - 1];
48

    
49
    ifstream tests_file(tests_filename.c_str());
50

    
51
    if (!tests_file.is_open()) {
52
        error("Cannot open file", tests_filename);
53
    }
54

    
55
    while (!tests_file.eof()) {
56
        string test_line;
57
        getline(tests_file, test_line);
58
        if (test_line.empty()) {
59
            continue;
60
        }
61
        cout << "testing " << test_line << "... ";
62
        /* Get graph file and pagerank file names */
63
        string graph_filename = test_line + ".txt";
64
        string pagerank_filename = test_line + "-pr.txt";
65
        /* Open pagerank file */
66
        ifstream pagerank_file(pagerank_filename.c_str());
67
        if (!pagerank_file.is_open()) {
68
            cerr << endl;
69
            error("Cannot open pagerank file", pagerank_filename);
70
        }
71

    
72
        /* Read graph file */
73
        t.set_numeric(numeric);
74
        t.set_delim(" ");
75
        t.set_trace(false);
76
        t.read_file(graph_filename);
77
        /* Calculate pagerank */
78
        t.pagerank();
79

    
80
        /* Read pagerank test results file */
81
        vector<double> pagerank_test_values;
82
        while (!pagerank_file.eof()) {
83
            string pagerank_line;
84
            getline(pagerank_file, pagerank_line);
85
            if (pagerank_line.find("s = ") != string::npos) {
86
                break;
87
            }
88
            string::size_type sep = pagerank_line.find(" = ");
89
            if (sep == string::npos) {
90
                    break;
91
            }
92
            string index_str = pagerank_line.substr(0, sep);
93
            size_t index = strtol(index_str.c_str(), NULL, 10);
94
            string value_str = pagerank_line.substr(sep + 3);
95
            double value;
96
            value = strtod(value_str.c_str(), NULL);
97
            if (index >= pagerank_test_values.size()) {
98
                pagerank_test_values.resize(index + 1);
99
            }
100
            pagerank_test_values[index] = value;
101
        }
102
        pagerank_file.close();
103

    
104
        /* Compare test results with calculated results */
105
        vector<double> pagerank_results = t.get_pagerank();
106

    
107
        bool test_ok = true;
108
        for (unsigned int i = 0; i < pagerank_results.size(); i++) {
109
            double result = pagerank_results[i];
110
            string name = t.get_node_name(i);
111
            size_t test_index = strtol(name.c_str(), NULL, 10);
112
            double test_result = pagerank_test_values[test_index];
113
            double diff = abs(result - test_result);
114
            if (diff > EPSILON) {
115
                cout << " error in calculation for " << name << ": "
116
                     << "result=" << result << " "
117
                     << "expected=" << test_result << " "
118
                     << "diff=" << diff << " ";
119
                test_ok = false;
120
                break;
121
            }
122
        }
123
        if (test_ok) {
124
            cout << "OK" << endl;
125
        } else {
126
            cout << "Failed" << endl;
127
        }
128
    }
129
    
130
}