File: variadic_generic_tuples.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (89 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download | duplicates (2)
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
// RUN: %target-run-simple-swift

// REQUIRES: executable_test

import StdlibUnittest

var tuples = TestSuite("VariadicGenericTuples")

// Metadata instantiation for tuples containing pack expansions

func makeTuple<each T>(_: repeat (each T).Type) -> Any.Type {
  return (repeat Array<each T>).self
}

tuples.test("makeTuple") {
  expectEqual("()", _typeName(makeTuple()))

  // Note that we unwrap the one-element tuple!
  expectEqual("Swift.Array<Swift.Int>", _typeName(makeTuple(Int.self)))

  expectEqual("(Swift.Array<Swift.Int>, Swift.Array<Swift.String>)", _typeName(makeTuple(Int.self, String.self)))
  expectEqual("(Swift.Array<Swift.Int>, Swift.Array<Swift.String>, Swift.Array<Swift.Float>)", _typeName(makeTuple(Int.self, String.self, Float.self)))
}

func makeTuple2<each T>(_: repeat (each T).Type) -> Any.Type {
  return (Int, repeat Array<each T>).self
}

tuples.test("makeTuple2") {
  // Note that we unwrap the one-element tuple!
  expectEqual("Swift.Int", _typeName(makeTuple2()))

  expectEqual("(Swift.Int, Swift.Array<Swift.Bool>)", _typeName(makeTuple2(Bool.self)))
  expectEqual("(Swift.Int, Swift.Array<Swift.Bool>, Swift.Array<Swift.Character>)", _typeName(makeTuple2(Bool.self, Character.self)))
  expectEqual("(Swift.Int, Swift.Array<Swift.Bool>, Swift.Array<Swift.Character>, Swift.Array<Swift.Double>)", _typeName(makeTuple2(Bool.self, Character.self, Double.self)))
}

func makeTuple3<each T, each U>(t: repeat (each T).Type, u: repeat (each U).Type) -> Any.Type {
  return (repeat each T, repeat each U).self
}

tuples.test("makeTuple3") {
  expectEqual("()", _typeName(makeTuple3()))

  // Note that we unwrap the one-element tuple!
  expectEqual("Swift.Int", _typeName(makeTuple3(t: Int.self)))
  expectEqual("Swift.Int", _typeName(makeTuple3(u: Int.self)))

  expectEqual("(Swift.Int, Swift.Float)", _typeName(makeTuple3(t: Int.self, u: Float.self)))
}

func makeTuple<each Element>(
  _ element: repeat each Element
) -> (repeat each Element) {
  return (repeat each element)
}

func expandTupleElements<each T: Equatable>(_ value: repeat each T) {
  let values = makeTuple(repeat each value)
  _ = (repeat expectEqual(each value, each values))
}

tuples.test("expandTuple") {
  expandTupleElements(1, "hello", true)
}

func tupleLabelMix<each T, each U>(t: repeat (each T).Type, u: repeat (each U).Type) -> Any.Type {
  return (Float, hello: Int, repeat each T, swift: String, repeat each U, Bool, world: UInt8).self
}

func oneElementLabeledTuple<each T>(t: repeat (each T).Type) -> Any.Type {
  return (label: Int, repeat each T).self
}

tuples.test("labels") {
  expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix()))
  expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Double, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(u: Double.self)))
  expectEqual("(Swift.Float, hello: Swift.Int, swift: Swift.String, Swift.Double, Swift.Int32, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(u: Double.self, Int32.self)))
  expectEqual("(Swift.Float, hello: Swift.Int, Swift.Character, swift: Swift.String, Swift.Double, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(t: Character.self, u: Double.self)))
  expectEqual("(Swift.Float, hello: Swift.Int, Swift.Character, Swift.Substring, swift: Swift.String, Swift.Double, Swift.Int32, Swift.Bool, world: Swift.UInt8)", _typeName(tupleLabelMix(t: Character.self, Substring.self, u: Double.self, Int32.self)))

  // FIXME: One-element labeled tuples
  expectEqual("Swift.Int", _typeName(oneElementLabeledTuple()))

  expectEqual("(label: Swift.Int, Swift.String)", _typeName(oneElementLabeledTuple(t: String.self)))
}


runAllTests()