~ubuntu-branches/ubuntu/intrepid/asio/intrepid

« back to all changes in this revision

Viewing changes to src/tests/unit/buffered_read_stream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Simon Richter
  • Date: 2007-09-07 11:10:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070907111041-f0uwhs0llvzj9ah5
Tags: upstream-0.3.8~rc3
ImportĀ upstreamĀ versionĀ 0.3.8~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// buffered_read_stream.cpp
 
3
// ~~~~~~~~~~~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
 
6
//
 
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
 
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
9
//
 
10
 
 
11
// Disable autolinking for unit tests.
 
12
#if !defined(BOOST_ALL_NO_LIB)
 
13
#define BOOST_ALL_NO_LIB 1
 
14
#endif // !defined(BOOST_ALL_NO_LIB)
 
15
 
 
16
// Test that header file is self-contained.
 
17
#include "asio/buffered_read_stream.hpp"
 
18
 
 
19
#include <boost/bind.hpp>
 
20
#include <cstring>
 
21
#include "asio.hpp"
 
22
#include "unit_test.hpp"
 
23
 
 
24
typedef asio::buffered_read_stream<
 
25
    asio::ip::tcp::socket> stream_type;
 
26
 
 
27
void test_sync_operations()
 
28
{
 
29
  using namespace std; // For memcmp.
 
30
 
 
31
  asio::io_service io_service;
 
32
 
 
33
  asio::ip::tcp::acceptor acceptor(io_service,
 
34
      asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 0));
 
35
  asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
 
36
  server_endpoint.address(asio::ip::address_v4::loopback());
 
37
 
 
38
  stream_type client_socket(io_service);
 
39
  client_socket.lowest_layer().connect(server_endpoint);
 
40
 
 
41
  stream_type server_socket(io_service);
 
42
  acceptor.accept(server_socket.lowest_layer());
 
43
 
 
44
  const char write_data[]
 
45
    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
46
  const asio::const_buffer write_buf = asio::buffer(write_data);
 
47
 
 
48
  std::size_t bytes_written = 0;
 
49
  while (bytes_written < sizeof(write_data))
 
50
  {
 
51
    bytes_written += client_socket.write_some(
 
52
        asio::buffer(write_buf + bytes_written));
 
53
  }
 
54
 
 
55
  char read_data[sizeof(write_data)];
 
56
  const asio::mutable_buffer read_buf = asio::buffer(read_data);
 
57
 
 
58
  std::size_t bytes_read = 0;
 
59
  while (bytes_read < sizeof(read_data))
 
60
  {
 
61
    bytes_read += server_socket.read_some(
 
62
        asio::buffer(read_buf + bytes_read));
 
63
  }
 
64
 
 
65
  BOOST_CHECK(bytes_written == sizeof(write_data));
 
66
  BOOST_CHECK(bytes_read == sizeof(read_data));
 
67
  BOOST_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
 
68
 
 
69
  bytes_written = 0;
 
70
  while (bytes_written < sizeof(write_data))
 
71
  {
 
72
    bytes_written += server_socket.write_some(
 
73
        asio::buffer(write_buf + bytes_written));
 
74
  }
 
75
 
 
76
  bytes_read = 0;
 
77
  while (bytes_read < sizeof(read_data))
 
78
  {
 
79
    bytes_read += client_socket.read_some(
 
80
        asio::buffer(read_buf + bytes_read));
 
81
  }
 
82
 
 
83
  BOOST_CHECK(bytes_written == sizeof(write_data));
 
84
  BOOST_CHECK(bytes_read == sizeof(read_data));
 
85
  BOOST_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
 
86
 
 
87
  server_socket.close();
 
88
  asio::error_code error;
 
89
  bytes_read = client_socket.read_some(
 
90
      asio::buffer(read_buf), error);
 
91
 
 
92
  BOOST_CHECK(bytes_read == 0);
 
93
  BOOST_CHECK(error == asio::error::eof);
 
94
 
 
95
  client_socket.close(error);
 
96
}
 
97
 
 
98
void handle_accept(const asio::error_code& e)
 
99
{
 
100
  BOOST_CHECK(!e);
 
101
}
 
102
 
 
103
void handle_write(const asio::error_code& e,
 
104
    std::size_t bytes_transferred,
 
105
    std::size_t* total_bytes_written)
 
106
{
 
107
  BOOST_CHECK(!e);
 
108
  if (e)
 
109
    throw asio::system_error(e); // Terminate test.
 
110
  *total_bytes_written += bytes_transferred;
 
111
}
 
