Added some initial unit tests.

This commit is contained in:
chris 2004-01-06 06:06:40 +00:00
parent e07b0beff4
commit 616430c05f
9 changed files with 383 additions and 180 deletions

View File

@ -8,7 +8,6 @@ noinst_PROGRAMS = \
tests/dgram_echo_server_test \
tests/echo_client_test \
tests/echo_server_test \
tests/error_handler_test \
tests/socket_accept_test \
tests/timed_accept_test \
tests/timed_connect_test \
@ -17,6 +16,8 @@ noinst_PROGRAMS = \
tests/tpc_echo_server_test \
tests/performance/client \
tests/performance/server \
tests/unit/error_handler_test \
tests/unit/fixed_buffer_test \
examples/chat/chat_client \
examples/chat/chat_server \
examples/echo/async_tcp_echo_server \
@ -30,6 +31,10 @@ noinst_PROGRAMS = \
examples/tutorial/timer3/timer \
examples/tutorial/timer4/timer
TESTS = \
tests/unit/error_handler_test \
tests/unit/fixed_buffer_test
noinst_HEADERS = \
examples/chat/chat_message.hpp
@ -40,7 +45,6 @@ tests_dgram_echo_server_test_SOURCES = tests/dgram_echo_server_test.cpp
tests_dgram_echo_client_test_SOURCES = tests/dgram_echo_client_test.cpp
tests_echo_client_test_SOURCES = tests/echo_client_test.cpp
tests_echo_server_test_SOURCES = tests/echo_server_test.cpp
tests_error_handler_test_SOURCES = tests/error_handler_test.cpp
tests_socket_accept_test_SOURCES = tests/socket_accept_test.cpp
tests_timed_accept_test_SOURCES = tests/timed_accept_test.cpp
tests_timed_connect_test_SOURCES = tests/timed_connect_test.cpp
@ -49,6 +53,8 @@ tests_timer_test_SOURCES = tests/timer_test.cpp
tests_tpc_echo_server_test_SOURCES = tests/tpc_echo_server_test.cpp
tests_performance_client_SOURCES = tests/performance/client.cpp
tests_performance_server_SOURCES = tests/performance/server.cpp
tests_unit_error_handler_test_SOURCES = tests/unit/error_handler_test.cpp
tests_unit_fixed_buffer_test_SOURCES = tests/unit/fixed_buffer_test.cpp
examples_chat_chat_client_SOURCES = examples/chat/chat_client.cpp
examples_chat_chat_server_SOURCES = examples/chat/chat_server.cpp
examples_echo_async_tcp_echo_server_SOURCES = examples/echo/async_tcp_echo_server.cpp

View File

@ -12,7 +12,6 @@ all: \
tests\dgram_echo_server_test.exe \
tests\echo_client_test.exe \
tests\echo_server_test.exe \
tests\error_handler_test.exe \
tests\socket_accept_test.exe \
tests\timed_accept_test.exe \
tests\timed_connect_test.exe \
@ -21,6 +20,8 @@ all: \
tests\tpc_echo_server_test.exe \
tests\performance\client.exe \
tests\performance\server.exe \
tests\unit\error_handler_test.exe \
tests\unit\fixed_buffer_test.exe \
examples\chat\chat_client.exe \
examples\chat\chat_server.exe \
examples\echo\async_tcp_echo_server.exe \

View File

@ -12,7 +12,6 @@ TEST_EXES = \
tests/dgram_echo_server_test.exe \
tests/echo_client_test.exe \
tests/echo_server_test.exe \
tests/error_handler_test.exe \
tests/socket_accept_test.exe \
tests/timed_accept_test.exe \
tests/timed_connect_test.exe \
@ -20,21 +19,23 @@ TEST_EXES = \
tests/timer_test.exe \
tests/tpc_echo_server_test.exe \
tests/performance/client.exe \
tests/performance/server.exe
tests/performance/server.exe \
tests/unit/error_handler_test.exe \
tests/unit/fixed_buffer_test.exe
EXAMPLE_EXES = \
examples\chat\chat_client.exe \
examples\chat\chat_server.exe \
examples\echo\async_tcp_echo_server.exe \
examples\echo\async_udp_echo_server.exe \
examples\echo\blocking_tcp_echo_client.exe \
examples\echo\blocking_tcp_echo_server.exe \
examples\echo\blocking_udp_echo_client.exe \
examples\echo\blocking_udp_echo_server.exe \
examples\tutorial\timer1\timer.exe \
examples\tutorial\timer2\timer.exe \
examples\tutorial\timer3\timer.exe \
examples\tutorial\timer4\timer.exe
examples/chat/chat_client.exe \
examples/chat/chat_server.exe \
examples/echo/async_tcp_echo_server.exe \
examples/echo/async_udp_echo_server.exe \
examples/echo/blocking_tcp_echo_client.exe \
examples/echo/blocking_tcp_echo_server.exe \
examples/echo/blocking_udp_echo_client.exe \
examples/echo/blocking_udp_echo_server.exe \
examples/tutorial/timer1/timer.exe \
examples/tutorial/timer2/timer.exe \
examples/tutorial/timer3/timer.exe \
examples/tutorial/timer4/timer.exe
all: $(TEST_EXES) $(EXAMPLE_EXES)

