CMake Changes

This is the changes made by Patrick Plenefisch converting the native
code to use CMake and the CMake Maven Plugin, as opposed to the
native Maven plugin. This is to allow for compatibility with newer
versions of the GCC toolchain. All the cpp sources were moved from
maven style directories to cpp style directories for CMake.

Change-Id: I67f5e3608948f37c83b0990d232105a3784f8593
This commit is contained in:
Brad Miller
2014-03-24 16:13:08 -04:00
parent 33134bef1d
commit 69d9ad70ab
804 changed files with 586 additions and 9377 deletions

View File

@@ -0,0 +1,35 @@
#ifndef __BAIOS__
#define __BAIOS__
#include "networktables2/stream/IOStream.h"
#include <vector>
#include <stdint.h>
#include <cstring>
#define UPDATE_BAIOS(len, ...) { uint8_t tmp[] = {__VA_ARGS__}; \
ByteArrayIOStream *input = new ByteArrayIOStream(tmp, len); \
stream = new DataIOStream(input); \
}
#define NEW_BAIOS(len, ...) \
DataIOStream* stream; \
UPDATE_BAIOS(len, __VA_ARGS__)
class ByteArrayIOStream : public IOStream
{
std::vector<uint8_t> buff;
public:
ByteArrayIOStream(uint8_t buf[], size_t length);
ByteArrayIOStream():buff(){}
virtual ~ByteArrayIOStream(){}
virtual int read(void* ptr, int numbytes);
virtual int write(const void* ptr, int numbytes);
virtual void flush(){}
virtual void close(){}
uint8_t* toByteArray(){return &buff[0];}
};
#endif

View File

@@ -0,0 +1,51 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::MakeMatcher;
using ::testing::Matcher;
using ::testing::MatcherInterface;
using ::testing::MatchResultListener;
template<class T> class IsArrayEqualToMatcher : public MatcherInterface<const T*> {
public:
IsArrayEqualToMatcher(const T* _exp, int _expLen){
exp = _exp;
expLen = _expLen;
}
virtual bool MatchAndExplain(const T* arg, MatchResultListener* listener) const {
*listener << "[";
for(int i = 0; i<expLen; ++i){
*listener << arg[i];
if(i<expLen-1)
*listener << ", ";
}
*listener << "]";
for(int i = 0; i<expLen; ++i){
if(exp[i]!=arg[i])
return false;
}
return true;
}
virtual void DescribeTo(::std::ostream* os) const {
*os << "is equal to array [";
for(int i = 0; i<expLen; ++i){
*os << exp[i];
if(i<expLen-1)
*os << ", ";
}
*os << "]";
}
virtual void DescribeNegationTo(::std::ostream* os) const {
*os << "is not equal to array";
}
private:
const T* exp;
int expLen;
};
template<class T> Matcher<const T*> IsArrayEqualTo(const T* exp, int expLen) {
return MakeMatcher(new IsArrayEqualToMatcher<T>(exp, expLen));
}

View File

@@ -0,0 +1,14 @@
#ifndef IOSTREAMPROVIDERMOCK_H_
#define IOSTREAMPROVIDERMOCK_H_
#include "networktables2/stream/IOStreamProvider.h"
class MockIOStreamProvider : public IOStreamProvider
{
public:
MOCK_METHOD0(accept, IOStream*());
MOCK_METHOD0(close, void());
};
#endif /* IOSTREAMPROVIDERMOCK_H_ */

View File

