Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init check #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 85 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,92 @@ set (LIBAIS_VERSION_MINOR 6)
# "${PROJECT_BINARY_DIR}/ais.h"
# )

include_directories("${PROJECT_BINARY_DIR}")
#include_directories("${PROJECT_BINARY_DIR}")

add_library(ais SHARED STATIC ais ais1_2_3 ais4_11 ais5 ais7_13 ais8 ais9 ais10 ais12 ais14 ais15 ais18 ais19 ais21 ais24 ais8_366_22 )
option(DISABLE_AUTOINIT "Disable automatic library initialization." OFF)
if(DISABLE_AUTOINIT)
add_definitions(-DAIS_INIT_NOCHECK)
endif()

add_executable(test_libais test_libais)
target_link_libraries (test_libais ais)
set (HEADERS
ais.h
ais8_001_22.h
ais8_366_22.h
)

set (SOURCES
ais
ais1_2_3
ais4_11
ais5
ais7_13
ais8 ais8_001_22 ais8_366_22
ais9
ais10
ais12
ais14
ais15
ais18
ais19
ais21
ais24
ais8_366_22
)

install(FILES ${HEADERS} DESTINATION include)

add_library(ais STATIC ${SOURCES} )
install(TARGETS ais DESTINATION lib)


# testing support

option(BUILD_TESTS "Enable testing." OFF)

if(BUILD_TESTS)

option(TESTS_NEED_PTHREADS "Enable if tests need to link agains pthreads" OFF)
option(SEPARATE_TESTS "Enable if separate tests should be built, otherwise a single test executable will be built containing all tests" ON)

find_path(gtest_INCLUDE_DIR gtest/test.h)
find_library(gtest_LIBRARY gtest)
find_library(gtest_main_LIBRARY gtest_main)

if(gtest_INCLUDE_DIR AND gtest_LIBRARY AND gtest_main_LIBRARY)
include_directories(${gtest_INCLUDE_DIR})
set(TEST_LIBS ${TEST_LIBS} ${gtest_LIBRARY} ${gtest_main_LIBRARY})
else()
message(FATAL_ERROR "gtest not found")
endif()

if(TESTS_NEED_PTHREADS)
find_package(Threads)
if(CMAKE_THREAD_LIBS_INIT AND CMAKE_USE_PTHREADS_INIT)
set(TEST_LIBS ${TEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})
else()
message(FATAL_ERROR "pthreads not found")
endif()
endif()

set (TEST_SOURCES
ais1_2_3_unittest
ais8_001_22_unittest
)

enable_testing()

if(SEPARATE_TESTS)
foreach(TEST_SOURCE ${TEST_SOURCES})
add_executable(${TEST_SOURCE} ${TEST_SOURCE})
target_link_libraries (${TEST_SOURCE} ais ${TEST_LIBS})
add_test(${TEST_SOURCE} ${TEST_SOURCE})
endforeach()
else()
add_executable(all_tests ${TEST_SOURCES})
target_link_libraries(all_tests ais ${TEST_LIBS})
add_test(all_tests all_tests)
endif()

endif()