View File

@ -12,7 +12,6 @@ all: \
tests\dgram_echo_server_test.exe \
tests\echo_client_test.exe \
tests\echo_server_test.exe \
tests\error_handler_test.exe \
tests\socket_accept_test.exe \
tests\timed_accept_test.exe \
tests\timed_connect_test.exe \
@ -21,6 +20,8 @@ all: \
tests\tpc_echo_server_test.exe \
tests\performance\client.exe \
tests\performance\server.exe \
tests\unit\error_handler_test.exe \
tests\unit\fixed_buffer_test.exe \
examples\chat\chat_client.exe \
examples\chat\chat_server.exe \
examples\echo\async_tcp_echo_server.exe \
@ -39,7 +40,6 @@ tests\dgram_echo_client_test.exe: tests\dgram_echo_client_test.obj
tests\dgram_echo_server_test.exe: tests\dgram_echo_server_test.obj
tests\echo_client_test.exe: tests\echo_client_test.obj
tests\echo_server_test.exe: tests\echo_server_test.obj
tests\error_handler_test.exe: tests\error_handler_test.obj
tests\socket_accept_test.exe: tests\socket_accept_test.obj
tests\timed_accept_test.exe: tests\timed_accept_test.obj
tests\timed_connect_test.exe: tests\timed_connect_test.obj
@ -48,6 +48,8 @@ tests\timer_test.exe: tests\timer_test.obj
tests\tpc_echo_server_test.exe: tests\tpc_echo_server_test.obj
tests\performance\client.exe: tests\performance\client.obj
tests\performance\server.exe: tests\performance\server.obj
tests\unit\error_handler_test.exe: tests\unit\error_handler_test.obj
tests\unit\fixed_buffer_test.exe: tests\unit\fixed_buffer_test.obj
examples\chat\chat_client.exe: examples\chat\chat_client.obj
examples\chat\chat_server.exe: examples\chat\chat_server.obj
examples\echo\async_tcp_echo_server.exe: examples\echo\async_tcp_echo_server.obj

View File

@ -1,161 +0,0 @@
#include <cassert>
#include <iostream>
#include <sstream>
#include "asio.hpp"
using namespace asio;
int main(int argc, char* argv[])
{
try
{
demuxer d;
stream_socket s(d);
socket_connector c(d);
socket_error expected_err;
c.connect(s, inet_address_v4(321, "0.0.0.0"), set_error(expected_err));
std::ostringstream os;
c.connect(s, inet_address_v4(321, "0.0.0.0"), log_error(os));
assert(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || log_error(os));
assert(os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && log_error(os));
assert(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || log_error(os));
assert(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && log_error(os));
assert(os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
log_error_if(os, error == expected_err));
assert(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
log_error_if(os, error != expected_err));
assert(os.str().empty());
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"), throw_error());
assert(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || throw_error());
}
catch (socket_error&)
{
assert(0);
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && throw_error());
assert(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || throw_error());
assert(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && throw_error());
}
catch (socket_error&)
{
assert(0);
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
throw_error_if(error == expected_err));
assert(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
throw_error_if(error != expected_err));
}
catch (socket_error&)
{
assert(0);
}
socket_error err;
c.connect(s, inet_address_v4(321, "0.0.0.0"), set_error(err));
assert(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || set_error(err));
assert(err != expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && set_error(err));
assert(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || set_error(err));
assert(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && set_error(err));
assert(err != expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
set_error_if(err, error == expected_err));
assert(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
set_error_if(err, error != expected_err));
assert(err != expected_err);
}
catch (socket_error& e)
{
std::cerr << "Unhandled socket error: " << e.message() << "\n";
}
catch (std::exception& e)
{
std::cerr << "Unhandled exception: " << e.what() << "\n";
}
return 0;
}

