@@ -40,9 +40,12 @@ bool KVStore::clear() {
4040}
4141
4242typename KVStoreInterface::res_t KVStore::remove (const Key& key) {
43+ auto el = kvmap.at (key);
4344 kvmap.erase (key);
4445
45- return 0 ;
46+ delete [] el.first ;
47+
48+ return 1 ;
4649}
4750
4851bool KVStore::exists (const Key& key) const {
@@ -55,6 +58,10 @@ bool KVStore::exists(const Key& key) const {
5558}
5659
5760typename KVStoreInterface::res_t KVStore::putBytes (const Key& key, const uint8_t b[], size_t s) {
61+ if (exists (key)) {
62+ remove (key);
63+ }
64+
5865 uint8_t * buf = new uint8_t [s];
5966 std::memset (buf, 0 , s);
6067 std::memcpy (buf, b, s);
@@ -87,39 +94,39 @@ TEST_CASE( "KVStore can store values of different types, get them and remove the
8794
8895 REQUIRE ( store.putChar (" 0" , value) == sizeof (value));
8996 REQUIRE ( store.getChar (" 0" ) == value );
90- REQUIRE ( store.remove (" 0" ) == 0 );
97+ REQUIRE ( store.remove (" 0" ) == 1 );
9198 }
9299
93100 SECTION ( " adding a uchar and getting it back" ) {
94101 unsigned char value = 0x55 ;
95102
96103 REQUIRE ( store.putUChar (" 0" , value) == sizeof (value));
97104 REQUIRE ( store.getUChar (" 0" ) == value );
98- REQUIRE ( store.remove (" 0" ) == 0 );
105+ REQUIRE ( store.remove (" 0" ) == 1 );
99106 }
100107
101108 SECTION ( " adding a short and getting it back" ) {
102109 short value = 0x5555 ;
103110
104111 REQUIRE ( store.putShort (" 0" , value) == sizeof (value));
105112 REQUIRE ( store.getShort (" 0" ) == value );
106- REQUIRE ( store.remove (" 0" ) == 0 );
113+ REQUIRE ( store.remove (" 0" ) == 1 );
107114 }
108115
109116 SECTION ( " adding an unsigned short and getting it back" ) {
110117 unsigned short value = 0x5555 ;
111118
112119 REQUIRE ( store.putUShort (" 0" , value) == sizeof (value));
113120 REQUIRE ( store.getUShort (" 0" ) == value );
114- REQUIRE ( store.remove (" 0" ) == 0 );
121+ REQUIRE ( store.remove (" 0" ) == 1 );
115122 }
116123
117124 SECTION ( " adding an uint32_t and getting it back" ) {
118125 uint32_t value = 0x01020304 ;
119126
120127 REQUIRE ( store.putUInt (" 0" , value) == sizeof (value));
121128 REQUIRE ( store.getUInt (" 0" ) == value );
122- REQUIRE ( store.remove (" 0" ) == 0 );
129+ REQUIRE ( store.remove (" 0" ) == 1 );
123130 }
124131
125132 SECTION ( " adding a string and getting it back" ) {
@@ -130,7 +137,7 @@ TEST_CASE( "KVStore can store values of different types, get them and remove the
130137
131138 store.getString (" 0" , res, 6 );
132139 REQUIRE ( strcmp (res, value) == 0 );
133- REQUIRE ( store.remove (" 0" ) == 0 );
140+ REQUIRE ( store.remove (" 0" ) == 1 );
134141 }
135142}
136143
@@ -184,4 +191,9 @@ TEST_CASE( "KVStore references are a useful tool to indirectly access kvstore",
184191
185192 REQUIRE (ref2 == 0x56565656 );
186193 }
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 );
187199}
0 commit comments