summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2016-06-07 19:32:09 +0000
committerZachary Turner <zturner@google.com>2016-06-07 19:32:09 +0000
commitbabe1202e2b5e9b9740f465955d76094e8f5c00b (patch)
treed6fa6929ae5a1386a6c0634332b3feff98f357d0 /unittests
parent6fc4b2ad52f0b2d35376ea66be0514bbfad159c4 (diff)
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272049 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/YAMLIOTest.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp
index 1e98568173d..5f35a802caa 100644
--- a/unittests/Support/YAMLIOTest.cpp
+++ b/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;