112
 
 
113
void handle_read(const asio::error_code& e,
 
114
    std::size_t bytes_transferred,
 
115
    std::size_t* total_bytes_read)
 
116
{
 
117
  BOOST_CHECK(!e);
 
118
  if (e)
 
119
    throw asio::system_error(e); // Terminate test.
 
120
  *total_bytes_read += bytes_transferred;
 
121
}
 
122
 
 
123
void handle_read_eof(const asio::error_code& e,
 
124
    std::size_t bytes_transferred)
 
125
{
 
126
  BOOST_CHECK(e == asio::error::eof);
 
127
  BOOST_CHECK(bytes_transferred == 0);
 
128
}
 
129
 
 
130
void test_async_operations()
 
131
{
 
132
  using namespace std; // For memcmp.
 
133
 
 
134
  asio::io_service io_service;
 
135
 
 
136
  asio::ip::tcp::acceptor acceptor(io_service,
 
137
      asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 0));
 
138
  asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
 
139
  server_endpoint.address(asio::ip::address_v4::loopback());
 
140
 
 
141
  stream_type client_socket(io_service);
 
142
  client_socket.lowest_layer().connect(server_endpoint);
 
143
 
 
144
  stream_type server_socket(io_service);
 
145
  acceptor.async_accept(server_socket.lowest_layer(), handle_accept);
 
146
  io_service.run();
 
147
  io_service.reset();
 
148
 
 
149
  const char write_data[]
 
150
    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
151
  const asio::const_buffer write_buf = asio::buffer(write_data);
 
152
 
 
153
  std::size_t bytes_written = 0;
 
154
  while (bytes_written < sizeof(write_data))
 
155
  {
 
156
    client_socket.async_write_some(
 
157
        asio::buffer(write_buf + bytes_written),
 
158
        boost::bind(handle_write, asio::placeholders::error,
 
159
          asio::placeholders::bytes_transferred, &bytes_written));
 
160
    io_service.run();
 
161
    io_service.reset();
 
162
  }
 
163
 
 
164
  char read_data[sizeof(write_data)];
 
165
  const asio::mutable_buffer read_buf = asio::buffer(read_data);
 
166
 
 
167
  std::size_t bytes_read = 0;
 
168
  while (bytes_read < sizeof(read_data))
 
169
  {
 
170
    server_socket.async_read_some(
 
171
        asio::buffer(read_buf + bytes_read),
 
172
        boost::bind(handle_read, asio::placeholders::error,
 
173
          asio::placeholders::bytes_transferred, &bytes_read));
 
174
    io_service.run();
 
175
    io_service.reset();
 
176
  }
 
177
 
 
178
  BOOST_CHECK(bytes_written == sizeof(write_data));
 
179
  BOOST_CHECK(bytes_read == sizeof(read_data));
 
180
  BOOST_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
 
181
 
 
182
  bytes_written = 0;
 
183
  while (bytes_written < sizeof(write_data))
 
184
  {
 
185
    server_socket.async_write_some(
 
186
        asio::buffer(write_buf + bytes_written),
 
187
        boost::bind(handle_write, asio::placeholders::error,
 
188
          asio::placeholders::bytes_transferred, &bytes_written));
 
189
    io_service.run();
 
190
    io_service.reset();
 
191
  }
 
192
 
 
193
  bytes_read = 0;
 
194
  while (bytes_read < sizeof(read_data))
 
195
  {
 
196
    client_socket.async_read_some(
 
197
        asio::buffer(read_buf + bytes_read),
 
198
        boost::bind(handle_read, asio::placeholders::error,
 
199
          asio::placeholders::bytes_transferred, &bytes_read));
 
200
    io_service.run();
 
201
    io_service.reset();
 
202
  }
 
203
 
 
204
  BOOST_CHECK(bytes_written == sizeof(write_data));
 
205
  BOOST_CHECK(bytes_read == sizeof(read_data));
 
206
  BOOST_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
 
207
 
 
208
  server_socket.close();
 
209
  client_socket.async_read_some(asio::buffer(read_buf), handle_read_eof);
 
210
}
 
211
 
 
212
test_suite* init_unit_test_suite(int argc, char* argv[])
 
213
{
 
214
  test_suite* test = BOOST_TEST_SUITE("buffered_read_stream");
 
215
  test->add(BOOST_TEST_CASE(&test_sync_operations));
 
216
  test->add(BOOST_TEST_CASE(&test_async_operations));
 
217
  return test;
 
218
}