Software APIs
error_unittest_util.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ERROR_UNITTEST_UTIL_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ERROR_UNITTEST_UTIL_H_
7 
8 #include <iostream>
9 #include <map>
10 #include <ostream>
11 #include <stdint.h>
12 #include <string>
13 
14 #include "sw/device/silicon_creator/lib/error.h"
15 
16 const inline std::map<std::string, uint32_t> &GetErrorMap() {
17 #define STRINGIFY(x) #x
18 #define ERROR_MAP_INIT(name, value) \
19  { STRINGIFY(name), value }
20  static std::map<std::string, uint32_t> errors = {
21  DEFINE_ERRORS(ERROR_MAP_INIT)};
22  return errors;
23 #undef ERROR_MAP_INIT
24 #undef STRINGIFY
25 }
26 
27 const inline std::map<uint32_t, std::string> &GetErrorToStringMap() {
28 #define STRINGIFY(x) #x
29 #define ERROR_MAP_INIT(name, value) \
30  { value, STRINGIFY(name) }
31  static std::map<uint32_t, std::string> errors = {
32  DEFINE_ERRORS(ERROR_MAP_INIT)};
33  return errors;
34 #undef ERROR_MAP_INIT
35 #undef STRINGIFY
36 }
37 
38 /**
39  * A custom printer for `rom_error_t` in unit tests.
40  */
41 void inline PrintTo(const rom_error_t &err, std::ostream *os) {
42  const std::map<uint32_t, std::string> &error_to_string_map =
43  GetErrorToStringMap();
44  // Print the symbolic name if it is known.
45  if (error_to_string_map.count(err)) {
46  *os << error_to_string_map.at(err);
47  } else {
48  *os << "Unrecognized rom_error_t value";
49  }
50  *os << " (0x" << std::hex << err << ")";
51 }
52 
53 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ERROR_UNITTEST_UTIL_H_