text
stringlengths
0
2.2M
typedef balxml::TypesParserUtil Util;
typedef balxml::TypesPrintUtil Print;
// ============================================================================
// ROUND TRIP TESTING MACHINERY
// ----------------------------------------------------------------------------
namespace TestMachinery {
#if defined(BSLS_PLATFORM_CPU_64_BIT) && !defined(BSLS_PLATFORM_OS_WINDOWS)
#define U_LONG_IS_64_BITS
// On 64 bit systems 'long' may be 32 or 64 bits.
#endif
template <class ENCODED_TYPE>
struct GetDecodeType {
// Meta-function to chose a type we parse/decode into. For most types we
// just use the type we encoded from.
typedef ENCODED_TYPE Type;
};
template <>
struct GetDecodeType<signed long int> {
// 'signed long int' is not supported to parse/decode into, as it may have
// a differing size on different 64 bit platforms.
#ifdef U_LONG_IS_64_BITS
typedef long long int Type;
#else
typedef int Type;
#endif
};
template <>
struct GetDecodeType<unsigned long int> {
// 'unsigned long int' is not supported to parse/decode into, as it may
// have a differing size on different 64 bit platforms.
#ifdef U_LONG_IS_64_BITS
typedef unsigned long long int Type;
#else
typedef unsigned int Type;
#endif
};
template <>
struct GetDecodeType<const char *> {
// Can't parse into a 'const char *', but can into a 'bsl::string'.
typedef bsl::string Type;
};
template <>
struct GetDecodeType<bslstl::StringRef> {
// Can't parse into a string reference, but can into a 'bsl::string'.
typedef bsl::string Type;
};
int intLength(const bsl::string_view& s)
// Return the length of 's' as an 'int' after verifying it fits in 'int'.
// The verifying part is more for show, we don't expect 2 gigabyte strings.
{
ASSERTV(s.length(),
s.length() <=
static_cast<bsl::size_t>(bsl::numeric_limits<int>::max()));
return static_cast<int>(s.length());
}
template <class TEST_DATA,
bsl::size_t NUM_DATA>
void printDecimalRoundTripTester(const TEST_DATA (&DATA)[NUM_DATA])
// Verify round trip of 'printDecimal'/'parseDecimal' for the values in the
// specified 'DATA' array of the deduced 'NUM_DATA' length. The deduced
// 'TEST_DATA' must provide a member 'Type' that will be printed from, and
// parsed into.
{
typedef typename TEST_DATA::Type Type;
typedef typename GetDecodeType<Type>::Type DecodeType;
// Some types we can print/encode from, but cannot parse/decode into.
// 'GetDecodeType' is the level of indirection that solves that issue.
for (bsl::size_t i = 0; i < NUM_DATA; ++i) {
const int LINE = DATA[i].d_line;
const Type INPUT = DATA[i].d_input;
bsl::stringstream ss;
Print::printDecimal(ss, INPUT);
ASSERTV(LINE, ss.good());
const bsl::string ENCODED(ss.str());
DecodeType decoded;
int rc;
ASSERTV(LINE, 0 == (rc = Util::parseDecimal(&decoded,
ENCODED.data(),
intLength(ENCODED))));
if (0 == rc) {