Skip to content

Commit 3b64575

Browse files
Merge pull request #2 from JakubAndrysek/test_nvs
2 parents f788911 + 834e85c commit 3b64575

File tree

2 files changed

+126
-20
lines changed

2 files changed

+126
-20
lines changed

tests/validation/nvs/nvs.ino

Lines changed: 120 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,141 @@
1+
#include <Arduino.h>
12
#include <Preferences.h>
23

4+
5+
struct TestData {
6+
uint8_t id;
7+
uint16_t value;
8+
};
9+
310
Preferences preferences;
411

12+
void validate_types() {
13+
assert(preferences.getType("char") == PT_I8);
14+
assert(preferences.getType("uchar") == PT_U8);
15+
assert(preferences.getType("short") == PT_I16);
16+
assert(preferences.getType("ushort") == PT_U16);
17+
assert(preferences.getType("int") == PT_I32);
18+
assert(preferences.getType("uint") == PT_U32);
19+
assert(preferences.getType("long") == PT_I32);
20+
assert(preferences.getType("ulong") == PT_U32);
21+
assert(preferences.getType("long64") == PT_I64);
22+
assert(preferences.getType("ulong64") == PT_U64);
23+
assert(preferences.getType("float") == PT_BLOB);
24+
assert(preferences.getType("double") == PT_BLOB);
25+
assert(preferences.getType("bool") == PT_U8);
26+
assert(preferences.getType("str") == PT_STR);
27+
assert(preferences.getType("strLen") == PT_STR);
28+
assert(preferences.getType("struct") == PT_BLOB);
29+
}
30+
31+
// Function to increment string values
32+
void incrementStringValues(String &val_string, char *val_string_buf, size_t buf_size) {
33+
// Extract the number from string and increment it
34+
val_string = "str" + String(val_string.substring(3).toInt() + 1);
35+
36+
// Extract the number from strLen and increment it
37+
String strLen_str = String(val_string_buf);
38+
int strLen_num = strLen_str.substring(6).toInt();
39+
snprintf(val_string_buf, buf_size, "strLen%d", strLen_num + 1);
40+
}
41+
542
void setup() {
643
Serial.begin(115200);
7-
844
while (!Serial) {
945
;
1046
}
1147

1248
preferences.begin("my-app", false);
1349

14-
// Get the counter value, if the key does not exist, return a default value of 0
15-
unsigned int counter = preferences.getUInt("counter", 0);
50+
// Get the preferences value and if not exists, use default parameter
51+
char val_char = preferences.getChar("char", 'A');
52+
unsigned char val_uchar = preferences.getUChar("uchar", 0);
53+
int16_t val_short = preferences.getShort("short", 0);
54+
uint16_t val_ushort = preferences.getUShort("ushort", 0);
55+
int32_t val_int = preferences.getInt("int", 0);
56+
uint32_t val_uint = preferences.getUInt("uint", 0);
57+
int64_t val_long = preferences.getLong("long", 0);
58+
uint32_t val_ulong = preferences.getULong("ulong", 0);
59+
int64_t val_long64 = preferences.getLong64("long64", 0);
60+
uint64_t val_ulong64 = preferences.getULong64("ulong64", 0);
61+
float val_float = preferences.getFloat("float", 0.0f);
62+
double val_double = preferences.getDouble("double", 0.0);
63+
bool val_bool = preferences.getBool("bool", false);
1664

17-
// Print the counter to Serial Monitor
18-
Serial.printf("Current counter value: %u\n", counter);
65+
// Strings
66+
String val_string = preferences.getString("str", "str0");
67+
char val_string_buf[20] = "strLen0";
68+
preferences.getString("strLen", val_string_buf, sizeof(val_string_buf));
1969

20-
// Increase counter by 1
21-
counter++;
70+
// Structure data
71+
TestData test_data = {0, 0};
2272

23-
// Store the counter to the Preferences
24-
preferences.putUInt("counter", counter);
73+
size_t struct_size = preferences.getBytes("struct", &test_data, sizeof(test_data));
74+
if (struct_size == 0) {
75+
// First time - set initial values using parameter names
76+
test_data.id = 1;
77+
test_data.value = 100;
78+
}
2579

26-
// Close the Preferences
27-
preferences.end();
80+
Serial.printf("Values from Preferences: ");
81+
Serial.printf("char: %c | uchar: %u | short: %d | ushort: %u | int: %ld | uint: %lu | ",
82+
val_char, val_uchar, val_short, val_ushort, val_int, val_uint);
83+
Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ",
84+
val_long, val_ulong, val_long64, val_ulong64);
85+
Serial.printf("float: %.2f | double: %.2f | bool: %s | str: %s | strLen: %s | struct: {id:%u,val:%u}\n",
86+
val_float, val_double, val_bool ? "true" : "false", val_string.c_str(), val_string_buf, test_data.id, test_data.value);
2887

