//===----------------------------------------------------------------------===// // // 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) import RopeModule import XCTest @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) class TestBigString: 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 = BigString._minimumCapacity let max = BigString._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. XCTAssertGreaterThanOrEqual(min, 1 << 48) #endif } func test_empty() { let s = BigString() s._invariantCheck() XCTAssertEqual(s.count, 0) XCTAssertEqual(s.unicodeScalars.count, 0) XCTAssertEqual(s.utf16.count, 0) XCTAssertEqual(s.utf8.count, 0) XCTAssertTrue(s.isEmpty) XCTAssertEqual(s.startIndex, s.endIndex) XCTAssertEqual(s.utf8.distance(from: s.startIndex, to: s.endIndex), 0) XCTAssertEqual(s.utf16.distance(from: s.startIndex, to: s.endIndex), 0) XCTAssertEqual(s.unicodeScalars.distance(from: s.startIndex, to: s.endIndex), 0) XCTAssertEqual(s.distance(from: s.startIndex, to: s.endIndex), 0) } func test_Equatable() { let a: BigString = "Cafe\u{301}" let b: BigString = "Café" XCTAssertEqual(a, b) XCTAssertNotEqual(a.unicodeScalars, b.unicodeScalars) XCTAssertNotEqual(a.utf8, b.utf8) XCTAssertNotEqual(a.utf16, b.utf16) } func test_descriptions() { let s: BigString = "Café" XCTAssertEqual(s.description, "Café") XCTAssertEqual(s.unicodeScalars.description, "Café") XCTAssertEqual(s.utf8.description, "<43 61 66 C3 A9>") XCTAssertEqual(s.utf16.description, "<0043 0061 0066 00E9>") } func test_string_conversion() { let big = BigString(sampleString) big._invariantCheck() XCTAssertEqual(big.count, sampleString.count) XCTAssertEqual(big.unicodeScalars.count, sampleString.unicodeScalars.count) XCTAssertEqual(big.utf16.count, sampleString.utf16.count) XCTAssertEqual(big.utf8.count, sampleString.utf8.count) let flat = String(big) XCTAssertEqual(flat, sampleString) } @discardableResult func checkCharacterIndices( _ flat: String, _ big: BigString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [BigString.Index]) { let indices1 = Array(flat.indices) + [flat.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) let c = min(indices1.count, indices2.count) for i in 0 ..< min(c, indices1.count - 1) { let c1 = flat[indices1[i]] let c2 = big[indices2[i]] XCTAssertEqual(c1, c2, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", file: file, line: line) } for i in 0 ..< c { let d1 = flat.utf8.distance(from: flat.startIndex, to: indices1[i]) let d2 = big.utf8.distance(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: BigString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [BigString.Index]) { let indices1 = Array(flat.unicodeScalars.indices) + [flat.unicodeScalars.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.unicodeScalars.index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) let c = min(indices1.count, indices2.count) for i in 0 ..< min(c, indices1.count - 1) { let s1 = flat.unicodeScalars[indices1[i]] let s2 = big.unicodeScalars[indices2[i]] XCTAssertEqual(s1, s2, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", 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.utf8.distance(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: BigString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [BigString.Index]) { let indices1 = Array(flat.utf8.indices) + [flat.utf8.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.utf8.index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) let c = min(indices1.count, indices2.count) for i in 0 ..< min(c, indices1.count - 1) { let c1 = flat.utf8[indices1[i]] let c2 = big.utf8[indices2[i]] XCTAssertEqual(c1, c2, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", 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.utf8.distance(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: BigString, file: StaticString = #file, line: UInt = #line ) -> (flat: [String.Index], big: [BigString.Index]) { let indices1 = Array(flat.utf16.indices) + [flat.utf16.endIndex] let indices2 = Array( sequence(first: big.startIndex) { $0 == big.endIndex ? nil : big.utf16.index(after: $0) } ) XCTAssertEqual(indices2.count, indices1.count, file: file, line: line) let c = min(indices1.count, indices2.count) for i in 0 ..< min(c, indices1.count - 1) { let c1 = flat.utf16[indices1[i]] let c2 = big.utf16[indices2[i]] XCTAssertEqual(c1, c2, "i: \(i), i1: \(indices1[i]), i2: \(indices2[i])", 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.utf8.distance(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_iterator_character() { let small: BigString = "Cafe\u{301} 👩‍👩‍👧" var it = small.makeIterator() XCTAssertEqual(it.next(), "C") XCTAssertEqual(it.next(), "a") XCTAssertEqual(it.next(), "f") XCTAssertEqual(it.next(), "e\u{301}") XCTAssertEqual(it.next(), " ") XCTAssertEqual(it.next(), "👩‍👩‍👧") XCTAssertNil(it.next()) let flat = sampleString let big = BigString(flat) let c1 = Array(flat) let c2 = Array(big) XCTAssertEqual(c1, c2) } func test_iterator_scalar() { let flat = sampleString let big = BigString(flat) let c1 = Array(flat.unicodeScalars) let c2 = Array(big.unicodeScalars) XCTAssertEqual(c1, c2) } func test_iterator_utf8() { let flat = sampleString let big = BigString(flat) let c1 = Array(flat.utf8) let c2 = Array(big.utf8) XCTAssertEqual(c1, c2) } func test_iterator_utf16() { let flat = sampleString let big = BigString(flat) let c1 = Array(flat.utf16) let c2 = Array(big.utf16) XCTAssertEqual(c1, c2) } func test_indices_character() { let flat = sampleString let big = BigString(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.index(i2, offsetBy: j - i) let b = String(big[i2 ..< j2]) XCTAssertEqual(b, a) } } } func test_indices_scalar() { let flat = sampleString let big = BigString(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.unicodeScalars.index(i2, offsetBy: j - i) let b = String(big[i2 ..< j2]) XCTAssertEqual(b, a) } } } func test_indices_utf16() { let flat = sampleString let big = BigString(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.utf16.index(i2, offsetBy: j - i) let b = String(big[i2 ..< j2]) XCTAssertEqual(b, a) } } } func test_indices_utf8() { let flat = sampleString let big = BigString(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.utf8.index(i2, offsetBy: j - i) let b = String(big[i2 ..< j2]) XCTAssertEqual(b, a) } } } func test_append_string() { let flat = sampleString let ref = BigString(flat) for stride in [1, 2, 4, 8, 16, 32, 64, 128, 250, 1000, 10000, 20000] { var big: BigString = "" 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 = BigString(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: BigString = "" var smol = "" for i in pieces.indices { let piece = pieces[i] let utf8Offset = pieces[..