|
| 1 | +//===--- ExponentialGrowthAppendingBinaryByteStreamTests.cpp -------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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/ExponentialGrowthAppendingBinaryByteStream.h" |
| 14 | +#include "llvm/Support/BinaryStreamReader.h" |
| 15 | +#include "llvm/Support/BinaryStreamWriter.h" |
| 16 | +#include "llvm/Testing/Support/Error.h" |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | + |
| 20 | +using namespace llvm; |
| 21 | +using namespace llvm::support; |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +class ExponentialGrowthAppendingBinaryByteStreamTest : public testing::Test {}; |
| 25 | + |
| 26 | +// This test case has been taken and adapted from |
| 27 | +// unittests/BinaryStreamTests.cpp in the LLVM project |
| 28 | +TEST_F(ExponentialGrowthAppendingBinaryByteStreamTest, ReadAndWrite) { |
| 29 | + StringRef Strings[] = {"1", "2", "3", "4"}; |
| 30 | + ExponentialGrowthAppendingBinaryByteStream Stream(support::little); |
| 31 | + |
| 32 | + BinaryStreamWriter Writer(Stream); |
| 33 | + BinaryStreamReader Reader(Stream); |
| 34 | + const uint8_t *Byte; |
| 35 | + StringRef Str; |
| 36 | + |
| 37 | + // When the stream is empty, it should report a 0 length and we should get an |
| 38 | + // error trying to read even 1 byte from it. |
| 39 | + BinaryStreamRef ConstRef(Stream); |
| 40 | + EXPECT_EQ(0U, ConstRef.getLength()); |
| 41 | + EXPECT_THAT_ERROR(Reader.readObject(Byte), Failed()); |
| 42 | + |
| 43 | + // But if we write to it, its size should increase and we should be able to |
| 44 | + // read not just a byte, but the string that was written. |
| 45 | + EXPECT_THAT_ERROR(Writer.writeCString(Strings[0]), Succeeded()); |
| 46 | + EXPECT_EQ(2U, ConstRef.getLength()); |
| 47 | + EXPECT_THAT_ERROR(Reader.readObject(Byte), Succeeded()); |
| 48 | + |
| 49 | + Reader.setOffset(0); |
| 50 | + EXPECT_THAT_ERROR(Reader.readCString(Str), Succeeded()); |
| 51 | + EXPECT_EQ(Str, Strings[0]); |
| 52 | + |
| 53 | + // If we drop some bytes from the front, we should still track the length as |
| 54 | + // the |
| 55 | + // underlying stream grows. |
| 56 | + BinaryStreamRef Dropped = ConstRef.drop_front(1); |
| 57 | + EXPECT_EQ(1U, Dropped.getLength()); |
| 58 | + |
| 59 | + EXPECT_THAT_ERROR(Writer.writeCString(Strings[1]), Succeeded()); |
| 60 | + EXPECT_EQ(4U, ConstRef.getLength()); |
| 61 | + EXPECT_EQ(3U, Dropped.getLength()); |
| 62 | + |
| 63 | + // If we drop zero bytes from the back, we should continue tracking the |
| 64 | + // length. |
| 65 | + Dropped = Dropped.drop_back(0); |
| 66 | + EXPECT_THAT_ERROR(Writer.writeCString(Strings[2]), Succeeded()); |
| 67 | + EXPECT_EQ(6U, ConstRef.getLength()); |
| 68 | + EXPECT_EQ(5U, Dropped.getLength()); |
| 69 | + |
| 70 | + // If we drop non-zero bytes from the back, we should stop tracking the |
| 71 | + // length. |
| 72 | + Dropped = Dropped.drop_back(1); |
| 73 | + EXPECT_THAT_ERROR(Writer.writeCString(Strings[3]), Succeeded()); |
| 74 | + EXPECT_EQ(8U, ConstRef.getLength()); |
| 75 | + EXPECT_EQ(4U, Dropped.getLength()); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(ExponentialGrowthAppendingBinaryByteStreamTest, WriteAtInvalidOffset) { |
| 79 | + ExponentialGrowthAppendingBinaryByteStream Stream(llvm::support::little); |
| 80 | + EXPECT_EQ(0U, Stream.getLength()); |
| 81 | + |
| 82 | + std::vector<uint8_t> InputData = {'T', 'e', 's', 't', 'T', 'e', 's', 't'}; |
| 83 | + auto Test = makeArrayRef(InputData).take_front(4); |
| 84 | + // Writing past the end of the stream is an error. |
| 85 | + EXPECT_THAT_ERROR(Stream.writeBytes(4, Test), Failed()); |
| 86 | + |
| 87 | + // Writing exactly at the end of the stream is ok. |
| 88 | + EXPECT_THAT_ERROR(Stream.writeBytes(0, Test), Succeeded()); |
| 89 | + EXPECT_EQ(Test, Stream.data()); |
| 90 | + |
| 91 | + // And now that the end of the stream is where we couldn't write before, now |
| 92 | + // we can write. |
| 93 | + EXPECT_THAT_ERROR(Stream.writeBytes(4, Test), Succeeded()); |
| 94 | + EXPECT_EQ(MutableArrayRef<uint8_t>(InputData), Stream.data()); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(ExponentialGrowthAppendingBinaryByteStreamTest, InitialSizeZero) { |
| 98 | + // Test that the stream also works with an initial size of 0, which doesn't |
| 99 | + // grow when doubled. |
| 100 | + ExponentialGrowthAppendingBinaryByteStream Stream(llvm::support::little); |
| 101 | + |
| 102 | + std::vector<uint8_t> InputData = {'T', 'e', 's', 't'}; |
| 103 | + auto Test = makeArrayRef(InputData).take_front(4); |
| 104 | + EXPECT_THAT_ERROR(Stream.writeBytes(0, Test), Succeeded()); |
| 105 | + EXPECT_EQ(Test, Stream.data()); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(ExponentialGrowthAppendingBinaryByteStreamTest, GrowMultipleSteps) { |
| 109 | + ExponentialGrowthAppendingBinaryByteStream Stream(llvm::support::little); |
| 110 | + |
| 111 | + // Test that the buffer can grow multiple steps at once, e.g. 1 -> 2 -> 4 |
| 112 | + std::vector<uint8_t> InputData = {'T', 'e', 's', 't'}; |
| 113 | + auto Test = makeArrayRef(InputData).take_front(4); |
| 114 | + EXPECT_THAT_ERROR(Stream.writeBytes(0, Test), Succeeded()); |
| 115 | + EXPECT_EQ(Test, Stream.data()); |
| 116 | +} |
| 117 | + |
| 118 | +TEST_F(ExponentialGrowthAppendingBinaryByteStreamTest, WriteIntoMiddle) { |
| 119 | + // Test that the stream resizes correctly if we write into its middle |
| 120 | + |
| 121 | + ExponentialGrowthAppendingBinaryByteStream Stream(llvm::support::little); |
| 122 | + |
| 123 | + // Test that the buffer can grow multiple steps at once, e.g. 1 -> 2 -> 4 |
| 124 | + std::vector<uint8_t> InitialData = {'T', 'e', 's', 't'}; |
| 125 | + auto InitialDataRef = makeArrayRef(InitialData); |
| 126 | + EXPECT_THAT_ERROR(Stream.writeBytes(0, InitialDataRef), Succeeded()); |
| 127 | + EXPECT_EQ(InitialDataRef, Stream.data()); |
| 128 | + |
| 129 | + std::vector<uint8_t> UpdateData = {'u'}; |
| 130 | + auto UpdateDataRef = makeArrayRef(UpdateData); |
| 131 | + EXPECT_THAT_ERROR(Stream.writeBytes(1, UpdateDataRef), Succeeded()); |
| 132 | + |
| 133 | + std::vector<uint8_t> DataAfterUpdate = {'T', 'u', 's', 't'}; |
| 134 | + auto DataAfterUpdateRef = makeArrayRef(DataAfterUpdate); |
| 135 | + EXPECT_EQ(DataAfterUpdateRef, Stream.data()); |
| 136 | + EXPECT_EQ(4u, Stream.getLength()); |
| 137 | + |
| 138 | + std::vector<uint8_t> InsertData = {'e', 'r'}; |
| 139 | + auto InsertDataRef = makeArrayRef(InsertData); |
| 140 | + EXPECT_THAT_ERROR(Stream.writeBytes(4, InsertDataRef), Succeeded()); |
| 141 | + |
| 142 | + std::vector<uint8_t> DataAfterInsert = {'T', 'u', 's', 't', 'e', 'r'}; |
| 143 | + auto DataAfterInsertRef = makeArrayRef(DataAfterInsert); |
| 144 | + EXPECT_EQ(DataAfterInsertRef, Stream.data()); |
| 145 | + EXPECT_EQ(6u, Stream.getLength()); |
| 146 | +} |
0 commit comments