mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-13 11:29:58 +03:00
nmeaPrintf Unittest
This commit is contained in:
parent
a0d2b3e493
commit
3b67689721
2 changed files with 86 additions and 0 deletions
|
@ -167,6 +167,10 @@ flight_mixer_unittest := \
|
||||||
gps_conversion_unittest_SRC := \
|
gps_conversion_unittest_SRC := \
|
||||||
$(USER_DIR)/common/gps_conversion.c
|
$(USER_DIR)/common/gps_conversion.c
|
||||||
|
|
||||||
|
gps_nmea_unittest_SRC := \
|
||||||
|
$(USER_DIR)/io/gps_utils.c \
|
||||||
|
$(USER_DIR)/common/printf.c \
|
||||||
|
$(USER_DIR)/common/typeconversion.c
|
||||||
|
|
||||||
io_serial_unittest_SRC := \
|
io_serial_unittest_SRC := \
|
||||||
$(USER_DIR)/io/serial.c \
|
$(USER_DIR)/io/serial.c \
|
||||||
|
|
82
src/test/unit/gps_nmea_unittest.cc
Normal file
82
src/test/unit/gps_nmea_unittest.cc
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Cleanflight.
|
||||||
|
*
|
||||||
|
* Cleanflight is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Cleanflight is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "platform.h"
|
||||||
|
|
||||||
|
#include "drivers/serial.h"
|
||||||
|
#include "io/serial.h"
|
||||||
|
|
||||||
|
#include "io/gps_utils.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "unittest_macros.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
static serialPort_t *gpsPort;
|
||||||
|
static int serialWritePos = 0;
|
||||||
|
|
||||||
|
#define SERIAL_BUFFER_SIZE 256
|
||||||
|
static uint8_t serialWriteBuffer[SERIAL_BUFFER_SIZE];
|
||||||
|
|
||||||
|
serialPort_t serialTestInstance;
|
||||||
|
|
||||||
|
void serialWrite(serialPort_t *instance, uint8_t ch)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(instance, &serialTestInstance);
|
||||||
|
EXPECT_LT(serialWritePos, sizeof(serialWriteBuffer));
|
||||||
|
serialWriteBuffer[serialWritePos++] = ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialWriteBuf(serialPort_t *instance, const uint8_t *data, int count)
|
||||||
|
{
|
||||||
|
while(count--)
|
||||||
|
serialWrite(instance, *data++);
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialTestResetBuffers()
|
||||||
|
{
|
||||||
|
gpsPort = &serialTestInstance;
|
||||||
|
memset(&serialWriteBuffer, 0, sizeof(serialWriteBuffer));
|
||||||
|
serialWritePos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// list of valid NMEA commands
|
||||||
|
const char * nmeaSamples[] = {
|
||||||
|
"$PUBX,41,1,0003,0001,38400,0*26\r\n",
|
||||||
|
"$PSRF103,00,6,00,0*23\r\n",
|
||||||
|
"$PCAS02,100*1E\r\n",
|
||||||
|
"$PSRF103,03,00,00,01*27\r\n",
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(nmeaUtilsTest, TestNmeaPrintf)
|
||||||
|
{
|
||||||
|
for (unsigned i = 0; i < ARRAYLEN(nmeaSamples); i++) {
|
||||||
|
serialTestResetBuffers();
|
||||||
|
const char* sample = nmeaSamples[i];
|
||||||
|
int starPos = strcspn(sample, "*");
|
||||||
|
EXPECT_EQ(strlen(sample) - strlen("*XX\r\n"), starPos);
|
||||||
|
const char* tx = strndup(sample, starPos);
|
||||||
|
nmeaPrintf(gpsPort, "%s", tx);
|
||||||
|
serialWrite(gpsPort, 0); // terminate string for simplicity
|
||||||
|
free((void*)tx);
|
||||||
|
ASSERT_STREQ((const char*)serialWriteBuffer, sample);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue