-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(test): Enhance NVS test DEMO #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
49af25a
e15e7f7
e14204c
834e85c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,35 +1,131 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <Arduino.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <Preferences.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <unity.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct TestData { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint8_t id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uint16_t value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Preferences preferences; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void validate_types() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("char") == PT_I8); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("uchar") == PT_U8); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("short") == PT_I16); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("ushort") == PT_U16); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("int") == PT_I32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("uint") == PT_U32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("long") == PT_I32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("ulong") == PT_U32); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("long64") == PT_I64); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("ulong64") == PT_U64); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("float") == PT_BLOB); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("double") == PT_BLOB); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("bool") == PT_U8); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("str") == PT_STR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("strLen") == PT_STR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(preferences.getType("struct") == PT_BLOB); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void setup() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Serial.begin(115200); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (!Serial) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
while (!Serial) {} | |
unsigned long startTime = millis(); | |
while (!Serial && (millis() - startTime < 5000)) {} |
Copilot uses AI. Check for mistakes.
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Using the Arduino String
class on constrained devices can fragment heap memory over time. For a stability-focused demo, consider using a fixed-size char
buffer and preferences.getString(key, buf, bufSize)
instead.
String val_string = preferences.getString("str", "str0"); | |
char val_string[20] = "str0"; // Fixed-size buffer for stability | |
preferences.getString("str", val_string, sizeof(val_string)); |
Copilot uses AI. Check for mistakes.
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All serial data may not flush before the ESP.restart()
call; consider adding Serial.flush()
or a short delay after printing to ensure complete transmission.
Copilot uses AI. Check for mistakes.
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using %lld
for val_long
(which comes from getLong
, typically a 32-bit value) can lead to mismatched output. Either switch to %ld
for 32-bit long
values or consistently use 64-bit APIs/types (e.g., getLong64
) if you intend 64-bit storage.
Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ", | |
Serial.printf("long: %ld | ulong: %lu | long64: %lld | ulong64: %llu | ", |
Copilot uses AI. Check for mistakes.
Outdated
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Extract the logic for incrementing the string and numeric part into a helper function to improve code readability and ease future modifications.
Copilot uses AI. Check for mistakes.
Copilot
AI
Jun 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There’s a long sequence of individual put*
calls for each type; consider abstracting this into a helper function or loop to reduce duplication and improve maintainability.
preferences.putChar("char", val_char); | |
preferences.putUChar("uchar", val_uchar); | |
preferences.putShort("short", val_short); | |
preferences.putUShort("ushort", val_ushort); | |
preferences.putInt("int", val_int); | |
preferences.putUInt("uint", val_uint); | |
preferences.putLong("long", val_long); | |
preferences.putULong("ulong", val_ulong); | |
preferences.putLong64("long64", val_long64); | |
preferences.putULong64("ulong64", val_ulong64); | |
preferences.putFloat("float", val_float); | |
preferences.putDouble("double", val_double); | |
preferences.putBool("bool", val_bool); | |
preferences.putString("str", val_string); | |
preferences.putString("strLen", val_string_buf); | |
preferences.putBytes("struct", &test_data, sizeof(test_data)); | |
struct PreferenceItem { | |
const char* key; | |
void* value; | |
const char* type; | |
}; | |
PreferenceItem items[] = { | |
{"char", &val_char, "char"}, | |
{"uchar", &val_uchar, "uchar"}, | |
{"short", &val_short, "short"}, | |
{"ushort", &val_ushort, "ushort"}, | |
{"int", &val_int, "int"}, | |
{"uint", &val_uint, "uint"}, | |
{"long", &val_long, "long"}, | |
{"ulong", &val_ulong, "ulong"}, | |
{"long64", &val_long64, "long64"}, | |
{"ulong64", &val_ulong64, "ulong64"}, | |
{"float", &val_float, "float"}, | |
{"double", &val_double, "double"}, | |
{"bool", &val_bool, "bool"}, | |
{"str", &val_string, "str"}, | |
{"strLen", &val_string_buf, "strLen"}, | |
{"struct", &test_data, "struct"} | |
}; | |
for (const auto& item : items) { | |
storePreference(item.key, item.value, item.type); | |
} |
Copilot uses AI. Check for mistakes.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,11 @@ | |
def test_nvs(dut): | ||
LOGGER = logging.getLogger(__name__) | ||
|
||
LOGGER.info("Expecting counter value 0") | ||
dut.expect_exact("Current counter value: 0") | ||
LOGGER.info("Expecting default values from Preferences") | ||
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}") | ||
|
||
LOGGER.info("Expecting counter value 1") | ||
dut.expect_exact("Current counter value: 1") | ||
LOGGER.info("Expecting updated preferences for the first time") | ||
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}") | ||
|
||
LOGGER.info("Expecting counter value 2") | ||
dut.expect_exact("Current counter value: 2") | ||
LOGGER.info("Expecting updated preferences for the second time") | ||
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}") | ||
Comment on lines
+8
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Using an exact match on such a long, hard-coded output string can make the test brittle; consider matching critical segments or using a regex to verify key values instead. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
Comment on lines
+8
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] This exact-match string is very long and makes the test fragile. Consider using a regex or splitting the check into multiple smaller Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the C
assert
macro here may not integrate well with the Unity framework; consider usingTEST_ASSERT_EQUAL_INT(PT_I8, preferences.getType("char"))
for clearer test reporting.Copilot uses AI. Check for mistakes.