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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
class MarkedCircularBufferTests: XCTestCase {
func testEmptyMark() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 8)
XCTAssertFalse(buf.hasMark)
XCTAssertNil(buf.markedElement)
XCTAssertNil(buf.markedElementIndex)
buf.mark()
XCTAssertFalse(buf.hasMark)
XCTAssertNil(buf.markedElement)
XCTAssertNil(buf.markedElementIndex)
}
func testSimpleMark() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 8)
for i in 1...4 { buf.append(i) }
buf.mark()
for i in 5...8 { buf.append(i) }
XCTAssertTrue(buf.hasMark)
XCTAssertEqual(buf.markedElement, 4)
XCTAssertEqual(buf.markedElementIndex, buf.index(buf.startIndex, offsetBy: 3))
for i in 0..<3 { XCTAssertFalse(buf.isMarked(index: buf.index(buf.startIndex, offsetBy: i))) }
XCTAssertTrue(buf.isMarked(index: buf.index(buf.startIndex, offsetBy: 3)))
for i in 4..<8 { XCTAssertFalse(buf.isMarked(index: buf.index(buf.startIndex, offsetBy: i))) }
}
func testPassingTheMark() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 8)
for i in 1...4 { buf.append(i) }
buf.mark()
for i in 5...8 { buf.append(i) }
for j in 1...3 {
XCTAssertEqual(buf.removeFirst(), j)
XCTAssertTrue(buf.hasMark)
XCTAssertEqual(buf.markedElement, 4)
XCTAssertEqual(buf.markedElementIndex, buf.index(buf.startIndex, offsetBy: 3 - j))
}
XCTAssertEqual(buf.removeFirst(), 4)
XCTAssertFalse(buf.hasMark)
XCTAssertNil(buf.markedElement)
XCTAssertNil(buf.markedElementIndex)
}
func testMovingTheMark() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 8)
for i in 1...8 {
buf.append(i)
buf.mark()
XCTAssertTrue(buf.hasMark)
XCTAssertEqual(buf.markedElement, i)
XCTAssertEqual(buf.markedElementIndex, buf.index(buf.startIndex, offsetBy: i - 1))
XCTAssertTrue(buf.isMarked(index: buf.index(buf.startIndex, offsetBy: i - 1)))
}
}
func testIndices() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
var allIndices: [MarkedCircularBuffer<Int>.Index] = []
var index = buf.startIndex
while index != buf.endIndex {
allIndices.append(index)
index = buf.index(after: index)
}
XCTAssertEqual(Array(buf.indices), allIndices)
}
func testFirst() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
XCTAssertEqual(buf.first, 1)
}
func testCount() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
XCTAssertEqual(buf.count, 4)
}
func testSubscript() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
XCTAssertEqual(buf[buf.startIndex], 1)
XCTAssertEqual(buf[buf.index(buf.startIndex, offsetBy: 3)], 4)
}
func testRangeSubscript() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
let range = buf.startIndex..<buf.index(buf.startIndex, offsetBy: 2)
XCTAssertEqual(buf[range].count, 2)
buf[range] = [0,1]
XCTAssertEqual(buf.firstIndex(of: 2), nil)
XCTAssertEqual(buf.count, 4)
}
func testIsEmpty() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
XCTAssertFalse(buf.isEmpty)
XCTAssertEqual(buf.removeFirst(), 1)
XCTAssertEqual(buf.removeFirst(), 2)
XCTAssertEqual(buf.removeFirst(), 3)
XCTAssertEqual(buf.removeFirst(), 4)
XCTAssertTrue(buf.isEmpty)
}
func testPopFirst() throws {
var buf = MarkedCircularBuffer<Int>(initialCapacity: 4)
for i in 1...4 {
buf.append(i)
}
XCTAssertFalse(buf.isEmpty)
XCTAssertEqual(buf.popFirst(), 1)
XCTAssertEqual(buf.popFirst(), 2)
XCTAssertEqual(buf.popFirst(), 3)
XCTAssertEqual(buf.popFirst(), 4)
XCTAssertNil(buf.popFirst())
XCTAssertTrue(buf.isEmpty)
}
}
|