From 2be4f09fd6c9293ef555567418c8ce5a1d1f2d3d Mon Sep 17 00:00:00 2001 From: Meno Abels Date: Fri, 9 Sep 2016 21:43:39 +0200 Subject: [PATCH] 58% passed --- .travis.yml | 10 ++++++---- cpp/.gitignore | 1 + cpp/CMakeLists.txt | 13 +++++-------- cpp/src/crunchy.hpp | 12 ++++++++++++ cpp/test/CMakeLists.txt | 33 +++++++++++++++++++++++---------- cpp/test/chai.hpp | 22 ++++++++++++++-------- cpp/test/mocha.hpp | 6 ++++-- cpp/test/test_ipaddress.cpp | 1 + cpp/test/test_ipv4.cpp | 1 + cpp/test/test_ipv6.cpp | 1 + cpp/test/test_ipv6_loopback.cpp | 1 + cpp/test/test_ipv6_mapped.cpp | 1 + cpp/test/test_ipv6_unspec.cpp | 1 + cpp/test/test_prefix128.cpp | 1 + cpp/test/test_prefix32.cpp | 2 ++ cpp/test/test_result.cpp | 1 + 16 files changed, 75 insertions(+), 32 deletions(-) diff --git a/.travis.yml b/.travis.yml index 909b5c0..08db108 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,10 @@ language: rust rust: - 1.8.0 -#before_install: -# - sudo apt-get -qq update -# - sudo apt-get install -y ruby -# - gem install rbtree +before_install: + - sudo apt-get -qq update + - sudo apt-get install -y libboost-all-dev cmake + install: - rvm use ruby-2.0.0-p598 - gem install construqt-ipaddress @@ -18,3 +18,5 @@ script: - (cd rust && cargo build --release --verbose) - (cd rust && cargo test --release --verbose) - (cd js && npm install && npm test) + - (cd cpp && cmake -DCMAKE_BUILD_TYPE=Release . && make) + - (cd cpp && ctest --timeout 60) diff --git a/cpp/.gitignore b/cpp/.gitignore index 94f354d..2ed2852 100644 --- a/cpp/.gitignore +++ b/cpp/.gitignore @@ -3,3 +3,4 @@ **/CMakeFiles/ **/CTestTestfile.cmake **/cmake_install.cmake +libipaddress.* diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 429dd05..75a3b8f 100755 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -31,14 +31,11 @@ find_package(Threads REQUIRED) include_directories(.) -add_executable(main main.cpp tunator.cpp tun_device.cpp if_addrs_macos.cpp if_addrs_linux.cpp) -add_dependencies(main SimpleWebSocketServer) -add_dependencies(main jsoncpp) -#target_link_libraries(main ${jsoncpp_LIBRARY_DIR}) -target_link_libraries(main ${jsoncpp_LIBRARIES}) -target_link_libraries(main ${Boost_LIBRARIES}) -target_link_libraries(main ${OPENSSL_LIBRARIES}) -target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT}) +file(GLOB SOURCES "src/*.cpp") +#add_library(ipaddress SHARED ${SOURCES}) +add_library(ipaddress STATIC ${SOURCES}) +target_link_libraries(ipaddress ${Boost_LIBRARIES}) + enable_testing() add_subdirectory(test) diff --git a/cpp/src/crunchy.hpp b/cpp/src/crunchy.hpp index d81c590..552f9e2 100644 --- a/cpp/src/crunchy.hpp +++ b/cpp/src/crunchy.hpp @@ -182,6 +182,18 @@ class Crunchy { s2 << std::dec; } else if (radix == 16) { s2 << std::hex; + } else if (radix == 2) { + std::vector bits; + auto my = this->num; + do { + size_t tmp = static_cast(my&0x1); + bits.push_back(tmp); + my = my >> 1; + } while(my != 0); + for (int i = bits.size()-1; i >= 0; --i) { + s2 << bits[i]; + } + return s2.str(); } else { throw NotImplementedException(); } diff --git a/cpp/test/CMakeLists.txt b/cpp/test/CMakeLists.txt index 70ca3be..b49e203 100755 --- a/cpp/test/CMakeLists.txt +++ b/cpp/test/CMakeLists.txt @@ -20,6 +20,9 @@ include_directories(${Boost_INCLUDE_DIR}) find_package(Threads REQUIRED) +set ( PROJECT_LINK_LIBS libipaddress.a ) +link_directories( .. ) + #add_executable(test_crunchy, test_crunchy.cpp) #add_test(test_crunchy test_crunchy) @@ -29,42 +32,52 @@ add_test(test_option test_option) add_executable(test_result test_result.cpp) add_test(test_result test_result) -add_executable(test_rle test_rle.cpp ../src/rle.cpp) +add_executable(test_rle test_rle.cpp) add_test(test_rle test_rle) +target_link_libraries(test_rle ${PROJECT_LINK_LIBS} ) -add_executable(test_crunchy test_crunchy.cpp ../src/crunchy.cpp) +add_executable(test_crunchy test_crunchy.cpp) add_test(test_crunchy test_crunchy) +target_link_libraries(test_crunchy ${PROJECT_LINK_LIBS} ) -add_executable(test_prefix32 test_prefix32.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_prefix32 test_prefix32.cpp) add_test(test_prefix32 test_prefix32) target_link_libraries(test_prefix32 ${Boost_LIBRARIES}) +target_link_libraries(test_prefix32 ${PROJECT_LINK_LIBS} ) -add_executable(test_prefix128 test_prefix128.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_prefix128 test_prefix128.cpp) add_test(test_prefix128 test_prefix128) target_link_libraries(test_prefix128 ${Boost_LIBRARIES}) +target_link_libraries(test_prefix128 ${PROJECT_LINK_LIBS} ) -add_executable(test_ipv6_unspec test_ipv6_unspec.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_ipv6_unspec test_ipv6_unspec.cpp) add_test(test_ipv6_unspec test_ipv6_unspec) target_link_libraries(test_ipv6_unspec ${Boost_LIBRARIES}) +target_link_libraries(test_ipv6_unspec ${PROJECT_LINK_LIBS} ) -add_executable(test_ipv6_mapped test_ipv6_mapped.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_ipv6_mapped test_ipv6_mapped.cpp) add_test(test_ipv6_mapped test_ipv6_mapped) target_link_libraries(test_ipv6_mapped ${Boost_LIBRARIES}) +target_link_libraries(test_ipv6_mapped ${PROJECT_LINK_LIBS} ) -add_executable(test_ipv6_loopback test_ipv6_loopback.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_ipv6_loopback test_ipv6_loopback.cpp) add_test(test_ipv6_loopback test_ipv6_loopback) target_link_libraries(test_ipv6_loopback ${Boost_LIBRARIES}) +target_link_libraries(test_ipv6_loopback ${PROJECT_LINK_LIBS} ) -add_executable(test_ipv6 test_ipv6.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_ipv6 test_ipv6.cpp) add_test(test_ipv6 test_ipv6) target_link_libraries(test_ipv6 ${Boost_LIBRARIES}) +target_link_libraries(test_ipv6 ${PROJECT_LINK_LIBS} ) -add_executable(test_ipv4 test_ipv4.cpp ../src/crunchy.cpp ../src/rle.cpp ../src/ipaddress.cpp ../src/ipv4.cpp) +add_executable(test_ipv4 test_ipv4.cpp) add_test(test_ipv4 test_ipv4) target_link_libraries(test_ipv4 ${Boost_LIBRARIES}) +target_link_libraries(test_ipv4 ${PROJECT_LINK_LIBS} ) -add_executable(test_ipaddress test_ipaddress.cpp ../src/crunchy.cpp ../src/ipaddress.cpp ../src/ipv4.cpp ../src/rle.cpp) +add_executable(test_ipaddress test_ipaddress.cpp) add_test(test_ipaddress test_ipaddress) target_link_libraries(test_ipaddress ${Boost_LIBRARIES}) +target_link_libraries(test_ipaddress ${PROJECT_LINK_LIBS} ) diff --git a/cpp/test/chai.hpp b/cpp/test/chai.hpp index 81b39da..4cbb8f8 100644 --- a/cpp/test/chai.hpp +++ b/cpp/test/chai.hpp @@ -22,7 +22,9 @@ namespace Chai { class Assert { public: - void isTrue(bool v, const char *msg = "") const { + size_t count = 0; + void isTrue(bool v, const char *msg = "") { + ++count; if (!v) { std::stringstream s2; s2 << "isTrue got false, "; @@ -30,7 +32,8 @@ namespace Chai { throw AssertError(s2.str().c_str()); } } - void isFalse(bool v, const char *msg = "") const { + void isFalse(bool v, const char *msg = "") { + ++count; if (v) { std::stringstream s2; s2 << "isFalse got true, "; @@ -38,16 +41,17 @@ namespace Chai { throw AssertError(s2.str().c_str()); } } - void equal(const std::string &t1, const char *t2, const char *msg = "") const { + void equal(const std::string &t1, const char *t2, const char *msg = "") { this->equal(t1, std::string(t2), msg); } - void equal(const char *t1, const std::string &t2, const char *msg = "") const { + void equal(const char *t1, const std::string &t2, const char *msg = "") { this->equal(std::string(t1), t2, msg); } - void equal(const char *t1, const char *t2, const char *msg = "") const { + void equal(const char *t1, const char *t2, const char *msg = "") { this->equal(std::string(t1), std::string(t2), msg); } - void equal(unsigned char t1, unsigned char t2, const char *msg = "") const { + void equal(unsigned char t1, unsigned char t2, const char *msg = "") { + ++count; if (t1 != t2) { std::stringstream s2; s2 << "uchar is not equal "; @@ -59,7 +63,8 @@ namespace Chai { } } - template void equal(T t1, T t2, const char *msg = "") const { + template void equal(T t1, T t2, const char *msg = "") { + ++count; if (t1 != t2) { std::stringstream s2; s2 << "is not equal "; @@ -109,7 +114,8 @@ namespace Chai { return s2.str(); } - template void deepEqual(std::vector left, std::vector right, const char *msg = "") const { + template void deepEqual(std::vector left, std::vector right, const char *msg = "") { + ++count; std::stringstream s2; s2 << "array size() missmatch:" << msg << ":" << Assert::vec_to_string(0, left) << "====" << diff --git a/cpp/test/mocha.hpp b/cpp/test/mocha.hpp index 9ce452b..9e019de 100644 --- a/cpp/test/mocha.hpp +++ b/cpp/test/mocha.hpp @@ -4,6 +4,7 @@ #include #include #include "chai.hpp" +#include typedef std::function MochaAction; @@ -13,8 +14,9 @@ static int okCount = 0; int exit() { std::cout << std::dec << std::endl << "Total " << okCount+failCount << " Ok " << okCount - << " Fail " << failCount << std::endl; - return failCount; + << " Fail " << failCount + << " Assertions " << Chai::assert.count << std::endl; + std::exit(std::min(failCount, 27)); } void describe(const char *title, MochaAction action) { diff --git a/cpp/test/test_ipaddress.cpp b/cpp/test/test_ipaddress.cpp index 3735e95..bb92785 100644 --- a/cpp/test/test_ipaddress.cpp +++ b/cpp/test/test_ipaddress.cpp @@ -191,4 +191,5 @@ int main() { Chai::assert.equal("10.0.1.1/24", a2.to_string()); }); }); + return exit(); } diff --git a/cpp/test/test_ipv4.cpp b/cpp/test/test_ipv4.cpp index a4db460..188e8df 100644 --- a/cpp/test/test_ipv4.cpp +++ b/cpp/test/test_ipv4.cpp @@ -628,4 +628,5 @@ int main() { Chai::assert.isTrue(Ipv4::parse_classful("192.168.256.257").isErr()); }); }); + return exit(); } diff --git a/cpp/test/test_ipv6.cpp b/cpp/test/test_ipv6.cpp index db8fe4f..b85e4a7 100644 --- a/cpp/test/test_ipv6.cpp +++ b/cpp/test/test_ipv6.cpp @@ -366,4 +366,5 @@ int main() { Ipv6::from_str(setup().hex, 16, 64).unwrap().to_string()); }); }); + return exit(); } diff --git a/cpp/test/test_ipv6_loopback.cpp b/cpp/test/test_ipv6_loopback.cpp index de16855..be1538b 100644 --- a/cpp/test/test_ipv6_loopback.cpp +++ b/cpp/test/test_ipv6_loopback.cpp @@ -41,4 +41,5 @@ int main() { Chai::assert.equal(true, setup().ip.is_ipv6()); }); }); + return exit(); } diff --git a/cpp/test/test_ipv6_mapped.cpp b/cpp/test/test_ipv6_mapped.cpp index 45be7b0..85a28de 100644 --- a/cpp/test/test_ipv6_mapped.cpp +++ b/cpp/test/test_ipv6_mapped.cpp @@ -87,4 +87,5 @@ int main() { it("test_method_ipv6", []() { Chai::assert.isTrue(setup().ip.is_ipv6()); }); it("test_mapped", []() { Chai::assert.isTrue(setup().ip.is_mapped()); }); }); + return exit(); } diff --git a/cpp/test/test_ipv6_unspec.cpp b/cpp/test/test_ipv6_unspec.cpp index 31536be..8aeec62 100644 --- a/cpp/test/test_ipv6_unspec.cpp +++ b/cpp/test/test_ipv6_unspec.cpp @@ -33,4 +33,5 @@ int main() { Chai::assert.equal(true, setup().ip.is_ipv6()); }); }); + return exit(); } diff --git a/cpp/test/test_prefix128.cpp b/cpp/test/test_prefix128.cpp index 3b89f55..512bc36 100644 --- a/cpp/test/test_prefix128.cpp +++ b/cpp/test/test_prefix128.cpp @@ -43,4 +43,5 @@ describe("prefix128", []() { } }); }); + return exit(); } diff --git a/cpp/test/test_prefix32.cpp b/cpp/test/test_prefix32.cpp index b0a3a81..90e6c77 100644 --- a/cpp/test/test_prefix32.cpp +++ b/cpp/test/test_prefix32.cpp @@ -60,6 +60,7 @@ describe("prefix32", []() { auto num = e.second; // console.log(e); auto prefix = IPAddress::parse_netmask_to_prefix(netmask).unwrap(); + std::cout << netmask << ":" << num << ":" << prefix; Chai::assert.equal(num, prefix); } }); @@ -128,4 +129,5 @@ describe("prefix32", []() { Chai::assert.equal("0.255.255.255", Ipv4::from_number(prefix.host_mask(), 0).unwrap().to_s()); }); }); + return exit(); } diff --git a/cpp/test/test_result.cpp b/cpp/test/test_result.cpp index 61d9486..30cf06c 100644 --- a/cpp/test/test_result.cpp +++ b/cpp/test/test_result.cpp @@ -38,4 +38,5 @@ int main(int, char **) { }); }); + return exit(); }