virtual void registerTest(TestCase const &testCase) {
     std::string name = testCase.getTestCaseInfo().name;
     if (name == "") {
         std::ostringstream oss;
         oss << "Anonymous test case " << ++m_unnamedCount;
         return registerTest(testCase.withName(oss.str()));
     }
     m_functions.push_back(testCase);
 }
        virtual void registerTest( const TestCaseInfo& testInfo ) {
            if( testInfo.getName() == "" ) {
                std::ostringstream oss;
                oss << testInfo.getName() << "unnamed/" << ++m_unnamedCount;
                return registerTest( TestCaseInfo( testInfo, oss.str() ) );
            }

            if( m_functions.find( testInfo ) == m_functions.end() ) {
                m_functions.insert( testInfo );
                m_functionsInOrder.push_back( testInfo );
                if( !testInfo.isHidden() )
                    m_nonHiddenFunctions.push_back( testInfo );
            }
            else {
                const TestCaseInfo& prev = *m_functions.find( testInfo );
                std::cerr   << "error: TEST_CASE( \"" << testInfo.getName() << "\" ) already defined.\n"
                            << "\tFirst seen at " << SourceLineInfo( prev.getLineInfo() ) << "\n"
                            << "\tRedefined at " << SourceLineInfo( testInfo.getLineInfo() ) << std::endl;
                exit(1);
            }
        }
    virtual void registerTest( TestCase const& testCase ) {
        std::string name = testCase.getTestCaseInfo().name;
        if( name == "" ) {
            std::ostringstream oss;
            oss << "Anonymous test case " << ++m_unnamedCount;
            return registerTest( testCase.withName( oss.str() ) );
        }

        if( m_functions.find( testCase ) == m_functions.end() ) {
            m_functions.insert( testCase );
            m_functionsInOrder.push_back( testCase );
            if( !testCase.isHidden() )
                m_nonHiddenFunctions.push_back( testCase );
        }
        else {
            TestCase const& prev = *m_functions.find( testCase );
            std::cerr   << "error: TEST_CASE( \"" << name << "\" ) already defined.\n"
                        << "\tFirst seen at " << prev.getTestCaseInfo().lineInfo << "\n"
                        << "\tRedefined at " << testCase.getTestCaseInfo().lineInfo << std::endl;
            exit(1);
        }
    }
Ejemplo n.º 4
0
StartupTest::StartupTest() {
    registerTest(this);
}
Ejemplo n.º 5
0
namespace test_zeromq {

    namespace detail {

        struct ContextDestroyer
        {
            void operator()(void* context) const
            {
                zmq_ctx_destroy(context);
            }
        };

        struct SocketCloser
        {
            void operator()(void* socket) const
            {
                zmq_close(socket);
            }
        };

    }

    using UPContext = std::unique_ptr < void, detail::ContextDestroyer >;
    using UPPublisher = std::unique_ptr < void, detail::SocketCloser >;
    using UPSubscriber = std::unique_ptr < void, detail::SocketCloser >;

#pragma warning(push)
#pragma warning(disable : 4996) // Disable warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    static bool publisherLoop()
    {
        std::cout << "BEGIN publisherLoop\n";

        auto context = UPContext(zmq_ctx_new());
        std::cout << "context = " << context.get() << "\n";

        auto publisher = UPPublisher(zmq_socket(context.get(), ZMQ_PUB));
        std::cout << "publisher = " << publisher.get() << "\n";

        int rc = zmq_bind(publisher.get(), "tcp://*:5556");
        if (rc != 0)
        {
            std::cout << "END publisherLoop = false, rc = " << rc << "\n";
            return false;
        }

        for (;;)
        {
            auto zipcode = int{ 12345 };
            auto temperature = int{ 215 };
            auto relhumidity = int{ 50 };

            char update[20] = { 0 };
            sprintf(update, "%05d %d %d", zipcode, temperature, relhumidity);
            zmq_send(publisher.get(), update, strlen(update) + 1, ZMQ_DONTWAIT);

            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }

        std::cout << "END publisherLoop = true\n";
        return true;
    }
#pragma warning(pop)

#pragma warning(push)
#pragma warning(disable : 4996) // Disable warning C4996: 'sscanf': This function or variable may be unsafe. Consider using sscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    static bool subscriberLoop()
    {
        std::cout << "BEGIN subscriberLoop\n";

        auto context = UPContext(zmq_ctx_new());
        std::cout << "context = " << context.get() << "\n";

        auto subscriber = UPSubscriber(zmq_socket(context.get(), ZMQ_SUB));
        std::cout << "subscriber = " << subscriber.get() << "\n";

        int rc = zmq_connect(subscriber.get(), "tcp://localhost:5556");
        if (rc != 0)
        {
            std::cout << "END subscriberLoop = false, rc = " << rc << "\n";
            return false;
        }

        //  Subscribe to zipcode, default is NYC, 10001
        rc = zmq_setsockopt(subscriber.get(), ZMQ_SUBSCRIBE, "12345", strlen("12345"));
        if (rc != 0)
        {
            std::cout << "END subscriberLoop = false, rc = " << rc << "\n";
            return false;
        }

        //  Process 100 updates
        int update_nbr;
        long total_temp = 0;
        for (update_nbr = 0; update_nbr < 100; update_nbr++)
        {
            char buffer[500] = { 0 };
            zmq_recv(subscriber.get(), buffer, _countof(buffer), 0);

            int zipcode = 0, temperature = 0, relhumidity = 0;
            sscanf(buffer, "%d %d %d", &zipcode, &temperature, &relhumidity);

            std::cout << "Received zipcode = " << zipcode << ", temperature = " << temperature << ", relhumidity = " << relhumidity << "\n";
        }

        std::cout << "END publisherLoop = true\n";
        return true;
    }
#pragma warning(pop)

    static void testPublisherSubscriber()
    {
        std::cout << "BEGIN test\n";

        std::thread publisherThread(publisherLoop);

        std::thread subscriberThread(subscriberLoop);

        subscriberThread.join();

        publisherThread.join();

        std::cout << "END test\n";
    }

    void test()
    {
        int major = 0;
        int minor = 0;
        int patch = 0;
        zmq_version(&major, &minor, &patch);
        std::cout << "Zeromq version = " << major << "." << minor << "." << patch << "\n";
    }

    static bool basicTestIsRegistered = registerTest("zeromq-basic", &test);
    static bool advancedTestIsRegistered = registerTest("zeromq-rich", &testPublisherSubscriber);

} // namespace test_zeromq