Skip to content

Commit ed86e47

Browse files
differentiating C string between arduino String class in example
1 parent 8dc056b commit ed86e47

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

examples/PreferencesValidation/PreferencesValidation.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,25 @@ void setup() {
222222
Serial.println();
223223
Serial.println();
224224

225-
Serial.println("Testing String operations");
225+
Serial.println("Testing C - String operations");
226226
if(!test_preferences("la mia stringsa asdasdafasdjdsnajdnaskjlasda\n\n\r\nsdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjl\r\nOK\r\nERROR",
227227
&preferences)) {
228-
Serial.println("String test failed");
228+
Serial.println("C - String test failed");
229+
preferences.remove(KEY);
230+
}
231+
232+
Serial.println();
233+
Serial.println();
234+
Serial.println();
235+
236+
Serial.println("Testing Arduino - String operations");
237+
if(!test_preferences<String>("la mia stringsa asdasdafasdjdsnajdnaskjlasda\n\n\r\nsdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjl\r\nOK\r\nERROR",
238+
std::bind(static_cast<size_t(Preferences::*)(const char*, String)>(&Preferences::putString), &preferences, std::placeholders::_1, std::placeholders::_2),
239+
[&preferences](const char* key) { // we need a lambda, because it requires a default value from apis
240+
return preferences.getString(key);
241+
}, &preferences)) {
242+
243+
Serial.println("Arduino - String test failed");
229244
preferences.remove(KEY);
230245
}
231246

examples/PreferencesValidation/test.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,70 @@ bool test_preferences(T value,
6262
return true;
6363
}
6464

65+
template<>
66+
bool test_preferences(String value,
67+
std::function<size_t(const char*, String)> putf,
68+
std::function<String(const char*)> getf,
69+
Preferences *preferences) {
70+
// we are going now to test all the apis of kvstore:
71+
// .1 We check that the key do not exist
72+
// .2 we put a value inside the KVStore
73+
// .3 we check that the size returned is correct
74+
// .4 we check again the key exists
75+
// .5 we get the value contained
76+
// .6 we compare the value with what we inserted at the beginning
77+
// .7 we remove the value
78+
79+
Serial.println("Testing isKey()");
80+
if(preferences->isKey(KEY)) {
81+
Serial.println("[Error] kvstore already contains a key");
82+
return false;
83+
}
84+
85+
Serial.println("Testing put()");
86+
size_t s;
87+
if((s=putf(KEY, value)) != value.length()) {
88+
Serial.println("[Error] kvstore put returned a size that is different from the expected one: ");
89+
Serial.print(s);
90+
Serial.print(" != ");
91+
Serial.println(value.length());
92+
return false;
93+
}
94+
95+
Serial.println("Testing isKey()");
96+
if(!preferences->isKey(KEY)) {
97+
Serial.println("[Error] The inserted key is not present in the KVStore");
98+
return false;
99+
}
100+
101+
Serial.println("Testing getBytesLength()");
102+
if((s=preferences->getBytesLength(KEY)) != value.length()+1) {
103+
Serial.println("[Error] The length of the value do not match with the expected one");
104+
Serial.print(s);
105+
Serial.print(" != ");
106+
Serial.println(value.length()+1);
107+
return false;
108+
}
109+
110+
Serial.println("Testing getf()");
111+
String val;
112+
if((val = getf(KEY)) != value) {
113+
Serial.print("[Error] get of the previously inserted value returned a wrong value: ");
114+
Serial.print(val);
115+
Serial.print(" != ");
116+
Serial.println(value);
117+
return false;
118+
}
119+
120+
Serial.println("Testing remove()");
121+
if(!preferences->remove(KEY)) {
122+
Serial.println("[Error] Failed removing the inserted key");
123+
return false;
124+
}
125+
126+
return true;
127+
}
128+
65129
bool test_preferences(char* value, Preferences *preferences) {
66130
// we are going now to test all the apis of preferences:
67131
// .1 We check that the key do not exist

0 commit comments

Comments
 (0)