nanoMODBUS/tests/modbusino_tests.h
Valerio De Benedetto 4605782fc7 Changes
2022-01-20 13:02:23 +01:00

70 lines
1.9 KiB
C

#include "modbusino.h"
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
unsigned int nesting = 0;
#define should(s) \
{ \
for (int i = 0; i < nesting; i++) { \
printf("\t"); \
} \
printf("Should %s\n", (s)); \
}
#define expect(c) assert(c)
#define check(err) (expect((err) == MBSN_ERROR_NONE))
#define reset(mbsn) (memset(&(mbsn), 0, sizeof(mbsn_t)))
#define test(f) (nesting++, (f), nesting--)
const uint8_t TEST_SERVER_ADDR = 1;
uint64_t now_ms() {
struct timespec ts = {0, 0};
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
return (uint64_t) (ts.tv_sec) * 1000 + (uint64_t) (ts.tv_nsec) / 1000000;
}
int read_byte_empty(uint8_t* b, int32_t timeout) {
return 0;
}
int write_byte_empty(uint8_t b, int32_t timeout) {
return 0;
}
int read_byte_timeout_1s(uint8_t* b, int32_t timeout) {
usleep(timeout * 1000);
return 0;
}
int read_byte_timeout_third(uint8_t* b, int32_t timeout) {
static int stage = 0;
switch (stage) {
case 0:
case 1:
*b = 1;
stage++;
return 1;
case 2:
assert(timeout > 0);
usleep(timeout * 1000 + 100 * 1000);
stage = 0;
return 0;
default:
stage = 0;
return -1;
}
}