frnetlib/tests/NetworkEncodingTest.cpp
Fred Nicolson ff25d11089 Removed socket mutexes. Added more tests. Improved examples.
Socket mutexes are no longer really required, and so have been removed.

Added more tests for network encoding functions, and the URL parser.

The URL parser now returns a path preceeded with a '/' instead of cutting it out. Added get_uri() to URL, for getting the whole URI, so users don't have to concat it themselves from the more specialised functions.

Fixed default socket connect timeout checking for the wrong value.

Fixed request_type_strings not containing all of the possible request types.

Fixed README using old socket close syntax.

Cleaned up the examples a bit.
2018-02-01 11:56:34 +00:00

88 lines
2.0 KiB
C++

//
// Created by fred.nicolson on 01/02/18.
//
#include <gtest/gtest.h>
#include "frnetlib/NetworkEncoding.h"
constexpr bool is_little_endian()
{
unsigned short x=0x0001;
auto p = reinterpret_cast<unsigned char*>(&x);
return *p != 0;
}
TEST(NetworkEncodingTest, test_htonf)
{
float input = std::numeric_limits<float>::max();
float result = htonf(input);
if(is_little_endian())
{
float manual;
std::reverse_copy((char*)&input, (char*)&input + sizeof(input), (uint8_t*)&manual);
ASSERT_EQ(memcmp(&result, &manual, sizeof(manual)), 0);
}
else
{
ASSERT_EQ(result, input);
}
}
TEST(NetworkEncodingTest, test_ntohf)
{
float input = std::numeric_limits<float>::max();
float encoded = htonf(input);
float decoded = ntohf(encoded);
ASSERT_EQ(input, decoded);
}
TEST(NetworkEncodingTest, test_htond)
{
double input = std::numeric_limits<double>::max();
double result = htond(input);
if(is_little_endian())
{
double manual;
std::reverse_copy((char*)&input, (char*)&input + sizeof(input), (uint8_t*)&manual);
ASSERT_EQ(memcmp(&result, &manual, sizeof(manual)), 0);
}
else
{
ASSERT_EQ(result, input);
}
}
TEST(NetworkEncodingTest, test_ntohd)
{
double input = std::numeric_limits<double>::max();
double encoded = htond(input);
double decoded = ntohd(encoded);
ASSERT_EQ(input, decoded);
}
TEST(NetworkEncodingTest, test_htonll)
{
uint64_t input = std::numeric_limits<uint64_t>::max();
uint64_t result = htonll(input);
if(is_little_endian())
{
uint64_t manual;
std::reverse_copy((char*)&input, (char*)&input + sizeof(input), (uint8_t*)&manual);
ASSERT_EQ(manual, result);
}
else
{
ASSERT_EQ(result, input);
}
}
TEST(NetworkEncodingTest, test_ntohll)
{
uint64_t input = std::numeric_limits<uint64_t>::max();
uint64_t encoded = htonll(input);
uint64_t decoded = ntohll(encoded);
ASSERT_EQ(input, decoded);
}