File: skypat.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 (429 lines) | stat: -rw-r--r-- 12,187 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//===- skypat.cpp ---------------------------------------------------------===//
//
//                     The SkyPat Team
//
// This file is distributed under the New BSD License.
// See LICENSE for details.
//
//===----------------------------------------------------------------------===//
#include <skypat/skypat.h>
#include <skypat/Support/Timer.h>
#include <skypat/Support/Perf.h>
#include <skypat/Support/ManagedStatic.h>
#include <skypat/Support/OStrStream.h>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <ios>
#include <ostream>
#include <iostream>
#include <stdint.h>
#include <algorithm>
#include <cstdio>
#include <time.h>

#include <skypat/Config/Config.h>

using namespace skypat;

/* Define the numebr of iteration of performance loop */
#define SKYPAT_PERFORM_LOOP_TIMES 1

namespace skypat{
/* Establish perf event string array */
char const *Perf_event_name[] = {
  "CPU CYCLES", "INST   NUM",
  "CACHE  REF", "CACHE MISS",
  "BR    INST", "BR  MISSES",
  "BUS CYCLES", "STALLFRONT",
  "STALL BACK", "REF CYCLES",
  "CPU  CLOCK", "TASK CLOCK",
  "PAGEFAULTS", "CTX SWITCH",
  "CPUMIGRATE", "PG FAULT m",
  "PG FAULT M", "ALIGNFAULT",
  "EMU  FAULT", "D U M M Y "
};
} // namespace of skypat

//===----------------------------------------------------------------------===//
// Non-member function
//===----------------------------------------------------------------------===//
testing::TestInfo*
testing::MakeAndRegisterTestInfo(const char* pCaseName, const char* pTestName,
                                 testing::TestFactoryBase* pFactory)
{
  return testing::UnitTest::self()->addTestInfo(pCaseName, pTestName, *pFactory);
}

std::string testing::GetBoolAssertionFailureMessage(
    const skypat::testing::AssertionResult& pAssertionResult,
    const char* pExpressionText,
    const char* pActualPredicateValue,
    const char* pExpectedPredicateValue)
{
  std::string result;
  OStrStream OS(result);
  OS << "Value of: " << pExpressionText
     << "\n  Actual:   " << pActualPredicateValue;
  if (pAssertionResult.hasMessage())
    OS << "(" << pAssertionResult.message() << ")";
  OS << "\n  Expected: " << pExpectedPredicateValue;
  return result;
}

//===----------------------------------------------------------------------===//
// PerfIterator
//===----------------------------------------------------------------------===//
testing::PerfIterator::PerfIterator(const char* pFile, int pLine)
  : m_Counter(0),
    m_pTimer(new internal::Timer()),
    m_pPerf(new internal::Perf()),
    m_pPerfResult(testing::UnitTest::self()->addPerfPartResult(pFile, pLine)) {

  m_pTimer->start();
  m_pPerf->start();
}

testing::PerfIterator::PerfIterator(const char* pFile, int pLine,\
									enum PerfEvent pEvent)
  : m_Counter(0),
    m_pTimer(new internal::Timer()),
    m_pPerf(new internal::Perf(pEvent)),
    m_pPerfResult(testing::UnitTest::self()->addPerfPartResult(pFile, pLine)) {

  m_pTimer->start();
  m_pPerf->start();
}

testing::PerfIterator::~PerfIterator()
{
  delete m_pTimer;
  delete m_pPerf;
}

bool testing::PerfIterator::hasNext() const
{
  if (m_Counter < SKYPAT_PERFORM_LOOP_TIMES)
    return true;

  m_pTimer->stop();
  m_pPerf->stop();

  m_pPerfResult->setTimerNum(m_pTimer->interval());
  m_pPerfResult->setPerfEventNum(m_pPerf->interval());
  m_pPerfResult->setPerfEventType(m_pPerf->eventType());
  return false;
}

testing::PerfIterator& testing::PerfIterator::next()
{
  ++m_Counter;
  return *this;
}

//===----------------------------------------------------------------------===//
// PartResult
//===----------------------------------------------------------------------===//
testing::PartResult::PartResult(const std::string& pFileName, int pLoC)
  : m_FileName(pFileName), m_LoC(pLoC), m_Message() {
}

