Add some unit tests for ArchSpec.
authorZachary Turner <zturner@google.com>
Tue, 13 Sep 2016 20:40:26 +0000 (20:40 +0000)
committerZachary Turner <zturner@google.com>
Tue, 13 Sep 2016 20:40:26 +0000 (20:40 +0000)
I'm was trying to do some cleanup and code modernization and in
doing so I needed to change ParseMachCPUDashSubtypeTriple to take
a StringRef.  To ensure I don't break anything, I'm adding some
unit tests for this function.  As a side benefit, this also expands
test coverage of this function to all platforms, since in general
this code would rarely be exercised on non Mac platforms, and never
in the test suite.

llvm-svn: 281387

lldb/include/lldb/Core/ArchSpec.h
lldb/source/Core/ArchSpec.cpp
lldb/unittests/Core/ArchSpecTest.cpp [new file with mode: 0644]
lldb/unittests/Core/CMakeLists.txt

index cd7449e..18bf96e 100644 (file)
@@ -625,6 +625,8 @@ protected:
 //------------------------------------------------------------------
 bool operator<(const ArchSpec &lhs, const ArchSpec &rhs);
 
+bool ParseMachCPUDashSubtypeTriple(const char *triple_cstr, ArchSpec &arch);
+
 } // namespace lldb_private
 
 #endif // #if defined(__cplusplus)
index 285e30e..34e8545 100644 (file)
@@ -821,8 +821,8 @@ bool ArchSpec::SetTriple(const llvm::Triple &triple) {
   return IsValid();
 }
 
-static bool ParseMachCPUDashSubtypeTriple(const char *triple_cstr,
-                                          ArchSpec &arch) {
+bool lldb_private::ParseMachCPUDashSubtypeTriple(const char *triple_cstr,
+                                                 ArchSpec &arch) {
   // Accept "12-10" or "12.10" as cpu type/subtype
   if (isdigit(triple_cstr[0])) {
     char *end = nullptr;
diff --git a/lldb/unittests/Core/ArchSpecTest.cpp b/lldb/unittests/Core/ArchSpecTest.cpp
new file mode 100644 (file)
index 0000000..995cd1a
--- /dev/null
@@ -0,0 +1,103 @@
+//===-- ArchSpecTest.cpp ----------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "lldb/Core/ArchSpec.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+TEST(ArchSpecTest, TestParseMachCPUDashSubtypeTripleSimple) {
+
+  // Success conditions.  Valid cpu/subtype combinations using both - and .
+  ArchSpec AS;
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(15, AS.GetMachOCPUSubType());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12.15", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(15, AS.GetMachOCPUSubType());
+
+  // Failure conditions.
+
+  // Valid string, unknown cpu/subtype.
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("13.11", AS));
+  EXPECT_EQ(0, AS.GetMachOCPUType());
+  EXPECT_EQ(0, AS.GetMachOCPUSubType());
+
+  // Missing / invalid cpu or subtype
+  AS = ArchSpec();
+  EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("13", AS));
+
+  AS = ArchSpec();
+  EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("13.A", AS));
+
+  AS = ArchSpec();
+  EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("A.13", AS));
+
+  // Empty string.
+  AS = ArchSpec();
+  EXPECT_FALSE(ParseMachCPUDashSubtypeTriple("", AS));
+}
+
+TEST(ArchSpecTest, TestParseMachCPUDashSubtypeTripleExtra) {
+  ArchSpec AS;
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15-vendor-os", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(15, AS.GetMachOCPUSubType());
+  EXPECT_EQ("vendor", AS.GetTriple().getVendorName());
+  EXPECT_EQ("os", AS.GetTriple().getOSName());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10-vendor-os-name", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+  EXPECT_EQ("vendor", AS.GetTriple().getVendorName());
+  EXPECT_EQ("os", AS.GetTriple().getOSName());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-15-vendor.os-name", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(15, AS.GetMachOCPUSubType());
+  EXPECT_EQ("vendor.os", AS.GetTriple().getVendorName());
+  EXPECT_EQ("name", AS.GetTriple().getOSName());
+
+  // These there should parse correctly, but the vendor / OS should be defaulted
+  // since they are unrecognized.
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10-vendor", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+  EXPECT_EQ("apple", AS.GetTriple().getVendorName());
+  EXPECT_EQ("", AS.GetTriple().getOSName());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12.10.10", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+  EXPECT_EQ("apple", AS.GetTriple().getVendorName());
+  EXPECT_EQ("", AS.GetTriple().getOSName());
+
+  AS = ArchSpec();
+  EXPECT_TRUE(ParseMachCPUDashSubtypeTriple("12-10.10", AS));
+  EXPECT_EQ(12, AS.GetMachOCPUType());
+  EXPECT_EQ(10, AS.GetMachOCPUSubType());
+  EXPECT_EQ("apple", AS.GetTriple().getVendorName());
+  EXPECT_EQ("", AS.GetTriple().getOSName());
+}
+
index 17edbfe..8ab43d6 100644 (file)
@@ -1,4 +1,5 @@
 add_lldb_unittest(LLDBCoreTests
+  ArchSpecTest.cpp
   BroadcasterTest.cpp
   DataExtractorTest.cpp
   ScalarTest.cpp