nds2-client - ClientDeveloper  0.16.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dummy_socket.hh
Go to the documentation of this file.
1 //
2 // Created by jonathan.hanks on 5/4/18.
3 //
4 
5 #ifndef NDS2_CLIENT_TEST_DUMMY_SOCKET_HH
6 #define NDS2_CLIENT_TEST_DUMMY_SOCKET_HH
7 
8 #include <iterator>
9 #include <string>
10 #include <vector>
11 
12 namespace nds_testing
13 {
15  {
16  std::vector< char > internal_; // use this unless constructed with a
17  // specific vector
18  std::vector< char >& s_; // reference the vector to use here
19 
21  {
22  }
23  explicit RecordingDummySocket( std::vector< char >& s )
24  : internal_( ), s_( s )
25  {
26  }
27  RecordingDummySocket( const RecordingDummySocket& other ) = delete;
28  RecordingDummySocket( RecordingDummySocket&& other ) = default;
29 
30  void
31  write_all( const char* start, const char* end )
32  {
33 
34  auto dest = std::back_inserter< std::vector< char > >( s_ );
35  std::copy( start, end, dest );
36  }
37 
38  std::string
39  str( )
40  {
41  return std::string( s_.data( ), s_.size( ) );
42  }
43  };
44 
45  struct DummySocket
46  {
47  typedef std::vector< char > storage;
48 
50 
51  DummySocket( const std::string& data )
52  : data_( data.data( ), data.data( ) + data.size( ) )
53  {
54  }
55 
56  DummySocket( const std::vector< char >& data ) : data_{ data }
57  {
58  }
59  DummySocket( std::vector< char >&& data ) : data_{ std::move( data ) }
60  {
61  }
62 
63  template < typename It >
64  void
65  _append_data( It start, It end )
66  {
67  storage::size_type cur_size = data_.size( );
68  data_.resize( cur_size + std::distance( start, end ) );
69  std::copy( start, end, data_.data( ) + cur_size );
70  }
71 
72  char*
73  read_available( char* start, char* end )
74  {
75  ::size_t len = end - start;
76  if ( data_.size( ) == 0 )
77  throw std::runtime_error( "Out of test data to read" );
78  if ( len > data_.size( ) )
79  len = data_.size( );
80  std::copy( data_.begin( ), data_.begin( ) + len, start );
81  std::move( data_.begin( ) + len, data_.end( ), data_.begin( ) );
82  data_.resize( data_.size( ) - len );
83  return start + len;
84  }
85  };
86 }
87 
88 #endif // NDS2_CLIENT_TEST_DUMMY_SOCKET_HH
DummySocket(const std::vector< char > &data)
Definition: dummy_socket.hh:56
DummySocket(const std::string &data)
Definition: dummy_socket.hh:51
void _append_data(It start, It end)
Definition: dummy_socket.hh:65
void write_all(const char *start, const char *end)
Definition: dummy_socket.hh:31
Definition: dummy_socket.hh:45
std::vector< char > & s_
Definition: dummy_socket.hh:18
char * read_available(char *start, char *end)
Definition: dummy_socket.hh:73
std::vector< char > storage
Definition: dummy_socket.hh:47
std::string str()
Definition: dummy_socket.hh:39
RecordingDummySocket(std::vector< char > &s)
Definition: dummy_socket.hh:23
std::vector< char > internal_
Definition: dummy_socket.hh:16
RecordingDummySocket()
Definition: dummy_socket.hh:20
DummySocket(std::vector< char > &&data)
Definition: dummy_socket.hh:59
storage data_
Definition: dummy_socket.hh:49
Definition: dummy_socket.hh:14