Public Access
Add native unit tests for the Nano firmware
- New native environment: pio test -e native - test/mocks: fake Arduino core, EEPROM and recording BROSE9323 driver - test/test_nano: 25 tests covering every command, bitmaps, bad input, flip time storage and the periodic full redraw - Board environments ignore tests so they are never uploaded Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
80f3f78152
commit
8415dee347
8 files changed
+505
-4
No files matched your search
@@ -0,0 +1,55 @@
|
||||
// Minimal Arduino API for running the Nano code on the PC (native test environment)
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
|
||||
#define HEX 16
|
||||
|
||||
class __FlashStringHelper;
|
||||
#define F(s) ((const __FlashStringHelper *)(s))
|
||||
|
||||
// Time is controlled by the tests
|
||||
inline uint32_t fake_millis = 0;
|
||||
inline uint32_t millis() { return fake_millis; }
|
||||
inline void delay(uint32_t ms) { fake_millis += ms; }
|
||||
|
||||
// Serial port: tests push bytes into `in` and read replies from `out`
|
||||
class FakeSerial
|
||||
{
|
||||
public:
|
||||
std::deque<char> in;
|
||||
std::string out;
|
||||
|
||||
void begin(unsigned long) {}
|
||||
int available() { return in.size(); }
|
||||
int read()
|
||||
{
|
||||
if (in.empty())
|
||||
return -1;
|
||||
char c = in.front();
|
||||
in.pop_front();
|
||||
return (uint8_t)c;
|
||||
}
|
||||
void flush() {}
|
||||
|
||||
void print(const __FlashStringHelper *s) { out += (const char *)s; }
|
||||
void print(const char *s) { out += s; }
|
||||
void print(int v, int base = 10)
|
||||
{
|
||||
char buf[16];
|
||||
snprintf(buf, sizeof(buf), base == HEX ? "%X" : "%d", v);
|
||||
out += buf;
|
||||
}
|
||||
void println(const __FlashStringHelper *s) { print(s); out += "\n"; }
|
||||
void println(const char *s) { print(s); out += "\n"; }
|
||||
void println(unsigned long v) { out += std::to_string(v) + "\n"; }
|
||||
};
|
||||
|
||||
using Print = FakeSerial;
|
||||
|
||||
inline FakeSerial Serial;
|
||||
@@ -0,0 +1,58 @@
|
||||
// Fake display driver for the native tests: records every call instead of flipping dots
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
struct DisplayCall
|
||||
{
|
||||
std::string what;
|
||||
int a, b, c;
|
||||
};
|
||||
|
||||
class BROSE9323
|
||||
{
|
||||
public:
|
||||
std::vector<DisplayCall> calls;
|
||||
bool direct = false;
|
||||
|
||||
BROSE9323(uint8_t, uint8_t, uint8_t, uint16_t) {}
|
||||
|
||||
void begin() { log("begin"); }
|
||||
void display(bool force = false) { log("display", force); }
|
||||
void drawPixel(int16_t x, int16_t y, uint16_t color) { log(direct ? "directPixel" : "pixel", x, y, color); }
|
||||
void fillScreen(uint16_t color) { log("fill", color); }
|
||||
void setDirect(bool d) { direct = d; }
|
||||
void setTiming(uint16_t t) { log("timing", t); }
|
||||
void printBuffer() { log("printBuffer"); }
|
||||
void setCursor(int16_t x, int16_t y) { log("cursor", x, y); }
|
||||
void setTextColor(uint16_t color, uint16_t background) { log("textColor", color, background); }
|
||||
void setTextSize(uint8_t size) { log("textSize", size); }
|
||||
size_t write(uint8_t ch)
|
||||
{
|
||||
log("write", ch);
|
||||
return 1;
|
||||
}
|
||||
void println() { log("newline"); }
|
||||
|
||||
// Helpers for the tests
|
||||
int count(const std::string &what) const
|
||||
{
|
||||
int n = 0;
|
||||
for (const DisplayCall &call : calls)
|
||||
n += call.what == what;
|
||||
return n;
|
||||
}
|
||||
const DisplayCall *last(const std::string &what) const
|
||||
{
|
||||
for (auto it = calls.rbegin(); it != calls.rend(); ++it)
|
||||
if (it->what == what)
|
||||
return &*it;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
void log(const char *what, int a = 0, int b = 0, int c = 0) { calls.push_back({what, a, b, c}); }
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
// In-memory EEPROM for the native tests (starts erased, all 0xFF like a new chip)
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
class FakeEEPROM
|
||||
{
|
||||
public:
|
||||
uint8_t mem[1024];
|
||||
|
||||
FakeEEPROM() { erase(); }
|
||||
void erase() { memset(mem, 0xFF, sizeof(mem)); }
|
||||
|
||||
template <typename T>
|
||||
T &get(int addr, T &t)
|
||||
{
|
||||
memcpy(&t, mem + addr, sizeof(T));
|
||||
return t;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &put(int addr, const T &t)
|
||||
{
|
||||
memcpy(mem + addr, &t, sizeof(T));
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
inline FakeEEPROM EEPROM;
|
||||
Reference in new issue
Block a user