FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_accuracy.cc
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2014-2015, Siemens AG
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 
24 #include <iostream>
25 #include <fstream>
26 #include <sstream>
27 #include <algorithm>
28 #include <string>
29 
30 #include "copyscan.hpp"
31 #include "regTypes.hpp"
32 #include "regscan.hpp"
33 
35 #define NUMTESTS 142
36 
37 using namespace std;
38 
43 class TestDataCheck : public CPPUNIT_NS::TestFixture
44 {
45  CPPUNIT_TEST_SUITE(TestDataCheck);
46  CPPUNIT_TEST(testDataCheck);
47  CPPUNIT_TEST_SUITE_END();
48 protected:
49  void testDataCheck();
50 } ;
51 
57 void HtmlEscapedOutput(ostream& out, const char* s)
58 {
59  char c = *s;
60  while (c)
61  {
62  switch (c)
63  {
64  case '<': out << "&lt;"; break;
65  case '>': out << "&gt;"; break;
66  case '&': out << "&amp;"; break;
67  default: out << (isprint(c) ? c : ' ');
68  }
69  s++;
70  c = *s;
71  }
72 }
73 
74 void GetReferenceResults(const string& fileName, list<match>& results)
75 {
76  ifstream t(fileName);
77  stringstream tstream;
78  tstream << t.rdbuf();
79  string s = tstream.str();
80  string::size_type pos = 0;
81  string::size_type cpos = 0;
82  for (;;)
83  {
84  string::size_type tpos = s.find("<s>", pos);
85  if (tpos == string::npos) break;
86  cpos += tpos - pos;
87  int start = cpos;
88  tpos += 3;
89  pos = s.find("</s>", tpos);
90  if (pos == string::npos) break;
91  cpos += pos - tpos;
92  pos += 4;
93  results.push_back(match(start, cpos, "r"));
94  }
95 }
96 
102  // Criterion if match m2 is sufficiently similar to a given match m
103  const match& m;
104 public:
105  overlappingMatch(const match& mm) : m(mm) { }
106  bool operator()(const match& m2) const
107  {
108  return !(m.end <= m2.start || m2.end <= m.start);
109  }
110 } ;
111 
115 void Display(ostream& out, ifstream& data, list<match>& l, list<match>& lcmp, const char*prein, const char*postin, const char*prenn, const char*postnn)
116 {
117  // Print results
118  data.clear();
119  for (match& m : l)
120  {
121  // Find in lcmp
122  bool in = find_if(lcmp.begin(), lcmp.end(), overlappingMatch(m)) != lcmp.end();
123  // Print match
124  int len = m.end - m.start;
125  char* str = new char[len+1];
126  data.seekg(m.start);
127  data.read(str,len);
128  str[len]=0;
129  out << "<p><em>[" << m.start << ":" << m.end << "]</em>" << (in ? prein : prenn);
130  HtmlEscapedOutput(out, str);
131  delete[] str;
132  out << (in ? postin : postnn) << "</p>" << endl;
133  }
134 }
135 
142 bool cmpMatches(const match &a, const match &b)
143 {
144  if (a.start < b.start)
145  return true;
146  if (a.start == b.start && a.end < b.end)
147  return true;
148  return false;
149 }
150 
160 {
161  // Test all instances
162  string fileNameBase = "../testdata/testdata";
163  // Create a copyright scanner and an author scanner
164  hCopyrightScanner hsc;
165  regexScanner hauth(regAuthor::getRegex(), regAuthor::getType());
166 
167  ofstream out("results.html");
168 
169  out << "<html><head><style type=\"text/css\">"
170  "body { font-family: sans-serif; } h1 { font-size: 14pt; } h2 { font-size: 10pt; } p { font-size: 10pt; } .falsepos { background-color: #FFC080; } .falseneg { background-color: #FF8080; }"
171  "</style></head><body>" << endl;
172 
173  // Scan files
174  for (int i = 0; i < NUMTESTS; i++)
175  {
176  string fileName = fileNameBase + to_string(i);
177  ifstream tstream(fileName);
178  list<match> lng, lauth, lrefs;
179  hsc.ScanFile(fileName, lng);
180  hauth.ScanFile(fileName, lauth);
181  // Merge lists lng and lauth
182  lng.merge(lauth, cmpMatches);
183  GetReferenceResults(fileName + "_raw", lrefs);
184 
185  out << "<h1>testdata" << i << "</h1>" << endl;
186  out << "<h2>HScanner</h2>" << endl;
187  Display(out, tstream, lng, lrefs, "<code>", "</code>", "<code class=\"falsepos\">", "</code>");
188  out << "<h2>Reference</h2>" << endl;
189  Display(out, tstream, lrefs, lng, "<code>", "</code>", "<code class=\"falseneg\">", "</code>");
190  }
191  out << "</body></html>" << endl;
192  cout << endl << "----- Test results written to results.html -----" << endl;
193 }
194 
195 CPPUNIT_TEST_SUITE_REGISTRATION( TestDataCheck );
void HtmlEscapedOutput(ostream &out, const char *s)
Escape HTML special characters.
const int start
Definition: scanners.hpp:46
Store the results of a regex match.
Definition: scanners.hpp:39
Unit test driver.
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>. The application passed in should match the script name in /etc/init.d
Definition: pkgConfig.php:1225
virtual void ScanFile(const string &fileName, list< match > &results) const
Helper function to scan file.
Definition: scanners.hpp:81
Provides a regex scanner using predefined regexs.
Definition: regscan.hpp:31
Helper to check overlapping results.
void testDataCheck()
Test agent on every file in ../testdata/ folder.
const int end
Definition: scanners.hpp:46
bool cmpMatches(const match &a, const match &b)
Compare matches.
void Display(ostream &out, ifstream &data, list< match > &l, list< match > &lcmp, const char *prein, const char *postin, const char *prenn, const char *postnn)
Print results to out.
Implementation of scanner class for copyright.
Definition: copyscan.hpp:29
#define NUMTESTS