@@ -40,9 +40,12 @@ bool KVStore::clear() {
40
40
}
41
41
42
42
typename KVStoreInterface::res_t KVStore::remove (const Key& key) {
43
+ auto el = kvmap.at (key);
43
44
kvmap.erase (key);
44
45
45
- return 0 ;
46
+ delete [] el.first ;
47
+
48
+ return 1 ;
46
49
}
47
50
48
51
bool KVStore::exists (const Key& key) const {
@@ -55,6 +58,10 @@ bool KVStore::exists(const Key& key) const {
55
58
}
56
59
57
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
+
58
65
uint8_t * buf = new uint8_t [s];
59
66
std::memset (buf, 0 , s);
60
67
std::memcpy (buf, b, s);
@@ -87,39 +94,39 @@ TEST_CASE( "KVStore can store values of different types, get them and remove the
87
94
88
95
REQUIRE ( store.putChar (" 0" , value) == sizeof (value));
89
96
REQUIRE ( store.getChar (" 0" ) == value );
90
- REQUIRE ( store.remove (" 0" ) == 0 );
97
+ REQUIRE ( store.remove (" 0" ) == 1 );
91
98
}
92
99
93
100
SECTION ( " adding a uchar and getting it back" ) {
94
101
unsigned char value = 0x55 ;
95
102
96
103
REQUIRE ( store.putUChar (" 0" , value) == sizeof (value));
97
104
REQUIRE ( store.getUChar (" 0" ) == value );
98
- REQUIRE ( store.remove (" 0" ) == 0 );
105
+ REQUIRE ( store.remove (" 0" ) == 1 );
99
106
}
100
107
101
108
SECTION ( " adding a short and getting it back" ) {
102
109
short value = 0x5555 ;
103
110
104
111
REQUIRE ( store.putShort (" 0" , value) == sizeof (value));
105
112
REQUIRE ( store.getShort (" 0" ) == value );
106
- REQUIRE ( store.remove (" 0" ) == 0 );
113
+ REQUIRE ( store.remove (" 0" ) == 1 );
107
114
}
108
115
109
116
SECTION ( " adding an unsigned short and getting it back" ) {
110
117
unsigned short value = 0x5555 ;
111
118
112
119
REQUIRE ( store.putUShort (" 0" , value) == sizeof (value));
113
120
REQUIRE ( store.getUShort (" 0" ) == value );
114
- REQUIRE ( store.remove (" 0" ) == 0 );
121
+ REQUIRE ( store.remove (" 0" ) == 1 );
115
122
}
116
123
117
124
SECTION ( " adding an uint32_t and getting it back" ) {
118
125
uint32_t value = 0x01020304 ;
119
126
120
127
REQUIRE ( store.putUInt (" 0" , value) == sizeof (value));
121
128
REQUIRE ( store.getUInt (" 0" ) == value );
122
- REQUIRE ( store.remove (" 0" ) == 0 );
129
+ REQUIRE ( store.remove (" 0" ) == 1 );
123
130
}
124
131
125
132
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
130
137
131
138
store.getString (" 0" , res, 6 );
132
139
REQUIRE ( strcmp (res, value) == 0 );
133
- REQUIRE ( store.remove (" 0" ) == 0 );
140
+ REQUIRE ( store.remove (" 0" ) == 1 );
134
141
}
135
142
}
136
143
@@ -184,4 +191,9 @@ TEST_CASE( "KVStore references are a useful tool to indirectly access kvstore",
184
191
185
192
REQUIRE (ref2 == 0x56565656 );
186
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 );
187
199
}
0 commit comments