Skip to content

Commit 229bef1

Browse files
committed
Squashed commit of the following:
commit 101ee55 Author: Asbelos <asbelos@btinternet.com> Date: Mon Aug 4 20:46:49 2025 +0100 ZTEST2 and ZTEST3 commit 21446af Author: Asbelos <asbelos@btinternet.com> Date: Fri Aug 1 22:22:45 2025 +0100 ztest experimant
1 parent e598496 commit 229bef1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Ztest.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <Arduino.h>
2+
#include "DIAG.h"
3+
#include "Ztest.h"
4+
#include "DCCEXParser.h"
5+
#include "StringFormatter.h"
6+
7+
RingStream * Ztest::ring = new RingStream(128);
8+
9+
void Ztest::parse(const FSH * command, const FSH * expect, bool (*test)() ) {
10+
11+
DIAG(F("ZTEST %S"), command);
12+
13+
// create copy of command in RAM
14+
auto commandLength= STRLEN_P((char *)command) + 1;
15+
char commandBuffer[commandLength+1];
16+
STRNCPY_P(commandBuffer, (PGM_P)command,commandLength+1);
17+
18+
if (expect) {
19+
// create response buffer to collect comparison
20+
ring->flush();
21+
ring->mark(0); // mark the start of the response
22+
DCCEXParser::parseOne(ring, (byte *)commandBuffer, ring);
23+
ring->commit();
24+
}
25+
else {
26+
// run without output test
27+
DCCEXParser::parseOne(& USB_SERIAL, (byte *)commandBuffer,nullptr);
28+
}
29+
30+
31+
// test the assert of side effects
32+
if (test) {
33+
auto result=test();
34+
DIAG(F("ZTEST assert %S"), result ? F("OK") : F("FAILED"));
35+
}
36+
37+
if (expect) {
38+
// Copy output to serial and check with expected
39+
ring->read(); // read redundant client id
40+
auto responseLength=ring->count();
41+
if (responseLength < 1) {
42+
DIAG(F("!!....(no resp) ZTEST"));
43+
return; // no response
44+
}
45+
46+
// show output while comparing
47+
char output[responseLength+1];
48+
for (int16_t i=0;i<responseLength;i++) output[i]=ring->read();
49+
output[responseLength]=0;
50+
DIAG(F("ZTEST response:%s"), output);
51+
DIAG(F("ZTEST expect :%S"), (char *)expect);
52+
if (STRCMP_P(output, (char *)expect) != 0)
53+
DIAG(F("ZTEST expect failed\n"));
54+
55+
}
56+
57+
}

Ztest.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef ztest_h
2+
#define ztest_h
3+
#include "RingStream.h"
4+
5+
class Ztest {
6+
public:
7+
static void parse(const FSH * command, const FSH * expect, bool (*test)() );
8+
9+
private:
10+
static RingStream * ring;
11+
};
12+
13+
#endif

0 commit comments

Comments
 (0)