Author: zturner Date: Tue Sep 13 15:40:26 2016 New Revision: 281387 URL: http://llvm.org/viewvc/llvm-project?rev=281387&view=rev Log: Add some unit tests for ArchSpec.
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. Added: lldb/trunk/unittests/Core/ArchSpecTest.cpp Modified: lldb/trunk/include/lldb/Core/ArchSpec.h lldb/trunk/source/Core/ArchSpec.cpp lldb/trunk/unittests/Core/CMakeLists.txt Modified: lldb/trunk/include/lldb/Core/ArchSpec.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/ArchSpec.h?rev=281387&r1=281386&r2=281387&view=diff ============================================================================== --- lldb/trunk/include/lldb/Core/ArchSpec.h (original) +++ lldb/trunk/include/lldb/Core/ArchSpec.h Tue Sep 13 15:40:26 2016 @@ -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) Modified: lldb/trunk/source/Core/ArchSpec.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ArchSpec.cpp?rev=281387&r1=281386&r2=281387&view=diff ============================================================================== --- lldb/trunk/source/Core/ArchSpec.cpp (original) +++ lldb/trunk/source/Core/ArchSpec.cpp Tue Sep 13 15:40:26 2016 @@ -821,8 +821,8 @@ bool ArchSpec::SetTriple(const llvm::Tri 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; Added: lldb/trunk/unittests/Core/ArchSpecTest.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/ArchSpecTest.cpp?rev=281387&view=auto ============================================================================== --- lldb/trunk/unittests/Core/ArchSpecTest.cpp (added) +++ lldb/trunk/unittests/Core/ArchSpecTest.cpp Tue Sep 13 15:40:26 2016 @@ -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()); +} + Modified: lldb/trunk/unittests/Core/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/CMakeLists.txt?rev=281387&r1=281386&r2=281387&view=diff ============================================================================== --- lldb/trunk/unittests/Core/CMakeLists.txt (original) +++ lldb/trunk/unittests/Core/CMakeLists.txt Tue Sep 13 15:40:26 2016 @@ -1,4 +1,5 @@ add_lldb_unittest(LLDBCoreTests + ArchSpecTest.cpp BroadcasterTest.cpp DataExtractorTest.cpp ScalarTest.cpp _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits