Compare commits

...

3 Commits

4 changed files with 245 additions and 78 deletions

View File

@ -116,7 +116,7 @@ typedef WUPSStorageError (*WUPSStorage_GetItemFunction)(wups_storage_root_item r
* @typedef WUPSStorage_GetItemSizeFunction
* @brief Type alias for the function pointer to get the size of an item in storage. For internal usage only.
*/
typedef WUPSStorageError (*WUPSStorage_GetItemSizeFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, uint32_t *outSize);
typedef WUPSStorageError (*WUPSStorage_GetItemSizeFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize);
/**
* @typedef WUPS_STORAGE_API_VERSION
@ -641,7 +641,7 @@ inline WUPSStorageError WUPSStorageAPI_GetBinary(wups_storage_item parent, const
* - WUPS_STORAGE_ERROR_NOT_FOUND: No item with the given key has been found inside the given parent.
* - WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED: The library is not initialized properly; make sure to use the WUPS_USE_STORAGE macro.
*/
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize);
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize);
#ifdef __cplusplus
}
@ -663,6 +663,28 @@ class WUPSStorageSubItem;
* @brief C++ wrapper for the WUPS storage API. See the C-API for detailed information about the possible return values.
*/
namespace WUPSStorageAPI {
template<typename T>
struct GetStorageItemType;
template<>
struct GetStorageItemType<std::string> {
static constexpr WUPSStorageItemTypes value = WUPS_STORAGE_ITEM_STRING;
};
template<>
struct GetStorageItemType<std::vector<uint8_t>> {
static constexpr WUPSStorageItemTypes value = WUPS_STORAGE_ITEM_BINARY;
};
/**
* @enum GetOptions
* Enumerates options for retrieving items from storage.
*/
enum GetOptions {
RESIZE_EXISTING_BUFFER, /**< Resizes the given buffer to the item size before loading an item from storage */
USE_EXISTING_BUFFER, /**< Does not resize the buffer before loading an item from storage */
};
/**
* @brief Gets a string representation of the specified storage status.
* @param err The storage error status.
@ -835,7 +857,10 @@ namespace WUPSStorageAPI {
* Returns an error code if an error occurs.
* \see WUPSStorageAPI_GetItemSize
*/
WUPSStorageError GetItemSize(std::string_view key, uint32_t &outSize) noexcept;
template<typename T>
inline WUPSStorageError GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
return WUPSStorageAPI_GetItemSize(nullptr, key.data(), WUPSStorageAPI::GetStorageItemType<T>::value, &outSize);
}
/**
* @brief Retrieve a value from the storage.
@ -848,11 +873,12 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options additional options, see @GetOptions for more information
* @return WUPSStorageError WUPS_STORAGE_ERROR_SUCCESS on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetItem
*/
template<typename T>
inline WUPSStorageError GetEx(wups_storage_item parent, std::string_view key, T &outValue) noexcept;
inline WUPSStorageError GetEx(wups_storage_item parent, std::string_view key, T &outValue, GetOptions options = RESIZE_EXISTING_BUFFER) noexcept;
/**
* @brief Retrieves a signed 32-bit integer value from a storage item.
@ -862,12 +888,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetS32
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<int32_t>(wups_storage_item parent, std::string_view key, int32_t &outValue) noexcept {
inline WUPSStorageError GetEx<int32_t>(wups_storage_item parent, std::string_view key, int32_t &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetS32(parent, key.data(), &outValue);
}
@ -879,12 +906,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetS64
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<int64_t>(wups_storage_item parent, std::string_view key, int64_t &outValue) noexcept {
inline WUPSStorageError GetEx<int64_t>(wups_storage_item parent, std::string_view key, int64_t &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetS64(parent, key.data(), &outValue);
}
@ -896,12 +924,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetU32
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<uint32_t>(wups_storage_item parent, std::string_view key, uint32_t &outValue) noexcept {
inline WUPSStorageError GetEx<uint32_t>(wups_storage_item parent, std::string_view key, uint32_t &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetU32(parent, key.data(), &outValue);
}
@ -913,12 +942,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetU64
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<uint64_t>(wups_storage_item parent, std::string_view key, uint64_t &outValue) noexcept {
inline WUPSStorageError GetEx<uint64_t>(wups_storage_item parent, std::string_view key, uint64_t &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetU64(parent, key.data(), &outValue);
}
@ -930,12 +960,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetBool
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<bool>(wups_storage_item parent, std::string_view key, bool &outValue) noexcept {
inline WUPSStorageError GetEx<bool>(wups_storage_item parent, std::string_view key, bool &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetBool(parent, key.data(), &outValue);
}
@ -947,12 +978,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetFloat
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<float>(wups_storage_item parent, std::string_view key, float &outValue) noexcept {
inline WUPSStorageError GetEx<float>(wups_storage_item parent, std::string_view key, float &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetFloat(parent, key.data(), &outValue);
}
@ -964,12 +996,13 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns `WUPS_STORAGE_ERROR_SUCCESS` on success, otherwise an appropriate error code.
* \see WUPSStorageAPI_GetFloat
* \see WUPSStorageAPI_GetItem
*/
template<>
inline WUPSStorageError GetEx<double>(wups_storage_item parent, std::string_view key, double &outValue) noexcept {
inline WUPSStorageError GetEx<double>(wups_storage_item parent, std::string_view key, double &outValue, GetOptions /* options */) noexcept {
return WUPSStorageAPI_GetDouble(parent, key.data(), &outValue);
}
@ -983,6 +1016,7 @@ namespace WUPSStorageAPI {
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved enum value will be stored.
* @param options unused parameter
* @return WUPSStorageError Returns WUPS_STORAGE_ERROR_SUCCESS on success, otherwise an appropriate error code.
*
* @note This function assumes that the enum type is of size sizeof(uint32_t).
@ -991,7 +1025,7 @@ namespace WUPSStorageAPI {
* \see WUPSStorageAPI_GetItem
*/
template<typename T>
inline WUPSStorageError GetEx(wups_storage_item parent, std::string_view key, T &outValue) noexcept {
inline WUPSStorageError GetEx(wups_storage_item parent, std::string_view key, T &outValue, GetOptions /* options */) noexcept {
static_assert(sizeof(T) == sizeof(uint32_t) && std::is_enum<T>::value, "T must be an enum of size sizeof(uint32_t)");
return WUPSStorageAPI_GetU32(parent, key.data(), (uint32_t *) &outValue);
}
@ -999,32 +1033,38 @@ namespace WUPSStorageAPI {
/**
* @brief Retrieves a vector of uint8_t from storage.
*
* This template specialization of the GetEx function retrieves a vector of uint8_t from the storage.
* It first checks if the outValue vector is empty (size == 0).
* If it is, it retrieves the size of the data and resizes the outValue vector to that size. Otherwise the existing vector (size) will the be used.
* Then, it retrieves the binary data from the storage using the WUPSStorageAPI_GetBinary function.
* If the retrieval is successful, the outValue vector is resized to the size of the retrieved data.
* If the retrieval fails, the outValue vector is resized to 0.
* This template specialization of the GetEx function retrieves a vector of uint8_t from the storage. \n
* Before getting the actual data, the buffer might be prepared depending on the given option parameter: \n
* - USE_EXISTING_BUFFER: the buffer will not be resized to the item size before getting the item. The caller has to make sure the buffer is big enough. \n
* - RESIZE_EXISTING_BUFFER: the buffer will be resized to the actual item size before getting the item. \n
* Then, it retrieves the binary data from the storage using the WUPSStorageAPI_GetBinary function. \n
* If the retrieval is successful, the outValue vector is resized to the size of the retrieved data. \n
* If the retrieval fails, the outValue vector is resized to 0. \n
*
* @param parent The parent storage item. Can be NULL to refer to the root of the storage.
* @param key The key under which the data is stored.
* @param[out] outValue A reference to the vector where the retrieved data will be stored. Will be resized to fit the data only if it's empty.
* @param options Defines how the given outValue buffer is used.
* @return WUPSStorageError WUPS_STORAGE_ERROR_SUCCESS on success, otherwise an appropriate error code.
*
* @see WUPSStorageAPI_GetBinary
* @see WUPSStorageAPI_GetItemSize
*/
template<>
inline WUPSStorageError GetEx<std::vector<uint8_t>>(wups_storage_item parent, std::string_view key, std::vector<uint8_t> &outValue) noexcept {
inline WUPSStorageError GetEx<std::vector<uint8_t>>(wups_storage_item parent, std::string_view key, std::vector<uint8_t> &outValue, GetOptions options) noexcept {
uint32_t outSize = 0;
if (outValue.empty()) {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key.data(), &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
switch (options) {
case USE_EXISTING_BUFFER:
break;
case RESIZE_EXISTING_BUFFER: {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key.data(), WUPS_STORAGE_ITEM_BINARY, &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
}
}
}
@ -1040,29 +1080,35 @@ namespace WUPSStorageAPI {
/**
* @brief Retrieves a string value from a storage item with the given key.
*
* If the input `outValue` is empty, the function first retrieves the size of the stored data using `WUPSStorageAPI_GetItemSize`.
* Before getting the actual data, the string might be resized to fit the data depending on the given option parameter: \n
* - USE_EXISTING_BUFFER: the string will not be resized to the item size before getting the item. The caller has to make sure the string is already big enough. \n
* - RESIZE_EXISTING_BUFFER: the string will be resized to the actual string size before getting the item. \n
* If the retrieval is successful, the function resizes `outValue` to the retrieved size.
* Then, it calls `WUPSStorageAPI_GetString` to retrieve the string from the storage item.
* If the retrieval is successful, the function resizes `outValue` to remove the null terminator.
*
* @param parent The parent storage item. Can be `NULL` to refer to the root of the storage.
* @param key The key under which the string is stored.
* @param[out] outValue A reference to a `std::string` object where the retrieved string will be stored. Will be resized to fit the data only if it's empty.
* @param options Defines how the given outValue buffer is used.
* @return WUPSStorageError WUPS_STORAGE_ERROR_SUCCESS on success, otherwise an appropriate error code.
*
* @see WUPSStorageAPI_GetBinary
* @see WUPSStorageAPI_GetString
*/
template<>
inline WUPSStorageError GetEx<std::string>(wups_storage_item parent, std::string_view key, std::string &outValue) noexcept {
inline WUPSStorageError GetEx<std::string>(wups_storage_item parent, std::string_view key, std::string &outValue, GetOptions options) noexcept {
uint32_t outSize = 0;
if (outValue.empty()) {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key.data(), &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
switch (options) {
case USE_EXISTING_BUFFER:
break;
case RESIZE_EXISTING_BUFFER: {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key.data(), WUPS_STORAGE_ITEM_STRING, &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
}
}
}
@ -1086,14 +1132,15 @@ namespace WUPSStorageAPI {
*
* @param key The key under which the value is stored.
* @param[out] outValue A reference to the variable where the retrieved value will be stored.
* @param[optional] options Defines how a given buffer might be used, only relevant for certain item types (e.g. strings or buffer). Default value is RESIZE_EXISTING_BUFFER.
* @return WUPSStorageError WUPS_STORAGE_ERROR_SUCCESS on success, otherwise an appropriate error code.
*
* @see WUPSStorageAPI_GetItem
* @see GetEx
*/
template<typename T>
inline WUPSStorageError Get(std::string_view key, T &outValue) noexcept {
return GetEx(nullptr, key.data(), outValue);
inline WUPSStorageError Get(std::string_view key, T &outValue, GetOptions options = RESIZE_EXISTING_BUFFER) noexcept {
return GetEx(nullptr, key.data(), outValue, options);
}
/**
@ -1321,23 +1368,21 @@ namespace WUPSStorageAPI {
/**
* @brief Retrieves the value associated with the specified key from a storage item.
* If the key is not found, stores the default value in the storage item.
* If the key is not found, stores the default value with taht key in the storage item.
*
* @tparam T The type of the value to retrieve and store.
* @param parent The storage item to retrieve and store the value from. Can be NULL to refer to the root of the storage
* @param key The key associated with the value.
* @param outValue [out] The output parameter to store the retrieved value.
* @param defaultValue The default value to store if the key is not found.
* @param defaultValue The default value to store in storage and outValue if the key does not exist.
* @return The error code indicating the success or failure of the operation.
*
* @details This function retrieves the value associated with the specified key from the given storage item.
* @details This function retrieves the value associated with the specified key from the given parent.
* If the key is not found, the default value is stored in the storage item using the StoreEx function.
* The retrieved or stored value is then assigned to the output parameter outValue.
* The function returns an error code indicating the success or failure of the operation.
* The possible error codes are defined in the WUPSStorageError enumeration.
* If the retrieval and storage operations are successful, the error code will be WUPS_STORAGE_ERROR_SUCCESS.
* If there are any errors, such as invalid arguments, memory allocation failure, I/O errors, or unknown errors,
* the error code will indicate the specific type of error occurred.
* \see GetEx
* \see StoreEx
*/
@ -1346,6 +1391,9 @@ namespace WUPSStorageAPI {
WUPSStorageError err = GetEx(parent, key, outValue);
if (err == WUPS_STORAGE_ERROR_NOT_FOUND) {
err = StoreEx(parent, key, defaultValue);
if (err == WUPS_STORAGE_ERROR_SUCCESS) {
outValue = defaultValue;
}
}
return err;
}
@ -1356,18 +1404,20 @@ namespace WUPSStorageAPI {
* @tparam T The type of the value.
* @param key The key of the value to retrieve or store.
* @param outValue The variable to store the retrieved or stored value.
* @param defaultValue The default value to store if the key does not exist.
* @param defaultValue The default value to store in storage and outValue if the key does not exist.
* @return WUPSStorageError An error code indicating the success or failure of the operation.
*
* This function retrieves a value associated with the given key from storage. If the key does not exist,
* it stores the default value provided and returns WUPS_STORAGE_ERROR_SUCCESS. If the storage API function
* encounters an error, the corresponding error code is returned.
* It is a wrapper around the `GetOrStoreDefaultEx` function, and stores into the root item of the storage by default.
* @details This function retrieves the value associated with the specified key from the root of the storage.
* If the key is not found, the default value is stored in the storage item using the StoreEx function.
* The retrieved or stored value is then assigned to the output parameter outValue.
* The function returns an error code indicating the success or failure of the operation.
* The possible error codes are defined in the WUPSStorageError enumeration.
* If the retrieval and storage operations are successful, the error code will be WUPS_STORAGE_ERROR_SUCCESS.
*
* @note This function internally calls the GetEx and StoreEx functions of the storage API.
*/
template<typename T>
inline WUPSStorageError GetOrStoreDefault(const char *key, T &outValue, const T &defaultValue) noexcept {
inline WUPSStorageError GetOrStoreDefault(std::string_view key, T &outValue, const T &defaultValue) noexcept {
return GetOrStoreDefaultEx<T>(nullptr, key, outValue, defaultValue);
}
} // namespace WUPSStorageAPI
@ -1463,7 +1513,10 @@ public:
*
* @see WUPSStorageAPI_GetItemSize()
*/
WUPSStorageError GetItemSize(std::string_view key, uint32_t &outSize) noexcept;
template<typename T>
WUPSStorageError GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
return WUPSStorageAPI_GetItemSize(mHandle, key.data(), WUPSStorageAPI::GetStorageItemType<T>::value, &outSize);
}
/**
* @brief Create a sub-item in this sub-item. (non-throwing)

View File

@ -147,12 +147,12 @@ WUPSStorageError WUPSStorageAPI_GetItem(wups_storage_item parent, const char *ke
}
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize) {
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize) {
if (outSize == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
if (__internal_functions.get_item_size_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.get_item_size_function_ptr(__internal_functions.__storageroot_item, parent, key, outSize);
return __internal_functions.get_item_size_function_ptr(__internal_functions.__storageroot_item, parent, key, itemType, outSize);
}

View File

@ -58,20 +58,10 @@ WUPSStorageSubItem WUPSStorageAPI::GetRootItem() noexcept {
return WUPSStorageSubItem(nullptr);
}
WUPSStorageError WUPSStorageAPI::GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
WUPSStorageSubItem item(nullptr);
return item.GetItemSize(key, outSize);
}
WUPSStorageError WUPSStorageSubItem::DeleteItem(std::string_view key) noexcept {
return WUPSStorageAPI_DeleteItem(mHandle, key.data());
}
WUPSStorageError WUPSStorageSubItem::GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
return WUPSStorageAPI_GetItemSize(mHandle, key.data(), &outSize);
}
std::optional<WUPSStorageSubItem> WUPSStorageSubItem::CreateSubItem(std::string_view key, WUPSStorageError &err) noexcept {
wups_storage_item outItem = {};
err = WUPSStorageAPI_CreateSubItem(mHandle, key.data(), &outItem);

View File

@ -1,8 +1,6 @@
#include "catch2/catch_test_macros.hpp"
#include "utils/logger.h"
#include "utils/utils.h"
#include <coreinit/ios.h>
#include <sys/stat.h>
#include <vector>
#include <wups/storage.h>
template<typename T>
@ -153,7 +151,7 @@ void CheckItemSize(const char *key, const T &expectedVal, const uint32_t expecte
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
uint32_t itemSize = 0;
res = WUPSStorageAPI::GetItemSize(key, itemSize);
res = WUPSStorageAPI::GetItemSize<T>(key, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(itemSize == expectedItemSize);
@ -164,7 +162,7 @@ void CheckItemSize(const char *key, const T &expectedVal, const uint32_t expecte
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
itemSize = 0;
res = WUPSStorageAPI::GetItemSize(key, itemSize);
res = WUPSStorageAPI::GetItemSize<T>(key, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(itemSize == expectedItemSize);
}
@ -194,9 +192,34 @@ TEST_CASE("Test getSize fails with other type") {
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
uint32_t itemSize = 0;
res = WUPSStorageAPI::GetItemSize(key, itemSize);
res = WUPSStorageAPI::GetItemSize<std::string>(key, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE);
REQUIRE(itemSize == 0);
itemSize = 0;
res = WUPSStorageAPI::GetItemSize<std::vector<uint8_t>>(key, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE);
REQUIRE(itemSize == 0);
}
TEST_CASE("Saving Base64 string as string works") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
constexpr auto *key = "test";
std::string storeStr = "97bb3f3b0ec8755028e5cdf56eaf01b47140a6cd";
res = WUPSStorageAPI::Store<std::string>(key, storeStr);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
res = WUPSStorageAPI::SaveStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
res = WUPSStorageAPI::ForceReloadStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::string retValue;
res = WUPSStorageAPI::Get(key, retValue);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(storeStr, retValue));
}
TEST_CASE("Delete non-existent item should fail") {
@ -475,7 +498,7 @@ TEST_CASE("Get (size) works after changing type to binary") {
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
uint32_t itemSize = 0;
res = WUPSStorageAPI::GetItemSize(itemName, itemSize);
res = WUPSStorageAPI::GetItemSize<std::string>(itemName, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE);
REQUIRE(itemSize == 0);
@ -485,7 +508,7 @@ TEST_CASE("Get (size) works after changing type to binary") {
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
itemSize = 0;
res = WUPSStorageAPI::GetItemSize(itemName, itemSize);
res = WUPSStorageAPI::GetItemSize<std::vector<uint8_t>>(itemName, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(itemSize == binaryDataSize);
@ -511,7 +534,7 @@ TEST_CASE("Change type from binary to int causes get size to fail") {
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
uint32_t itemSize = 0;
res = WUPSStorageAPI::GetItemSize(itemName, itemSize);
res = WUPSStorageAPI::GetItemSize<std::vector<uint8_t>>(itemName, itemSize);
REQUIRE(res == WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE);
REQUIRE(itemSize == 0);
}
@ -601,7 +624,7 @@ TEST_CASE("Store string and load it as binary fails") {
REQUIRE(res == WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE);
}
TEST_CASE("Get binary fails if buffer is too small") {
TEST_CASE("USE_EXISTING_BUFFER: Get binary fails if buffer is too small") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
@ -614,11 +637,11 @@ TEST_CASE("Get binary fails if buffer is too small") {
std::vector<uint8_t> readBinaryData;
REQUIRE(!binaryData.empty());
readBinaryData.resize(binaryData.size() - 1); // Make the buffer just a bit to small
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData);
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL);
}
TEST_CASE("Get binary works with exact buffer size") {
TEST_CASE("USE_EXISTING_BUFFER: Get binary works with exact buffer size") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
@ -631,12 +654,62 @@ TEST_CASE("Get binary works with exact buffer size") {
std::vector<uint8_t> readBinaryData;
REQUIRE(!binaryData.empty());
readBinaryData.resize(binaryData.size());
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData);
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(binaryData, readBinaryData));
}
TEST_CASE("Get string fails if buffer is too small") {
TEST_CASE("USE_EXISTING_BUFFER: Get binary works if buffer is too big") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
constexpr auto *itemName = "item";
std::vector<uint8_t> binaryData = {8, 4, 5, 4, 12, 4, 2};
res = WUPSStorageAPI::Store<std::vector<uint8_t>>(itemName, binaryData);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::vector<uint8_t> readBinaryData;
REQUIRE(!binaryData.empty());
readBinaryData.resize(binaryData.size() * 2);
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(binaryData, readBinaryData));
}
void test_binary_get_RESIZE_EXISTING_BUFFER_generic(std::vector<uint8_t> &binaryData, uint32_t buffer_size) {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
constexpr auto *itemName = "item";
res = WUPSStorageAPI::Store<std::vector<uint8_t>>(itemName, binaryData);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::vector<uint8_t> readBinaryData;
REQUIRE(!binaryData.empty());
readBinaryData.resize(buffer_size);
res = WUPSStorageAPI::Get<std::vector<uint8_t>>(itemName, readBinaryData, WUPSStorageAPI::GetOptions::RESIZE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(binaryData, readBinaryData));
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get binary fails if buffer is empty") {
std::vector<uint8_t> binaryData = {8, 4, 5, 4, 12, 4, 2};
test_binary_get_RESIZE_EXISTING_BUFFER_generic(binaryData, 0);
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get binary works with exact buffer size") {
std::vector<uint8_t> binaryData = {8, 4, 5, 4, 12, 4, 2};
test_binary_get_RESIZE_EXISTING_BUFFER_generic(binaryData, binaryData.size());
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get binary works if buffer is too big") {
std::vector<uint8_t> binaryData = {8, 4, 5, 4, 12, 4, 2};
test_binary_get_RESIZE_EXISTING_BUFFER_generic(binaryData, binaryData.size() * 2);
}
TEST_CASE("USE_EXISTING_BUFFER: Get string fails if buffer is too small") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
@ -650,11 +723,11 @@ TEST_CASE("Get string fails if buffer is too small") {
REQUIRE(!strData.empty());
// Make the buffer just a bit to small
readStr.resize(strData.length() - 1);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL);
}
TEST_CASE("Get string works with exact buffer size") {
TEST_CASE("USE_EXISTING_BUFFER: Get string works with exact buffer size") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
@ -668,11 +741,62 @@ TEST_CASE("Get string works with exact buffer size") {
REQUIRE(!strData.empty());
// We need to add one byte because of the null terminator
readStr.resize(strData.length() + 1);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(strData, readStr));
}
TEST_CASE("USE_EXISTING_BUFFER: Get string works if buffer it too big.") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
constexpr auto *itemName = "item";
std::string strData = "Random string I just need for this test.";
res = WUPSStorageAPI::Store<std::string>(itemName, strData);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::string readStr;
REQUIRE(!strData.empty());
// We need to add one byte because of the null terminator
readStr.resize(strData.length() * 2);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr, WUPSStorageAPI::GetOptions::USE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(strData, readStr));
}
void test_string_get_RESIZE_EXISTING_BUFFER_generic(std::string &strData, uint32_t buffer_size) {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
constexpr auto *itemName = "item";
res = WUPSStorageAPI::Store<std::string>(itemName, strData);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
std::string readStr;
REQUIRE(!strData.empty());
readStr.resize(buffer_size);
res = WUPSStorageAPI::Get<std::string>(itemName, readStr, WUPSStorageAPI::GetOptions::RESIZE_EXISTING_BUFFER);
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);
REQUIRE(isEqual(strData, readStr));
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get string works if buffer is too small") {
std::string strData = "Random string I just need for this test.";
test_string_get_RESIZE_EXISTING_BUFFER_generic(strData, 0);
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get string works if buffer is perfect") {
std::string strData = "Random string I just need for this test.";
test_string_get_RESIZE_EXISTING_BUFFER_generic(strData, strData.length() + 1); //add null terminator
}
TEST_CASE("RESIZE_EXISTING_BUFFER: Get string works if buffer is too big") {
std::string strData = "Random string I just need for this test.";
test_string_get_RESIZE_EXISTING_BUFFER_generic(strData, strData.length() * 2);
}
TEST_CASE("Create two sub-items, storing in first still works") {
auto res = WUPSStorageAPI::WipeStorage();
REQUIRE(res == WUPS_STORAGE_ERROR_SUCCESS);