2 * SPDX-License-Identifier: LGPL-2.1-only
6 * @brief Mocking ML Agent APIs
7 * @see https://github.com/nnstreamer/nnstreamer
8 * @author Wook Song <wook.song16@samsung.com>
11 #ifndef __NNS_MLAGENT_MOCK_H__
12 #define __NNS_MLAGENT_MOCK_H__
16 #include <unordered_map>
24 #endif /* __cplusplus */
27 * @brief Initialize the static unique_ptr of MockMLAgent
29 void ml_agent_mock_init ();
32 * @brief C-wrapper for the MockModel's method add_model.
33 * @param[in] name The model's name to add
34 * @param[in] path The model's name to add
35 * @param[in] app_info The model's name to add
36 * @param[in] is_activated The model's name to add
37 * @param[in] desc The model's version to add
38 * @param[in] version The JSON c-string containing the information of the model matching to the given name and version
39 * @return true if there is a matching model to the given name and version, otherwise a negative integer
40 * @note ML Agent provides the original implementation of this function and
41 * the following implementation is only for testing purposes.
43 bool ml_agent_mock_add_model (const gchar *name, const gchar *path, const gchar *info,
44 const bool is_activated, const char *desc, const guint version);
47 * @brief C-wrapper for the MockModel's method get_model.
48 * @param[in] name The model's name to retreive
49 * @param[in] version The model's version to retreive
50 * @return A pointer to the model matching the given information. If there is no model possible, NULL is returned.
52 MockModel *ml_agent_mock_get_model (const gchar *name, const guint version);
55 * @brief Pass the JSON c-string generated by the ML Agent mock class to the caller.
56 * @param[in] name The model's name to retreive
57 * @param[in] version The model's version to retreive
58 * @param[out] description The JSON c-string, containing the information of the model matching the given name and version
59 * @param[out] err The return location for a recoverable error. This can be NULL.
60 * @return 0 if there is a matching model to the given name and version otherwise a negative integer
61 * @note ML Agent provides the original implementation of this function and
62 * the following implementation is only for testing purposes.
64 gint ml_agent_model_get (
65 const gchar *name, const guint version, gchar **description, GError **err);
68 #endif /* __cplusplus */
71 * @brief Class for mock Model object
75 MockModel () = delete;
76 MockModel (std::string name, std::string path = {}, std::string app_info = {},
77 bool is_activated = false, std::string desc = {}, guint version = 0U)
78 : name_{ name }, path_{ path }, app_info_{ app_info },
79 is_activated_{ is_activated }, desc_{ desc }, version_{ version } {
83 MockModel (const MockModel &other)
84 : MockModel (other.name_, other.path_, other.app_info_,
85 other.is_activated_, other.desc_, other.version_){
89 MockModel (MockModel &&other) noexcept
91 *this = std::move (other);
93 MockModel &operator= (const MockModel &other) = delete;
94 MockModel &operator= (MockModel &&other) noexcept
99 this->name_ = std::move (other.name_);
100 this->path_ = std::move (other.path_);
101 this->app_info_ = std::move (other.app_info_);
102 this->is_activated_ = std::exchange (other.is_activated_, false);
103 this->desc_ = std::move (other.desc_);
104 this->version_ = std::exchange (other.version_, 0U);
109 bool operator== (const MockModel &model) const
111 if (name_ == model.name_ && version_ == model.version_)
117 std::string name () const
122 std::string path () const
126 std::string app_info () const
130 bool is_activated () const
132 return is_activated_;
135 std::string desc () const
140 guint version () const
146 void path (const std::string &path)
151 void is_activated (bool flag)
153 is_activated_ = flag;
156 void desc (const std::string &description)
161 void app_info (const std::string &info)
166 void version (guint ver)
172 * @brief Generate C-stringified JSON
173 * @return C-stringified JSON
175 gchar *to_cstr_json ();
180 std::string app_info_{};
181 bool is_activated_{ false };
183 guint version_{ 0U };
191 bool add_model (MockModel model)
193 std::string key = model.name ();
196 auto range = models_dict_.equal_range (key);
198 for (auto it = range.first; it != range.second; ++it) {
199 if (model == it->second)
204 models_dict_.insert (std::make_pair (key, model));
209 MockModel *get_model (const gchar *name, guint version)
211 std::string key{ name };
212 auto range = models_dict_.equal_range (key);
214 for (auto it = range.first; it != range.second; ++it) {
215 if (version == it->second.version ())
223 std::unordered_multimap<std::string, MockModel> models_dict_;
226 #endif /* __NNS_MLAGENT_MOCK_H__ */