diff options
| author | Zachary Turner <zturner@google.com> | 2016-06-07 19:32:09 +0000 |
|---|---|---|
| committer | Zachary Turner <zturner@google.com> | 2016-06-07 19:32:09 +0000 |
| commit | 4fbf61d5180d093309711ae2e57088cae494e314 (patch) | |
| tree | 216acb9d73b7fd5f168c456e3216549b1f9fb62b /llvm/unittests | |
| parent | 8b61759c058c63c52f6cb81d9ffc195ba65aa859 (diff) | |
| download | bcm5719-llvm-4fbf61d5180d093309711ae2e57088cae494e314.tar.gz bcm5719-llvm-4fbf61d5180d093309711ae2e57088cae494e314.zip | |
[yaml] Add a ScalarTraits for mapping endian aware types.
This allows mapping of any endian-aware type whose underlying
type (e.g. uint32_t) provides a ScalarTraits specialization.
Reviewed by: majnemer
Differential Revision: http://reviews.llvm.org/D21057
llvm-svn: 272049
Diffstat (limited to 'llvm/unittests')
| -rw-r--r-- | llvm/unittests/Support/YAMLIOTest.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index 1e98568173d..5f35a802caa 100644 --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/Casting.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/Format.h" #include "llvm/Support/YAMLTraits.h" #include "gtest/gtest.h" @@ -388,6 +389,111 @@ TEST(YAMLIO, TestReadWriteBuiltInTypes) { } } +//===----------------------------------------------------------------------===// +// Test endian-aware types +//===----------------------------------------------------------------------===// + +struct EndianTypes { + typedef llvm::support::detail::packed_endian_specific_integral< + float, llvm::support::little, llvm::support::unaligned> + ulittle_float; + typedef llvm::support::detail::packed_endian_specific_integral< + double, llvm::support::little, llvm::support::unaligned> + ulittle_double; + + llvm::support::ulittle64_t u64; + llvm::support::ulittle32_t u32; + llvm::support::ulittle16_t u16; + llvm::support::little64_t s64; + llvm::support::little32_t s32; + llvm::support::little16_t s16; + ulittle_float f; + ulittle_double d; +}; + +namespace llvm { +namespace yaml { +template <> struct MappingTraits<EndianTypes> { + static void mapping(IO &io, EndianTypes &et) { + io.mapRequired("u64", et.u64); + io.mapRequired("u32", et.u32); + io.mapRequired("u16", et.u16); + io.mapRequired("s64", et.s64); + io.mapRequired("s32", et.s32); + io.mapRequired("s16", et.s16); + io.mapRequired("f", et.f); + io.mapRequired("d", et.d); + } +}; +} +} + +// +// Test the reading of all endian scalar conversions +// +TEST(YAMLIO, TestReadEndianTypes) { + EndianTypes map; + Input yin("---\n" + "u64: 5000000000\n" + "u32: 4000000000\n" + "u16: 65000\n" + "s64: -5000000000\n" + "s32: -2000000000\n" + "s16: -32000\n" + "f: 3.25\n" + "d: -2.8625\n" + "...\n"); + yin >> map; + + EXPECT_FALSE(yin.error()); + EXPECT_EQ(map.u64, 5000000000ULL); + EXPECT_EQ(map.u32, 4000000000U); + EXPECT_EQ(map.u16, 65000); + EXPECT_EQ(map.s64, -5000000000LL); + EXPECT_EQ(map.s32, -2000000000L); + EXPECT_EQ(map.s16, -32000); + EXPECT_EQ(map.f, 3.25f); + EXPECT_EQ(map.d, -2.8625); +} + +// +// Test writing then reading back all endian-aware scalar types +// +TEST(YAMLIO, TestReadWriteEndianTypes) { + std::string intermediate; + { + EndianTypes map; + map.u64 = 6000000000ULL; + map.u32 = 3000000000U; + map.u16 = 50000; + map.s64 = -6000000000LL; + map.s32 = -2000000000; + map.s16 = -32000; + map.f = 3.25f; + map.d = -2.8625; + + llvm::raw_string_ostream ostr(intermediate); + Output yout(ostr); + yout << map; + } + + { + Input yin(intermediate); + EndianTypes map; + yin >> map; + + EXPECT_FALSE(yin.error()); + EXPECT_EQ(map.u64, 6000000000ULL); + EXPECT_EQ(map.u32, 3000000000U); + EXPECT_EQ(map.u16, 50000); + EXPECT_EQ(map.s64, -6000000000LL); + EXPECT_EQ(map.s32, -2000000000L); + EXPECT_EQ(map.s16, -32000); + EXPECT_EQ(map.f, 3.25f); + EXPECT_EQ(map.d, -2.8625); + } +} + struct StringTypes { llvm::StringRef str1; llvm::StringRef str2; |

