Skip to content

Commit 95cd4db

Browse files
authored
Merge pull request #7380 from gottesmm/diverse_stack_unittest
2 parents ab7c550 + 3c58e42 commit 95cd4db

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed

unittests/Basic/BlotMapVectorTest.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#define DEBUG_TYPE "swift-blot-map-vector-test"
1314
#include "swift/Basic/BlotMapVector.h"
1415
#include "swift/Basic/LLVM.h"
1516
#include "swift/Basic/Lazy.h"
@@ -19,6 +20,7 @@
1920
#include "llvm/ADT/StringExtras.h"
2021
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/Support/raw_ostream.h"
23+
#include "llvm/Support/Debug.h"
2224
#include "gtest/gtest.h"
2325
#include <map>
2426
#include <set>
@@ -93,31 +95,31 @@ class CtorTester {
9395

9496
CtorTester() : Value(new int(-1)) {
9597
dump("Constructing ()");
96-
llvm::outs() << "\n";
98+
DEBUG(llvm::errs() << "\n");
9799
// EXPECT_TRUE(ConstructedTesters->insert(this));
98100
assert(!isLive());
99101
fflush(stdout);
100102
}
101103

102104
explicit CtorTester(EmptyTester) : Value(new int(-2)) {
103105
dump("Constructing Empty");
104-
llvm::outs() << "\n";
106+
DEBUG(llvm::errs() << "\n");
105107
// EXPECT_TRUE(ConstructedTesters->insert(this));
106108
assert(!isLive());
107109
fflush(stdout);
108110
}
109111

110112
explicit CtorTester(TombstoneTester) : Value(new int(-3)) {
111113
dump("Constructing Tombstone");
112-
llvm::outs() << "\n";
114+
DEBUG(llvm::errs() << "\n");
113115
// EXPECT_TRUE(ConstructedTesters->insert(this));
114116
assert(!isLive());
115117
fflush(stdout);
116118
}
117119

118120
explicit CtorTester(int V) : Value(new int(V)) {
119121
dump("Constructing Normal");
120-
llvm::outs() << "\n";
122+
DEBUG(llvm::errs() << "\n");
121123
EXPECT_TRUE(ConstructedTesters->insert(this));
122124
assert(!isIgnorableTester());
123125
assert(isLive());
@@ -126,7 +128,7 @@ class CtorTester {
126128

127129
explicit CtorTester(uint32_t V) : Value(new int(V)) {
128130
dump("Constructing Normal");
129-
llvm::outs() << "\n";
131+
DEBUG(llvm::errs() << "\n");
130132
EXPECT_TRUE(ConstructedTesters->insert(this));
131133
assert(!isIgnorableTester());
132134
assert(isLive());
@@ -136,7 +138,7 @@ class CtorTester {
136138
CtorTester(const CtorTester &Arg) : Value(new int(*Arg.Value.get())) {
137139
dump("CopyConstructing");
138140
Arg.dump(" From");
139-
llvm::outs() << "\n";
141+
DEBUG(llvm::errs() << "\n");
140142
if (!Arg.isIgnorableTester()) {
141143
EXPECT_TRUE(ConstructedTesters->insert(this));
142144
fflush(stdout);
@@ -146,7 +148,7 @@ class CtorTester {
146148
CtorTester(CtorTester &&Arg) : Value(new int(-1)) {
147149
dump("Operator Move Constructor");
148150
Arg.dump(" From");
149-
llvm::outs() << "\n";
151+
DEBUG(llvm::errs() << "\n");
150152
assert(Value);
151153
assert(Arg.Value);
152154
// If Arg is not ignorable, it will be now and we will not be.
@@ -155,13 +157,13 @@ class CtorTester {
155157
EXPECT_EQ(1u, ConstructedTesters->erase(&Arg));
156158
}
157159
std::swap(Value, Arg.Value);
158-
fflush(stdout);
160+
DEBUG(fflush(stdout));
159161
}
160162

161163
CtorTester &operator=(const CtorTester &Arg) {
162164
dump("Operator Copy Assignment");
163165
Arg.dump(" From");
164-
llvm::outs() << "\n";
166+
DEBUG(llvm::errs() << "\n");
165167
assert(Value);
166168
assert(Arg.Value);
167169

@@ -178,7 +180,7 @@ class CtorTester {
178180
CtorTester &operator=(CtorTester &&Arg) {
179181
dump("Operator Move Assignment");
180182
Arg.dump(" From");
181-
llvm::outs() << "\n";
183+
DEBUG(llvm::errs() << "\n");
182184
assert(Value);
183185
assert(Arg.Value);
184186
if (!Arg.isIgnorableTester() && isIgnorableTester()) {
@@ -190,14 +192,14 @@ class CtorTester {
190192
}
191193

192194
std::swap(Value, Arg.Value);
193-
fflush(stdout);
195+
DEBUG(fflush(stdout));
194196
return *this;
195197
}
196198

197199
~CtorTester() {
198200
bool IsIgnorable = isIgnorableTester();
199201
dump("Destroying");
200-
llvm::outs() << "\n";
202+
DEBUG(llvm::errs() << "\n");
201203
delete Value.get();
202204
Value = nullptr;
203205
fflush(stdout);
@@ -224,9 +226,9 @@ class CtorTester {
224226
Addr += llvm::utohexstr(uintptr_t(this));
225227
std::string ValueAddr = "0x";
226228
ValueAddr += llvm::utohexstr(uintptr_t(Value.get()));
227-
llvm::outs() << Name << " <Tester Addr:" << Addr
228-
<< " ValueAddr:" << ValueAddr << " Value:" << *Value.get()
229-
<< ">";
229+
DEBUG(llvm::errs() << Name << " <Tester Addr:" << Addr
230+
<< " ValueAddr:" << ValueAddr << " Value:" << *Value.get()
231+
<< ">");
230232
}
231233
};
232234

@@ -236,7 +238,7 @@ void CtorTesterSet::dumpLiveTesters() const {
236238
continue;
237239
llvm::SmallString<64> Hex;
238240
std::string Addr = llvm::utohexstr(uintptr_t(Tester));
239-
llvm::outs() << "<Tester Addr:" << Addr << " Value:" << Tester->getValue()
241+
llvm::errs() << "<Tester Addr:" << Addr << " Value:" << Tester->getValue()
240242
<< ">\n";
241243
}
242244
}
@@ -331,7 +333,7 @@ template <typename T> class BlotMapVectorTest : public ::testing::Test {
331333

332334
~BlotMapVectorTest() override {
333335
ConstructedTesters->verifyTesters();
334-
llvm::outs() << "Destroying Fixture\n";
336+
DEBUG(llvm::errs() << "Destroying Fixture\n");
335337
ConstructedTesters->finalize();
336338
}
337339

unittests/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_swift_unittest(SwiftBasicTests
99
BlotMapVectorTest.cpp
1010
ClusteredBitVectorTest.cpp
1111
DemangleTest.cpp
12+
DiverseStackTest.cpp
1213
EditorPlaceholderTest.cpp
1314
EncodedSequenceTest.cpp
1415
FileSystemTest.cpp

unittests/Basic/DiverseStackTest.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===--- DiverseStackTest.cpp ---------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Basic/DiverseStack.h"
14+
#include "gtest/gtest.h"
15+
16+
using namespace swift;
17+
18+
namespace {
19+
20+
struct ParentType {
21+
uint8_t allocatedSize;
22+
23+
public:
24+
ParentType(uint8_t allocatedSize) : allocatedSize(allocatedSize) {}
25+
26+
unsigned allocated_size() const { return allocatedSize; }
27+
};
28+
29+
struct TwoByteType : ParentType {
30+
uint8_t Value;
31+
32+
TwoByteType(uint8_t Value) : ParentType(sizeof(*this)), Value(Value) {}
33+
};
34+
35+
struct ThreeByteType : ParentType {
36+
uint16_t Value;
37+
38+
ThreeByteType(uint16_t Value) : ParentType(sizeof(*this)), Value(Value) {}
39+
};
40+
41+
} // end anonymous namespace
42+
43+
TEST(DiverseStack, MonomorphicPushPop) {
44+
DiverseStack<ParentType, 128> Stack;
45+
46+
EXPECT_TRUE(Stack.empty());
47+
48+
constexpr size_t TwoByteDataSize = 5;
49+
uint8_t InputData[TwoByteDataSize] = {5, 9, 1, 2, 10};
50+
for (unsigned i = 0; i < TwoByteDataSize; ++i) {
51+
Stack.push<TwoByteType>(TwoByteType(InputData[i]));
52+
}
53+
54+
EXPECT_FALSE(Stack.empty());
55+
56+
for (int i = TwoByteDataSize - 1; i >= 0; --i) {
57+
TwoByteType T = reinterpret_cast<TwoByteType &>(Stack.top());
58+
Stack.pop();
59+
EXPECT_EQ(T.Value, InputData[i]);
60+
}
61+
62+
EXPECT_TRUE(Stack.empty());
63+
}
64+
65+
// We test the property here that iterating forward through the stack iterates
66+
// in stack order. This is a bit counter-intuitive for people used to vector
67+
// stacks.
68+
TEST(DiverseStack, Iterate) {
69+
DiverseStack<ParentType, 128> Stack;
70+
71+
constexpr size_t TwoByteDataSize = 5;
72+
uint8_t InputData[TwoByteDataSize] = {5, 9, 1, 2, 10};
73+
for (unsigned i = 0; i < TwoByteDataSize; ++i) {
74+
Stack.push<TwoByteType>(TwoByteType(InputData[i]));
75+
}
76+
77+
const uint8_t *Ptr = &InputData[TwoByteDataSize - 1];
78+
for (auto II = Stack.begin(), IE = Stack.end(); II != IE;) {
79+
TwoByteType T = reinterpret_cast<TwoByteType &>(*II);
80+
EXPECT_EQ(T.Value, *Ptr);
81+
--Ptr;
82+
++II;
83+
}
84+
}

0 commit comments

Comments
 (0)