File: Test.cpp

package info (click to toggle)
skypat 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 664 kB
  • sloc: cpp: 2,545; makefile: 220; ansic: 78; sh: 67
file content (108 lines) | stat: -rw-r--r-- 2,914 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
//===- Test.cpp -----------------------------------------------------------===//
//
//                     The SkyPat Team
//
// This file is distributed under the New BSD License.
// See LICENSE for details.
//
//===----------------------------------------------------------------------===//
#include <skypat/skypat.h>
#include <skypat/Listeners/PrettyResultPrinter.h>
#include <skypat/Listeners/CSVResultPrinter.h>
#include <skypat/Support/Path.h>
#include <time.h>
#include <cassert>
#include <unistd.h>
#include <string>
#include <cstdlib>

using namespace skypat;

//===----------------------------------------------------------------------===//
// Helper Functions
//===----------------------------------------------------------------------===//
static inline void help(const int& pArgc, char* pArgv[])
{
  testing::Log::getOStream() << "Usage:\n"
                             << "\t" << pArgv[0] << " [options...]\n\n"
                             << "Options:\n"
                             << "\t-c [file]  toutput CSV to [file]\n"
                             << "\t-h         Show this help manual\n";
}

//===----------------------------------------------------------------------===//
// Test
//===----------------------------------------------------------------------===//
Test::~Test()
{
  // MUST KEEP THIS DESTRUCTOR
}

void Test::run()
{
  this->TestBody();
}

void Test::Initialize(const std::string& pProgName)
{
  testing::UnitTest::self()->repeater().add(new PrettyResultPrinter());
  if (!testing::UnitTest::self()->addRunCase(pProgName))
    testing::UnitTest::self()->addAllRunCases();
}

void Test::Initialize(const std::string& pProgName, const std::string& pCSVResult)
{
  if (!pCSVResult.empty()) {
    CSVResultPrinter* printer = new CSVResultPrinter();
    if (printer->open(pCSVResult)) {
      testing::UnitTest::self()->repeater().add(printer);
    }
    else {
      testing::Log::getOStream() << "Failed to open file `" << pCSVResult << "`\n";
      delete printer;
    }
  }
  else
    testing::UnitTest::self()->repeater().add(new PrettyResultPrinter());

  if (!testing::UnitTest::self()->addRunCase(pProgName))
    testing::UnitTest::self()->addAllRunCases();
}

void Test::Initialize(const int& pArgc, char* pArgv[])
{
  // Choose user's printer
  int opt;
  std::string csvFile;
  while ((opt = getopt(pArgc, pArgv, "c:h")) != -1 ) {
    switch (opt) {
      case 'c':
        csvFile = optarg;
        break;
      case 'h':
      default:
        help(pArgc, pArgv);
        return;
    }
  }

  // Choice runnable tests
  Path progname(pArgv[0]);
  progname = progname.filename();

  Initialize(progname.native(), csvFile);
}

void Test::RunAll()
{
  testing::UnitTest::self()->RunAll();
}

void Test::Sleep(int pMS)
{
  assert(pMS > 0 && "Cannot sleep zero milliseconds");

  struct timespec ts = { pMS / 1000, (pMS % 1000) * 1000 * 1000 };
  nanosleep(&ts, NULL);
}