View File

@ -0,0 +1,7 @@
.deps
.dirstamp
*.exe
*_test
*.ilk
*.pdb
*.tds

View File

@ -0,0 +1,149 @@
#include <sstream>
#include "asio.hpp"
#include "unit_test.hpp"
using namespace asio;
void error_handler_test()
{
demuxer d;
stream_socket s(d);
socket_connector c(d);
socket_error expected_err;
c.connect(s, inet_address_v4(321, "0.0.0.0"), set_error(expected_err));
std::ostringstream os;
c.connect(s, inet_address_v4(321, "0.0.0.0"), log_error(os));
UNIT_TEST_CHECK(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || log_error(os));
UNIT_TEST_CHECK(os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && log_error(os));
UNIT_TEST_CHECK(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || log_error(os));
UNIT_TEST_CHECK(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && log_error(os));
UNIT_TEST_CHECK(os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
log_error_if(os, error == expected_err));
UNIT_TEST_CHECK(!os.str().empty());
os.str("");
c.connect(s, inet_address_v4(321, "0.0.0.0"),
log_error_if(os, error != expected_err));
UNIT_TEST_CHECK(os.str().empty());
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"), throw_error());
UNIT_TEST_CHECK(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || throw_error());
}
catch (socket_error&)
{
UNIT_TEST_CHECK(0);
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && throw_error());
UNIT_TEST_CHECK(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || throw_error());
UNIT_TEST_CHECK(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && throw_error());
}
catch (socket_error&)
{
UNIT_TEST_CHECK(0);
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
throw_error_if(error == expected_err));
UNIT_TEST_CHECK(0);
}
catch (socket_error&)
{
}
try
{
c.connect(s, inet_address_v4(321, "0.0.0.0"),
throw_error_if(error != expected_err));
}
catch (socket_error&)
{
UNIT_TEST_CHECK(0);
}
socket_error err;
c.connect(s, inet_address_v4(321, "0.0.0.0"), set_error(err));
UNIT_TEST_CHECK(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err || set_error(err));
UNIT_TEST_CHECK(err != expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error == expected_err && set_error(err));
UNIT_TEST_CHECK(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err || set_error(err));
UNIT_TEST_CHECK(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
error != expected_err && set_error(err));
UNIT_TEST_CHECK(err != expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
set_error_if(err, error == expected_err));
UNIT_TEST_CHECK(err == expected_err);
c.connect(s, inet_address_v4(321, "0.0.0.0"),
set_error_if(err, error != expected_err));
UNIT_TEST_CHECK(err != expected_err);
}
UNIT_TEST(error_handler_test)

View File

