Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95ae8cf

Browse files
feat(test): Enhance NVS test (#11481)
* feat(test): Enhance NVS test * fix(nvs): Remove unused Unity header and improve Serial wait loop * refactor(nvs): Extract string increment logic into a separate function * refactor(test): Format long strings in expect_exact calls for better readability
1 parent dc82467 commit 95ae8cf

File tree

2 files changed

+136
-20
lines changed

2 files changed

+136
-20
lines changed

‎tests/validation/nvs/nvs.ino‎

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

4+
struct TestData {
5+
uint8_t id;
6+
uint16_t value;
7+
};
8+
39
Preferences preferences;
410

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

1247
preferences.begin("my-app", false);
1348

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

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

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

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

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

29-
// Wait 1 second
30-
delay(1000);
87+
// Increment the values
88+
val_char += 1; // Increment char A -> B
89+
val_uchar += 1;
90+
val_short += 1;
91+
val_ushort += 1;
92+
val_int += 1;
93+
val_uint += 1;
94+
val_long += 1;
95+
val_ulong += 1;
96+
val_long64 += 1;
97+
val_ulong64 += 1;
98+
val_float += 1.1f;
99+
val_double += 1.1;
100+
val_bool = !val_bool; // Toggle boolean value
101+
102+
// Increment string values using function
103+
incrementStringValues(val_string, val_string_buf, sizeof(val_string_buf));
104+
105+
test_data.id += 1;
106+
test_data.value += 10;
107+
108+
// Store the updated values back to Preferences
109+
preferences.putChar("char", val_char);
110+
preferences.putUChar("uchar", val_uchar);
111+
preferences.putShort("short", val_short);
112+
preferences.putUShort("ushort", val_ushort);
113+
preferences.putInt("int", val_int);
114+
preferences.putUInt("uint", val_uint);
115+
preferences.putLong("long", val_long);
116+
preferences.putULong("ulong", val_ulong);
117+
preferences.putLong64("long64", val_long64);
118+
preferences.putULong64("ulong64", val_ulong64);
119+
preferences.putFloat("float", val_float);
120+
preferences.putDouble("double", val_double);
121+
preferences.putBool("bool", val_bool);
122+
preferences.putString("str", val_string);
123+
preferences.putString("strLen", val_string_buf);
124+
preferences.putBytes("struct", &test_data, sizeof(test_data));
31125

32-
// Restart ESP
126+
// Check if the keys exist
127+
assert(preferences.isKey("char"));
128+
assert(preferences.isKey("struct"));
129+
130+
// Validate the types of the keys
131+
validate_types();
132+
133+
// Close the Preferences, wait and restart
134+
preferences.end();
135+
Serial.flush();
136+
delay(1000);
33137
ESP.restart();
34138
}
35139

‎tests/validation/nvs/test_nvs.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
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(
9+
"Values from Preferences: char: A | uchar: 0 | short: 0 | ushort: 0 | int: 0 | uint: 0 | long: 0 | ulong: 0 | "
10+
"long64: 0 | ulong64: 0 | float: 0.00 | double: 0.00 | bool: false | str: str0 | strLen: strLen0 | "
11+
"struct: {id:1,val:100}"
12+
)
913

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

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /