Skip to content

Commit 3dafe2f

Browse files
Defining Test suite
1 parent d0406d3 commit 3dafe2f

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed

Diff for: extras/test/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(testArduinoKVStore)
3+
4+
Include(FetchContent)
5+
6+
FetchContent_Declare(
7+
Catch2
8+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
9+
GIT_TAG v3.4.0
10+
)
11+
12+
FetchContent_MakeAvailable(Catch2)
13+
14+
set(TEST_TARGET ${CMAKE_PROJECT_NAME})
15+
16+
##########################################################################
17+
18+
set(CMAKE_CXX_STANDARD 11)
19+
20+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
21+
22+
include_directories(../../src)
23+
24+
set(TEST_SRCS
25+
src/kvstore/test_kvstore.cpp
26+
src/kvstore/test_kvstore_type.cpp
27+
)
28+
29+
set(TEST_DUT_SRCS
30+
../../src/kvstore/kvstore.cpp
31+
)
32+
##########################################################################
33+
34+
add_compile_definitions(HOST)
35+
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
36+
add_compile_options(-Wno-cast-function-type)
37+
38+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
39+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy")
40+
41+
add_executable( ${TEST_TARGET} ${TEST_SRCS} ${TEST_DUT_SRCS} )
42+
target_compile_definitions( ${TEST_TARGET} PUBLIC SOURCE_DIR="${CMAKE_SOURCE_DIR}" )
43+
44+
target_link_libraries( ${TEST_TARGET} Catch2WithMain )

Diff for: extras/test/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# adding tests
2+
3+
follow guide in https://github.com/catchorg/Catch2/tree/devel/docs in order to add more tests
4+
5+
Add the source file for the test in `extras/test/CMakeLists.txt` inside of `${TEST_SRCS}` variable and eventually the source file you want to test in `${TEST_DUT_SRCS}`

Diff for: extras/test/src/kvstore/test_kvstore.cpp

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
}

Diff for: extras/test/src/kvstore/test_kvstore_type.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
13+
#include <kvstore/kvstore.h>
14+
15+
TEST_CASE( "Testing KVStore getType utility function", "[kvstore][utility]" ) {
16+
17+
REQUIRE(KVStoreInterface::getType((int8_t) 0x55) == KVStoreInterface::PT_I8);
18+
REQUIRE(KVStoreInterface::getType((uint8_t) 0x55) == KVStoreInterface::PT_U8);
19+
REQUIRE(KVStoreInterface::getType((int16_t) 0x55) == KVStoreInterface::PT_I16);
20+
REQUIRE(KVStoreInterface::getType((uint16_t) 0x55) == KVStoreInterface::PT_U16);
21+
REQUIRE(KVStoreInterface::getType((int32_t) 0x55) == KVStoreInterface::PT_I32);
22+
REQUIRE(KVStoreInterface::getType((uint32_t) 0x55) == KVStoreInterface::PT_U32);
23+
REQUIRE(KVStoreInterface::getType((int64_t) 0x55) == KVStoreInterface::PT_I64);
24+
REQUIRE(KVStoreInterface::getType((uint64_t) 0x55) == KVStoreInterface::PT_U64);
25+
REQUIRE(KVStoreInterface::getType((bool) true) == KVStoreInterface::PT_I8);
26+
27+
28+
char string[] = "my test string";
29+
REQUIRE(KVStoreInterface::getType(string) == KVStoreInterface::PT_STR);
30+
31+
char* cstr = nullptr;
32+
REQUIRE(KVStoreInterface::getType(cstr) == KVStoreInterface::PT_STR);
33+
REQUIRE(KVStoreInterface::getType("my test string") == KVStoreInterface::PT_STR);
34+
35+
uint8_t buf[] = { 0x55, 0x55 };
36+
REQUIRE(KVStoreInterface::getType(buf) == KVStoreInterface::PT_BLOB);
37+
// REQUIRE(KVStoreInterface::getType() == KVStoreInterface::PT_INVALID);
38+
}

0 commit comments

Comments
 (0)