Index: lib/Fuzzer/test/AdapterUnittest.cpp =================================================================== --- /dev/null +++ lib/Fuzzer/test/AdapterUnittest.cpp @@ -0,0 +1,89 @@ +#include "gtest/gtest.h" + +namespace fuzzer { + +template +size_t UnpackPrimitive(const uint8_t *Data, size_t Size, T* Value) { + if (Size < sizeof(T)) + return Size; + *Value = *reinterpret_cast(Data); + return Size - sizeof(T); +} + +template +size_t UnpackSingle(const uint8_t *Data, size_t Size, T* Value); + +template<> +size_t UnpackSingle(const uint8_t *Data, size_t Size, bool* Value) { + return UnpackPrimitive(Data, Size, Value); +} + +template<> +size_t UnpackSingle(const uint8_t *Data, size_t Size, int* Value) { + return UnpackPrimitive(Data, Size, Value); +} + +template +typename std::enable_if::value, bool>::type +UnpackImpl(const uint8_t *Data, size_t Size, TupleT* Tuple) { + return true; +} + +template +typename std::enable_if::value, bool>::type +UnpackImpl(const uint8_t *Data, size_t Size, TupleT* Tuple) { + typedef typename std::tuple_element::type ElemType; + size_t NewSize = UnpackSingle(Data, Size, &std::get(*Tuple)); + if (NewSize == Size) { + return false; + } + + return UnpackImpl(Data + (Size - NewSize), NewSize, Tuple); +} + +template +bool Unpack(const uint8_t *Data, size_t Size, std::tuple* Tuple) { + return UnpackImpl<0, std::tuple>(Data, Size, Tuple); +} + +template +bool Unpack(std::tuple* Tuple, std::initializer_list data) { + std::vector v(data); + return Unpack(v.data(), v.size(), Tuple); +} + +TEST(Unpack, Bool) { + std::tuple T; + EXPECT_TRUE(Unpack(&T, {1})); + EXPECT_TRUE(std::get<0>(T)); + + EXPECT_TRUE(Unpack(&T, {0})); + EXPECT_FALSE(std::get<0>(T)); + + EXPECT_FALSE(Unpack(&T, {})); +} + +TEST(Unpack, BoolBool) { + std::tuple T; + EXPECT_TRUE(Unpack(&T, {1, 0})); + EXPECT_TRUE(std::get<0>(T)); + EXPECT_FALSE(std::get<1>(T)); + + EXPECT_TRUE(Unpack(&T, {0, 1})); + EXPECT_FALSE(std::get<0>(T)); + EXPECT_TRUE(std::get<1>(T)); + + EXPECT_FALSE(Unpack(&T, {})); + EXPECT_FALSE(Unpack(&T, {10})); +} + +TEST(Unpack, BoolInt) { + std::tuple T; + EXPECT_TRUE(Unpack(&T, {1, 16, 2, 0, 0})); + EXPECT_TRUE(std::get<0>(T)); + EXPECT_EQ(528, std::get<1>(T)); + + EXPECT_FALSE(Unpack(&T, {1, 2})); +} + +} // namespace fuzzer Index: lib/Fuzzer/test/CMakeLists.txt =================================================================== --- lib/Fuzzer/test/CMakeLists.txt +++ lib/Fuzzer/test/CMakeLists.txt @@ -90,6 +90,7 @@ add_executable(LLVMFuzzer-Unittest FuzzerUnittest.cpp + AdapterUnittest.cpp $ )