File: integer_conversions.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 (131 lines) | stat: -rw-r--r-- 3,308 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: reflection

var a : Int

func test_Int8() {
  var i8 : Int8
  i8 = -0x1
  print(Int(i8))
}

func test_UInt8() {
  var ui8 : UInt8
  ui8 = 0xFF
  print(ui8)
  var i8 = Int8(bitPattern: ui8)
  print(Int(i8))
}

func test_UInt32() {
  var ui32 : UInt32
  ui32 = 0xFFFFFFFF
  print(ui32)
  var i8 : Int8
  i8 = Int8(ui32 & (0xF))
  print(String(i8))
}

test_Int8()
test_UInt8()
test_UInt32()

var tentwenty : UInt64
tentwenty = 1000
tentwenty += 20
print(tentwenty)

// CHECK:  -1
// CHECK:  255
// CHECK:  4294967295
// CHECK:  15
// CHECK:  1020


// Test generic conversions from floating point

print(Int8._convert(from: -128))
print(Int8._convert(from: 128))
print(Int8._convert(from: -127))
print(Int8._convert(from: 127))

// CHECK:  (value: Optional(-128), exact: true)
// CHECK:  (value: nil, exact: false)
// CHECK:  (value: Optional(-127), exact: true)
// CHECK:  (value: Optional(127), exact: true)

print(Int8._convert(from: -129))
print(Int8._convert(from: -128.999))
print(Int8._convert(from: -128.001))
print(Int8._convert(from: -127.999))
print(Int8._convert(from: -127.001))
print(Int8._convert(from: 127.001))
print(Int8._convert(from: 127.999))

// CHECK:  (value: nil, exact: false)
// CHECK:  (value: Optional(-128), exact: false)
// CHECK:  (value: Optional(-128), exact: false)
// CHECK:  (value: Optional(-127), exact: false)
// CHECK:  (value: Optional(-127), exact: false)
// CHECK:  (value: Optional(127), exact: false)
// CHECK:  (value: Optional(127), exact: false)

print(Int8._convert(from: 0))
print(Int8._convert(from: -0.0))
print(Int8._convert(from: 0.001))
print(Int8._convert(from: -0.001))

// CHECK:  (value: Optional(0), exact: true)
// CHECK:  (value: Optional(0), exact: true)
// CHECK:  (value: Optional(0), exact: false)
// CHECK:  (value: Optional(0), exact: false)

print(Int8._convert(from: Double.leastNonzeroMagnitude))
print(Int8._convert(from: -Double.leastNonzeroMagnitude))
print(Int8._convert(from: Double.leastNormalMagnitude))
print(Int8._convert(from: -Double.leastNormalMagnitude))

// CHECK:  (value: Optional(0), exact: false)
// CHECK:  (value: Optional(0), exact: false)
// CHECK:  (value: Optional(0), exact: false)
// CHECK:  (value: Optional(0), exact: false)

print(UInt8._convert(from: -1))
print(UInt8._convert(from: -0.9))
print(UInt8._convert(from: 255))
print(UInt8._convert(from: 256))
print(UInt8._convert(from: Double.infinity))
print(UInt8._convert(from: -Double.infinity))
print(UInt8._convert(from: Double.nan))

// CHECK:  (value: nil, exact: false)
// CHECK:  (value: Optional(0), exact: false)
// CHECK:  (value: Optional(255), exact: true)
// CHECK:  (value: nil, exact: false)
// CHECK:  (value: nil, exact: false)
// CHECK:  (value: nil, exact: false)
// CHECK:  (value: nil, exact: false)

let f = Float(Int64.min)
let ff = Int64._convert(from: f)
let fff = Int64(f)
print(fff == ff.value!)
// CHECK:  true

let g = f.nextUp
let gg = Int64._convert(from: g)
let ggg = Int64(g)
print(ggg == gg.value!)
// CHECK:  true

let h = Float(Int64.max)
let hh = Int64._convert(from: h)
print(hh)
// CHECK:  (value: nil, exact: false)

let i = h.nextDown
let ii = Int64._convert(from: i)
let iii = Int64(i)
print(iii == ii.value!)
// CHECK:  true