libcamera: utils: Add hex stream output helper

Add a utils::hex() function that simplifies writing hexadecimal values
to an ostream. The function handles the '0x' prefix, the field width and
the fill character automatically. Use it through the libcamera code
base, and add a test.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart 2019-10-13 22:22:04 +03:00
parent ce739e616c
commit f391048a7b
11 changed files with 147 additions and 22 deletions

53
test/utils.cpp Normal file
View file

@ -0,0 +1,53 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2018, Google Inc.
*
* utils.cpp - Miscellaneous utility tests
*/
#include <iostream>
#include <sstream>
#include "test.h"
#include "utils.h"
using namespace std;
using namespace libcamera;
class UtilsTest : public Test
{
protected:
int run()
{
std::ostringstream os;
std::string ref;
os << utils::hex(static_cast<int32_t>(0x42)) << " ";
ref += "0x00000042 ";
os << utils::hex(static_cast<uint32_t>(0x42)) << " ";
ref += "0x00000042 ";
os << utils::hex(static_cast<int64_t>(0x42)) << " ";
ref += "0x0000000000000042 ";
os << utils::hex(static_cast<uint64_t>(0x42)) << " ";
ref += "0x0000000000000042 ";
os << utils::hex(static_cast<int32_t>(0x42), 4) << " ";
ref += "0x0042 ";
os << utils::hex(static_cast<uint32_t>(0x42), 1) << " ";
ref += "0x42 ";
os << utils::hex(static_cast<int64_t>(0x42), 4) << " ";
ref += "0x0042 ";
os << utils::hex(static_cast<uint64_t>(0x42), 1) << " ";
ref += "0x42 ";
std::string s = os.str();
if (s != ref) {
cerr << "utils::hex() test failed, expected '" << ref
<< "', got '" << s << "'";
return TestFail;
}
return TestPass;
}
};
TEST_REGISTER(UtilsTest)