aboutsummaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorDaniel Agar <daniel@agar.ca>2015-01-02 17:44:28 -0500
committerDaniel Agar <daniel@agar.ca>2015-01-02 23:05:43 -0500
commit859185ac72877a305b3e08d98a6ace8ae8196c92 (patch)
treeb4220aca15696e803cb7eacfc2d02f198fa56087 /unittests
parent61d57539fb534a7f99ed8416a4102fb5fc0138d2 (diff)
downloadpx4-firmware-859185ac72877a305b3e08d98a6ace8ae8196c92.tar.gz
px4-firmware-859185ac72877a305b3e08d98a6ace8ae8196c92.tar.bz2
px4-firmware-859185ac72877a305b3e08d98a6ace8ae8196c92.zip
move autodeclination unittest to gtest and delete the sample
Diffstat (limited to 'unittests')
-rw-r--r--unittests/CMakeLists.txt26
-rw-r--r--unittests/Makefile28
-rw-r--r--unittests/autodeclination_test.cpp53
-rw-r--r--unittests/sample.cc68
-rw-r--r--unittests/sample.h43
-rw-r--r--unittests/sample_unittest.cc153
6 files changed, 38 insertions, 333 deletions
diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt
new file mode 100644
index 000000000..d264ae8cd
--- /dev/null
+++ b/unittests/CMakeLists.txt
@@ -0,0 +1,26 @@
+cmake_minimum_required(VERSION 2.8)
+project(unittests)
+enable_testing()
+
+set(GTEST_DIR gtest)
+add_subdirectory(${GTEST_DIR})
+include_directories(${GTEST_DIR}/include)
+include_directories(${CMAKE_SOURCE_DIR})
+include_directories(${CMAKE_SOURCE_DIR}/../src)
+include_directories(${CMAKE_SOURCE_DIR}/../src/modules)
+include_directories(${CMAKE_SOURCE_DIR}/../src/lib)
+
+add_definitions(-D__EXPORT=)
+
+function(add_gtest)
+ foreach(test_name ${ARGN})
+ target_link_libraries(${test_name} gtest_main)
+ add_test(${test_name}Test ${test_name})
+ endforeach()
+endfunction()
+
+
+# add each test
+# todo: add mixer_test sbus2_test st24_test sf0x_test
+add_executable(autodeclination_test autodeclination_test.cpp ${CMAKE_SOURCE_DIR}/../src/lib/geo_lookup/geo_mag_declination.c)
+add_gtest(autodeclination_test)
diff --git a/unittests/Makefile b/unittests/Makefile
index e55411a75..e68570868 100644
--- a/unittests/Makefile
+++ b/unittests/Makefile
@@ -40,7 +40,7 @@ gtest_main.a: gtest-all.o gtest_main.o
$(AR) $(ARFLAGS) $@ $^
-all: sample_unittest mixer_test sbus2_test autodeclination_test st24_test sf0x_test
+all: mixer_test sbus2_test st24_test sf0x_test
MIXER_FILES=../src/systemcmds/tests/test_mixer.cpp \
../src/systemcmds/tests/test_conv.cpp \
@@ -66,24 +66,6 @@ SF0X_FILES= \
sf0x_test.cpp \
../src/drivers/sf0x/sf0x_parser.cpp
-AUTODECLINATION_FILES= ../src/lib/geo_lookup/geo_mag_declination.c \
- hrt.cpp \
- autodeclination_test.cpp
-
-# Builds a sample test. A test should link with either gtest.a or
-# gtest_main.a, depending on whether it defines its own main()
-# function.
-sample.o: sample.cc sample.h $(GTEST_HEADERS)
- $(CC) $(CFLAGS) -c sample.cc
-
-sample_unittest.o: sample_unittest.cc \
- sample.h $(GTEST_HEADERS)
- $(CC) $(CFLAGS) -c sample_unittest.cc
-
-sample_unittest: sample.o sample_unittest.o gtest_main.a
- $(CC) $(CFLAGS) $^ -o $@
-
-
mixer_test: $(MIXER_FILES)
$(CC) -o mixer_test $(MIXER_FILES) $(CFLAGS)
@@ -93,19 +75,15 @@ sbus2_test: $(SBUS2_FILES)
sf0x_test: $(SF0X_FILES)
$(CC) -o sf0x_test $(SF0X_FILES) $(CFLAGS)
-autodeclination_test: $(SBUS2_FILES)
- $(CC) -o autodeclination_test $(AUTODECLINATION_FILES) $(CFLAGS)
-
st24_test: $(ST24_FILES)
$(CC) -o st24_test $(ST24_FILES) $(CFLAGS)
-unittests: clean mixer_test sbus2_test sf0x_test autodeclination_test st24_test
+unittests: clean mixer_test sbus2_test sf0x_test st24_test
./mixer_test
./sbus2_test
./sf0x_test
- ./autodeclination_test
./st24_test
.PHONY: clean
clean:
- rm -f gtest.a gtest_main.a *.o $(ODIR)/*.o *~ core $(INCDIR)/*~ sample_unittest mixer_test sbus2_test autodeclination_test st24_test sf0x_test
+ rm -f gtest.a gtest_main.a *.o $(ODIR)/*.o *~ core $(INCDIR)/*~ sample_unittest mixer_test sbus2_test st24_test sf0x_test
diff --git a/unittests/autodeclination_test.cpp b/unittests/autodeclination_test.cpp
index 1bda6eb79..a27d5f100 100644
--- a/unittests/autodeclination_test.cpp
+++ b/unittests/autodeclination_test.cpp
@@ -1,52 +1,17 @@
-
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
-#include <math.h>
-#include <systemlib/mixer/mixer.h>
-#include <systemlib/err.h>
+#include <unistd.h>
+
#include <drivers/drv_hrt.h>
-#include <px4iofirmware/px4io.h>
-// #include "../../src/systemcmds/tests/tests.h"
#include <geo/geo.h>
+#include <px4iofirmware/px4io.h>
+#include <systemlib/err.h>
+#include <systemlib/mixer/mixer.h>
-int main(int argc, char *argv[]) {
- warnx("autodeclination test started");
-
- char* latstr = 0;
- char* lonstr = 0;
- char* declstr = 0;
-
- if (argc < 4) {
- warnx("Too few arguments. Using default lat / lon and declination");
- latstr = "47.0";
- lonstr = "8.0";
- declstr = "0.6";
- } else {
- latstr = argv[1];
- lonstr = argv[2];
- declstr = argv[3];
- }
-
- char* p_end;
-
- float lat = strtod(latstr, &p_end);
- float lon = strtod(lonstr, &p_end);
- float decl_truth = strtod(declstr, &p_end);
-
- float declination = get_mag_declination(lat, lon);
-
- printf("lat: %f lon: %f, expected dec: %f, estimated dec: %f\n", lat, lon, declination, decl_truth);
-
- int ret = 0;
-
- // Fail if the declination differs by more than one degree
- float decldiff = fabs(decl_truth - declination);
- if (decldiff > 0.5f) {
- warnx("declination differs more than 1 degree: difference: %12.8f", decldiff);
- ret = 1;
- }
+#include "gtest/gtest.h"
- return ret;
+TEST(AutoDeclinationTest, AutoDeclination) {
+ ASSERT_NEAR(get_mag_declination(47.0, 8.0), 0.6, 0.5) << "declination differs more than 1 degree";
}
diff --git a/unittests/sample.cc b/unittests/sample.cc
deleted file mode 100644
index 3f0c838f2..000000000
--- a/unittests/sample.cc
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright 2005, Google Inc.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
-#include "sample.h"
-
-// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
-int Factorial(int n) {
- int result = 1;
- for (int i = 1; i <= n; i++) {
- result *= i;
- }
-
- return result;
-}
-
-// Returns true iff n is a prime number.
-bool IsPrime(int n) {
- // Trivial case 1: small numbers
- if (n <= 1) return false;
-
- // Trivial case 2: even numbers
- if (n % 2 == 0) return n == 2;
-
- // Now, we have that n is odd and n >= 3.
-
- // Try to divide n by every odd number i, starting from 3
- for (int i = 3; ; i += 2) {
- // We only have to try i up to the squre root of n
- if (i > n/i) break;
-
- // Now, we have i <= n/i < n.
- // If n is divisible by i, n is not prime.
- if (n % i == 0) return false;
- }
-
- // n has no integer factor in the range (1, n), and thus is prime.
- return true;
-}
diff --git a/unittests/sample.h b/unittests/sample.h
deleted file mode 100644
index 3dfeb98c4..000000000
--- a/unittests/sample.h
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright 2005, Google Inc.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
-#ifndef GTEST_SAMPLES_SAMPLE1_H_
-#define GTEST_SAMPLES_SAMPLE1_H_
-
-// Returns n! (the factorial of n). For negative n, n! is defined to be 1.
-int Factorial(int n);
-
-// Returns true iff n is a prime number.
-bool IsPrime(int n);
-
-#endif // GTEST_SAMPLES_SAMPLE1_H_
diff --git a/unittests/sample_unittest.cc b/unittests/sample_unittest.cc
deleted file mode 100644
index 8dc3f5c38..000000000
--- a/unittests/sample_unittest.cc
+++ /dev/null
@@ -1,153 +0,0 @@
-// Copyright 2005, Google Inc.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-// A sample program demonstrating using Google C++ testing framework.
-//
-// Author: wan@google.com (Zhanyong Wan)
-
-
-// This sample shows how to write a simple unit test for a function,
-// using Google C++ testing framework.
-//
-// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
-
-
-// Step 1. Include necessary header files such that the stuff your
-// test logic needs is declared.
-//
-// Don't forget gtest.h, which declares the testing framework.
-
-#include <limits.h>
-#include "sample.h"
-#include "gtest/gtest.h"
-
-
-// Step 2. Use the TEST macro to define your tests.
-//
-// TEST has two parameters: the test case name and the test name.
-// After using the macro, you should define your test logic between a
-// pair of braces. You can use a bunch of macros to indicate the
-// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
-// examples of such macros. For a complete list, see gtest.h.
-//
-// <TechnicalDetails>
-//
-// In Google Test, tests are grouped into test cases. This is how we
-// keep test code organized. You should put logically related tests
-// into the same test case.
-//
-// The test case name and the test name should both be valid C++
-// identifiers. And you should not use underscore (_) in the names.
-//
-// Google Test guarantees that each test you define is run exactly
-// once, but it makes no guarantee on the order the tests are
-// executed. Therefore, you should write your tests in such a way
-// that their results don't depend on their order.
-//
-// </TechnicalDetails>
-
-
-// Tests Factorial().
-
-// Tests factorial of negative numbers.
-TEST(FactorialTest, Negative) {
- // This test is named "Negative", and belongs to the "FactorialTest"
- // test case.
- EXPECT_EQ(1, Factorial(-5));
- EXPECT_EQ(1, Factorial(-1));
- EXPECT_GT(Factorial(-10), 0);
-
- // <TechnicalDetails>
- //
- // EXPECT_EQ(expected, actual) is the same as
- //
- // EXPECT_TRUE((expected) == (actual))
- //
- // except that it will print both the expected value and the actual
- // value when the assertion fails. This is very helpful for
- // debugging. Therefore in this case EXPECT_EQ is preferred.
- //
- // On the other hand, EXPECT_TRUE accepts any Boolean expression,
- // and is thus more general.
- //
- // </TechnicalDetails>
-}
-
-// Tests factorial of 0.
-TEST(FactorialTest, Zero) {
- EXPECT_EQ(1, Factorial(0));
-}
-
-// Tests factorial of positive numbers.
-TEST(FactorialTest, Positive) {
- EXPECT_EQ(1, Factorial(1));
- EXPECT_EQ(2, Factorial(2));
- EXPECT_EQ(6, Factorial(3));
- EXPECT_EQ(40320, Factorial(8));
-}
-
-
-// Tests IsPrime()
-
-// Tests negative input.
-TEST(IsPrimeTest, Negative) {
- // This test belongs to the IsPrimeTest test case.
-
- EXPECT_FALSE(IsPrime(-1));
- EXPECT_FALSE(IsPrime(-2));
- EXPECT_FALSE(IsPrime(INT_MIN));
-}
-
-// Tests some trivial cases.
-TEST(IsPrimeTest, Trivial) {
- EXPECT_FALSE(IsPrime(0));
- EXPECT_FALSE(IsPrime(1));
- EXPECT_TRUE(IsPrime(2));
- EXPECT_TRUE(IsPrime(3));
-}
-
-// Tests positive input.
-TEST(IsPrimeTest, Positive) {
- EXPECT_FALSE(IsPrime(4));
- EXPECT_TRUE(IsPrime(5));
- EXPECT_FALSE(IsPrime(6));
- EXPECT_TRUE(IsPrime(23));
-}
-
-// Step 3. Call RUN_ALL_TESTS() in main().
-//
-// We do this by linking in src/gtest_main.cc file, which consists of
-// a main() function which calls RUN_ALL_TESTS() for us.
-//
-// This runs all the tests you've defined, prints the result, and
-// returns 0 if successful, or 1 otherwise.
-//
-// Did you notice that we didn't register the tests? The
-// RUN_ALL_TESTS() macro magically knows about all the tests we
-// defined. Isn't this convenient?