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