@ -0,0 +1,135 @@
//
// fixed_buffer_test.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003, 2004 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Permission to use, copy, modify, distribute and sell this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both the copyright
// notice and this permission notice appear in supporting documentation. This
// software is provided "as is" without express or implied warranty, and with
// no claim as to its suitability for any purpose.
//
#include "asio.hpp"
#include "unit_test.hpp"
using namespace asio;
void fixed_buffer_test()
{
fixed_buffer<32> fb;
const fixed_buffer<32>& const_fb = fb;
UNIT_TEST_CHECK(fb.capacity() == 32);
UNIT_TEST_CHECK(fb.empty());
UNIT_TEST_CHECK(fb.size() == 0);
UNIT_TEST_CHECK(fb.begin() == fb.end());
UNIT_TEST_CHECK(const_fb.begin() == const_fb.end());
fb.push('A');
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 1);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'A');
UNIT_TEST_CHECK(const_fb.front() == 'A');
UNIT_TEST_CHECK(fb.back() == 'A');
UNIT_TEST_CHECK(const_fb.back() == 'A');
fb.front() = 'B';
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 1);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'B');
UNIT_TEST_CHECK(const_fb.front() == 'B');
UNIT_TEST_CHECK(fb.back() == 'B');
UNIT_TEST_CHECK(const_fb.back() == 'B');
fb.back() = 'C';
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 1);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'C');
UNIT_TEST_CHECK(const_fb.front() == 'C');
UNIT_TEST_CHECK(fb.back() == 'C');
UNIT_TEST_CHECK(const_fb.back() == 'C');
fb.pop();
UNIT_TEST_CHECK(fb.empty());
UNIT_TEST_CHECK(fb.size() == 0);
UNIT_TEST_CHECK(fb.begin() == fb.end());
UNIT_TEST_CHECK(const_fb.begin() == const_fb.end());
fb.push('D', 32);
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 32);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'D');
UNIT_TEST_CHECK(const_fb.front() == 'D');
UNIT_TEST_CHECK(fb.back() == 'D');
UNIT_TEST_CHECK(const_fb.back() == 'D');
for (size_t i = 0; i < fb.size(); ++i)
{
UNIT_TEST_CHECK(fb[i] == 'D');
UNIT_TEST_CHECK(const_fb[i] == 'D');
}
fb.front() = 'E';
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 32);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'E');
UNIT_TEST_CHECK(const_fb.front() == 'E');
UNIT_TEST_CHECK(fb.back() == 'D');
UNIT_TEST_CHECK(const_fb.back() == 'D');
fb.pop();
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 31);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
UNIT_TEST_CHECK(fb.front() == 'D');
UNIT_TEST_CHECK(const_fb.front() == 'D');
UNIT_TEST_CHECK(fb.back() == 'D');
UNIT_TEST_CHECK(const_fb.back() == 'D');
for (char* p = fb.begin(); p != fb.end(); ++p)
UNIT_TEST_CHECK(*p == 'D');
for (const char* cp = const_fb.begin(); cp != const_fb.end(); ++cp)
UNIT_TEST_CHECK(*cp == 'D');
fb.pop(31);
UNIT_TEST_CHECK(fb.empty());
UNIT_TEST_CHECK(fb.size() == 0);
UNIT_TEST_CHECK(fb.begin() == fb.end());
UNIT_TEST_CHECK(const_fb.begin() == const_fb.end());
fb.resize(16);
UNIT_TEST_CHECK(!fb.empty());
UNIT_TEST_CHECK(fb.size() == 16);
UNIT_TEST_CHECK(fb.begin() != fb.end());
UNIT_TEST_CHECK(const_fb.begin() != const_fb.end());
fb.clear();
UNIT_TEST_CHECK(fb.empty());
UNIT_TEST_CHECK(fb.size() == 0);
UNIT_TEST_CHECK(fb.begin() == fb.end());
UNIT_TEST_CHECK(const_fb.begin() == const_fb.end());
}
UNIT_TEST(fixed_buffer_test)

View File

@ -0,0 +1,63 @@
//
// unit_test.hpp
// ~~~~~~~~~~~~~
//
// Copyright (c) 2003, 2004 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Permission to use, copy, modify, distribute and sell this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both the copyright
// notice and this permission notice appear in supporting documentation. This
// software is provided "as is" without express or implied warranty, and with
// no claim as to its suitability for any purpose.
//
#ifndef UNIT_TEST_HPP
#define UNIT_TEST_HPP
#include <iostream>
static int unit_test_exit_code = 0;
inline int unit_test(const char* name, void (*func)(void))
{
std::cout << "INFO: " << name << " started\n";
try
{
func();
}
catch (...)
{
std::cout << "FAIL: unhandled exception\n";
unit_test_exit_code = 1;
}
std::cout << "INFO: " << name << " ended with exit code ";
std::cout << unit_test_exit_code << "\n";
return unit_test_exit_code;
}
#define UNIT_TEST(name) int main() { return unit_test(#name, name); }
inline void unit_test_info(const char* file, int line, const char* msg)
{
std::cout << "INFO: " << file << "(" << line << "):" << msg << "\n";
}
#define UNIT_TEST_INFO(s) unit_test_info(__FILE__, __LINE__, s)
inline void unit_test_check(bool condition, const char* file, int line,
const char* msg)
{
if (!condition)
{
std::cout << "FAIL: " << file << "(" << line << "):" << msg << "\n";
unit_test_exit_code = 1;
}
}
#define UNIT_TEST_CHECK(c) unit_test_check((c), __FILE__, __LINE__, #c)
#endif // UNIT_TEST_HPP