|
| 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