testing::PartResult::PartResult(const std::string& pFileName,
                                int pLoC,
                                const std::string& pMessage)
  : m_FileName(pFileName), m_LoC(pLoC), m_Message(pMessage) {
}

//===----------------------------------------------------------------------===//
// TestPartResult
//===----------------------------------------------------------------------===//
testing::TestPartResult::TestPartResult(Type pType,
                                        const std::string& pFileName,
                                        int pLoC,
                                        const std::string& pMessage)
  : PartResult(pFileName, pLoC, pMessage), m_Type(pType) {
}

testing::TestPartResult&
testing::TestPartResult::appendUserMessage(const std::string& pMessage)
{
  if (!pMessage.empty())
    update_message(message() + "\n" + pMessage);
  return *this;
}

//===----------------------------------------------------------------------===//
// PerfPartResult
//===----------------------------------------------------------------------===//
testing::PerfPartResult::PerfPartResult(const std::string& pFileName,
                                        int pLoC)
  : PartResult(pFileName, pLoC) {
}

testing::Interval testing::PerfPartResult::getTimerNum() const
{
  return m_PerfTimerNum;
}

testing::Interval testing::PerfPartResult::getPerfEventNum() const
{
  return m_PerfEventNum;
}

testing::Interval testing::PerfPartResult::getPerfEventType() const
{
  return m_PerfEventType;
}

void testing::PerfPartResult::setTimerNum(testing::Interval pTimerNum)
{
  m_PerfTimerNum = pTimerNum;
  OStrStream os(m_Message);
  os << pTimerNum << " ns;";
}

void testing::PerfPartResult::setPerfEventNum(testing::Interval pEventNum)
{
  m_PerfEventNum = pEventNum;
}

void testing::PerfPartResult::setPerfEventType(testing::Interval pEventType)
{
  m_PerfEventType = pEventType;
}

//===----------------------------------------------------------------------===//
// TestResult
//===----------------------------------------------------------------------===//
testing::TestResult::TestResult(const TestInfo& pInfo)
  : m_Info(pInfo), m_Conclusion(kNotTested) {
}

testing::TestResult::~TestResult()
{
}

bool testing::TestResult::isPassed() const
{
  return (kPassed == m_Conclusion);
}

bool testing::TestResult::isFailed() const
{
  return (kFailed == m_Conclusion);
}

const testing::TestResult::Reliability& testing::TestResult::reliability() const
{
  return m_Info.getTestResults();
}

const testing::TestResult::Performance& testing::TestResult::performance() const
{
  return m_Info.getPerfResults();
}

//===----------------------------------------------------------------------===//
// TestCase
//===----------------------------------------------------------------------===//
testing::TestCase::TestCase(const std::string& pCaseName)
  : m_CaseName(pCaseName)
{
}

testing::TestCase::~TestCase()
{
  InfoList::iterator info, iEnd = m_InfoList.end();
  for (info = m_InfoList.begin(); info != iEnd; ++info) {
    delete (*info);
  }
}

testing::TestInfo*
testing::TestCase::addTestInfo(const std::string& pTestName,
                               testing::TestFactoryBase& pFactory)
{
  testing::TestInfo* info = new testing::TestInfo(this, pTestName, pFactory);
  m_InfoList.push_back(info);
  return info;
}

//===----------------------------------------------------------------------===//
// TestInfo
//===----------------------------------------------------------------------===//
testing::TestInfo::TestInfo(TestCase* pTestCase,
                            const std::string& pTestName,
                            testing::TestFactoryBase& pFactory)
  : m_pTestCase(pTestCase),
    m_TestName(pTestName),
    m_Result(*this),
    m_pFactory(&pFactory) {
}

testing::TestInfo::~TestInfo()
{
  delete m_pFactory;
  TestPartResultList::iterator tt, tEnd = m_TestResultList.end();
  for (tt = m_TestResultList.begin(); tt != tEnd; ++tt) {
    delete (*tt);
  }
  PerfPartResultList::iterator pt, pEnd = m_PerfResultList.end();
  for (pt = m_PerfResultList.begin(); pt != pEnd; ++pt) {
    delete (*pt);
  }
}

