5 #ifndef NDS2_CLIENT_BASIC_IO_HH
6 #define NDS2_CLIENT_BASIC_IO_HH
10 #include "common/status_codes.hh"
25 template <
typename BufferedReader >
29 Basic_IO( BufferedReader& r ) : r_( r )
36 nds_impl::common::status_code
39 nds_impl::common::status_code result;
40 r_.read_exactly( result.data( ),
41 result.data( ) + result.size( ) );
54 std::array< char, 8 > buf;
55 r_.read_exactly( buf.data( ), buf.data( ) + buf.size( ) );
56 for (
const auto byte : buf )
58 val = ( val << 4 ) | from_hex_nibble( byte );
71 uint32_t tmp = read_uint32_hex( );
72 return *
reinterpret_cast< float*
>( &tmp );
79 from_hex_nibble(
int nibble )
81 if ( nibble >=
'0' && nibble <=
'9' )
85 if ( nibble >=
'a' && nibble <=
'f' )
87 return ( nibble -
'a' ) + 10;
89 if ( nibble >=
'A' && nibble <=
'F' )
91 return ( nibble -
'A' ) + 10;
93 throw std::invalid_argument(
94 "Unknown input found while converting a hex value" );
105 #ifdef _NDS_IMPL_ENABLE_CATCH_TESTS_
107 #include "common/status_codes.hh"
108 #include "socket/buffered_reader.hh"
109 #include "tests/dummy_socket.hh"
112 TEST_CASE(
"You should be able to create a basic_io object",
113 "[nds2,basic_io,create]" )
115 using namespace nds_testing;
117 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
120 sock_type buf_s( s );
121 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s );
124 TEST_CASE(
"You should be able to read a nds1 status code",
"[nds1]" )
126 using namespace nds_testing;
127 using namespace nds_impl::common;
129 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
131 DummySocket s1(
"0000000d" );
132 sock_type buf_s1( s1 );
133 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s1 );
135 REQUIRE( b.read_status_code( ) == STATUS_DAQD_OK );
136 REQUIRE( b.read_status_code( ) == STATUS_DAQD_NOT_FOUND );
139 TEST_CASE(
"You should be able to read a 32bit ascii/hex value",
140 "[nds1,basic_io,hex]" )
142 using namespace nds_testing;
143 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
145 DummySocket s1(
"0000034000000000f7F8f9aB" );
146 sock_type buf_s1( s1 );
147 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s1 );
149 REQUIRE( b.read_uint32_hex( ) == 0x340 );
150 REQUIRE( b.read_uint32_hex( ) == 0x0 );
151 REQUIRE( b.read_uint32_hex( ) == 0xf7f8f9ab );
154 TEST_CASE(
"Exceptions are thrown when reading a bad 32bit ascii/hex value",
155 "[nds1,basic_io,hex]" )
157 using namespace nds_testing;
158 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
162 DummySocket s1(
"0000&3400000" );
163 sock_type buf_s1( s1 );
164 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s1 );
166 REQUIRE_THROWS_AS( b.read_uint32_hex( ), std::invalid_argument );
167 REQUIRE_THROWS( b.read_uint32_hex( ) );
170 TEST_CASE(
"You should be able to read a 32bit float ascii/hex value",
171 "[nds1,basic_io,hex]" )
173 using namespace nds_testing;
174 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
176 DummySocket s1(
"3f800000" );
177 sock_type buf_s1( s1 );
178 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s1 );
180 REQUIRE( b.read_float32_hex( ) == 1.0f );
184 "Exceptions are thrown when reading a bad 32bit float ascii/hex value",
185 "[nds1,basic_io,hex]" )
187 using namespace nds_testing;
188 typedef nds_impl::Socket::BufferedReader< DummySocket > sock_type;
192 DummySocket s1(
"3f8&0000000" );
193 sock_type buf_s1( s1 );
194 nds_impl::nds1::common::Basic_IO< sock_type > b( buf_s1 );
196 REQUIRE_THROWS_AS( b.read_float32_hex( ), std::invalid_argument );
197 REQUIRE_THROWS( b.read_float32_hex( ) );
200 #endif // _NDS_IMPL_ENABLE_CATCH_TESTS_
202 #endif // NDS2_CLIENT_BASIC_IO_HH