1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-16 12:55:19 +03:00

Adding unit test for some gps conversion code to demystify it.

This commit is contained in:
Dominic Clifton 2014-05-24 17:54:57 +01:00
parent b81f73cb5d
commit b0e1c934d4
6 changed files with 112 additions and 43 deletions

View file

@ -26,11 +26,11 @@ TEST_DIR = .
CPPFLAGS += -isystem $(GTEST_DIR)/inc
# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread
CXXFLAGS += -g -Wall -Wextra -pthread -ggdb -O0
# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = battery_unittest flight_imu_unittest
TESTS = battery_unittest flight_imu_unittest gps_conversion_unittest
# All Google Test headers. Usually you shouldn't change this
# definition.
@ -96,3 +96,14 @@ flight_imu_unittest.o : $(TEST_DIR)/flight_imu_unittest.cc \
flight_imu_unittest : flight_imu.o flight_imu_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
gps_conversion.o : $(USER_DIR)/gps_conversion.c $(USER_DIR)/gps_conversion.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/gps_conversion.c -I$(TEST_DIR)
gps_conversion_unittest.o : $(TEST_DIR)/gps_conversion_unittest.cc \
$(USER_DIR)/gps_conversion.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/gps_conversion_unittest.cc -I$(USER_DIR)
gps_conversion_unittest : gps_conversion.o gps_conversion_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

View file

@ -0,0 +1,50 @@
#include <stdint.h>
#include <limits.h>
#include "gps_conversion.h"
#include "unittest_macros.h"
#include "gtest/gtest.h"
// See http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
TEST(GpsConversionTest, GPSCoordToDegrees_BadString)
{
// expect
uint32_t result = GPS_coord_to_degrees("diediedie");
EXPECT_EQ(result, 0);
}
typedef struct gpsConversionExpectation_s {
char *coord;
uint32_t degrees;
} gpsConversionExpectation_t;
TEST(GpsConversionTest, GPSCoordToDegrees_NMEA_Values)
{
gpsConversionExpectation_t gpsConversionExpectations[] = {
{"0.0", 0},
{"000.0", 0},
{"00000.0000", 0},
{"0.0001", 16}, // smallest value
{"25599.9999", 2566666650}, // largest value
{"25599.99999", 2566666650}, // too many fractional digits
{"25699.9999", 16666650}, // overflowed without detection
{"5321.6802", 533613366},
{"00630.3372", 65056200},
};
// expect
uint8_t testIterationCount = sizeof(gpsConversionExpectations) / sizeof(gpsConversionExpectation_t);
// expect
for (uint8_t index = 0; index < testIterationCount; index ++) {
gpsConversionExpectation_t *expectation = &gpsConversionExpectations[index];
printf("iteration: %d\n", index);
uint32_t result = GPS_coord_to_degrees(expectation->coord);
EXPECT_EQ(result, expectation->degrees);
}
}