forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMacrosImpl.cc
89 lines (78 loc) · 2.33 KB
/
TestMacrosImpl.cc
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file testdetail/TestMacrosImpl.cc
//---------------------------------------------------------------------------//
#include "TestMacrosImpl.hh"
#include <cstdio>
#include <cstring>
#include <string>
#include "celeritas_config.h"
#include "corecel/Assert.hh"
#include "corecel/io/ColorUtils.hh"
#include "JsonComparer.hh"
namespace celeritas
{
namespace testdetail
{
//---------------------------------------------------------------------------//
/*!
* Number of base-10 digits in an unsigned integer.
*
* This function is useful for pre-calculating field widths for printing.
*/
int num_digits(unsigned long val)
{
if (val == 0)
return 1;
int result = 0;
unsigned long cur = 1;
while (cur <= val)
{
cur *= 10;
++result;
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Return a replacement string if the given string is too long.
*
* where too long means > digits digits.
*/
char const*
trunc_string(unsigned int digits, char const* str, char const* trunc)
{
CELER_EXPECT(str && trunc);
CELER_EXPECT(digits > 0);
CELER_EXPECT(std::strlen(trunc) <= digits);
if (std::strlen(str) > digits)
{
return trunc;
}
return str;
}
//---------------------------------------------------------------------------//
/*!
* Compare two JSON objects.
*/
::testing::AssertionResult IsJsonEq(char const*,
char const*,
std::string_view expected,
std::string_view actual)
{
JsonComparer compare{};
auto result = compare(expected, actual);
if (!result)
{
// Print actual result for copy-pasting into "expected" expression
result << "\n/*** ACTUAL ***/\nR\"json(" << actual
<< ")json\"\n/******/";
}
return result;
}
//---------------------------------------------------------------------------//
} // namespace testdetail
} // namespace celeritas