|
| 1 | +/* |
| 2 | + * This file is part of Arduino_KVStore. |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Arduino SA |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <catch2/catch_test_macros.hpp> |
| 12 | +#include <catch2/catch_template_test_macros.hpp> |
| 13 | + |
| 14 | +#include <kvstore/kvstore.h> |
| 15 | +#include <map> |
| 16 | +#include <cstdint> |
| 17 | +#include <cstring> |
| 18 | +#include <stdexcept> |
| 19 | + |
| 20 | +class KVStore: public KVStoreInterface { |
| 21 | +public: |
| 22 | + bool begin() { return true; } |
| 23 | + bool end() { return true; } |
| 24 | + bool clear(); |
| 25 | + |
| 26 | + typename KVStoreInterface::res_t remove(const Key& key) override; |
| 27 | + bool exists(const Key& key) const override; |
| 28 | + typename KVStoreInterface::res_t putBytes(const Key& key, const uint8_t b[], size_t s) override; |
| 29 | + typename KVStoreInterface::res_t getBytes(const Key& key, uint8_t b[], size_t s) const override; |
| 30 | + size_t getBytesLength(const Key& key) const override; |
| 31 | + |
| 32 | +private: |
| 33 | + std::map<Key, std::pair<uint8_t*, size_t>> kvmap; |
| 34 | +}; |
| 35 | + |
| 36 | +bool KVStore::clear() { |
| 37 | + kvmap.clear(); |
| 38 | + |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +typename KVStoreInterface::res_t KVStore::remove(const Key& key) { |
| 43 | + auto el = kvmap.at(key); |
| 44 | + kvmap.erase(key); |
| 45 | + |
| 46 | + delete [] el.first; |
| 47 | + |
| 48 | + return 1; |
| 49 | +} |
| 50 | + |
| 51 | +bool KVStore::exists(const Key& key) const { |
| 52 | + try { |
| 53 | + kvmap.at(key); |
| 54 | + return true; |
| 55 | + } catch(const std::out_of_range&) { |
| 56 | + return false; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +typename KVStoreInterface::res_t KVStore::putBytes(const Key& key, const uint8_t b[], size_t s) { |
| 61 | + if(exists(key)) { |
| 62 | + remove(key); |
| 63 | + } |
| 64 | + |
| 65 | + uint8_t* buf = new uint8_t[s]; |
| 66 | + std::memset(buf, 0, s); |
| 67 | + std::memcpy(buf, b, s); |
| 68 | + |
| 69 | + kvmap[key] = {buf, s}; |
| 70 | + |
| 71 | + return s; |
| 72 | +} |
| 73 | + |
| 74 | +typename KVStoreInterface::res_t KVStore::getBytes(const Key& key, uint8_t b[], size_t s) const { |
| 75 | + auto el = kvmap.at(key); |
| 76 | + |
| 77 | + std::memcpy(b, el.first, s <= el.second? s : el.second); |
| 78 | + |
| 79 | + return el.second; |
| 80 | +} |
| 81 | + |
| 82 | +size_t KVStore::getBytesLength(const Key& key) const { |
| 83 | + auto el = kvmap.at(key); |
| 84 | + |
| 85 | + return el.second; |
| 86 | +} |
| 87 | + |
| 88 | +TEST_CASE( "KVStore can store values of different types, get them and remove them", "[kvstore][putgetremove]" ) { |
| 89 | + KVStore store; |
| 90 | + store.begin(); |
| 91 | + |
| 92 | + SECTION( "adding a char and getting it back" ) { |
| 93 | + char value = 'A'; |
| 94 | + |
| 95 | + REQUIRE( store.putChar("0", value) == sizeof(value)); |
| 96 | + REQUIRE( store.getChar("0") == value ); |
| 97 | + REQUIRE( store.remove("0") == 1 ); |
| 98 | + } |
| 99 | + |
| 100 | + SECTION( "adding a uchar and getting it back" ) { |
| 101 | + unsigned char value = 0x55; |
| 102 | + |
| 103 | + REQUIRE( store.putUChar("0", value) == sizeof(value)); |
| 104 | + REQUIRE( store.getUChar("0") == value ); |
| 105 | + REQUIRE( store.remove("0") == 1 ); |
| 106 | + } |
| 107 | + |
| 108 | + SECTION( "adding a short and getting it back" ) { |
| 109 | + short value = 0x5555; |
| 110 | + |
| 111 | + REQUIRE( store.putShort("0", value) == sizeof(value)); |
| 112 | + REQUIRE( store.getShort("0") == value ); |
| 113 | + REQUIRE( store.remove("0") == 1 ); |
| 114 | + } |
| 115 | + |
| 116 | + SECTION( "adding an unsigned short and getting it back" ) { |
| 117 | + unsigned short value = 0x5555; |
| 118 | + |
| 119 | + REQUIRE( store.putUShort("0", value) == sizeof(value)); |
| 120 | + REQUIRE( store.getUShort("0") == value ); |
| 121 | + REQUIRE( store.remove("0") == 1 ); |
| 122 | + } |
| 123 | + |
| 124 | + SECTION( "adding an uint32_t and getting it back" ) { |
| 125 | + uint32_t value = 0x01020304; |
| 126 | + |
| 127 | + REQUIRE( store.putUInt("0", value) == sizeof(value)); |
| 128 | + REQUIRE( store.getUInt("0") == value ); |
| 129 | + REQUIRE( store.remove("0") == 1 ); |
| 130 | + } |
| 131 | + |
| 132 | + SECTION( "adding a string and getting it back" ) { |
| 133 | + char value[] = "pippo"; |
| 134 | + char res[6]; |
| 135 | + |
| 136 | + REQUIRE( store.putString("0", value) == strlen(value)); |
| 137 | + |
| 138 | + store.getString("0", res, 6); |
| 139 | + REQUIRE( strcmp(res, value) == 0 ); |
| 140 | + REQUIRE( store.remove("0") == 1 ); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +TEST_CASE( "KVStore references are a useful tool to indirectly access kvstore", "[kvstore][references]" ) { |
| 147 | + KVStore store; |
| 148 | + store.begin(); |
| 149 | + |
| 150 | + REQUIRE( store.put("0", (uint8_t) 0x55) == 1); |
| 151 | + REQUIRE( store.put("1", (uint16_t) 0x5555) == 2); |
| 152 | + REQUIRE( store.put("2", (uint32_t) 0x55555555) == 4); |
| 153 | + REQUIRE( store.put("3", (uint32_t) 0x55555555) == 4); |
| 154 | + |
| 155 | + SECTION( "I can get an uint8_t reference and update its value indirectly" ) { |
| 156 | + auto ref = store.get<uint8_t>("0"); |
| 157 | + |
| 158 | + REQUIRE( ref == 0x55 ); |
| 159 | + ref = 0x56; |
| 160 | + |
| 161 | + REQUIRE( store.getUChar("0") == 0x56 ); |
| 162 | + } |
| 163 | + |
| 164 | + SECTION( "I can get an uint16_t reference and update its value indirectly" ) { |
| 165 | + auto ref = store.get<uint16_t>("1"); |
| 166 | + |
| 167 | + REQUIRE( ref == 0x5555 ); |
| 168 | + ref = 0x5656; |
| 169 | + |
| 170 | + REQUIRE( store.getUShort("1") == 0x5656 ); |
| 171 | + } |
| 172 | + |
| 173 | + SECTION( "I can get an uint32_t reference and update its value indirectly" ) { |
| 174 | + auto ref = store.get<uint32_t>("2"); |
| 175 | + |
| 176 | + REQUIRE( ref == 0x55555555 ); |
| 177 | + ref = 0x56565656; |
| 178 | + |
| 179 | + REQUIRE( store.getUInt("2") == 0x56565656 ); |
| 180 | + } |
| 181 | + |
| 182 | + SECTION( "If I update the value from a reference I am able to load it from another one" ) { |
| 183 | + auto ref1 = store.get<uint32_t>("3"); |
| 184 | + auto ref2 = store.get<uint32_t>("3"); |
| 185 | + |
| 186 | + REQUIRE(ref1 == 0x55555555); |
| 187 | + REQUIRE(ref2 == 0x55555555); |
| 188 | + |
| 189 | + ref1 = 0x56565656; |
| 190 | + ref2.load(); |
| 191 | + |
| 192 | + REQUIRE(ref2 == 0x56565656); |
| 193 | + } |
| 194 | + |
| 195 | + REQUIRE( store.remove("0") == 1 ); |
| 196 | + REQUIRE( store.remove("1") == 1 ); |
| 197 | + REQUIRE( store.remove("2") == 1 ); |
| 198 | + REQUIRE( store.remove("3") == 1 ); |
| 199 | +} |
0 commit comments