Skip to content

Commit 3c78d1a

Browse files
Defining Test suite
1 parent d0406d3 commit 3c78d1a

File tree

4 files changed

+274
-0
lines changed

4 files changed

+274
-0
lines changed

extras/test/CMakeLists.txt

Lines changed: 44 additions & 0 deletions
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 )

extras/test/README.md

Lines changed: 5 additions & 0 deletions
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}`
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
kvmap.erase(key);
44+
45+
return 0;
46+
}
47+
48+
bool KVStore::exists(const Key& key) const {
49+
try {
50+
kvmap.at(key);
51+
return true;
52+
} catch(const std::out_of_range&) {
53+
return false;
54+
}
55+
}
56+
57+
typename KVStoreInterface::res_t KVStore::putBytes(const Key& key, const uint8_t b[], size_t s) {
58+
uint8_t* buf = new uint8_t[s];
59+
std::memset(buf, 0, s);
60+
std::memcpy(buf, b, s);
61+
62+
kvmap[key] = {buf, s};
63+
64+
return s;
65+
}
66+
67+
typename KVStoreInterface::res_t KVStore::getBytes(const Key& key, uint8_t b[], size_t s) const {
68+
auto el = kvmap.at(key);
69+
70+
std::memcpy(b, el.first, s <= el.second? s : el.second);
71+
72+
return el.second;
73+
}
74+
75+
size_t KVStore::getBytesLength(const Key& key) const {
76+
auto el = kvmap.at(key);
77+
78+
return el.second;
79+
}
80+
81+
TEST_CASE( "KVStore can store values of different types, get them and remove them", "[kvstore][putgetremove]" ) {
82+
KVStore store;
83+
store.begin();
84+
85+
SECTION( "adding a char and getting it back" ) {
86+
char value = 'A';
87+
88+
REQUIRE( store.putChar("0", value) == sizeof(value));
89+
REQUIRE( store.getChar("0") == value );
90+
REQUIRE( store.remove("0") == 0 );
91+
}
92+
93+
SECTION( "adding a uchar and getting it back" ) {
94+
unsigned char value = 0x55;
95+
96+
REQUIRE( store.putUChar("0", value) == sizeof(value));
97+
REQUIRE( store.getUChar("0") == value );
98+
REQUIRE( store.remove("0") == 0 );
99+
}
100+
101+
SECTION( "adding a short and getting it back" ) {
102+
short value = 0x5555;
103+
104+
REQUIRE( store.putShort("0", value) == sizeof(value));
105+
REQUIRE( store.getShort("0") == value );
106+
REQUIRE( store.remove("0") == 0 );
107+
}
108+
109+
SECTION( "adding an unsigned short and getting it back" ) {
110+
unsigned short value = 0x5555;
111+
112+
REQUIRE( store.putUShort("0", value) == sizeof(value));
113+
REQUIRE( store.getUShort("0") == value );
114+
REQUIRE( store.remove("0") == 0 );
115+
}
116+
117+
SECTION( "adding an uint32_t and getting it back" ) {
118+
uint32_t value = 0x01020304;
119+
120+
REQUIRE( store.putUInt("0", value) == sizeof(value));
121+
REQUIRE( store.getUInt("0") == value );
122+
REQUIRE( store.remove("0") == 0 );
123+
}
124+
125+
SECTION( "adding a string and getting it back" ) {
126+
char value[] = "pippo";
127+
char res[6];
128+
129+
REQUIRE( store.putString("0", value) == strlen(value));
130+
131+
store.getString("0", res, 6);
132+
REQUIRE( strcmp(res, value) == 0 );
133+
REQUIRE( store.remove("0") == 0 );
134+
}
135+
}
136+
137+
138+
139+
TEST_CASE( "KVStore references are a useful tool to indirectly access kvstore", "[kvstore][references]" ) {
140+
KVStore store;
141+
store.begin();
142+
143+
REQUIRE( store.put("0", (uint8_t) 0x55) == 1);
144+
REQUIRE( store.put("1", (uint16_t) 0x5555) == 2);
145+
REQUIRE( store.put("2", (uint32_t) 0x55555555) == 4);
146+
REQUIRE( store.put("3", (uint32_t) 0x55555555) == 4);
147+
148+
SECTION( "I can get an uint8_t reference and update its value indirectly" ) {
149+
auto ref = store.get<uint8_t>("0");
150+
151+
REQUIRE( ref == 0x55 );
152+
ref = 0x56;
153+
154+
REQUIRE( store.getUChar("0") == 0x56 );
155+
}
156+
157+
SECTION( "I can get an uint16_t reference and update its value indirectly" ) {
158+
auto ref = store.get<uint16_t>("1");
159+
160+
REQUIRE( ref == 0x5555 );
161+
ref = 0x5656;
162+
163+
REQUIRE( store.getUShort("1") == 0x5656 );
164+
}
165+
166+
SECTION( "I can get an uint32_t reference and update its value indirectly" ) {
167+
auto ref = store.get<uint32_t>("2");
168+
169+
REQUIRE( ref == 0x55555555 );
170+
ref = 0x56565656;
171+
172+
REQUIRE( store.getUInt("2") == 0x56565656 );
173+
}
174+
175+
SECTION( "If I update the value from a reference I am able to load it from another one" ) {
176+
auto ref1 = store.get<uint32_t>("3");
177+
auto ref2 = store.get<uint32_t>("3");
178+
179+
REQUIRE(ref1 == 0x55555555);
180+
REQUIRE(ref2 == 0x55555555);
181+
182+
ref1 = 0x56565656;
183+
ref2.load();
184+
185+
REQUIRE(ref2 == 0x56565656);
186+
}
187+
}
Lines changed: 38 additions & 0 deletions
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)