@@ -0,0 +1,35 @@
#ifndef MOCK_SERVER_CONN__H
#define MOCK_SERVER_CONN__H
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "networktables2/server/ServerConnectionAdapter.h"
class MockServerConnectionAdapter : public ServerConnectionAdapter {
public:
MOCK_METHOD1(gotoState, void(ServerConnectionState* newState));
MOCK_METHOD1(badMessage, void(BadMessageException& e));
MOCK_METHOD1(ioException, void(IOException& e));
MOCK_METHOD1(shutdown, void(bool closeStream));
MOCK_METHOD1(clientHello, void(uint16_t protocolRevision));
MOCK_METHOD1(protocolVersionUnsupported, void(uint16_t protocolRevision));
MOCK_METHOD0(keepAlive, void());
MOCK_METHOD0(serverHelloComplete, void());
MOCK_METHOD0(flush, void());
MOCK_METHOD0(ensureAlive, void());
MOCK_METHOD0(getConnectionState, ServerConnectionState*());
MOCK_METHOD1(offerIncomingAssignment, void(NetworkTableEntry* entry));
MOCK_METHOD1(offerOutgoingAssignment, void(NetworkTableEntry* entry));
MOCK_METHOD1(offerOutgoingUpdate, void(NetworkTableEntry* entry));
MOCK_METHOD3(offerIncomingUpdate, void(NetworkTableEntry* entry, SequenceNumber sequenceNumber, EntryValue value));
MOCK_METHOD1(getEntry, NetworkTableEntry*(EntryId id));
MOCK_METHOD0(stop, void());
MOCK_METHOD0(isRunning, bool());
};
#endif /* IOSTREAMPROVIDERMOCK_H_ */

View File

@@ -0,0 +1,17 @@
#ifndef NTTHREADMANGRRMOCK_H_
#define NTTHREADMANGRRMOCK_H_
#include "networktables2/thread/NTThreadManager.h"
class MockNTThreadManager : public NTThreadManager
{
public:
MOCK_METHOD2(newBlockingPeriodicThread, NTThread*(PeriodicRunnable* r, const char* name));
};
#endif /* IOSTREAMPROVIDERMOCK_H_ */

View File

@@ -0,0 +1,15 @@
#ifndef NTTHREADMOCK_H_
#define NTTHREADMOCK_H_
#include "networktables2/thread/NTThreadManager.h"
class MockNTThread : public NTThread
{
public:
MOCK_METHOD0(stop, void());
MOCK_METHOD0(isRunning, bool());
};
#endif /* IOSTREAMPROVIDERMOCK_H_ */

View File

@@ -0,0 +1,30 @@
#ifndef IOSTREAMPROVIDERMOCK_H_
#define IOSTREAMPROVIDERMOCK_H_
#include "networktables2/NetworkTableEntry.h"
class MockNetworkTableEntry : public NetworkTableEntry
{
public:
MOCK_METHOD0(makeDirty, void());
MOCK_METHOD0(makeClean, void());
MOCK_METHOD0(isDirty, bool());
MOCK_METHOD0(clearId, void());
MOCK_METHOD0(getId, EntryId());
MOCK_METHOD0(getValue, EntryValue());
MOCK_METHOD0(getType, NetworkTableEntryType*());
MOCK_METHOD0(getSequenceNumber, SequenceNumber());
MOCK_METHOD3(forcePut, void(SequenceNumber newSequenceNumber, NetworkTableEntryType* type, EntryValue newValue));
MOCK_METHOD2(putValue, bool(SequenceNumber newSequenceNumber, EntryValue newValue));
MOCK_METHOD2(forcePut, void(SequenceNumber newSequenceNumber, EntryValue newValue));
MOCK_METHOD1(sendValue, void(DataIOStream& iostream));
MOCK_METHOD1(setId, void(EntryId id));
MOCK_METHOD1(send, void(NetworkTableConnection& connection));
MOCK_METHOD1(fireListener, void(TableListenerManager& listenerManager));
};
#endif /* IOSTREAMPROVIDERMOCK_H_ */

View File

@@ -0,0 +1,22 @@
#include "ByteArrayIOStream.h"
#include <cstring>
#include "networktables2/util/IOException.h"
ByteArrayIOStream::ByteArrayIOStream(uint8_t buf[], size_t len) : buff()
{
buff.assign(buf, buf + len);
}
int ByteArrayIOStream::read(void* ptr, int numbytes)
{
if(numbytes>buff.size())
throw IOException("read past end of stream");
memcpy(ptr, &buff[0], numbytes);
buff.erase (buff.begin(),buff.begin()+numbytes);
}
int ByteArrayIOStream::write(const void* ptr, int numbytes)
{
for (int i = 0; i < numbytes; i++)
buff.push_back(*((char*)ptr+i));
}

