OneOrManyQueueTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import XCTest
  17. @testable import GRPCNIOTransportCore
  18. internal final class OneOrManyQueueTests: XCTestCase {
  19. func testIsEmpty() {
  20. XCTAssertTrue(OneOrManyQueue<Int>().isEmpty)
  21. }
  22. func testIsEmptyManyBacked() {
  23. XCTAssertTrue(OneOrManyQueue<Int>.manyBacked.isEmpty)
  24. }
  25. func testCount() {
  26. var queue = OneOrManyQueue<Int>()
  27. XCTAssertEqual(queue.count, 0)
  28. queue.append(1)
  29. XCTAssertEqual(queue.count, 1)
  30. }
  31. func testCountManyBacked() {
  32. var manyBacked = OneOrManyQueue<Int>.manyBacked
  33. XCTAssertEqual(manyBacked.count, 0)
  34. for i in 1 ... 100 {
  35. manyBacked.append(1)
  36. XCTAssertEqual(manyBacked.count, i)
  37. }
  38. }
  39. func testAppendAndPop() {
  40. var queue = OneOrManyQueue<Int>()
  41. XCTAssertNil(queue.pop())
  42. queue.append(1)
  43. XCTAssertEqual(queue.count, 1)
  44. XCTAssertEqual(queue.pop(), 1)
  45. XCTAssertNil(queue.pop())
  46. XCTAssertEqual(queue.count, 0)
  47. XCTAssertTrue(queue.isEmpty)
  48. }
  49. func testAppendAndPopManyBacked() {
  50. var manyBacked = OneOrManyQueue<Int>.manyBacked
  51. XCTAssertNil(manyBacked.pop())
  52. manyBacked.append(1)
  53. XCTAssertEqual(manyBacked.count, 1)
  54. manyBacked.append(2)
  55. XCTAssertEqual(manyBacked.count, 2)
  56. XCTAssertEqual(manyBacked.pop(), 1)
  57. XCTAssertEqual(manyBacked.count, 1)
  58. XCTAssertEqual(manyBacked.pop(), 2)
  59. XCTAssertEqual(manyBacked.count, 0)
  60. XCTAssertNil(manyBacked.pop())
  61. XCTAssertTrue(manyBacked.isEmpty)
  62. }
  63. func testIndexes() {
  64. var queue = OneOrManyQueue<Int>()
  65. XCTAssertEqual(queue.startIndex, 0)
  66. XCTAssertEqual(queue.endIndex, 0)
  67. // Non-empty.
  68. queue.append(1)
  69. XCTAssertEqual(queue.startIndex, 0)
  70. XCTAssertEqual(queue.endIndex, 1)
  71. }
  72. func testIndexesManyBacked() {
  73. var queue = OneOrManyQueue<Int>.manyBacked
  74. XCTAssertEqual(queue.startIndex, 0)
  75. XCTAssertEqual(queue.endIndex, 0)
  76. for i in 1 ... 100 {
  77. queue.append(i)
  78. XCTAssertEqual(queue.startIndex, 0)
  79. XCTAssertEqual(queue.endIndex, i)
  80. }
  81. }
  82. func testIndexAfter() {
  83. var queue = OneOrManyQueue<Int>()
  84. XCTAssertEqual(queue.startIndex, queue.endIndex)
  85. XCTAssertEqual(queue.index(after: queue.startIndex), queue.endIndex)
  86. queue.append(1)
  87. XCTAssertNotEqual(queue.startIndex, queue.endIndex)
  88. XCTAssertEqual(queue.index(after: queue.startIndex), queue.endIndex)
  89. }
  90. func testSubscript() throws {
  91. var queue = OneOrManyQueue<Int>()
  92. queue.append(42)
  93. let index = try XCTUnwrap(queue.firstIndex(of: 42))
  94. XCTAssertEqual(queue[index], 42)
  95. }
  96. func testSubscriptManyBacked() throws {
  97. var queue = OneOrManyQueue<Int>.manyBacked
  98. for i in 0 ... 100 {
  99. queue.append(i)
  100. }
  101. for i in 0 ... 100 {
  102. XCTAssertEqual(queue[i], i)
  103. }
  104. }
  105. }
  106. extension OneOrManyQueue where Element == Int {
  107. static var manyBacked: Self {
  108. var queue = OneOrManyQueue()
  109. // Append and pop to move to the 'many' backing.
  110. queue.append(1)
  111. queue.append(2)
  112. XCTAssertEqual(queue.pop(), 1)
  113. XCTAssertEqual(queue.pop(), 2)
  114. return queue
  115. }
  116. }