Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\test-util.cpp
Line
Count
Source (jump to first uncovered line)
1
//  Copyright 2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include <iostream>
6
#include <fstream>
7
#include <string>
8
#include <filesystem> // C++17 FS library
9
10
#include "catch.hpp"
11
12
#include "test-util.h"
13
14
namespace testutil {
15
16
namespace fs = std::filesystem;
17
using path = std::filesystem::path;
18
19
std::string
20
fullFilename(const std::string& filename)
21
0
{
22
0
  path basedir = std::filesystem::current_path().parent_path().parent_path() / "herald-tests";
23
0
  path cppCsv = basedir / "cpp" / filename;
24
0
  // Cannot check existence as this function is generally used to generate the output file name
25
0
  return cppCsv.string();
26
0
}
27
28
void
29
validateEqual(const std::string& filename)
30
{
31
  path basedir = std::filesystem::current_path().parent_path().parent_path() / "herald-tests"; // throws filesystem_error if path does not exist
32
  
33
  // get reference to each file
34
  path cppCsv = basedir / "cpp" / filename;
35
  path androidCsv = basedir / "android" / filename;
36
  path iosCsv = basedir / "ios" / filename;
37
  // ensure files all exist
38
  REQUIRE(fs::exists(cppCsv));
39
  REQUIRE(fs::exists(androidCsv));
40
  REQUIRE(fs::exists(iosCsv));
41
42
  // read line by line
43
  std::string cppLine;
44
  std::string androidLine;
45
  std::string iosLine;
46
  std::ifstream cppIn(cppCsv.string());
47
  std::ifstream androidIn(androidCsv.string());
48
  std::ifstream iosIn(iosCsv.string());
49
  // ensure files are all readable
50
  REQUIRE(cppIn.is_open());
51
  REQUIRE(androidIn.is_open());
52
  REQUIRE(iosIn.is_open());
53
54
  int line = 1;
55
56
  // always read one line per file, for EVERY file per iteration (& not &&)
57
  while (std::getline(cppIn, cppLine)) {
58
    if (!std::getline(androidIn, androidLine)) {
59
      break;
60
    }
61
    if (!std::getline(iosIn, iosLine)) {
62
      break;
63
    }
64
    // compare lines
65
    INFO("C++ file line is " << line);
66
    REQUIRE(androidLine == iosLine); // idiot check first (error lies elsewhere)
67
    REQUIRE(cppLine == androidLine);
68
    REQUIRE(cppLine == iosLine);
69
    // above errors if not equal
70
    ++line;
71
  }
72
  // ensure files are all at an end (We've not got less data than the other files)
73
  // - Get current file position
74
  auto cppCurrentPos = cppIn.tellg();
75
  auto androidCurrentPos = androidIn.tellg();
76
  auto iosCurrentPos = iosIn.tellg();
77
  // - Seek to end
78
  cppIn.seekg(0, std::ios::end);
79
  androidIn.seekg(0, std::ios::end);
80
  iosIn.seekg(0, std::ios::end);
81
  // - ensure current position is also the same as the end (we haven't moved)
82
  REQUIRE(cppCurrentPos == cppIn.tellg());
83
  REQUIRE(androidCurrentPos == androidIn.tellg());
84
  REQUIRE(iosCurrentPos == iosIn.tellg());
85
}
86
87
}