Commit 0db28b3b authored by Eric Fossum's avatar Eric Fossum

Moved to cppunit tests, but still need emc tests.

parent 65e10106
......@@ -43,13 +43,14 @@ TESTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tests
# Test Files
TESTFILES= \
${TESTDIR}/TestFiles/f1 \
${TESTDIR}/TestFiles/f2
${TESTDIR}/TestFiles/f3 \
${TESTDIR}/TestFiles/f1
# Test Object Files
TESTOBJECTFILES= \
${TESTDIR}/tests/emc1072test.o \
${TESTDIR}/tests/linuxi2ctest.o
${TESTDIR}/tests/i2ctestrunner.o \
${TESTDIR}/tests/linuxi2ctestclass.o
# C Compiler Flags
CFLAGS=
......@@ -92,25 +93,31 @@ ${OBJECTDIR}/src/linux-i2c.o: src/linux-i2c.cpp
.build-tests-conf: .build-tests-subprojects .build-conf ${TESTFILES}
.build-tests-subprojects:
${TESTDIR}/TestFiles/f3: ${TESTDIR}/tests/i2ctestrunner.o ${TESTDIR}/tests/linuxi2ctestclass.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f3 $^ ${LDLIBSOPTIONS} `cppunit-config --libs`
${TESTDIR}/TestFiles/f1: ${TESTDIR}/tests/emc1072test.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f1 $^ ${LDLIBSOPTIONS}
${TESTDIR}/TestFiles/f2: ${TESTDIR}/tests/linuxi2ctest.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f2 $^ ${LDLIBSOPTIONS}
${TESTDIR}/tests/i2ctestrunner.o: tests/i2ctestrunner.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -g -Iinclude -std=c++98 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/i2ctestrunner.o tests/i2ctestrunner.cpp
${TESTDIR}/tests/emc1072test.o: tests/emc1072test.cpp
${TESTDIR}/tests/linuxi2ctestclass.o: tests/linuxi2ctestclass.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -g -Iinclude -I. -std=c++98 -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/emc1072test.o tests/emc1072test.cpp
$(COMPILE.cc) -g -Iinclude -std=c++98 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/linuxi2ctestclass.o tests/linuxi2ctestclass.cpp
${TESTDIR}/tests/linuxi2ctest.o: tests/linuxi2ctest.cpp
${TESTDIR}/tests/emc1072test.o: tests/emc1072test.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -g -Iinclude -I. -std=c++98 -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/linuxi2ctest.o tests/linuxi2ctest.cpp
$(COMPILE.cc) -g -Iinclude -I. -std=c++98 -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/emc1072test.o tests/emc1072test.cpp
${OBJECTDIR}/src/emc1072_nomain.o: ${OBJECTDIR}/src/emc1072.o src/emc1072.cpp
......@@ -143,8 +150,8 @@ ${OBJECTDIR}/src/linux-i2c_nomain.o: ${OBJECTDIR}/src/linux-i2c.o src/linux-i2c.
.test-conf:
@if [ "${TEST}" = "" ]; \
then \
${TESTDIR}/TestFiles/f3 || true; \
${TESTDIR}/TestFiles/f1 || true; \
${TESTDIR}/TestFiles/f2 || true; \
else \
./${TEST} || true; \
fi
......
......@@ -43,13 +43,14 @@ TESTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tests
# Test Files
TESTFILES= \
${TESTDIR}/TestFiles/f1 \
${TESTDIR}/TestFiles/f2
${TESTDIR}/TestFiles/f3 \
${TESTDIR}/TestFiles/f1
# Test Object Files
TESTOBJECTFILES= \
${TESTDIR}/tests/emc1072test.o \
${TESTDIR}/tests/linuxi2ctest.o
${TESTDIR}/tests/i2ctestrunner.o \
${TESTDIR}/tests/linuxi2ctestclass.o
# C Compiler Flags
CFLAGS=
......@@ -92,25 +93,31 @@ ${OBJECTDIR}/src/linux-i2c.o: src/linux-i2c.cpp
.build-tests-conf: .build-tests-subprojects .build-conf ${TESTFILES}
.build-tests-subprojects:
${TESTDIR}/TestFiles/f3: ${TESTDIR}/tests/i2ctestrunner.o ${TESTDIR}/tests/linuxi2ctestclass.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f3 $^ ${LDLIBSOPTIONS} `cppunit-config --libs`
${TESTDIR}/TestFiles/f1: ${TESTDIR}/tests/emc1072test.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f1 $^ ${LDLIBSOPTIONS}
${TESTDIR}/TestFiles/f2: ${TESTDIR}/tests/linuxi2ctest.o ${OBJECTFILES:%.o=%_nomain.o}
${MKDIR} -p ${TESTDIR}/TestFiles
${LINK.cc} -o ${TESTDIR}/TestFiles/f2 $^ ${LDLIBSOPTIONS}
${TESTDIR}/tests/i2ctestrunner.o: tests/i2ctestrunner.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -O2 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/i2ctestrunner.o tests/i2ctestrunner.cpp
${TESTDIR}/tests/emc1072test.o: tests/emc1072test.cpp
${TESTDIR}/tests/linuxi2ctestclass.o: tests/linuxi2ctestclass.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -O2 -I. -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/emc1072test.o tests/emc1072test.cpp
$(COMPILE.cc) -O2 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/linuxi2ctestclass.o tests/linuxi2ctestclass.cpp
${TESTDIR}/tests/linuxi2ctest.o: tests/linuxi2ctest.cpp
${TESTDIR}/tests/emc1072test.o: tests/emc1072test.cpp
${MKDIR} -p ${TESTDIR}/tests
${RM} "$@.d"
$(COMPILE.cc) -O2 -I. -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/linuxi2ctest.o tests/linuxi2ctest.cpp
$(COMPILE.cc) -O2 -I. -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/emc1072test.o tests/emc1072test.cpp
${OBJECTDIR}/src/emc1072_nomain.o: ${OBJECTDIR}/src/emc1072.o src/emc1072.cpp
......@@ -143,8 +150,8 @@ ${OBJECTDIR}/src/linux-i2c_nomain.o: ${OBJECTDIR}/src/linux-i2c.o src/linux-i2c.
.test-conf:
@if [ "${TEST}" = "" ]; \
then \
${TESTDIR}/TestFiles/f3 || true; \
${TESTDIR}/TestFiles/f1 || true; \
${TESTDIR}/TestFiles/f2 || true; \
else \
./${TEST} || true; \
fi
......
......@@ -22,17 +22,19 @@
displayName="Test Files"
projectFiles="false"
kind="TEST_LOGICAL_FOLDER">
<logicalFolder name="f1"
displayName="EMC1072 Test"
<logicalFolder name="f3"
displayName="CppUnit Tests"
projectFiles="true"
kind="TEST">
<itemPath>tests/emc1072test.cpp</itemPath>
<itemPath>tests/i2ctestrunner.cpp</itemPath>
<itemPath>tests/linuxi2ctestclass.cpp</itemPath>
<itemPath>tests/linuxi2ctestclass.h</itemPath>
</logicalFolder>
<logicalFolder name="f2"
displayName="Linux I2C Test"
<logicalFolder name="f1"
displayName="EMC1072 Test"
projectFiles="true"
kind="TEST">
<itemPath>tests/linuxi2ctest.cpp</itemPath>
<itemPath>tests/emc1072test.cpp</itemPath>
</logicalFolder>
</logicalFolder>
<logicalFolder name="ExternalFiles"
......@@ -81,19 +83,18 @@
<output>${TESTDIR}/TestFiles/f1</output>
</linkerTool>
</folder>
<folder path="TestFiles/f2">
<folder path="TestFiles/f3">
<cTool>
<incDir>
<pElem>.</pElem>
</incDir>
<commandLine>`cppunit-config --cflags`</commandLine>
</cTool>
<ccTool>
<incDir>
<pElem>.</pElem>
</incDir>
<commandLine>`cppunit-config --cflags`</commandLine>
</ccTool>
<linkerTool>
<output>${TESTDIR}/TestFiles/f2</output>
<output>${TESTDIR}/TestFiles/f3</output>
<linkerLibItems>
<linkerOptionItem>`cppunit-config --libs`</linkerOptionItem>
</linkerLibItems>
</linkerTool>
</folder>
<item path="include/I2cBase.h" ex="false" tool="3" flavor2="0">
......@@ -108,7 +109,11 @@
</item>
<item path="tests/emc1072test.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctest.cpp" ex="false" tool="1" flavor2="0">
<item path="tests/i2ctestrunner.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctestclass.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctestclass.h" ex="false" tool="3" flavor2="0">
</item>
</conf>
<conf name="Release" type="2">
......@@ -146,19 +151,18 @@
<output>${TESTDIR}/TestFiles/f1</output>
</linkerTool>
</folder>
<folder path="TestFiles/f2">
<folder path="TestFiles/f3">
<cTool>
<incDir>
<pElem>.</pElem>
</incDir>
<commandLine>`cppunit-config --cflags`</commandLine>
</cTool>
<ccTool>
<incDir>
<pElem>.</pElem>
</incDir>
<commandLine>`cppunit-config --cflags`</commandLine>
</ccTool>
<linkerTool>
<output>${TESTDIR}/TestFiles/f2</output>
<output>${TESTDIR}/TestFiles/f3</output>
<linkerLibItems>
<linkerOptionItem>`cppunit-config --libs`</linkerOptionItem>
</linkerLibItems>
</linkerTool>
</folder>
<item path="include/I2cBase.h" ex="false" tool="3" flavor2="0">
......@@ -173,7 +177,11 @@
</item>
<item path="tests/emc1072test.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctest.cpp" ex="false" tool="1" flavor2="0">
<item path="tests/i2ctestrunner.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctestclass.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="tests/linuxi2ctestclass.h" ex="false" tool="3" flavor2="0">
</item>
</conf>
</confs>
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: i2ctestrunner.cpp
* Author: ericfoss
*
* Created on Feb 14, 2017, 9:11:07 PM
*/
// CppUnit site http://sourceforge.net/projects/cppunit/files
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/portability/Stream.h>
class ProgressListener : public CPPUNIT_NS::TestListener {
public:
ProgressListener()
: m_lastTestFailed(false) {
}
~ProgressListener() {
}
void startTest(CPPUNIT_NS::Test *test) {
CPPUNIT_NS::stdCOut() << test->getName();
CPPUNIT_NS::stdCOut() << "\n";
CPPUNIT_NS::stdCOut().flush();
m_lastTestFailed = false;
}
void addFailure(const CPPUNIT_NS::TestFailure &failure) {
CPPUNIT_NS::stdCOut() << " : " << (failure.isError() ? "error" : "assertion");
m_lastTestFailed = true;
}
void endTest(CPPUNIT_NS::Test *test) {
if (!m_lastTestFailed)
CPPUNIT_NS::stdCOut() << " : OK";
CPPUNIT_NS::stdCOut() << "\n";
}
private:
/// Prevents the use of the copy constructor.
ProgressListener(const ProgressListener &copy);
/// Prevents the use of the copy operator.
void operator=(const ProgressListener &copy);
private:
bool m_lastTestFailed;
};
int main() {
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener(&result);
// Add a listener that print dots as test run.
ProgressListener progress;
controller.addListener(&progress);
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
runner.run(controller);
// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
outputter.write();
return result.wasSuccessful() ? 0 : 1;
}
/*
* File: linuxi2ctest.cpp
* Author: ericfoss
*/
#include <iostream>
#include <stdexcept>
#include <stdlib.h>
#include "linux-i2c.h"
/*
* Simple C++ Test Suite
*/
const char GOOD_ADDR = 0x4c;
const char *GOOD_PATH = "/dev/i2c-2";
void bad_path_test() {
bool threw = false;
try {
LinuxI2c i2c("/fake/path", GOOD_ADDR);
} catch(...) {
threw = true;
}
if (!threw) {
std::cout << "%TEST_FAILED% time=0 testname=bad_path_test (linuxi2ctest) message=Bad constructor didn't throw" << std::endl;
}
}
void bad_address_test() {
bool threw = false;
try {
LinuxI2c i2c("/dev/i2c2", 0x00);
} catch(...) {
threw = true;
}
if (!threw) {
std::cout << "%TEST_FAILED% time=0 testname=bad_address_test (linuxi2ctest) message=Bad constructor didn't throw" << std::endl;
}
}
void good_construtor_test() {
bool threw = true;
std::string msg;
try {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
threw = false;
} catch(const std::runtime_error &e) {
msg = e.what();
}
if (threw) {
std::cout << "%TEST_FAILED% time=0 testname=good_construtor_test (linuxi2ctest) message=" << msg << std::endl;
}
}
void send_test() {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
char byte = 0x00;
if (!i2c.send(&byte, 1)) {
std::cout << "%TEST_FAILED% time=0 testname=send_test (linuxi2ctest) message=Could not send" << std::endl;
}
}
void long_send_test() {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
char byte[40];
std::cout << "*** Write should fail ***" << std::endl;
if (i2c.send(byte, 40)) {
std::cout << "%TEST_FAILED% time=0 testname=long_send_test (linuxi2ctest) message=Sent a message that was too long" << std::endl;
}
}
void recv_test() {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
char byte = 0x00;
if (!i2c.recv(&byte, 1)) {
std::cout << "%TEST_FAILED% time=0 testname=recv_test (linuxi2ctest) message=Could not recv" << std::endl;
}
if (byte == 0x00) {
std::cout << "%TEST_FAILED% time=0 testname=recv_test (linuxi2ctest) message=Received message was zero" << std::endl;
}
}
void long_recv_test() {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
char byte[40];
std::cout << "*** Read should fail ***" << std::endl;
if (i2c.recv(byte, 40)) {
std::cout << "%TEST_FAILED% time=0 testname=long_recv_test (linuxi2ctest) message=Received a message that was too long" << std::endl;
}
}
void send_recv_test() {
LinuxI2c i2c("/dev/i2c-2", GOOD_ADDR);
char byte = 0x00;
if (!i2c.send_recv(&byte, 1, 1)) {
std::cout << "%TEST_FAILED% time=0 testname=send_recv_test (linuxi2ctest) message=Could not send_recv" << std::endl;
}
if (byte == 0x00) {
std::cout << "%TEST_FAILED% time=0 testname=send_recv_test (linuxi2ctest) message=Received message was zero" << std::endl;
}
}
int main(int argc, char** argv) {
std::cout << "%SUITE_STARTING% linuxi2ctest" << std::endl;
std::cout << "%SUITE_STARTED%" << std::endl;
std::cout << "%TEST_STARTED% bad_path_test (linuxi2ctest)" << std::endl;
bad_path_test();
std::cout << "%TEST_FINISHED% time=0 bad_path_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% bad_address_test (linuxi2ctest)" << std::endl;
bad_address_test();
std::cout << "%TEST_FINISHED% time=0 bad_address_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% good_construtor_test (linuxi2ctest)" << std::endl;
good_construtor_test();
std::cout << "%TEST_FINISHED% time=0 good_construtor_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% send_test (linuxi2ctest)" << std::endl;
send_test();
std::cout << "%TEST_FINISHED% time=0 send_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% long_send_test (linuxi2ctest)" << std::endl;
long_send_test();
std::cout << "%TEST_FINISHED% time=0 long_send_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% recv_test (linuxi2ctest)" << std::endl;
recv_test();
std::cout << "%TEST_FINISHED% time=0 recv_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% long_recv_test (linuxi2ctest)" << std::endl;
long_recv_test();
std::cout << "%TEST_FINISHED% time=0 long_recv_test (linuxi2ctest)" << std::endl;
std::cout << "%TEST_STARTED% send_recv_test (linuxi2ctest)" << std::endl;
send_recv_test();
std::cout << "%TEST_FINISHED% time=0 send_recv_test (linuxi2ctest)" << std::endl;
std::cout << "%SUITE_FINISHED% time=0" << std::endl;
return (EXIT_SUCCESS);
}
/*
* File: linuxi2ctestclass.cpp
* Author: ericfoss
*
* Created on Feb 14, 2017, 9:11:07 PM
*/
#include "linuxi2ctestclass.h"
CPPUNIT_TEST_SUITE_REGISTRATION(linuxi2ctestclass);
linuxi2ctestclass::linuxi2ctestclass() {
}
linuxi2ctestclass::~linuxi2ctestclass() {
}
void linuxi2ctestclass::setUp() {
obj = new LinuxI2c("/dev/i2c-2", 0x4c);
}
void linuxi2ctestclass::tearDown() {
if (obj) {
delete(obj);
}
}
void linuxi2ctestclass::send_test() {
char byte = 0x00;
CPPUNIT_ASSERT(obj->send(&byte, 1));
}
void linuxi2ctestclass::long_send_test() {
char byte[40];
CPPUNIT_ASSERT(!obj->send(byte, 40));
}
void linuxi2ctestclass::recv_test() {
char byte = 0x00;
CPPUNIT_ASSERT(obj->recv(&byte, 1));
CPPUNIT_ASSERT(byte != 0x00);
}
void linuxi2ctestclass::long_recv_test() {
char byte[40];
std::cout << "*** Read should fail ***" << std::endl;
CPPUNIT_ASSERT(!obj->recv(byte, 40));
}
void linuxi2ctestclass::send_recv_test() {
char byte = 0x00;
CPPUNIT_ASSERT(obj->send_recv(&byte, 1, 1));
CPPUNIT_ASSERT(byte != 0x00);
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: linuxi2ctestclass.h
* Author: ericfoss
*
* Created on Feb 14, 2017, 9:11:06 PM
*/
#ifndef LINUXI2CTESTCLASS_H
#define LINUXI2CTESTCLASS_H
#include <cppunit/extensions/HelperMacros.h>
#include "../include/linux-i2c.h"
class linuxi2ctestclass : public CPPUNIT_NS::TestFixture {
CPPUNIT_TEST_SUITE(linuxi2ctestclass);
CPPUNIT_TEST(send_test);
CPPUNIT_TEST(long_send_test);
CPPUNIT_TEST(recv_test);
CPPUNIT_TEST(long_recv_test);
CPPUNIT_TEST(send_recv_test);
CPPUNIT_TEST_SUITE_END();
public:
linuxi2ctestclass();
virtual ~linuxi2ctestclass();
void setUp();
void tearDown();
private:
void send_test();
void long_send_test();
void recv_test();
void long_recv_test();
void send_recv_test();
LinuxI2c *obj;
};
#endif /* LINUXI2CTESTCLASS_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment