Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Sources/Vexil/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ extension Int8: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = Int8(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(Int8.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -221,8 +221,8 @@ extension Int16: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = Int16(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(Int16.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -234,8 +234,8 @@ extension Int32: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = Int32(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(Int32.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -247,8 +247,8 @@ extension Int64: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = Int64(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(Int64.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -260,8 +260,8 @@ extension UInt: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = UInt(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(UInt.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -273,8 +273,8 @@ extension UInt8: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = UInt8(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(UInt8.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -286,8 +286,8 @@ extension UInt16: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = UInt16(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(UInt16.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -299,8 +299,8 @@ extension UInt32: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = UInt32(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(UInt32.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand All @@ -312,8 +312,8 @@ extension UInt64: FlagValue {
public typealias BoxedValueType = Int

public init? (boxedFlagValue: BoxedFlagValue) {
guard let value = Int(boxedFlagValue: boxedFlagValue) else { return nil }
self = UInt64(value)
guard let value = Int(boxedFlagValue: boxedFlagValue).flatMap(UInt64.init(exactly:)) else { return nil }
self = value
}

public var boxedFlagValue: BoxedFlagValue {
Expand Down
39 changes: 39 additions & 0 deletions Tests/VexilTests/FlagValueUnboxingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ final class FlagValueUnboxingTests: XCTestCase {
}


// MARK: - Integer overflows

func testInt8FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(Int8.max) + 1)

XCTAssertNil(Int8(boxedFlagValue: boxed))
}

func testInt16FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(Int16.max) + 1)

XCTAssertNil(Int16(boxedFlagValue: boxed))
}

func testInt32FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(Int32.max) + 1)

XCTAssertNil(Int32(boxedFlagValue: boxed))
}

func testUInt8FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(UInt8.max) + 1)

XCTAssertNil(UInt8(boxedFlagValue: boxed))
}

func testUInt16FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(UInt16.max) + 1)

XCTAssertNil(UInt16(boxedFlagValue: boxed))
}

func testUInt32FlagValueWithOverflow () {
let boxed = BoxedFlagValue.integer(Int(UInt32.max) + 1)

XCTAssertNil(UInt32(boxedFlagValue: boxed))
}


// MARK: - Floating Point Flag Values

func testFloatFlagValue () {
Expand Down