//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2023 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 // //===----------------------------------------------------------------------===// #if swift(>=5.8) && DEBUG @testable import RopeModule import XCTest @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) class TestBString: XCTestCase { override class func setUp() { // Turn off output buffering. setbuf(stdout, nil) setbuf(stderr, nil) super.setUp() } override func setUp() { print("Global seed: \(globalSeed)") } func test_capacity() { let min = _BString.minimumCapacity let max = _BString.maximumCapacity XCTAssertLessThanOrEqual(min, max) #if !DEBUG // Debug builds have smaller nodes // We want big strings to hold at least as many UTF-8 code units as a regular String. // We want big strings to hold at least as many UTF-8 code units as a regular String. XCTAssertGreaterThanOrEqual(min, 1 << 48) #endif } func test_empty() { let s = _BString() s.invariantCheck() XCTAssertEqual(s.characterCount, 0) XCTAssertEqual(s.unicodeScalarCount, 0) XCTAssertEqual(s.utf16Count, 0) XCTAssertEqual(s.utf8Count, 0) XCTAssertTrue(s.isEmpty) XCTAssertEqual(s.startIndex, s.endIndex) XCTAssertEqual(s.startIndex._utf8Offset, 0) } func test_string_conversion() { let big = _BString(sampleString) big.invariantCheck() XCTAssertEqual(big.characterCount, sampleString.count) XCTAssertEqual(big.unicodeScalarCount, sampleString.unicodeScalars.count) XCTAssertEqual(big.utf16Count, sampleString.utf16.count) XCTAssertEqual(big.utf8Count, sampleString.utf8.count) let flat = String(_from: big) XCTAssertEqual(flat, sampleString) } @discardableResult func checkCharacterIndices( _ flat: String, _ big: _BString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [_BString.Index]) { // Check iterators var it1 = flat.makeIterator() var it2 = big.makeCharacterIterator() while true { let a = it1.next() let b = it2.next() guard a == b else { XCTAssertEqual(a, b) break } if a == nil { break } } // Check indices let indices1 = Array(flat.indices) + [flat.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.characterIndex(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) for i in 0 ..< min(indices1.count, indices2.count) { let d1 = flat.utf8.distance(from: flat.startIndex, to: indices1[i]) let d2 = big.utf8Distance(from: big.startIndex, to: indices2[i]) XCTAssertEqual(d2, d1, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", file: file, line: line) } return (indices1, indices2) } @discardableResult func checkScalarIndices( _ flat: String, _ big: _BString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [_BString.Index]) { // Check iterators var it1 = flat.unicodeScalars.makeIterator() var it2 = big.makeUnicodeScalarIterator() while true { let a = it1.next() let b = it2.next() guard a == b else { XCTAssertEqual(a, b) break } if a == nil { break } } // Check indices let indices1 = Array(flat.unicodeScalars.indices) + [flat.unicodeScalars.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.unicodeScalarIndex(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) for i in 0 ..< min(indices1.count, indices2.count) { let d1 = flat.utf8.distance(from: flat.startIndex, to: indices1[i]) let d2 = big.utf8Distance(from: big.startIndex, to: indices2[i]) XCTAssertEqual(d2, d1, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", file: file, line: line) } return (indices1, indices2) } @discardableResult func checkUTF8Indices( _ flat: String, _ big: _BString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [_BString.Index]) { // Check iterators var it1 = flat.utf8.makeIterator() var it2 = big.makeUTF8Iterator() while true { let a = it1.next() let b = it2.next() guard a == b else { XCTAssertEqual(a, b) break } if a == nil { break } } // Check indices let indices1 = Array(flat.utf8.indices) + [flat.utf8.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.utf8Index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) for i in 0 ..< min(indices1.count, indices2.count) { let d1 = flat.utf8.distance(from: flat.startIndex, to: indices1[i]) let d2 = big.utf8Distance(from: big.startIndex, to: indices2[i]) XCTAssertEqual(d2, d1, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", file: file, line: line) } return (indices1, indices2) } @discardableResult func checkUTF16Indices( _ flat: String, _ big: _BString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [_BString.Index]) { // Check iterators var it1 = flat.utf16.makeIterator() var it2 = big.makeUTF16Iterator() while true { let a = it1.next() let b = it2.next() guard a == b else { XCTAssertEqual(a, b) break } if a == nil { break } } // Check indices let indices1 = Array(flat.utf16.indices) + [flat.utf16.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.utf16Index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) for i in 0 ..< min(indices1.count, indices2.count) { let d1 = flat.utf8.distance(from: flat.startIndex, to: indices1[i]) let d2 = big.utf8Distance(from: big.startIndex, to: indices2[i]) XCTAssertEqual(d2, d1, "i: \(i), i1: \(indices1[i]._description), i2: \(indices2[i])", file: file, line: line) } return (indices1, indices2) } func test_indices_character() { let flat = sampleString let big = _BString(flat) let (indices1, indices2) = checkCharacterIndices(flat, big) let c = min(indices1.count, indices2.count) for i in randomStride(from: 0, to: c, by: 5, seed: 0) { for j in randomStride(from: i, to: c, by: 5, seed: i) { let i1 = indices1[i] let j1 = indices1[j] let a = String(sampleString[i1 ..< j1]) let i2 = indices2[i] let j2 = big.characterIndex(i2, offsetBy: j - i) let b = String(_from: big, in: i2 ..< j2) XCTAssertEqual(b, a) } } } func test_indices_scalar() { let flat = sampleString let big = _BString(flat) let (indices1, indices2) = checkScalarIndices(flat, big) let c = min(indices1.count, indices2.count) for i in randomStride(from: 0, to: c, by: 20, seed: 0) { for j in randomStride(from: i, to: c, by: 20, seed: i) { let a = String(sampleString.unicodeScalars[indices1[i] ..< indices1[j]]) let i2 = indices2[i] let j2 = big.unicodeScalarIndex(i2, offsetBy: j - i) let b = String(_from: big, in: i2 ..< j2) XCTAssertEqual(b, a) } } } func test_indices_utf16() { let flat = sampleString let big = _BString(flat) let (indices1, indices2) = checkUTF16Indices(flat, big) let c = min(indices1.count, indices2.count) for i in randomStride(from: 0, to: c, by: 20, seed: 0) { for j in randomStride(from: i, to: c, by: 20, seed: i) { let a = String(sampleString.unicodeScalars[indices1[i] ..< indices1[j]]) let i2 = indices2[i] let j2 = big.utf16Index(i2, offsetBy: j - i) let b = String(_from: big, in: i2 ..< j2) XCTAssertEqual(b, a) } } } func test_indices_utf8() { let flat = sampleString let big = _BString(flat) let (indices1, indices2) = checkUTF8Indices(flat, big) let c = min(indices1.count, indices2.count) for i in randomStride(from: 0, to: c, by: 40, seed: 0) { for j in randomStride(from: i, to: c, by: 40, seed: i) { let a = String(sampleString.unicodeScalars[indices1[i] ..< indices1[j]]) let i2 = indices2[i] let j2 = big.utf8Index(i2, offsetBy: j - i) let b = String(_from: big, in: i2 ..< j2) XCTAssertEqual(b, a) } } } func test_append_string() { let flat = sampleString let ref = _BString(flat) for stride in [1, 2, 4, 8, 16, 32, 64, 128, 250, 1000, 10000, 20000] { var big: _BString = "" var i = flat.startIndex while i < flat.endIndex { let j = flat.unicodeScalars.index(i, offsetBy: stride, limitedBy: flat.endIndex) ?? flat.endIndex let next = String(flat[i ..< j]) big.append(contentsOf: next) //big.invariantCheck() //XCTAssertEqual(String(big)[...], s[.. [(i: Int, str: String)] { var pieces: [(i: Int, str: String)] = [] var c = 0 var i = str.startIndex while i < str.endIndex { let j = str.unicodeScalars.index(i, offsetBy: stride, limitedBy: str.endIndex) ?? str.endIndex let next = String(str[i ..< j]) pieces.append((c, next)) c += 1 i = j } return pieces } func test_insert_string() { let flat = sampleString let ref = _BString(flat) for stride in [1, 2, 4, 8, 16, 32, 64, 128, 250, 1000, 10000, 20000] { print("Stride: \(stride)") var pieces = pieces(of: flat, by: stride) var rng = RepeatableRandomNumberGenerator(seed: 0) pieces.shuffle(using: &rng) var big: _BString = "" var smol = "" for i in pieces.indices { let piece = pieces[i] let utf8Offset = pieces[..