29-
// Wait 1 second
30-
delay(1000);
3188

32-
// Restart ESP
89+
// Increment the values
90+
val_char += 1; // Increment char A -> B
91+
val_uchar += 1;
92+
val_short += 1;
93+
val_ushort += 1;
94+
val_int += 1;
95+
val_uint += 1;
96+
val_long += 1;
97+
val_ulong += 1;
98+
val_long64 += 1;
99+
val_ulong64 += 1;
100+
val_float += 1.1f;
101+
val_double += 1.1;
102+
val_bool = !val_bool; // Toggle boolean value
103+
104+
// Increment string values using function
105+
incrementStringValues(val_string, val_string_buf, sizeof(val_string_buf));
106+
107+
test_data.id += 1;
108+
test_data.value += 10;
109+
110+
// Store the updated values back to Preferences
111+
preferences.putChar("char", val_char);
112+
preferences.putUChar("uchar", val_uchar);
113+
preferences.putShort("short", val_short);
114+
preferences.putUShort("ushort", val_ushort);
115+
preferences.putInt("int", val_int);
116+
preferences.putUInt("uint", val_uint);
117+
preferences.putLong("long", val_long);
118+
preferences.putULong("ulong", val_ulong);
119+
preferences.putLong64("long64", val_long64);
120+
preferences.putULong64("ulong64", val_ulong64);
121+
preferences.putFloat("float", val_float);
122+
preferences.putDouble("double", val_double);
123+
preferences.putBool("bool", val_bool);
124+
preferences.putString("str", val_string);
125+
preferences.putString("strLen", val_string_buf);
126+
preferences.putBytes("struct", &test_data, sizeof(test_data));
127+
128+
// Check if the keys exist
129+
assert(preferences.isKey("char"));
130+
assert(preferences.isKey("struct"));
131+
132+
// Validate the types of the keys
133+
validate_types();
134+
135+
// Close the Preferences, wait and restart
136+
preferences.end();
137+
Serial.flush();
138+
delay(1000);
33139
ESP.restart();
34140
}
35141

tests/validation/nvs/test_nvs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
def test_nvs(dut):
55
LOGGER = logging.getLogger(__name__)
66

7-
LOGGER.info("Expecting counter value 0")
8-
dut.expect_exact("Current counter value: 0")
7+
LOGGER.info("Expecting default values from Preferences")
8+
dut.expect_exact("Values from Preferences: char: A | uchar: 0 | short: 0 | ushort: 0 | int: 0 | uint: 0 | long: 0 | ulong: 0 | long64: 0 | ulong64: 0 | float: 0.00 | double: 0.00 | bool: false | str: str0 | strLen: strLen0 | struct: {id:1,val:100}")
99

10-
LOGGER.info("Expecting counter value 1")
11-
dut.expect_exact("Current counter value: 1")
10+
LOGGER.info("Expecting updated preferences for the first time")
11+
dut.expect_exact("Values from Preferences: char: B | uchar: 1 | short: 1 | ushort: 1 | int: 1 | uint: 1 | long: 1 | ulong: 1 | long64: 1 | ulong64: 1 | float: 1.10 | double: 1.10 | bool: true | str: str1 | strLen: strLen1 | struct: {id:2,val:110}")
1212

13-
LOGGER.info("Expecting counter value 2")
14-
dut.expect_exact("Current counter value: 2")
13+
LOGGER.info("Expecting updated preferences for the second time")
14+
dut.expect_exact("Values from Preferences: char: C | uchar: 2 | short: 2 | ushort: 2 | int: 2 | uint: 2 | long: 2 | ulong: 2 | long64: 2 | ulong64: 2 | float: 2.20 | double: 2.20 | bool: false | str: str2 | strLen: strLen2 | struct: {id:3,val:120}")

0 commit comments

Comments
 (0)