forked from typelift/SwiftCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLattice.swift
128 lines (110 loc) · 3.17 KB
/
Lattice.swift
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
//
// Lattice.swift
// SwiftCheck
//
// Created by Robert Widmann on 5/2/15.
// Copyright (c) 2015 TypeLift. All rights reserved.
// Released under the MIT license.
//
/// Lattice types are types that have definable upper and lower limits. For
/// types like the `Int` and `Float`, their limits are the minimum and maximum
/// possible values representable in their bit- width. While the definition of
/// a "limit" is flexible, generally custom types that wish to conform to
/// `LatticeType` must come with some kind of supremum or infimum.
public protocol LatticeType {
/// The lower limit of the type.
static var min : Self { get }
/// The upper limit of the type.
static var max : Self { get }
}
extension Bool : LatticeType {
/// The lower limit of the `Bool` type.
public static var min : Bool {
return false
}
/// The upper limit of the `Bool` type.
public static var max : Bool {
return true
}
}
extension Character : LatticeType {
/// The lower limit of the `Character` type.
public static var min : Character {
return "\0"
}
/// The upper limit of the `Character` type.
public static var max : Character {
return "\u{FFFFF}"
}
}
extension UInt : LatticeType {}
extension UInt8 : LatticeType {}
extension UInt16 : LatticeType {}
extension UInt32 : LatticeType {}
extension UInt64 : LatticeType {}
extension Int : LatticeType {}
extension Int8 : LatticeType {}
extension Int16 : LatticeType {}
extension Int32 : LatticeType {}
extension Int64 : LatticeType {}
extension Float : LatticeType {
/// The lower limit of the `Float` type.
public static var min : Float {
return FLT_MIN
}
/// The upper limit of the `Float` type.
public static var max : Float {
return FLT_MAX
}
}
extension Double : LatticeType {
/// The lower limit of the `Double` type.
public static var min : Double {
return DBL_MIN
}
/// The upper limit of the `Double` type.
public static var max : Double {
return DBL_MAX
}
}
extension AnyForwardIndex : LatticeType {
/// The lower limit of the `AnyForwardIndex` type.
public static var min : AnyForwardIndex {
return AnyForwardIndex(Int64.min)
}
/// The upper limit of the `AnyForwardIndex` type.
public static var max : AnyForwardIndex {
return AnyForwardIndex(Int64.max)
}
}
extension AnyRandomAccessIndex : LatticeType {
/// The lower limit of the `AnyRandomAccessIndex` type.
public static var min : AnyRandomAccessIndex {
return AnyRandomAccessIndex(Int64.min)
}
/// The upper limit of the `AnyRandomAccessIndex` type.
public static var max : AnyRandomAccessIndex {
return AnyRandomAccessIndex(Int64.max)
}
}
/// float.h does not export Float80's limits, nor does the Swift Standard Library.
// rdar://18404510
//extension Swift.Float80 : LatticeType {
// public static var min : Swift.Float80 {
// return LDBL_MIN
// }
//
// public static var min : Swift.Float80 {
// return LDBL_MAX
// }
//}
#if os(Linux)
import Glibc
/// Matches http://www.opensource.apple.com/source/gcc/gcc-934.3/float.h
public var FLT_MAX: Float = 3.40282347e+38
public var FLT_MIN: Float = 1.17549435e-38
public var DBL_MAX: Double = 1.7976931348623157e+308
public var DBL_MIN: Double = 2.2250738585072014e-308
#else
import Darwin
#endif