# There must be a new line after each command!
5 changes: 4 additions & 1 deletion ais.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class AisMsg {
//AisMsg() {init();}
//protected:
void init() {
//std::cout << "AisMsg_init: setting ok" << std::endl;
#ifndef AIS_INIT_NOCHECK
if(!nmea_ord_initialized)
build_nmea_lookup();
#endif
status = AIS_OK;
#ifndef NDEBUG
// FIX: should we be setting these? The individual messages need to do this.
Expand Down
3 changes: 2 additions & 1 deletion ais1_2_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ using namespace std;

Ais1_2_3::Ais1_2_3(const char *nmea_payload) {
assert(nmea_payload);
assert(nmea_ord_initialized); // Make sure we have the lookup table built
init();
assert(nmea_ord_initialized); // Make sure we have the lookup table built


if (strlen(nmea_payload) != 168/6) { status = AIS_ERR_BAD_BIT_COUNT; return; }

Expand Down
8 changes: 4 additions & 4 deletions ais1_2_3_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using namespace std;

TEST(TestAis1_2_3,AisMsg) {
TEST(TestAis123,AisMsg) {
AisMsg a;
a.init();
ASSERT_EQ(AIS_OK, a.get_error());
Expand All @@ -17,8 +17,8 @@ import binary
bv = binary.ais6tobitvec('15Mq4J0P01EREODRv4@74gv00HRq')
print bv
*/
TEST(TestAis1_2_3, BitDecoding) {
build_nmea_lookup();
TEST(TestAis123, BitDecoding) {
//build_nmea_lookup();
const string m_str("15Mq4J0P01EREODRv4@74gv00HRq");
const string bits_expected("000001000101011101111001000100011010000000100000000000000001010101100010010101011111010100100010111110000100010000000111000100101111111110000000000000011000100010111001");
bitset<168> bs;
Expand Down Expand Up @@ -59,7 +59,7 @@ TEST(TestAis1_2_3, BitDecoding) {
slot_offset: n/a
*/

TEST(TestAis1_2_3, AisMsg1) {
TEST(TestAis123, AisMsg1) {
const string m_str("AIVDM,1,1,,B,15Mq4J0P01EREODRv4@74gv00HRq,0*72,b003669970,1272412824");
const string body(nth_field(m_str,5,','));
ASSERT_STREQ("15Mq4J0P01EREODRv4@74gv00HRq",body.c_str());
Expand Down
3 changes: 2 additions & 1 deletion ais8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

Ais8::Ais8(const char *nmea_payload) {
assert(nmea_payload);
assert(nmea_ord_initialized); // Make sure we have the lookup table built
init();
assert(nmea_ord_initialized); // Make sure we have the lookup table built

const int payload_len = strlen(nmea_payload)*6 - 46; // in bits w/o DAC/FI
//std::cout << "payload_len: " << strlen(nmea_payload) << " " << strlen(nmea_payload)*6 << " " << payload_len << " " << payload_len / 8 << "\n";
if (payload_len < 0 or payload_len > 952) {
Expand Down
2 changes: 1 addition & 1 deletion ais8_001_22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Ais8_001_22_SubArea* ais8_001_22_subarea_factory(const std::bitset<AIS8_MAX_BITS
Ais8_001_22::Ais8_001_22(const char *nmea_payload, const size_t pad) {
//std::cerr << "WARNING: Ais8_001_22 is totally untests" << std::endl;
assert(nmea_payload);
assert(nmea_ord_initialized); // Make sure we have the lookup table built

assert(strlen(nmea_payload) >= 33);

init();
Expand Down
39 changes: 34 additions & 5 deletions ais8_001_22_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const string nth_field(const string &str, const size_t n, const char c) {
}
#endif

TEST(EmptyTest, Empty) {
TEST(ais8dac001fi22, Empty) {
// FIX: test the empty string case. What should it do?
}

TEST(HelperTest, Helper) {
TEST(ais8dac001fi22, Helper) {
// Does nth_field work right?
const string msg_str = "!AIVDM,1,1,,A,81mg=5@0EP:0>H0007P>0<D1<qp400000,0*1D";
const string body(nth_field(msg_str,5,','));
Expand All @@ -58,14 +58,14 @@ TEST(HelperTest, Helper) {
}

// This is really not working right. What is going on?
TEST(PointTest, Point) {
build_nmea_lookup();
TEST(ais8dac001fi22, Point) {
//build_nmea_lookup();

// AreaNotice: type=0 start=2011-07-06 00:00:00 duration=60 m link_id=10 sub-areas: 1
const string msg_str = "!AIVDM,1,1,,A,81mg=5@0EP:0>H0007P>0<D1<qp400000,0*1D";
const string body(nth_field(msg_str,5,','));
//cout << "body: " << body << endl;
Ais8_001_22 msg(body.c_str());
Ais8_001_22 msg(body.c_str(),0);
ASSERT_EQ(AIS_OK, msg.get_error());
//cout << "msg8_1_22: " << endl;
//msg.print();
Expand All @@ -77,3 +77,32 @@ TEST(PointTest, Point) {
EXPECT_EQ(msg.dac,1);
EXPECT_EQ(msg.fi,22);
} // PointTest

TEST(ais8dac001fi22, Polygon) {
{
// !AIVDM,2,1,2,A,81mg=5@0EPO0VVbh00P=t<Ra<9;400000TFKVP1vL>>,0*74
// !AIVDM,2,2,2,A,?J000,3*52

const char * payload = "81mg=5@0EPO0VVbh00P=t<Ra<9;400000TFKVP1vL>>?J000";
Ais8_001_22 msg(payload,3);
//msg.print();

ASSERT_EQ(AIS_OK, msg.get_error());
EXPECT_EQ(msg.sub_areas.size(),2);

const char * payload2 = "81mg=5@0EPd0VV`800@>2EKI>@uT00000TFNWP1Od>J";
Ais8_001_22 msg2(payload2,0);
//msg2.print();

ASSERT_EQ(AIS_OK, msg2.get_error());


// empty polygon
const char * payload3 = "81mg=5@0EPD0Vn3PJip1TeD1TeD400000P0000000001J000";
Ais8_001_22 msg3(payload3,3);
//msg3.print();

ASSERT_EQ(AIS_OK, msg3.get_error());

}
}