Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit fe3e971

Browse files
committed
Clean up bitcode munging tests.
Cleans up bitcode munging tests in several ways: 1) Extracts out common definitions into a single header file. 2) Puts all test/code for munging in same namespace. 3) Removes test name argument from calls to runTest. 4) Uses ARRAY and ARRAY_TERM macros to pass array arguments to bitcode munger constructors and runTest calls. BUG=None [email protected] Review URL: https://codereview.chromium.org/1140153004
1 parent 47508eb commit fe3e971

File tree

10 files changed

+356
-540
lines changed

10 files changed

+356
-540
lines changed

include/llvm/Bitcode/NaCl/NaClBitcodeMunge.h

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ class NaClBitcodeMunger {
7474

7575
/// Creates MungedInput and DumpStream for running tests, based on
7676
/// given Munges. Returns true if able to set up test.
77-
bool setupTest(
78-
const char *TestName, const uint64_t Munges[], size_t MungesSize,
79-
bool AddHeader);
77+
bool setupTest(const uint64_t Munges[], size_t MungesSize, bool AddHeader);
78+
79+
// TODO(kschimpf): The following function is deprecated and only
80+
// provided until subzero is updated to use the new API that no
81+
// longer uses test names.
82+
bool setupTest(const char *, const uint64_t Munges[], size_t MungesSize,
83+
bool AddHeader) {
84+
return setupTest(Munges, MungesSize, AddHeader);
85+
}
8086

8187
/// Cleans up state after a test. Returns true if no errors found.
8288
bool cleanupTest();
@@ -170,23 +176,12 @@ class NaClWriteMunger : public NaClBitcodeMunger {
170176

171177
/// Writes munged bitcode and puts error messages into DumpResults.
172178
/// Returns true if successful.
173-
bool runTest(const char* TestName, const uint64_t Munges[],
174-
size_t MungesSize);
175-
176-
/// Same as above, but without test name.
177-
bool runTest(const uint64_t Munges[], size_t MungesSize) {
178-
return runTest("Test", Munges, MungesSize);
179-
}
179+
bool runTest(const uint64_t Munges[], size_t MungesSize);
180180

181181
/// Same as above, but without any edits.
182-
bool runTest(const char* TestName) {
183-
uint64_t NoMunges[] = {0};
184-
return runTest(TestName, NoMunges, 0);
185-
}
186-
187-
/// Same as above, but without test name.
188182
bool runTest() {
189-
return runTest("Test");
183+
uint64_t NoMunges[] = {0};
184+
return runTest(NoMunges, 0);
190185
}
191186
};
192187

@@ -202,38 +197,36 @@ class NaClObjDumpMunger : public NaClBitcodeMunger {
202197
/// Runs function NaClObjDump on the sequence of records associated
203198
/// with the instance. The memory buffer containing the bitsequence
204199
/// associated with the record is automatically generated, and
205-
/// passed to NaClObjDump. TestName is the name associated with the
206-
/// memory buffer. If AddHeader is true, test assumes that the
200+
/// passed to NaClObjDump. If AddHeader is true, test assumes that the
207201
/// sequence of records doesn't contain a header record, and the
208202
/// test should add one. Arguments NoRecords and NoAssembly are
209203
/// passed to NaClObjDump. Returns true if test succeeds without
210204
/// errors.
211-
bool runTestWithFlags(const char *TestName, bool AddHeader,
212-
bool NoRecords, bool NoAssembly) {
205+
bool runTestWithFlags(bool AddHeader, bool NoRecords, bool NoAssembly) {
213206
uint64_t NoMunges[] = {0};
214-
return runTestWithFlags(TestName, NoMunges, 0, AddHeader, NoRecords,
215-
NoAssembly);
207+
return runTestWithFlags(NoMunges, 0, AddHeader, NoRecords, NoAssembly);
216208
}
217209

218210
/// Same as above except it runs function NaClObjDump with flags
219211
/// NoRecords and NoAssembly set to false, and AddHeader set to true.
220-
bool runTest(const char *TestName) {
221-
return runTestWithFlags(TestName, true, false, false);
212+
bool runTest() {
213+
return runTestWithFlags(true, false, false);
222214
}
223215

224-
/// Same as above but without test name.
225-
bool runTest() {
226-
return runTest("Test");
216+
// TODO(kschimpf): The following function is deprecated and only
217+
// provided until subzero is updated to use the new API that no
218+
// longer uses test names.
219+
bool runTest(const char *) {
220+
return runTest();
227221
}
228222

229223
/// Same as above, but only print out assembly and errors.
230-
bool runTestForAssembly(const char *TestName) {
231-
return runTestWithFlags(TestName, true, true, false);
224+
bool runTestForAssembly() {
225+
return runTestWithFlags(true, true, false);
232226
}
233-
234227
/// Same as above, but only generate error messages.
235-
bool runTestForErrors(const char *TestName) {
236-
return runTestWithFlags(TestName, true, true, true);
228+
bool runTestForErrors() {
229+
return runTestWithFlags(true, true, true);
237230
}
238231

239232
/// Runs function llvm::NaClObjDump on the sequence of records
@@ -244,30 +237,31 @@ class NaClObjDumpMunger : public NaClBitcodeMunger {
244237
/// with the memory buffer. Arguments NoRecords and NoAssembly are
245238
/// passed to NaClObjDump. Returns true if test succeeds without
246239
/// errors.
247-
bool runTestWithFlags(const char* TestName, const uint64_t Munges[],
248-
size_t MungesSize, bool AddHeader,
249-
bool NoRecords, bool NoAssembly);
240+
bool runTestWithFlags(const uint64_t Munges[], size_t MungesSize,
241+
bool AddHeader, bool NoRecords, bool NoAssembly);
250242

251243
/// Same as above except it runs function NaClObjDump with flags
252244
/// NoRecords and NoAssembly set to false, and AddHeader set to
253245
/// true.
254-
bool runTest(const char* TestName, const uint64_t Munges[],
255-
size_t MungesSize) {
256-
return runTestWithFlags(TestName, Munges, MungesSize, true, false, false);
246+
bool runTest(const uint64_t Munges[], size_t MungesSize) {
247+
return runTestWithFlags(Munges, MungesSize, true, false, false);
257248
}
258249

259-
bool runTest(const uint64_t Munges[], size_t MungesSize) {
260-
return runTest("Test", Munges, MungesSize);
250+
bool runTestForAssembly(const uint64_t Munges[], size_t MungesSize) {
251+
return runTestWithFlags(Munges, MungesSize, true, true, false);
261252
}
262253

263-
bool runTestForAssembly(const char* TestName, const uint64_t Munges[],
254+
// TODO(kschimpf): The following function is deprecated and only
255+
// provided until subzero is updated to use the new API that no
256+
// longer uses test names.
257+
bool runTestForAssembly(const char *, const uint64_t Munges[],
264258
size_t MungesSize) {
265-
return runTestWithFlags(TestName, Munges, MungesSize, true, true, false);
259+
return runTestForAssembly(Munges, MungesSize);
266260
}
267261

268-
bool runTestForErrors(const char* TestName, const uint64_t Munges[],
269-
size_t MungesSize) {
270-
return runTestWithFlags(TestName, Munges, MungesSize, true, true, true);
262+
263+
bool runTestForErrors(const uint64_t Munges[], size_t MungesSize) {
264+
return runTestWithFlags(Munges, MungesSize, true, true, true);
271265
}
272266
};
273267

@@ -280,18 +274,12 @@ class NaClParseBitcodeMunger : public NaClBitcodeMunger {
280274

281275
/// Runs function llvm::NaClParseBitcodeFile, and puts error messages
282276
/// into DumpResults. Returns true if parse is successful.
283-
bool runTest(const char* TestName, const uint64_t Munges[],
284-
size_t MungesSize, bool VerboseErrors);
285-
286-
/// Same as above, but without test name.
287-
bool runTest(const uint64_t Munges[], size_t MungesSize, bool VerboseErrors) {
288-
return runTest("Test", Munges, MungesSize, VerboseErrors);
289-
}
277+
bool runTest(const uint64_t Munges[], size_t MungesSize, bool VerboseErrors);
290278

291279
// Same as above, but without any edits.
292-
bool runTest(const char* TestName, bool VerboseErrors) {
280+
bool runTest(bool VerboseErrors) {
293281
uint64_t NoMunges[] = {0};
294-
return runTest(TestName, NoMunges, 0, VerboseErrors);
282+
return runTest(NoMunges, 0, VerboseErrors);
295283
}
296284
};
297285

@@ -302,12 +290,11 @@ class NaClCompressMunger : public NaClBitcodeMunger {
302290
uint64_t RecordTerminator)
303291
: NaClBitcodeMunger(Records, RecordsSize, RecordTerminator) {}
304292

305-
bool runTest(const char* TestName, const uint64_t Munges[],
306-
size_t MungesSize);
293+
bool runTest(const uint64_t Munges[], size_t MungesSize);
307294

308-
bool runTest(const char* TestName) {
295+
bool runTest() {
309296
uint64_t NoMunges[] = {0};
310-
return runTest(TestName, NoMunges, 0);
297+
return runTest(NoMunges, 0);
311298
}
312299
};
313300

lib/Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ using namespace llvm;
2929
// For debugging. When true, shows each test being run.
3030
static bool TraceTestRuns = false;
3131

32-
bool NaClBitcodeMunger::setupTest(
33-
const char *TestName, const uint64_t Munges[], size_t MungesSize,
34-
bool AddHeader) {
32+
bool NaClBitcodeMunger::setupTest(const uint64_t Munges[], size_t MungesSize,
33+
bool AddHeader) {
3534
assert(DumpStream == nullptr && "Test run with DumpStream already defined");
3635
assert(MungedInput.get() == nullptr
3736
&& "Test run with MungedInput already defined");
@@ -42,7 +41,7 @@ bool NaClBitcodeMunger::setupTest(
4241
MungedInputBuffer.clear();
4342

4443
if (TraceTestRuns) {
45-
errs() << "*** Run test: " << TestName << "\n";
44+
errs() << "*** Run test:\n";
4645
}
4746

4847
MungedBitcode.munge(Munges, MungesSize, RecordTerminator);
@@ -63,8 +62,7 @@ bool NaClBitcodeMunger::setupTest(
6362
MungedInputBuffer.push_back('\0');
6463

6564
MungedInput = MemoryBuffer::getMemBuffer(
66-
StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1),
67-
TestName);
65+
StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1));
6866
return true;
6967
}
7068

@@ -114,10 +112,9 @@ getLinesWithTextMatch(const std::string &Substring, bool MustBePrefix) const {
114112
return Messages;
115113
}
116114

117-
bool NaClWriteMunger::runTest(const char* Name, const uint64_t Munges[],
118-
size_t MungesSize) {
115+
bool NaClWriteMunger::runTest(const uint64_t Munges[], size_t MungesSize) {
119116
bool AddHeader = true;
120-
if (!setupTest(Name, Munges, MungesSize, AddHeader))
117+
if (!setupTest(Munges, MungesSize, AddHeader))
121118
return cleanupTest();
122119
MemoryBufferRef InputRef(MungedInput->getMemBufferRef());
123120
NaClMungedBitcode WrittenBitcode(MemoryBuffer::getMemBuffer(InputRef));
@@ -126,9 +123,9 @@ bool NaClWriteMunger::runTest(const char* Name, const uint64_t Munges[],
126123
}
127124

128125
bool NaClObjDumpMunger::runTestWithFlags(
129-
const char *Name, const uint64_t Munges[], size_t MungesSize,
130-
bool AddHeader, bool NoRecords, bool NoAssembly) {
131-
if (!setupTest(Name, Munges, MungesSize, AddHeader))
126+
const uint64_t Munges[], size_t MungesSize, bool AddHeader,
127+
bool NoRecords, bool NoAssembly) {
128+
if (!setupTest(Munges, MungesSize, AddHeader))
132129
return cleanupTest();
133130

134131
if (NaClObjDump(MungedInput.get()->getMemBufferRef(),
@@ -137,11 +134,10 @@ bool NaClObjDumpMunger::runTestWithFlags(
137134
return cleanupTest();
138135
}
139136

140-
bool NaClParseBitcodeMunger::runTest(
141-
const char *Name, const uint64_t Munges[], size_t MungesSize,
142-
bool VerboseErrors) {
137+
bool NaClParseBitcodeMunger::runTest(const uint64_t Munges[], size_t MungesSize,
138+
bool VerboseErrors) {
143139
bool AddHeader = true;
144-
if (!setupTest(Name, Munges, MungesSize, AddHeader))
140+
if (!setupTest(Munges, MungesSize, AddHeader))
145141
return cleanupTest();
146142

147143
LLVMContext &Context = getGlobalContext();
@@ -160,10 +156,9 @@ bool NaClParseBitcodeMunger::runTest(
160156
return cleanupTest();
161157
}
162158

163-
bool NaClCompressMunger::runTest(const char* Name, const uint64_t Munges[],
164-
size_t MungesSize) {
159+
bool NaClCompressMunger::runTest(const uint64_t Munges[], size_t MungesSize) {
165160
bool AddHeader = true;
166-
if (!setupTest(Name, Munges, MungesSize, AddHeader))
161+
if (!setupTest(Munges, MungesSize, AddHeader))
167162
return cleanupTest();
168163

169164
NaClBitcodeCompressor Compressor;

unittests/Bitcode/NaClCompressTests.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11-
#include "llvm/ADT/STLExtras.h"
12-
#include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h"
13-
#include "llvm/Bitcode/NaCl/NaClBitcodeParser.h"
14-
#include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
11+
#include "NaClMungeTest.h"
1512

16-
#include "gtest/gtest.h"
13+
#include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
1714

1815
using namespace llvm;
1916

20-
namespace {
21-
22-
static const uint64_t Terminator = 0x5768798008978675LL;
17+
namespace naclmungetest {
2318

2419
// Note: Tests fix for bug in
2520
// https://code.google.com/p/nativeclient/issues/detail?id=4104
@@ -41,9 +36,8 @@ TEST(NaClCompressTests, FixedModuleAbbrevIdBug) {
4136
};
4237

4338
// Show textual version of sample input.
44-
NaClObjDumpMunger DumpMunger(BitcodeRecords,
45-
array_lengthof(BitcodeRecords), Terminator);
46-
EXPECT_TRUE(DumpMunger.runTest("Test fixed module abbreviation ID bug"));
39+
NaClObjDumpMunger DumpMunger(ARRAY_TERM(BitcodeRecords));
40+
EXPECT_TRUE(DumpMunger.runTest());
4741
EXPECT_EQ(
4842
" 0:0|<65532, 80, 69, 88, 69, 1, 0,|Magic Number: 'PEXE' (80, 69, 8"
4943
"8, 69)\n"
@@ -63,7 +57,7 @@ TEST(NaClCompressTests, FixedModuleAbbrevIdBug) {
6357
// Show that we can compress as well.
6458
NaClCompressMunger CompressMunger(BitcodeRecords,
6559
array_lengthof(BitcodeRecords), Terminator);
66-
EXPECT_TRUE(CompressMunger.runTest("Test fixed module abbreviation ID bug"));
60+
EXPECT_TRUE(CompressMunger.runTest());
6761
}
6862

69-
}
63+
} // end of namespace naclmungetest

unittests/Bitcode/NaClMungeTest.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===- llvm/unittest/Bitcode/NaClMungeTest.h - Test munging utils ---------===//
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+
// Contains common utilities used in bitcode munge tests.
11+
12+
#ifndef LLVM_UNITTEST_BITCODE_NACLMUNGETEST_H
13+
#define LLVM_UNITTEST_BITCODE_NACLMUNGETEST_H
14+
15+
#include "llvm/ADT/STLExtras.h"
16+
#include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h"
17+
18+
#include "gtest/gtest.h"
19+
20+
namespace naclmungetest {
21+
22+
const uint64_t Terminator = 0x5768798008978675LL;
23+
24+
#define ARRAY(name) name, array_lengthof(name)
25+
26+
#define ARRAY_TERM(name) ARRAY(name), Terminator
27+
28+
inline std::string stringify(llvm::NaClMungedBitcode &MungedBitcode) {
29+
std::string Buffer;
30+
llvm::raw_string_ostream StrBuf(Buffer);
31+
MungedBitcode.print(StrBuf);
32+
return StrBuf.str();
33+
}
34+
35+
inline std::string stringify(llvm::NaClBitcodeMunger &Munger) {
36+
return stringify(Munger.getMungedBitcode());
37+
}
38+
39+
} // end of namespace naclmungetest
40+
41+
#endif // end LLVM_UNITTEST_BITCODE_NACLMUNGETEST_H

0 commit comments

Comments
 (0)