FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_accuracy.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
12 
13 #include <iostream>
14 #include <fstream>
15 #include <sstream>
16 #include <algorithm>
17 #include <string>
18 
19 #include "copyscan.hpp"
20 #include "regTypes.hpp"
21 #include "regscan.hpp"
22 
24 #define NUMTESTS 142
25 
26 using namespace std;
27 
32 class TestDataCheck : public CPPUNIT_NS::TestFixture
33 {
34  CPPUNIT_TEST_SUITE(TestDataCheck);
35  CPPUNIT_TEST(testDataCheck);
36  CPPUNIT_TEST_SUITE_END();
37 protected:
38  void testDataCheck();
39 } ;
40 
46 void HtmlEscapedOutput(ostream& out, const char* s)
47 {
48  char c = *s;
49  while (c)
50  {
51  switch (c)
52  {
53  case '<': out << "&lt;"; break;
54  case '>': out << "&gt;"; break;
55  case '&': out << "&amp;"; break;
56  default: out << (isprint(c) ? c : ' ');
57  }
58  s++;
59  c = *s;
60  }
61 }
62 
63 void GetReferenceResults(const string& fileName, list<match>& results)
64 {
65  ifstream t(fileName);
66  stringstream tstream;
67  tstream << t.rdbuf();
68  string s = tstream.str();
69  string::size_type pos = 0;
70  string::size_type cpos = 0;
71  for (;;)
72  {
73  string::size_type tpos = s.find("<s>", pos);
74  if (tpos == string::npos) break;
75  cpos += tpos - pos;
76  int start = cpos;
77  tpos += 3;
78  pos = s.find("</s>", tpos);
79  if (pos == string::npos) break;
80  cpos += pos - tpos;
81  pos += 4;
82  results.push_back(match(start, cpos, "r"));
83  }
84 }
85 
91  // Criterion if match m2 is sufficiently similar to a given match m
92  const match& m;
93 public:
94  overlappingMatch(const match& mm) : m(mm) { }
95  bool operator()(const match& m2) const
96  {
97  return !(m.end <= m2.start || m2.end <= m.start);
98  }
99 } ;
100 
104 void Display(ostream& out, ifstream& data, list<match>& l, list<match>& lcmp, const char*prein, const char*postin, const char*prenn, const char*postnn)
105 {
106  // Print results
107  data.clear();
108  for (match& m : l)
109  {
110  // Find in lcmp
111  bool in = find_if(lcmp.begin(), lcmp.end(), overlappingMatch(m)) != lcmp.end();
112  // Print match
113  int len = m.end - m.start;
114  char* str = new char[len+1];
115  data.seekg(m.start);
116  data.read(str,len);
117  str[len]=0;
118  out << "<p><em>[" << m.start << ":" << m.end << "]</em>" << (in ? prein : prenn);
119  HtmlEscapedOutput(out, str);
120  delete[] str;
121  out << (in ? postin : postnn) << "</p>" << endl;
122  }
123 }
124 
131 bool cmpMatches(const match &a, const match &b)
132 {
133  if (a.start < b.start)
134  return true;
135  if (a.start == b.start && a.end < b.end)
136  return true;
137  return false;
138 }
139 
149 {
150  // Test all instances
151  string fileNameBase = "../testdata/testdata";
152  // Create a copyright scanner and an author scanner
153  hCopyrightScanner hsc;
154  regexScanner hauth(regAuthor::getRegex(), regAuthor::getType());
155 
156  ofstream out("results.html");
157 
158  out << "<html><head><style type=\"text/css\">"
159  "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; }"
160  "</style></head><body>" << endl;
161 
162  // Scan files
163  for (int i = 0; i < NUMTESTS; i++)
164  {
165  string fileName = fileNameBase + to_string(i);
166  ifstream tstream(fileName);
167  list<match> lng, lauth, lrefs;
168  hsc.ScanFile(fileName, lng);
169  hauth.ScanFile(fileName, lauth);
170  // Merge lists lng and lauth
171  lng.merge(lauth, cmpMatches);
172  GetReferenceResults(fileName + "_raw", lrefs);
173 
174  out << "<h1>testdata" << i << "</h1>" << endl;
175  out << "<h2>HScanner</h2>" << endl;
176  Display(out, tstream, lng, lrefs, "<code>", "</code>", "<code class=\"falsepos\">", "</code>");
177  out << "<h2>Reference</h2>" << endl;
178  Display(out, tstream, lrefs, lng, "<code>", "</code>", "<code class=\"falseneg\">", "</code>");
179  }
180  out << "</body></html>" << endl;
181  cout << endl << "----- Test results written to results.html -----" << endl;
182 }
183 
184 CPPUNIT_TEST_SUITE_REGISTRATION( TestDataCheck );
Unit test driver.
void testDataCheck()
Test agent on every file in ../testdata/ folder.
Implementation of scanner class for copyright.
Definition: copyscan.hpp:18
Helper to check overlapping results.
Provides a regex scanner using predefined regexs.
Definition: regscan.hpp:21
virtual void ScanFile(const string &fileName, list< match > &results) const
Helper function to scan file.
Definition: scanners.hpp:70
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....
Definition: pkgConfig.php:1214
Store the results of a regex match.
Definition: scanners.hpp:28
const int start
Definition: scanners.hpp:35
const int end
Definition: scanners.hpp:35
#define NUMTESTS
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.
bool cmpMatches(const match &a, const match &b)
Compare matches.
void HtmlEscapedOutput(ostream &out, const char *s)
Escape HTML special characters.