Created
October 3, 2014 17:53
-
-
Save binary1248/37afa24e803bfe2dcb90 to your computer and use it in GitHub Desktop.
SFML sf::Packet test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Network.hpp> | |
#include <random> | |
#include <string> | |
#include <cstdint> | |
#include <iostream> | |
struct uniform_bool_distribution | |
{ | |
public: | |
typedef bool result_type; | |
template<typename U> | |
result_type operator() (U& generator) | |
{ | |
return distribution(generator) >= 0; | |
} | |
private: | |
std::uniform_int_distribution<int8_t> distribution; | |
}; | |
template<typename T> | |
struct get_value_type | |
{ | |
typedef typename T::value_type type; | |
}; | |
template<> | |
struct get_value_type<sf::String> | |
{ | |
typedef sf::Uint32 type; | |
}; | |
template<typename T> | |
struct uniform_string_distribution | |
{ | |
public: | |
typedef T result_type; | |
typedef typename get_value_type<result_type>::type element_type; | |
template<typename U> | |
result_type operator() (U& generator) | |
{ | |
auto str = result_type(); | |
auto length = lengthDistribution(generator); | |
for (decltype(length) i = 0; i < length; i++) | |
str += charDistribution(generator); | |
return str; | |
} | |
private: | |
std::uniform_int_distribution<element_type> charDistribution; | |
std::uniform_int_distribution<uint8_t> lengthDistribution; | |
}; | |
std::ostream& operator <<(std::ostream& stream, const sf::String& str) | |
{ | |
return stream << str.toAnsiString(); | |
} | |
template<typename T, typename U, typename V> | |
void pack(T& packet, U& generator, V& distribution) | |
{ | |
packet << distribution(generator); | |
} | |
template<typename T, typename U, typename V> | |
bool check(T& packet, U& verifier, V& distribution) | |
{ | |
typename V::result_type value; | |
packet >> value; | |
auto verification = distribution(verifier); | |
if (value != verification) | |
{ | |
std::cout << "Mismatch: " << value << " != " << verification << std::endl; | |
return false; | |
} | |
return true; | |
} | |
int main() | |
{ | |
auto packet = sf::Packet(); | |
auto seed = std::random_device()(); | |
auto generator = std::mt19937_64(seed); | |
auto verifier = std::mt19937_64(seed); | |
// Signed integers | |
auto int64Dist = std::uniform_int_distribution<int64_t>(); | |
auto int32Dist = std::uniform_int_distribution<int32_t>(); | |
auto int16Dist = std::uniform_int_distribution<int16_t>(); | |
auto int8Dist = std::uniform_int_distribution<int8_t>(); | |
// Unsigned integers | |
auto uint64Dist = std::uniform_int_distribution<uint64_t>(); | |
auto uint32Dist = std::uniform_int_distribution<uint32_t>(); | |
auto uint16Dist = std::uniform_int_distribution<uint16_t>(); | |
auto uint8Dist = std::uniform_int_distribution<uint8_t>(); | |
// Floating point types | |
auto doubleDist = std::uniform_real_distribution<double>(); | |
auto floatDist = std::uniform_real_distribution<float>(); | |
// String types | |
auto stringDist = uniform_string_distribution<std::string>(); | |
auto sfStringDist = uniform_string_distribution<sf::String>(); | |
// Boolean types | |
auto boolDist = uniform_bool_distribution(); | |
// Test 100 rounds of 100 sets of values | |
for (auto j = 0; j < 100; j++) | |
{ | |
for (auto i = 0; i < 100; i++) | |
{ | |
pack(packet, generator, int64Dist); | |
pack(packet, generator, int32Dist); | |
pack(packet, generator, int16Dist); | |
pack(packet, generator, int8Dist); | |
pack(packet, generator, uint64Dist); | |
pack(packet, generator, uint32Dist); | |
pack(packet, generator, uint16Dist); | |
pack(packet, generator, uint8Dist); | |
pack(packet, generator, doubleDist); | |
pack(packet, generator, floatDist); | |
pack(packet, generator, stringDist); | |
pack(packet, generator, sfStringDist); | |
pack(packet, generator, boolDist); | |
} | |
for (auto i = 0; i < 100; i++) | |
{ | |
if (!check(packet, verifier, int64Dist ) || | |
!check(packet, verifier, int32Dist ) || | |
!check(packet, verifier, int16Dist ) || | |
!check(packet, verifier, int8Dist ) || | |
!check(packet, verifier, uint64Dist ) || | |
!check(packet, verifier, uint32Dist ) || | |
!check(packet, verifier, uint16Dist ) || | |
!check(packet, verifier, uint8Dist ) || | |
!check(packet, verifier, doubleDist ) || | |
!check(packet, verifier, floatDist ) || | |
!check(packet, verifier, stringDist ) || | |
!check(packet, verifier, sfStringDist) || | |
!check(packet, verifier, boolDist )) | |
{ | |
std::cout << "Failed at position " << j * 100 + i << std::endl; | |
return 1; | |
} | |
} | |
std::cout << j << "%" << std::endl; | |
} | |
std::cout << "Complete." << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment