From 3989ad94f2deed64c81c8019f259ec20a0e2f457 Mon Sep 17 00:00:00 2001 From: Mariano Lizarraga Date: Thu, 13 Jan 2011 16:10:43 -0600 Subject: [PATCH] Changed for a simpler way unit tests can be set up and run. Now tests can be simple classes which implement the tests --- qgcunittest/AutoTest.h | 85 +++++++++++++++++ qgcunittest/SlugsMavUnitTest.cc | 20 ++++ qgcunittest/SlugsMavUnitTest.h | 27 ++++++ qgcunittest/UASUnitTest.cc | 156 ++++++++++++++++++++++++++++++++ qgcunittest/UASUnitTest.h | 50 ++++++++++ qgcunittest/testSuite.cc | 28 ++++++ 6 files changed, 366 insertions(+) create mode 100755 qgcunittest/AutoTest.h create mode 100644 qgcunittest/SlugsMavUnitTest.cc create mode 100644 qgcunittest/SlugsMavUnitTest.h create mode 100644 qgcunittest/UASUnitTest.cc create mode 100644 qgcunittest/UASUnitTest.h create mode 100755 qgcunittest/testSuite.cc diff --git a/qgcunittest/AutoTest.h b/qgcunittest/AutoTest.h new file mode 100755 index 000000000..e353d3d30 --- /dev/null +++ b/qgcunittest/AutoTest.h @@ -0,0 +1,85 @@ +/** +* @author Rob Caldecott +* @note This was obtained from http://qtcreator.blogspot.com/2010/04/sample-multiple-unit-test-project.html +* +*/ + +#ifndef AUTOTEST_H +#define AUTOTEST_H + +#include +#include +#include +#include + +namespace AutoTest +{ + typedef QList TestList; + + inline TestList& testList() + { + static TestList list; + return list; + } + + inline bool findObject(QObject* object) + { + TestList& list = testList(); + if (list.contains(object)) + { + return true; + } + foreach (QObject* test, list) + { + if (test->objectName() == object->objectName()) + { + return true; + } + } + return false; + } + + inline void addTest(QObject* object) + { + TestList& list = testList(); + if (!findObject(object)) + { + list.append(object); + } + } + + inline int run(int argc, char *argv[]) + { + int ret = 0; + + foreach (QObject* test, testList()) + { + ret += QTest::qExec(test, argc, argv); + } + + return ret; + } +} + +template +class Test +{ +public: + QSharedPointer child; + + Test(const QString& name) : child(new T) + { + child->setObjectName(name); + AutoTest::addTest(child.data()); + } +}; + +#define DECLARE_TEST(className) static Test t(#className); + +#define TEST_MAIN \ + int main(int argc, char *argv[]) \ + { \ + return AutoTest::run(argc, argv); \ + } + +#endif // AUTOTEST_H diff --git a/qgcunittest/SlugsMavUnitTest.cc b/qgcunittest/SlugsMavUnitTest.cc new file mode 100644 index 000000000..0e1549701 --- /dev/null +++ b/qgcunittest/SlugsMavUnitTest.cc @@ -0,0 +1,20 @@ +#include "SlugsMavUnitTest.h" + +SlugsMavUnitTest::SlugsMavUnitTest() +{ +} + +void SlugsMavUnitTest::initTestCase() +{ + +} + +void SlugsMavUnitTest::cleanupTestCase() +{ + +} + +void SlugsMavUnitTest::first_test() +{ + QCOMPARE(1,2); +} diff --git a/qgcunittest/SlugsMavUnitTest.h b/qgcunittest/SlugsMavUnitTest.h new file mode 100644 index 000000000..493e4a4c5 --- /dev/null +++ b/qgcunittest/SlugsMavUnitTest.h @@ -0,0 +1,27 @@ +#ifndef SLUGSMAVUNITTEST_H +#define SLUGSMAVUNITTEST_H + +#include +#include +#include +#include "UAS.h" +#include "MAVLinkProtocol.h" +#include "UASInterface.h" +#include "AutoTest.h" + +class SlugsMavUnitTest : public QObject +{ + Q_OBJECT +public: + SlugsMavUnitTest(); +signals: + +private slots: + void initTestCase(); + void cleanupTestCase(); + void first_test(); +}; + +DECLARE_TEST(SlugsMavUnitTest) + +#endif // SLUGSMAVUNITTEST_H diff --git a/qgcunittest/UASUnitTest.cc b/qgcunittest/UASUnitTest.cc new file mode 100644 index 000000000..7aa85377b --- /dev/null +++ b/qgcunittest/UASUnitTest.cc @@ -0,0 +1,156 @@ +#include "UASUnitTest.h" + +UASUnitTest::UASUnitTest() +{ +} + +void UASUnitTest::initTestCase() +{ + mav= new MAVLinkProtocol(); + uas=new UAS(mav,UASID); +} + +void UASUnitTest::cleanupTestCase() +{ + delete uas; + delete mav; + +} + +void UASUnitTest::getUASID_test() +{ + // Test a default ID of zero is assigned + UAS* uas2 = new UAS(mav); + QCOMPARE(uas2->getUASID(), 0); + delete uas2; + + // Test that the chosen ID was assigned at construction + QCOMPARE(uas->getUASID(), UASID); + + // Make sure that no other ID was sert + QEXPECT_FAIL("", "When you set an ID it does not use the default ID of 0", Continue); + QCOMPARE(uas->getUASID(), 0); +} + +void UASUnitTest::getUASName_test() +{ + // Test that the name is build as MAV + ID + QCOMPARE(uas->getUASName(), "MAV 0" + QString::number(UASID)); + +} + +void UASUnitTest::getUpTime_test() +{ + UAS* uas2 = new UAS(mav); + // Test that the uptime starts at zero to a + // precision of seconds + QCOMPARE(floor(uas2->getUptime()/1000.0), 0.0); + + // Sleep for three seconds + QTest::qSleep(3000); + + // Test that the up time is computed correctly to a + // precision of seconds + QCOMPARE(floor(uas2->getUptime()/1000.0), 3.0); + + delete uas2; +} + +void UASUnitTest::getCommunicationStatus_test() +{ + // Verify that upon construction the Comm status is disconnected + QCOMPARE(uas->getCommunicationStatus(), static_cast(UASInterface::COMM_DISCONNECTED)); +} + +void UASUnitTest::filterVoltage_test() +{ + float verificar=uas->filterVoltage(0.4f); + // Verify that upon construction the Comm status is disconnected + QCOMPARE(verificar, 8.52f); +} +void UASUnitTest:: getAutopilotType_test() +{ + int verificar=uas->getAutopilotType(); + // Verify that upon construction the autopilot is set to -1 + QCOMPARE(verificar, -1); +} +void UASUnitTest::setAutopilotType_test() +{ + uas->setAutopilotType(2); + // Verify that the autopilot is set + QCOMPARE(uas->getAutopilotType(), 2); +} + +void UASUnitTest::getStatusForCode_test() +{ + QString state, desc; + state = ""; + desc = ""; + + uas->getStatusForCode(MAV_STATE_UNINIT, state, desc); + QVERIFY(state == "UNINIT"); + + uas->getStatusForCode(MAV_STATE_UNINIT, state, desc); + QVERIFY(state == "UNINIT"); + + uas->getStatusForCode(MAV_STATE_BOOT, state, desc); + QVERIFY(state == "BOOT"); + + uas->getStatusForCode(MAV_STATE_CALIBRATING, state, desc); + QVERIFY(state == "CALIBRATING"); + + uas->getStatusForCode(MAV_STATE_ACTIVE, state, desc); + QVERIFY(state == "ACTIVE"); + + uas->getStatusForCode(MAV_STATE_STANDBY, state, desc); + QVERIFY(state == "STANDBY"); + + uas->getStatusForCode(MAV_STATE_CRITICAL, state, desc); + QVERIFY(state == "CRITICAL"); + + uas->getStatusForCode(MAV_STATE_EMERGENCY, state, desc); + QVERIFY(state == "EMERGENCY"); + + uas->getStatusForCode(MAV_STATE_POWEROFF, state, desc); + QVERIFY(state == "SHUTDOWN"); + + uas->getStatusForCode(5325, state, desc); + QVERIFY(state == "UNKNOWN"); +} + + +void UASUnitTest::getLocalX_test() +{ + QCOMPARE(uas->getLocalX(), 0.0); +} +void UASUnitTest::getLocalY_test() +{ + QCOMPARE(uas->getLocalY(), 0.0); +} +void UASUnitTest::getLocalZ_test() +{ + QCOMPARE(uas->getLocalZ(), 0.0); +} +void UASUnitTest::getLatitude_test() +{ QCOMPARE(uas->getLatitude(), 0.0); +} +void UASUnitTest::getLongitude_test() +{ + QCOMPARE(uas->getLongitude(), 0.0); +} +void UASUnitTest::getAltitude_test() +{ + QCOMPARE(uas->getAltitude(), 0.0); +} +void UASUnitTest::getRoll_test() +{ + QCOMPARE(uas->getRoll(), 0.0); +} +void UASUnitTest::getPitch_test() +{ + QCOMPARE(uas->getPitch(), 0.0); +} +void UASUnitTest::getYaw_test() +{ + QCOMPARE(uas->getYaw(), 0.0); +} diff --git a/qgcunittest/UASUnitTest.h b/qgcunittest/UASUnitTest.h new file mode 100644 index 000000000..67f1eb6d8 --- /dev/null +++ b/qgcunittest/UASUnitTest.h @@ -0,0 +1,50 @@ +#ifndef UASUNITTEST_H +#define UASUNITTEST_H + +#include +#include +#include +#include "UAS.h" +#include "MAVLinkProtocol.h" +#include "UASInterface.h" +#include "AutoTest.h" + +class UASUnitTest : public QObject +{ + Q_OBJECT +public: + #define UASID 50 + MAVLinkProtocol* mav; + UAS* uas; + UASUnitTest(); + +signals: + +private slots: + void initTestCase(); + void cleanupTestCase(); + void getUASID_test(); + void getUASName_test(); + void getUpTime_test(); + void getCommunicationStatus_test(); + void filterVoltage_test(); + void getAutopilotType_test(); + void setAutopilotType_test(); + void getStatusForCode_test(); + void getLocalX_test(); + void getLocalY_test(); + void getLocalZ_test(); + void getLatitude_test(); + void getLongitude_test(); + void getAltitude_test(); + void getRoll_test(); + void getPitch_test(); + void getYaw_test(); + +protected: + UAS *prueba; + +}; + +DECLARE_TEST(UASUnitTest) +#endif // UASUNITTEST_H diff --git a/qgcunittest/testSuite.cc b/qgcunittest/testSuite.cc new file mode 100755 index 000000000..0489b2298 --- /dev/null +++ b/qgcunittest/testSuite.cc @@ -0,0 +1,28 @@ +/** +* @author Rob Caldecott +* @note This was obtained from http://qtcreator.blogspot.com/2010/04/sample-multiple-unit-test-project.html +* +*/ + +#include "AutoTest.h" +#include + +#if 1 +// This is all you need to run all the tests +TEST_MAIN +#else +// Or supply your own main function +int main(int argc, char *argv[]) +{ + int failures = AutoTest::run(argc, argv); + if (failures == 0) + { + qDebug() << "ALL TESTS PASSED"; + } + else + { + qDebug() << failures << " TESTS FAILED!"; + } + return failures; +} +#endif -- 2.22.0