From f391048a7b987a149d0ba5421846b9b5ab916338 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 13 Oct 2019 22:22:04 +0300 Subject: libcamera: utils: Add hex stream output helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- test/utils.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test/utils.cpp (limited to 'test/utils.cpp') diff --git a/test/utils.cpp b/test/utils.cpp new file mode 100644 index 00000000..9fe0d477 --- /dev/null +++ b/test/utils.cpp @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * utils.cpp - Miscellaneous utility tests + */ + +#include +#include + +#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(0x42)) << " "; + ref += "0x00000042 "; + os << utils::hex(static_cast(0x42)) << " "; + ref += "0x00000042 "; + os << utils::hex(static_cast(0x42)) << " "; + ref += "0x0000000000000042 "; + os << utils::hex(static_cast(0x42)) << " "; + ref += "0x0000000000000042 "; + os << utils::hex(static_cast(0x42), 4) << " "; + ref += "0x0042 "; + os << utils::hex(static_cast(0x42), 1) << " "; + ref += "0x42 "; + os << utils::hex(static_cast(0x42), 4) << " "; + ref += "0x0042 "; + os << utils::hex(static_cast(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) -- cgit v1.2.1