File: ArrayBuffer_CopyContents.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (84 lines) | stat: -rw-r--r-- 2,255 bytes parent folder | download
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
//===--- ArrayBuffer_CopyContents.swift -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: swift_stdlib_asserts
// REQUIRES: foundation

// Test is crashing with older runtimes. rdar://82125328
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

import Foundation
import StdlibUnittest

let suite = TestSuite("ArrayBuffer_CopyContents")
defer { runAllTests() }


var trackedCount = 0
var nextBaseSerialNumber = 0

/// A type that will be bridged verbatim to Objective-C
class Thing: NSObject {
  var value: Int
  var serialNumber: Int

  func foo() { }

  required init(_ value: Int) {
    trackedCount += 1
    nextBaseSerialNumber += 1
    serialNumber = nextBaseSerialNumber
    self.value = value
  }

  deinit {
    assert(serialNumber > 0, "double destruction!")
    trackedCount -= 1
    serialNumber = -serialNumber
  }

  override func isEqual(_ other: Any?) -> Bool {
    return (other as? Thing)?.value == self.value
  }

  override var hash: Int { value }
}


suite.test("nativeArray/_copyContents") {
  let array = [Thing(0), Thing(1), Thing(2), Thing(3)]
  expectEqualSequence(array._copyToNewArray(), array)
}

suite.test("nativeArraySlice/_copyContents") {
  let array = (0 ..< 100).map { Thing($0) }
  expectEqualSequence(
    array[20 ..< 30]._copyToNewArray(),
    (20 ..< 30).map { Thing($0) })
}

suite.test("bridgedArray/_copyContents") {
  let array = NSArray(array: (0 ..< 5).map { Thing($0) }) as! [Thing]
  expectEqualSequence(
    array._copyToNewArray(),
    (0 ..< 5).map { Thing($0) })
}

suite.test("bridgedArraySlice/_copyContents") {
  let array = NSArray(array: (0 ..< 100).map { Thing($0) }) as! [Thing]
  expectEqualSequence(
    array[20 ..< 30]._copyToNewArray(),
    (20 ..< 30).map { Thing($0) })
}