5 #ifndef NDS2_CLIENT_BASIC_IO_HH
6 #define NDS2_CLIENT_BASIC_IO_HH
25 template <
typename BufferedReader >
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 )
72 return *
reinterpret_cast< float*
>( &tmp );
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_
112 TEST_CASE(
"You should be able to create a basic_io object",
113 "[nds2,basic_io,create]" )
115 using namespace nds_testing;
120 sock_type buf_s( 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;
132 sock_type buf_s1( s1 );
139 TEST_CASE(
"You should be able to read a 32bit ascii/hex value",
140 "[nds1,basic_io,hex]" )
142 using namespace nds_testing;
146 sock_type buf_s1( 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;
163 sock_type buf_s1( 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;
177 sock_type buf_s1( 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;
193 sock_type buf_s1( 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
Definition: basic_io.hh:26
std::array< char, 4 > status_code
Definition: status_codes.hh:14
const auto STATUS_DAQD_OK
Definition: status_codes.hh:16
std::uint32_t read_uint32_hex()
Definition: basic_io.hh:51
TEST_CASE("daq_strlcpy copies strings safely when buffers are sufficiently large")
Definition: test_bsd_string.cc:9
nds_impl::common::status_code read_status_code()
Definition: basic_io.hh:37
Definition: dummy_socket.hh:45
int from_hex_nibble(int nibble)
Definition: basic_io.hh:79
const auto STATUS_DAQD_NOT_FOUND
Definition: status_codes.hh:20
float read_float32_hex()
Definition: basic_io.hh:69
BufferedReader & r_
Definition: basic_io.hh:76
Basic_IO(BufferedReader &r)
Definition: basic_io.hh:29
Definition: buffered_reader.hh:17