arduino-photometrics/test/EEPROM_test_struct.cpp
2025-12-09 12:46:12 +01:00

366 lines
14 KiB
C++

#include <Arduino.h>
#include <unity.h>
#include "storage_interface.h"
#include <EEPROM.h>
uint32_t epoch = 1764249314;
// WARNING : copy from storage interface (if somes changes are apply on storageinterface's OFFSET_ apply it here too)
static constexpr uint8_t OFFSET_MEASURES_SCH = 1, OFFSET_NB_PHOTO_SENSOR = 2, OFFSET_NB_TEMP_SENSOR = 3, OFFSET_PHOTO_MEASURES_SIZE = 4, OFFSET_TEMP_MEASURES_SIZE = 5, OFFSET_NB_MEASURES = 6, OFFSET_NEXT_PACKAGE = 8, OFFSET_START_DATA_MEASURES = 10; // unit in byte
static constexpr uint16_t MEGA_2560_EEPROM_SIZE = 4096;
void test_set_struct(){
Storage_interface test;
uint16_t offset = 0;
uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
/**** classic case test ****/
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
uint8_t flags;
flags = EEPROM.read(offset);
// if package flag right set
TEST_ASSERT(flags & 0b10000000);
// test schedule flags
TEST_ASSERT(flags & 0b01000000);
TEST_ASSERT(!(flags & 0b00100000));
// test schedule
TEST_ASSERT(EEPROM.read(offset + OFFSET_MEASURES_SCH) == schedule);
// test photo res flag
TEST_ASSERT(flags & 0b00010000);
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_PHOTO_SENSOR) == nb_photo_sensor);
// test temp flag
TEST_ASSERT(flags & 0b00001000);
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_TEMP_SENSOR) == nb_temp_sensor);
// test if this package is the latest write
TEST_ASSERT(flags & 0b00000100);
// test type size write
TEST_ASSERT(EEPROM.read(offset + OFFSET_PHOTO_MEASURES_SIZE) == sizeofuint8);
TEST_ASSERT(EEPROM.read(offset + OFFSET_TEMP_MEASURES_SIZE) == sizeofuint8);
/**** partial schedule timestamp case test ****/
schedule = 3;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
flags = EEPROM.read(offset);
// test schedule flags
TEST_ASSERT(!(flags & 0b01000000));
TEST_ASSERT(flags & 0b00100000);
// test schedule
TEST_ASSERT(EEPROM.read(offset + OFFSET_MEASURES_SCH) == schedule);
/**** without timestamp case test ****/
schedule = 0;
timestamp = false;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
flags = EEPROM.read(offset);
// test schedule flags
TEST_ASSERT(!(flags & 0b01000000));
TEST_ASSERT(!(flags & 0b00100000));
// test schedule
TEST_ASSERT(EEPROM.read(offset + OFFSET_MEASURES_SCH) == schedule);
/**** without a type of sensor ****/
schedule = 0;
timestamp = true;
photo_sensor = true;
nb_photo_sensor = 14;
temp_sensor = false;
nb_temp_sensor = 0;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
flags = EEPROM.read(offset);
// test photo res flag
TEST_ASSERT(flags & 0b00010000);
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_PHOTO_SENSOR) == nb_photo_sensor);
// test temp flag
TEST_ASSERT(!(flags & 0b00001000));
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_TEMP_SENSOR) == nb_temp_sensor);
photo_sensor = false;
nb_photo_sensor = 0;
temp_sensor = true;
nb_temp_sensor = 15;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
flags = EEPROM.read(offset);
// test photo res flag
TEST_ASSERT(!(flags & 0b00010000));
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_PHOTO_SENSOR) == nb_photo_sensor);
// test temp flag
TEST_ASSERT(flags & 0b00001000);
TEST_ASSERT(EEPROM.read(offset + OFFSET_NB_TEMP_SENSOR) == nb_temp_sensor);
}
void test_get_struct(){
Storage_interface test;
uint16_t offset = 0, r_next_package, r_nb_measures;
uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
/**** classic case test ****/
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
uint8_t r_schedule, r_nb_photo_sensor, r_nb_temp_sensor, r_sizeof_temp, r_sizeof_photo;
bool r_timestamp, r_is_final, r_photo_sensor, r_temp_sensor;
//uint8_t flags;
//flags = EEPROM.read(offset);
test.get_struct(offset, &r_timestamp, &r_is_final, &r_photo_sensor, &r_temp_sensor, &r_schedule, &r_nb_photo_sensor, &r_nb_temp_sensor, &r_sizeof_photo, &r_sizeof_temp, &r_next_package, &r_nb_measures);
// test if this package is the latest write
TEST_ASSERT(r_is_final);
//EEPROM.get(offset + OFFSET_NB_MEASURES, r_nb_measures);
TEST_ASSERT(EEPROM.read(offset + OFFSET_PHOTO_MEASURES_SIZE) == sizeofuint8);
TEST_ASSERT(r_nb_measures == 0);
TEST_ASSERT(r_timestamp == timestamp);
TEST_ASSERT(r_photo_sensor == photo_sensor);
TEST_ASSERT(r_temp_sensor == temp_sensor);
TEST_ASSERT(r_schedule == schedule);
TEST_ASSERT(r_nb_photo_sensor == nb_photo_sensor);
TEST_ASSERT(r_nb_temp_sensor == nb_temp_sensor);
TEST_ASSERT(r_sizeof_temp == sizeofuint8);
TEST_ASSERT(r_sizeof_photo == sizeofuint8);
}
void test_add_measure(){
// test if every values is normaly store
Storage_interface test;
uint16_t offset = 0, reading_head;
uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
// Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head
EEPROM.get(offset, reading_head);
bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor];
for (int i = 0; i < nb_photo_sensor; i++){
photo_val_array[i] = i;
}
for (int i = 0; i < nb_temp_sensor; i++){
temp_val_array[i] = i;
}
test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
uint32_t horo;
EEPROM.get(offset + OFFSET_START_DATA_MEASURES, horo);
TEST_ASSERT(horo == epoch);
for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t); i < nb_photo_sensor; i++, reading_head += sizeofuint8){
TEST_ASSERT(EEPROM.read(reading_head) == photo_val_array[i]);
}
for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t) + sizeofuint8 * nb_photo_sensor; i < nb_temp_sensor; i++, reading_head += sizeofuint8){
TEST_ASSERT(EEPROM.read(reading_head) == temp_val_array[i]);
}
}
// void test_put_measure(){
// Storage_interface test;
// uint16_t offset = 0, reading_head;
// uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
// // Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head
// EEPROM.get(offset, reading_head);
// bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
// test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
// uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor];
// for (int i = 0; i < nb_photo_sensor; i++){
// photo_val_array[i] = i;
// }
// for (int i = 0; i < nb_temp_sensor; i++){
// temp_val_array[i] = i;
// }
// test.put_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
// uint32_t horo;
// EEPROM.get(offset + OFFSET_START_DATA_MEASURES, horo);
// TEST_ASSERT(horo == epoch);
// for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t); i < nb_photo_sensor; i++, reading_head += sizeofuint8){
// TEST_ASSERT(EEPROM.read(reading_head) == photo_val_array[i]);
// }
// for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t) + sizeofuint8 * nb_photo_sensor; i < nb_temp_sensor; i++, reading_head += sizeofuint8){
// TEST_ASSERT(EEPROM.read(reading_head) == temp_val_array[i]);
// }
// }
// void test_get_measure(){
// Storage_interface test;
// uint16_t offset = 0, reading_head;
// uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
// // Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head
// EEPROM.get(offset, reading_head);
// bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
// test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
// uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor];
// for (int i = 0; i < nb_photo_sensor; i++){
// photo_val_array[i] = i;
// }
// for (int i = 0; i < nb_temp_sensor; i++){
// temp_val_array[i] = i;
// }
// // test on first measure of the first package
// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
// uint32_t v_epoch;
// uint8_t v_photo_val_array[nb_photo_sensor], v_temp_val_array[nb_temp_sensor];
// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0);
// TEST_ASSERT(v_epoch == epoch);
// for (int i = 0; i < nb_photo_sensor; i++){
// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
// }
// for (int i = 0; i < nb_temp_sensor; i++){
// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
// }
// // test on the second measure of the first package
// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1);
// TEST_ASSERT(v_epoch == epoch);
// for (int i = 0; i < nb_photo_sensor; i++){
// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
// }
// for (int i = 0; i < nb_temp_sensor; i++){
// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
// }
// // test on the first measure of the seconde package
// test.add_last_package(timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
// test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
// test.get_measure(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0);
// TEST_ASSERT(v_epoch == epoch);
// for (int i = 0; i < nb_photo_sensor; i++){
// TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
// }
// for (int i = 0; i < nb_temp_sensor; i++){
// TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
// }
// }
void test_get_measure_at(){
Storage_interface test;
uint16_t offset = 0, reading_head;
uint8_t schedule = 0, nb_photo_sensor = 2, nb_temp_sensor = 1, sizeofuint8 = sizeof(uint8_t);
// Useless folowing EEPROM.GET, here only for avoid wrong flags of unused variable of reading_head
EEPROM.get(offset, reading_head);
bool timestamp = true, is_final = true, photo_sensor = true, temp_sensor = true;
test.set_struct(offset, timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
uint8_t photo_val_array[nb_photo_sensor], temp_val_array[nb_temp_sensor];
for (int i = 0; i < nb_photo_sensor; i++){
photo_val_array[i] = i;
}
for (int i = 0; i < nb_temp_sensor; i++){
temp_val_array[i] = i;
}
// test on first measure of the first package
test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
uint32_t v_epoch;
uint8_t v_photo_val_array[nb_photo_sensor], v_temp_val_array[nb_temp_sensor];
test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0, 0);
TEST_ASSERT(v_epoch == epoch);
for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t); i < nb_photo_sensor; i++, reading_head += sizeofuint8){
TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
}
for (int i = 0, reading_head = offset + OFFSET_START_DATA_MEASURES + sizeof(uint32_t) + sizeofuint8 * nb_photo_sensor; i < nb_temp_sensor; i++, reading_head += sizeofuint8){
TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
}
// test on the second measure of the first package
test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 0, 1);
TEST_ASSERT(v_epoch == epoch);
for (int i = 0; i < nb_photo_sensor; i++){
TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
}
for (int i = 0; i < nb_temp_sensor; i++){
TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
}
// test on the first measure of the seconde package
test.add_last_package(timestamp, is_final, photo_sensor, temp_sensor, schedule, nb_photo_sensor, nb_temp_sensor, sizeofuint8, sizeofuint8);
test.add_measure(photo_val_array, temp_val_array, epoch, nb_photo_sensor, nb_temp_sensor);
test.get_measure_at(v_photo_val_array, v_temp_val_array, &v_epoch, &nb_photo_sensor, &nb_temp_sensor, 1, 0);
TEST_ASSERT(v_epoch == epoch);
for (int i = 0; i < nb_photo_sensor; i++){
TEST_ASSERT(v_photo_val_array[i] == photo_val_array[i]);
}
for (int i = 0; i < nb_temp_sensor; i++){
TEST_ASSERT(v_temp_val_array[i] == temp_val_array[i]);
}
}
void setup(void)
{
Serial.begin(9600);
}
//WARNING: Tests are not exhaustive and do not cover all possibilities.
int main( int argc, char **argv) {
UNITY_BEGIN();
// RUN_TEST(test_get_measure);
RUN_TEST(test_get_measure_at);
RUN_TEST(test_add_measure);
RUN_TEST(test_get_struct);
RUN_TEST(test_set_struct);
// RUN_TEST(test_put_measure);
UNITY_END();
}