View File

@@ -0,0 +1,33 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "mocks/networktables2/IOStreamProviderMock.h"
#include "mocks/networktables2/NTThreadManagerMock.h"
#include "mocks/networktables2/NTThreadMock.h"
#include "networktables2/server/NetworkTableServer.h"
using namespace testing;
TEST(ServerTest, testClose) {
MockIOStreamProvider streamProvider;
MockNTThreadManager threadManager;
MockNTThread* thread1 = new MockNTThread();
MockNTThread* thread2 = new MockNTThread();
EXPECT_CALL(threadManager, newBlockingPeriodicThread(A<PeriodicRunnable*>(),
AnyOf(StrEq("Server Incoming Stream Monitor Thread"), StrEq("Write Manager Thread"))))
.Times(AtLeast(1))
.WillOnce(Return(thread1))
.WillOnce(Return(thread2));
EXPECT_CALL(streamProvider, close()).Times(1);
EXPECT_CALL(*thread1, stop()).Times(1);
EXPECT_CALL(*thread2, stop()).Times(1);
NetworkTableEntryTypeManager typeManager;
NetworkTableServer* server = new NetworkTableServer(streamProvider, typeManager, threadManager);
delete server;//will close stream provider
//do not have to delete threads they are deleted by the server
}

View File

@@ -0,0 +1,178 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "networktables2/type/NetworkTableEntryTypeManager.h"
#include "networktables2/type/DefaultEntryTypes.h"
#include "networktables2/connection/DataIOStream.h"
#include "ByteArrayIOStream.h"
#include "TesterTemplate.h"
#include "networktables2/util/IOException.h"
TEST(EntryType, testIdToType) {
NetworkTableEntryTypeManager* typeManager = new NetworkTableEntryTypeManager();
EXPECT_EQ(&DefaultEntryTypes::BOOLEAN, typeManager->GetType(0));
EXPECT_EQ(&DefaultEntryTypes::DOUBLE, typeManager->GetType(1));
EXPECT_EQ(&DefaultEntryTypes::STRING, typeManager->GetType(2));
EXPECT_EQ(NULL, typeManager->GetType(3));
EXPECT_EQ(NULL, typeManager->GetType(4));
EXPECT_EQ(NULL, typeManager->GetType(5));
EXPECT_EQ(NULL, typeManager->GetType(127));
EXPECT_EQ(NULL, typeManager->GetType(-1));
delete typeManager;
}
TEST(EntryType, testTypeIds) {
EXPECT_EQ(DefaultEntryTypes::BOOLEAN.id, 0);
EXPECT_EQ(DefaultEntryTypes::DOUBLE.id, 1);
EXPECT_EQ(DefaultEntryTypes::STRING.id, 2);
}
TEST(EntryType, testTypeNames) {
EXPECT_STREQ(DefaultEntryTypes::BOOLEAN.name, "Boolean");
EXPECT_STREQ(DefaultEntryTypes::DOUBLE.name, "Double");
EXPECT_STREQ(DefaultEntryTypes::STRING.name, "String");
}
TEST(EntryType, testBooleanRead) {
NEW_BAIOS(1,0)
EXPECT_FALSE(DefaultEntryTypes::BOOLEAN.readValue(*stream).b);
delete stream;
UPDATE_BAIOS(1,1)
EXPECT_TRUE(DefaultEntryTypes::BOOLEAN.readValue(*stream).b);
delete stream;
UPDATE_BAIOS(0)
ASSERT_THROW(DefaultEntryTypes::BOOLEAN.readValue(*stream), IOException);
delete stream;
}
TEST(EntryType, testBooleanWrite) {
ByteArrayIOStream *output = new ByteArrayIOStream();
EntryValue ev;
ev.b = false;
DataIOStream* stream = new DataIOStream(output);
DefaultEntryTypes::BOOLEAN.sendValue(ev, *stream);
uint8_t expA[1] = {0};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expA, 1));
delete stream;
output = new ByteArrayIOStream();
stream = new DataIOStream(output);
ev.b = true;
DefaultEntryTypes::BOOLEAN.sendValue(ev, *stream);
uint8_t expb[1] = {1};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expb, 1));
delete stream;
}
/* TODO: fix this throw
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DefaultEntryTypes.BOOLEAN.sendValue(new Object(), new DataOutputStream(output));
fail();
} catch (IOException e) {}
}
*/
TEST(EntryType, testDoubleRead) {
NEW_BAIOS(8, 64, 41, 0, 0, 0, 0, 0, 0)
EXPECT_EQ(12.5, DefaultEntryTypes::DOUBLE.readValue(*stream).f);
delete stream;
UPDATE_BAIOS(8, 64, 100, 0, 0, 0, 0, 0, 0)
EXPECT_EQ(160, DefaultEntryTypes::DOUBLE.readValue(*stream).f);
delete stream;
UPDATE_BAIOS(0)
ASSERT_THROW(DefaultEntryTypes::DOUBLE.readValue(*stream), IOException);
delete stream;
}
TEST(EntryType, testDoubleWrite) {
ByteArrayIOStream *output = new ByteArrayIOStream();
DataIOStream* stream = new DataIOStream(output);
EntryValue ev;
ev.f = 12.5;
DefaultEntryTypes::DOUBLE.sendValue(ev, *stream);
uint8_t expA[] = {64, 41, 0, 0, 0, 0, 0, 0};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expA, 8));
delete stream;
output = new ByteArrayIOStream();
stream = new DataIOStream(output);
ev.f = 160;
DefaultEntryTypes::DOUBLE.sendValue(ev, *stream);
uint8_t expb[] = {64, 100, 0, 0, 0, 0, 0, 0};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expb, 8));
delete stream;
}
/*
@Te TODO: cache
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DefaultEntryTypes.DOUBLE.sendValue(new Object(), new DataOutputStream(output));
fail();
} catch (IOException e) {}
}
*/
TEST(EntryType, testStringRead) {
NEW_BAIOS(10, 0, 8, 77, 121, 32, 69, 110, 116, 114, 121)
std::string* str = (std::string*)DefaultEntryTypes::STRING.readValue(*stream).ptr;
EXPECT_STREQ("My Entry", str->c_str());
delete stream;
delete str;
UPDATE_BAIOS(7, 0, 5, 86, 97, 76, 117, 69)
str = (std::string*)DefaultEntryTypes::STRING.readValue(*stream).ptr;
EXPECT_STREQ("VaLuE", str->c_str());
delete stream;
delete str;
UPDATE_BAIOS(0)
ASSERT_THROW(DefaultEntryTypes::STRING.readValue(*stream), IOException);
delete stream;
}
TEST(EntryType, testStringWrite) {
ByteArrayIOStream *output = new ByteArrayIOStream();
DataIOStream* stream = new DataIOStream(output);
EntryValue ev;
ev.ptr = new std::string("My Entry");
DefaultEntryTypes::STRING.sendValue(ev, *stream);
uint8_t expA[] = {0, 8, 77, 121, 32, 69, 110, 116, 114, 121};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expA, 10));
delete stream;
delete (std::string*)ev.ptr;
output = new ByteArrayIOStream();
stream = new DataIOStream(output);
ev.ptr = new std::string("VaLuE");
DefaultEntryTypes::STRING.sendValue(ev, *stream);
uint8_t expb[] = {0, 5, 86, 97, 76, 117, 69};
EXPECT_THAT(output->toByteArray(), IsArrayEqualTo(expb, 7));
delete stream;
delete (std::string*)ev.ptr;
}
/*TODO: throw
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
DefaultEntryTypes.STRING.sendValue(new Object(), new DataOutputStream(output));
fail();
} catch (IOException e) {}
}
}*/