void testing::TestInfo::run()
{
  UnitTest& unittest = *UnitTest::self();
  Repeater& repeater = unittest.repeater();
  skypat::Test* test = m_pFactory->CreateTest();
  if (NULL != test) {
    repeater.OnSetUpStart(unittest);
    test->SetUp();
    repeater.OnSetUpEnd(unittest);

    repeater.OnTestStart(*this);
    test->run();
    repeater.OnTestEnd(*this);

    repeater.OnTearDownStart(unittest);
    test->TearDown();
    repeater.OnTearDownEnd(unittest);
  }
  delete test;
}

void testing::TestInfo::addTestPartResult(const TestPartResult& pResult)
{
  if (m_TestResultList.empty()) {
    m_Result.setConclusion(testing::TestResult::kPassed);
  }

  if (testing::TestPartResult::kSuccess != pResult.type()) {
    m_Result.setConclusion(testing::TestResult::kFailed);
    m_TestResultList.push_back(new TestPartResult(pResult));
  }
}

testing::PerfPartResult*
testing::TestInfo::addPerfPartResult(const char* pFile, int pLine)
{
  PerfPartResult* perf_pr = new PerfPartResult(pFile, pLine);
  m_PerfResultList.push_back(perf_pr);
  return perf_pr;
}

//===----------------------------------------------------------------------===//
// AssertionResult
//===----------------------------------------------------------------------===//
skypat::testing::AssertionResult::AssertionResult(const AssertionResult& pOther)
  : m_bSuccess(pOther.m_bSuccess), m_Message(pOther.m_Message) {
}

skypat::testing::AssertionResult::AssertionResult(bool pSuccess)
  : m_bSuccess(pSuccess) {
}

skypat::testing::AssertionResult
skypat::testing::AssertionResult::operator!() const
{
  AssertionResult negative(!m_bSuccess);
  negative << m_Message;
  return negative;
}

template <typename T> skypat::testing::AssertionResult&
skypat::testing::AssertionResult::operator<<(const T& pValue)
{
  skypat::OStrStream OS(m_Message);
  OS << pValue;
  return *this;
}

skypat::testing::AssertionResult&
skypat::testing::AssertionResult::operator<<(
                  ::std::ostream& (*basic_manipulator)(::std::ostream& stream))
{
  skypat::OStrStream OS(m_Message);
  OS << basic_manipulator;
  return *this;
}

//===----------------------------------------------------------------------===//
// Message
//===----------------------------------------------------------------------===//
skypat::testing::Message::Message()
  : m_Message(), m_OSS(m_Message) {
}

//===----------------------------------------------------------------------===//
// AssertHelper
//===----------------------------------------------------------------------===//
skypat::testing::AssertHelper::AssertHelper(TestPartResult::Type pType,
                                         const std::string& pFile,
                                         int pLineOfCode,
                                         const std::string& pMessage)
  : m_Result(pType, pFile, pLineOfCode, pMessage) {
  // m_Result is a TestPartResult
}

// Store a run-time result
void skypat::testing::AssertHelper::operator=(const Message& pMesg)
{
  m_Result.appendUserMessage(pMesg.str());
  UnitTest::self()->addTestPartResult(m_Result);
}

//===----------------------------------------------------------------------===//
// Log
//===----------------------------------------------------------------------===//
testing::Log::Log(Severity pSeverity,
                  const std::string& pFileName,
                  int pLoC)
  : m_Severity(pSeverity) {
  const char* const mesg =
    kInfo    == pSeverity ? "[  INFO ]" :
    kWarning == pSeverity ? "[WARNING]" :
    kError   == pSeverity ? "[ ERROR ]" : "[ FATAL ]";

  getOStream() << std::endl << mesg << FormatFileLocation(pFileName, pLoC)
               << ": ";
}

testing::Log::~Log()
{
  getOStream() << std::endl;
  if (kFatal == m_Severity) {
    shutdown();
    fflush(stderr);
    exit(1);
  }
}

::std::ostream& testing::Log::getOStream()
{
  return ::std::cerr;
}

std::string
testing::Log::FormatFileLocation(const std::string& pFileName, int pLoC)
{
  std::string result;
  OStrStream OS(result);
  if (pFileName.empty())
    OS << "(unknown file)";
  else
    OS << pFileName;

  if (pLoC < 0) {
    OS << ":";
    return result;
  }
#ifdef _MSC_VER
  OS << "(" << pLoC << "):";
#else
  OS << ":" << pLoC << ":";
#endif  // _MSC_VER
  return result;
}