• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------- Triple.cpp - Triple unit tests ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/Triple.h"
12 
13 using namespace llvm;
14 
15 namespace {
16 
TEST(TripleTest,BasicParsing)17 TEST(TripleTest, BasicParsing) {
18   Triple T;
19 
20   T = Triple("");
21   EXPECT_EQ("", T.getArchName().str());
22   EXPECT_EQ("", T.getVendorName().str());
23   EXPECT_EQ("", T.getOSName().str());
24   EXPECT_EQ("", T.getEnvironmentName().str());
25 
26   T = Triple("-");
27   EXPECT_EQ("", T.getArchName().str());
28   EXPECT_EQ("", T.getVendorName().str());
29   EXPECT_EQ("", T.getOSName().str());
30   EXPECT_EQ("", T.getEnvironmentName().str());
31 
32   T = Triple("--");
33   EXPECT_EQ("", T.getArchName().str());
34   EXPECT_EQ("", T.getVendorName().str());
35   EXPECT_EQ("", T.getOSName().str());
36   EXPECT_EQ("", T.getEnvironmentName().str());
37 
38   T = Triple("---");
39   EXPECT_EQ("", T.getArchName().str());
40   EXPECT_EQ("", T.getVendorName().str());
41   EXPECT_EQ("", T.getOSName().str());
42   EXPECT_EQ("", T.getEnvironmentName().str());
43 
44   T = Triple("----");
45   EXPECT_EQ("", T.getArchName().str());
46   EXPECT_EQ("", T.getVendorName().str());
47   EXPECT_EQ("", T.getOSName().str());
48   EXPECT_EQ("-", T.getEnvironmentName().str());
49 
50   T = Triple("a");
51   EXPECT_EQ("a", T.getArchName().str());
52   EXPECT_EQ("", T.getVendorName().str());
53   EXPECT_EQ("", T.getOSName().str());
54   EXPECT_EQ("", T.getEnvironmentName().str());
55 
56   T = Triple("a-b");
57   EXPECT_EQ("a", T.getArchName().str());
58   EXPECT_EQ("b", T.getVendorName().str());
59   EXPECT_EQ("", T.getOSName().str());
60   EXPECT_EQ("", T.getEnvironmentName().str());
61 
62   T = Triple("a-b-c");
63   EXPECT_EQ("a", T.getArchName().str());
64   EXPECT_EQ("b", T.getVendorName().str());
65   EXPECT_EQ("c", T.getOSName().str());
66   EXPECT_EQ("", T.getEnvironmentName().str());
67 
68   T = Triple("a-b-c-d");
69   EXPECT_EQ("a", T.getArchName().str());
70   EXPECT_EQ("b", T.getVendorName().str());
71   EXPECT_EQ("c", T.getOSName().str());
72   EXPECT_EQ("d", T.getEnvironmentName().str());
73 }
74 
TEST(TripleTest,ParsedIDs)75 TEST(TripleTest, ParsedIDs) {
76   Triple T;
77 
78   T = Triple("i386-apple-darwin");
79   EXPECT_EQ(Triple::x86, T.getArch());
80   EXPECT_EQ(Triple::Apple, T.getVendor());
81   EXPECT_EQ(Triple::Darwin, T.getOS());
82   EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
83 
84   T = Triple("x86_64-pc-linux-gnu");
85   EXPECT_EQ(Triple::x86_64, T.getArch());
86   EXPECT_EQ(Triple::PC, T.getVendor());
87   EXPECT_EQ(Triple::Linux, T.getOS());
88   EXPECT_EQ(Triple::GNU, T.getEnvironment());
89 
90   T = Triple("powerpc-dunno-notsure");
91   EXPECT_EQ(Triple::ppc, T.getArch());
92   EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
93   EXPECT_EQ(Triple::UnknownOS, T.getOS());
94   EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
95 
96   T = Triple("arm-none-none-eabi");
97   EXPECT_EQ(Triple::arm, T.getArch());
98   EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
99   EXPECT_EQ(Triple::UnknownOS, T.getOS());
100   EXPECT_EQ(Triple::EABI, T.getEnvironment());
101 
102   T = Triple("huh");
103   EXPECT_EQ(Triple::UnknownArch, T.getArch());
104 }
105 
Join(StringRef A,StringRef B,StringRef C)106 static std::string Join(StringRef A, StringRef B, StringRef C) {
107   std::string Str = A; Str += '-'; Str += B; Str += '-'; Str += C;
108   return Str;
109 }
110 
Join(StringRef A,StringRef B,StringRef C,StringRef D)111 static std::string Join(StringRef A, StringRef B, StringRef C, StringRef D) {
112   std::string Str = A; Str += '-'; Str += B; Str += '-'; Str += C; Str += '-';
113   Str += D; return Str;
114 }
115 
TEST(TripleTest,Normalization)116 TEST(TripleTest, Normalization) {
117 
118   EXPECT_EQ("", Triple::normalize(""));
119   EXPECT_EQ("-", Triple::normalize("-"));
120   EXPECT_EQ("--", Triple::normalize("--"));
121   EXPECT_EQ("---", Triple::normalize("---"));
122   EXPECT_EQ("----", Triple::normalize("----"));
123 
124   EXPECT_EQ("a", Triple::normalize("a"));
125   EXPECT_EQ("a-b", Triple::normalize("a-b"));
126   EXPECT_EQ("a-b-c", Triple::normalize("a-b-c"));
127   EXPECT_EQ("a-b-c-d", Triple::normalize("a-b-c-d"));
128 
129   EXPECT_EQ("i386-b-c", Triple::normalize("i386-b-c"));
130   EXPECT_EQ("i386-a-c", Triple::normalize("a-i386-c"));
131   EXPECT_EQ("i386-a-b", Triple::normalize("a-b-i386"));
132   EXPECT_EQ("i386-a-b-c", Triple::normalize("a-b-c-i386"));
133 
134   EXPECT_EQ("a-pc-c", Triple::normalize("a-pc-c"));
135   EXPECT_EQ("-pc-b-c", Triple::normalize("pc-b-c"));
136   EXPECT_EQ("a-pc-b", Triple::normalize("a-b-pc"));
137   EXPECT_EQ("a-pc-b-c", Triple::normalize("a-b-c-pc"));
138 
139   EXPECT_EQ("a-b-linux", Triple::normalize("a-b-linux"));
140   EXPECT_EQ("--linux-b-c", Triple::normalize("linux-b-c"));
141   EXPECT_EQ("a--linux-c", Triple::normalize("a-linux-c"));
142 
143   EXPECT_EQ("i386-pc-a", Triple::normalize("a-pc-i386"));
144   EXPECT_EQ("i386-pc-", Triple::normalize("-pc-i386"));
145   EXPECT_EQ("-pc-linux-c", Triple::normalize("linux-pc-c"));
146   EXPECT_EQ("-pc-linux", Triple::normalize("linux-pc-"));
147 
148   EXPECT_EQ("i386", Triple::normalize("i386"));
149   EXPECT_EQ("-pc", Triple::normalize("pc"));
150   EXPECT_EQ("--linux", Triple::normalize("linux"));
151 
152   EXPECT_EQ("x86_64--linux-gnu", Triple::normalize("x86_64-gnu-linux"));
153 
154   // Check that normalizing a permutated set of valid components returns a
155   // triple with the unpermuted components.
156   StringRef C[4];
157   for (int Arch = 1+Triple::UnknownArch; Arch < Triple::InvalidArch; ++Arch) {
158     C[0] = Triple::getArchTypeName(Triple::ArchType(Arch));
159     for (int Vendor = 1+Triple::UnknownVendor; Vendor <= Triple::PC;
160          ++Vendor) {
161       C[1] = Triple::getVendorTypeName(Triple::VendorType(Vendor));
162       for (int OS = 1+Triple::UnknownOS; OS <= Triple::Minix; ++OS) {
163         C[2] = Triple::getOSTypeName(Triple::OSType(OS));
164 
165         // If a value has multiple interpretations, then the permutation
166         // test will inevitably fail.  Currently this is only the case for
167         // "psp" which parses as both an architecture and an O/S.
168         if (OS == Triple::Psp)
169           continue;
170 
171         std::string E = Join(C[0], C[1], C[2]);
172         EXPECT_EQ(E, Triple::normalize(Join(C[0], C[1], C[2])));
173 
174         EXPECT_EQ(E, Triple::normalize(Join(C[0], C[2], C[1])));
175         EXPECT_EQ(E, Triple::normalize(Join(C[1], C[2], C[0])));
176         EXPECT_EQ(E, Triple::normalize(Join(C[1], C[0], C[2])));
177         EXPECT_EQ(E, Triple::normalize(Join(C[2], C[0], C[1])));
178         EXPECT_EQ(E, Triple::normalize(Join(C[2], C[1], C[0])));
179 
180         for (int Env = 1+Triple::UnknownEnvironment; Env <= Triple::MachO;
181              ++Env) {
182           C[3] = Triple::getEnvironmentTypeName(Triple::EnvironmentType(Env));
183 
184           std::string F = Join(C[0], C[1], C[2], C[3]);
185           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[1], C[2], C[3])));
186 
187           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[1], C[3], C[2])));
188           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[2], C[3], C[1])));
189           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[2], C[1], C[3])));
190           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[3], C[1], C[2])));
191           EXPECT_EQ(F, Triple::normalize(Join(C[0], C[3], C[2], C[1])));
192           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[2], C[3], C[0])));
193           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[2], C[0], C[3])));
194           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[3], C[0], C[2])));
195           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[3], C[2], C[0])));
196           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[0], C[2], C[3])));
197           EXPECT_EQ(F, Triple::normalize(Join(C[1], C[0], C[3], C[2])));
198           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[3], C[0], C[1])));
199           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[3], C[1], C[0])));
200           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[0], C[1], C[3])));
201           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[0], C[3], C[1])));
202           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[1], C[3], C[0])));
203           EXPECT_EQ(F, Triple::normalize(Join(C[2], C[1], C[0], C[3])));
204           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[0], C[1], C[2])));
205           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[0], C[2], C[1])));
206           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[1], C[2], C[0])));
207           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[1], C[0], C[2])));
208           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[2], C[0], C[1])));
209           EXPECT_EQ(F, Triple::normalize(Join(C[3], C[2], C[1], C[0])));
210         }
211       }
212     }
213   }
214 
215   EXPECT_EQ("a-b-psp", Triple::normalize("a-b-psp"));
216   EXPECT_EQ("psp-b-c", Triple::normalize("psp-b-c"));
217 
218   // Various real-world funky triples.  The value returned by GCC's config.sub
219   // is given in the comment.
220   EXPECT_EQ("i386--mingw32", Triple::normalize("i386-mingw32")); // i386-pc-mingw32
221   EXPECT_EQ("x86_64--linux-gnu", Triple::normalize("x86_64-linux-gnu")); // x86_64-pc-linux-gnu
222   EXPECT_EQ("i486--linux-gnu", Triple::normalize("i486-linux-gnu")); // i486-pc-linux-gnu
223   EXPECT_EQ("i386-redhat-linux", Triple::normalize("i386-redhat-linux")); // i386-redhat-linux-gnu
224   EXPECT_EQ("i686--linux", Triple::normalize("i686-linux")); // i686-pc-linux-gnu
225   EXPECT_EQ("arm-none--eabi", Triple::normalize("arm-none-eabi")); // arm-none-eabi
226 }
227 
TEST(TripleTest,MutateName)228 TEST(TripleTest, MutateName) {
229   Triple T;
230   EXPECT_EQ(Triple::UnknownArch, T.getArch());
231   EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
232   EXPECT_EQ(Triple::UnknownOS, T.getOS());
233   EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
234 
235   T.setArchName("i386");
236   EXPECT_EQ(Triple::x86, T.getArch());
237   EXPECT_EQ("i386--", T.getTriple());
238 
239   T.setVendorName("pc");
240   EXPECT_EQ(Triple::x86, T.getArch());
241   EXPECT_EQ(Triple::PC, T.getVendor());
242   EXPECT_EQ("i386-pc-", T.getTriple());
243 
244   T.setOSName("linux");
245   EXPECT_EQ(Triple::x86, T.getArch());
246   EXPECT_EQ(Triple::PC, T.getVendor());
247   EXPECT_EQ(Triple::Linux, T.getOS());
248   EXPECT_EQ("i386-pc-linux", T.getTriple());
249 
250   T.setEnvironmentName("gnu");
251   EXPECT_EQ(Triple::x86, T.getArch());
252   EXPECT_EQ(Triple::PC, T.getVendor());
253   EXPECT_EQ(Triple::Linux, T.getOS());
254   EXPECT_EQ("i386-pc-linux-gnu", T.getTriple());
255 
256   T.setOSName("freebsd");
257   EXPECT_EQ(Triple::x86, T.getArch());
258   EXPECT_EQ(Triple::PC, T.getVendor());
259   EXPECT_EQ(Triple::FreeBSD, T.getOS());
260   EXPECT_EQ("i386-pc-freebsd-gnu", T.getTriple());
261 
262   T.setOSAndEnvironmentName("darwin");
263   EXPECT_EQ(Triple::x86, T.getArch());
264   EXPECT_EQ(Triple::PC, T.getVendor());
265   EXPECT_EQ(Triple::Darwin, T.getOS());
266   EXPECT_EQ("i386-pc-darwin", T.getTriple());
267 
268 }
269 
270 }
271