Skip to content

Commit 3c58e42

Browse files
committed
Add unittests for DiverseStack.
1 parent 6d1058c commit 3c58e42

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

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)