|
1 | 1 | import JSON
|
2 | 2 | import Testing
|
3 | 3 |
|
4 |
| -struct IntegerOverflow |
| 4 | +@Suite |
| 5 | +enum IntegerOverflow |
5 | 6 | {
|
6 |
| - private |
7 |
| - func expect<Signed>(_ value:Int64, overflows:Signed.Type) |
8 |
| - where Signed:SignedInteger & JSONDecodable |
| 7 | + @Test |
| 8 | + static func AsInt8() |
9 | 9 | {
|
10 |
| - let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
11 |
| - value: .number(.init(value))) |
12 |
| - |
13 |
| - #expect(throws: JSON.DecodingError<Never?>.self) |
14 |
| - { |
15 |
| - let _:Signed = try field.decode() |
16 |
| - } |
| 10 | + Self.expect(256, overflows: Int8.self) |
17 | 11 | }
|
18 |
| - private |
19 |
| - func decode<Signed>(_ value:Int64, to:Signed.Type) |
20 |
| - where Signed:SignedInteger & JSONDecodable |
21 |
| - { |
22 |
| - let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
23 |
| - value: .number(.init(value))) |
24 | 12 |
|
25 |
| - #expect(throws: Never.self) |
26 |
| - { |
27 |
| - let _:Signed = try field.decode() |
28 |
| - } |
| 13 | + @Test |
| 14 | + static func AsInt16() |
| 15 | + { |
| 16 | + Self.decode(256, to: Int16.self) |
29 | 17 | }
|
30 | 18 |
|
31 |
| - private |
32 |
| - func expect<Unsigned>(_ value:UInt64, overflows:Unsigned.Type) |
33 |
| - where Unsigned:UnsignedInteger & JSONDecodable |
| 19 | + @Test |
| 20 | + static func AsInt32() |
34 | 21 | {
|
35 |
| - let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
36 |
| - value: .number(.init(value))) |
37 |
| - |
38 |
| - #expect(throws: JSON.DecodingError<Never?>.self) |
39 |
| - { |
40 |
| - let _:Unsigned = try field.decode() |
41 |
| - } |
| 22 | + Self.decode(256, to: Int32.self) |
42 | 23 | }
|
43 |
| - private |
44 |
| - func decode<Unsigned>(_ value:UInt64, to:Unsigned.Type) |
45 |
| - where Unsigned:UnsignedInteger & JSONDecodable |
46 |
| - { |
47 |
| - let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
48 |
| - value: .number(.init(value))) |
49 | 24 |
|
50 |
| - #expect(throws: Never.self) |
51 |
| - { |
52 |
| - let _:Unsigned = try field.decode() |
53 |
| - } |
54 |
| - } |
55 |
| -} |
56 |
| -extension IntegerOverflow |
57 |
| -{ |
58 |
| - @Test("Int8") |
59 |
| - func int8() |
| 25 | + @Test |
| 26 | + static func AsInt64() |
60 | 27 | {
|
61 |
| - self.expect(256, overflows: Int8.self) |
| 28 | + Self.decode(256, to: Int64.self) |
62 | 29 | }
|
63 | 30 |
|
64 |
| - @Test("Int16") |
65 |
| - func int16() |
| 31 | + @Test |
| 32 | + static func AsInt64Max() |
66 | 33 | {
|
67 |
| - self.decode(256, to: Int16.self) |
| 34 | + Self.decode(Int64.max, to: Int64.self) |
68 | 35 | }
|
69 | 36 |
|
70 |
| - @Test("Int32") |
71 |
| - func int32() |
| 37 | + @Test |
| 38 | + static func AsInt64Min() |
72 | 39 | {
|
73 |
| - self.decode(256, to: Int32.self) |
| 40 | + Self.decode(Int64.min, to: Int64.self) |
74 | 41 | }
|
75 | 42 |
|
76 |
| - @Test("Int64") |
77 |
| - func int64() |
| 43 | + @Test |
| 44 | + static func AsInt() |
78 | 45 | {
|
79 |
| - self.decode(256, to: Int64.self) |
| 46 | + Self.decode(256, to: Int.self) |
80 | 47 | }
|
81 | 48 |
|
82 |
| - @Test("Int64.max") |
83 |
| - func int64max() |
| 49 | + @Test |
| 50 | + static func AsUInt8() |
84 | 51 | {
|
85 |
| - self.decode(Int64.max, to: Int64.self) |
| 52 | + Self.expect(256, overflows: UInt8.self) |
86 | 53 | }
|
87 | 54 |
|
88 |
| - @Test("Int64.min") |
89 |
| - func int64min() |
| 55 | + @Test |
| 56 | + static func AsUInt16() |
90 | 57 | {
|
91 |
| - self.decode(Int64.min, to: Int64.self) |
| 58 | + Self.decode(256, to: UInt16.self) |
92 | 59 | }
|
93 | 60 |
|
94 |
| - @Test("Int") |
95 |
| - func int() |
| 61 | + @Test |
| 62 | + static func AsUInt32() |
96 | 63 | {
|
97 |
| - self.decode(256, to: Int.self) |
| 64 | + Self.decode(256, to: UInt32.self) |
98 | 65 | }
|
99 | 66 |
|
100 |
| - @Test("UInt8") |
101 |
| - func uint8() |
| 67 | + @Test |
| 68 | + static func AsUInt64() |
102 | 69 | {
|
103 |
| - self.expect(256, overflows: UInt8.self) |
| 70 | + Self.decode(256, to: UInt64.self) |
104 | 71 | }
|
105 | 72 |
|
106 |
| - @Test("UInt16") |
107 |
| - func uint16() |
| 73 | + @Test |
| 74 | + static func AsUInt64Max() |
108 | 75 | {
|
109 |
| - self.decode(256, to: UInt16.self) |
| 76 | + Self.decode(UInt64.max, to: UInt64.self) |
110 | 77 | }
|
111 | 78 |
|
112 |
| - @Test("UInt32") |
113 |
| - func uint32() |
| 79 | + @Test |
| 80 | + static func AsUInt() |
114 | 81 | {
|
115 |
| - self.decode(256, to: UInt32.self) |
| 82 | + Self.decode(256, to: UInt.self) |
116 | 83 | }
|
| 84 | +} |
| 85 | +extension IntegerOverflow |
| 86 | +{ |
| 87 | + private |
| 88 | + static func expect<Signed>(_ value:Int64, overflows:Signed.Type) |
| 89 | + where Signed:SignedInteger & JSONDecodable |
| 90 | + { |
| 91 | + let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
| 92 | + value: .number(.init(value))) |
117 | 93 |
|
118 |
| - @Test("UInt64") |
119 |
| - func uint64() |
| 94 | + #expect(throws: JSON.DecodingError<Never?>.self) |
| 95 | + { |
| 96 | + let _:Signed = try field.decode() |
| 97 | + } |
| 98 | + } |
| 99 | + private |
| 100 | + static func decode<Signed>(_ value:Int64, to:Signed.Type) |
| 101 | + where Signed:SignedInteger & JSONDecodable |
120 | 102 | {
|
121 |
| - self.decode(256, to: UInt64.self) |
| 103 | + let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
| 104 | + value: .number(.init(value))) |
| 105 | + |
| 106 | + #expect(throws: Never.self) |
| 107 | + { |
| 108 | + let _:Signed = try field.decode() |
| 109 | + } |
122 | 110 | }
|
123 | 111 |
|
124 |
| - @Test("UInt64.max") |
125 |
| - func uint64max() |
| 112 | + private |
| 113 | + static func expect<Unsigned>(_ value:UInt64, overflows:Unsigned.Type) |
| 114 | + where Unsigned:UnsignedInteger & JSONDecodable |
126 | 115 | {
|
127 |
| - self.decode(UInt64.max, to: UInt64.self) |
128 |
| - } |
| 116 | + let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
| 117 | + value: .number(.init(value))) |
129 | 118 |
|
130 |
| - @Test("UInt") |
131 |
| - func uint() |
| 119 | + #expect(throws: JSON.DecodingError<Never?>.self) |
| 120 | + { |
| 121 | + let _:Unsigned = try field.decode() |
| 122 | + } |
| 123 | + } |
| 124 | + private |
| 125 | + static func decode<Unsigned>(_ value:UInt64, to:Unsigned.Type) |
| 126 | + where Unsigned:UnsignedInteger & JSONDecodable |
132 | 127 | {
|
133 |
| - self.decode(256, to: UInt.self) |
| 128 | + let field:JSON.FieldDecoder<Never?> = .init(key: nil, |
| 129 | + value: .number(.init(value))) |
| 130 | + |
| 131 | + #expect(throws: Never.self) |
| 132 | + { |
| 133 | + let _:Unsigned = try field.decode() |
| 134 | + } |
134 | 135 | }
|
135 | 136 | }
|
136 |
| - |
0 commit comments