Skip to content

For testing Foundation on Android #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2017
Merged
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
17 changes: 17 additions & 0 deletions Foundation/NSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

public init?(name tzName: String, data aData: Data?) {
#if os(Android)
var tzName = tzName
if tzName == "UTC" || tzName == "GMT" {
tzName = "GMT+0000"
}
else if !(tzName.hasPrefix("GMT+") || tzName.hasPrefix("GMT-")) {
NSLog("Time zone database not available on Android")
}
#endif
super.init()
if !_CFTimeZoneInit(_cfObject, tzName._cfObject, aData?._cfObject) {
return nil
Expand Down Expand Up @@ -170,6 +179,14 @@ open class NSTimeZone : NSObject, NSCopying, NSSecureCoding, NSCoding {
extension NSTimeZone {

open class var system: TimeZone {
#if os(Android)
var now = time(nil), info = tm()
if localtime_r(&now, &info) != nil {
// NOTE: this is not a real time zone but a fixed offset from GMT.
// It will be incorrect outside the current daylight saving period.
return TimeZone(reference: NSTimeZone(forSecondsFromGMT: info.tm_gmtoff))
}
#endif
return CFTimeZoneCopySystem()._swiftObject
}

Expand Down
6 changes: 4 additions & 2 deletions TestFoundation/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class _TCPSocket {
public private(set) var port: UInt16

init(port: UInt16?) throws {
#if os(Linux)
#if os(Linux) && !os(Android)
let SOCKSTREAM = Int32(SOCK_STREAM.rawValue)
#else
let SOCKSTREAM = SOCK_STREAM
Expand Down Expand Up @@ -102,7 +102,9 @@ class _TCPSocket {
// asking to accept incoming connections if the firewall is enabled.
let addr = UInt32(INADDR_LOOPBACK).bigEndian
let netPort = UInt16(bigEndian: port ?? 0)
#if os(Linux)
#if os(Android)
return sockaddr_in(sin_family: sa_family_t(AF_INET), sin_port: netPort, sin_addr: in_addr(s_addr: addr), __pad: (0,0,0,0,0,0,0,0))
#elseif os(Linux)
return sockaddr_in(sin_family: sa_family_t(AF_INET), sin_port: netPort, sin_addr: in_addr(s_addr: addr), sin_zero: (0,0,0,0,0,0,0,0))
#else
return sockaddr_in(sin_len: 0, sin_family: sa_family_t(AF_INET), sin_port: netPort, sin_addr: in_addr(s_addr: addr), sin_zero: (0,0,0,0,0,0,0,0))
Expand Down
8 changes: 7 additions & 1 deletion TestFoundation/TestCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class TestCodable : XCTestCase {

// MARK: - TimeZone
lazy var timeZoneValues: [TimeZone] = {
#if !os(Android)
var values = [
TimeZone(identifier: "America/Los_Angeles")!,
TimeZone(identifier: "UTC")!,
Expand All @@ -318,7 +319,12 @@ class TestCodable : XCTestCase {
// causing encode -> decode -> compare test to fail.
values.append(TimeZone.current)
#endif

#else
var values = [
TimeZone(identifier: "UTC")!,
TimeZone.current
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the problem here is that Android doesn't come with a reasonable ICU?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, options are very limited for Android date/time conversion from C

#endif
return values
}()

Expand Down
4 changes: 3 additions & 1 deletion TestFoundation/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class TestDecimal: XCTestCase {
}
}
}
XCTAssertEqual(Decimal(186243*15673), Decimal(186243) * Decimal(15673))
XCTAssertEqual(Decimal(186243 * 15673 as Int64), Decimal(186243) * Decimal(15673))
}

func test_Misc() {
Expand Down Expand Up @@ -308,7 +308,9 @@ class TestDecimal: XCTestCase {
XCTAssertFalse(Decimal.nan.isTotallyOrdered(belowOrEqualTo: Decimal(2.3)))
XCTAssertTrue(Decimal(2) < Decimal(3))
XCTAssertTrue(Decimal(3) > Decimal(2))
#if !arch(arm)
XCTAssertEqual(3275573729074, Decimal(1234).hashValue)
#endif
XCTAssertEqual(Decimal(-9), Decimal(1) - Decimal(10))
XCTAssertEqual(Decimal(3), Decimal(2).nextUp)
XCTAssertEqual(Decimal(2), Decimal(3).nextDown)
Expand Down
2 changes: 2 additions & 0 deletions TestFoundation/TestHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class TestHTTPCookieStorage: XCTestCase {
}

func test_cookieInXDGSpecPath() {
#if !os(Android)
//Test without setting the environment variable
let testCookie = HTTPCookie(properties: [
.name: "TestCookie0",
Expand Down Expand Up @@ -287,5 +288,6 @@ class TestHTTPCookieStorage: XCTestCase {
let terminationReason = task.terminationReason
XCTAssertEqual(terminationReason, Process.TerminationReason.exit)
try? fm.removeItem(atPath: testPath)
#endif
}
}
6 changes: 4 additions & 2 deletions TestFoundation/TestISO8601DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class TestISO8601DateFormatter: XCTestCase {
isoFormatter.formatOptions = [.withMonth, .withDay, .withWeekOfYear, .withDashSeparatorInDate]
XCTAssertEqual(isoFormatter.string(from: someDateTime!), "10-W40-06")

#if !os(Android)
/*
The following tests cover various cases when changing the .formatOptions property with a different TimeZone set.
*/
Expand Down Expand Up @@ -141,7 +142,7 @@ class TestISO8601DateFormatter: XCTestCase {

isoFormatter.formatOptions = [.withDay, .withWeekOfYear, .withMonth, .withTimeZone, .withColonSeparatorInTimeZone, .withDashSeparatorInDate]
XCTAssertEqual(isoFormatter.string(from: someDateTime!), "10-W40-06-07:00")
#endif
}


Expand Down Expand Up @@ -248,6 +249,7 @@ class TestISO8601DateFormatter: XCTestCase {
formatOptions = [.withMonth, .withDay, .withWeekOfYear, .withDashSeparatorInDate]
XCTAssertEqual(ISO8601DateFormatter.string(from: someDateTime!, timeZone: timeZone!, formatOptions: formatOptions), "10-W40-06")

#if !os(Android)
/*
The following tests cover various cases when changing the .formatOptions property with a different TimeZone set.
*/
Expand Down Expand Up @@ -289,7 +291,7 @@ class TestISO8601DateFormatter: XCTestCase {

formatOptions = [.withDay, .withWeekOfYear, .withMonth, .withTimeZone, .withColonSeparatorInTimeZone, .withDashSeparatorInDate]
XCTAssertEqual(ISO8601DateFormatter.string(from: someDateTime!, timeZone: timeZone!, formatOptions: formatOptions), "10-W40-06-07:00")
#endif
}

}
12 changes: 12 additions & 0 deletions TestFoundation/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,15 @@ class TestJSONEncoder : XCTestCase {
}

func test_codingOfInt64() {
#if !arch(arm)
test_codingOf(value: Int64(-9000000000000000042), toAndFrom: "-9000000000000000042")
#endif
}

func test_codingOfUInt64() {
#if !arch(arm)
test_codingOf(value: UInt64(9000000000000000042), toAndFrom: "9000000000000000042")
#endif
}

func test_codingOfInt() {
Expand All @@ -367,7 +371,11 @@ class TestJSONEncoder : XCTestCase {
case 4: // 32-bit
test_codingOf(value: Int(-2000000042), toAndFrom: "-2000000042")
case 8: // 64-bit
#if arch(arm)
break
#else
test_codingOf(value: Int(-9000000000000000042), toAndFrom: "-9000000000000000042")
#endif
default:
XCTFail("Unexpected UInt size: \(intSize)")
}
Expand All @@ -379,7 +387,11 @@ class TestJSONEncoder : XCTestCase {
case 4: // 32-bit
test_codingOf(value: UInt(2000000042), toAndFrom: "2000000042")
case 8: // 64-bit
#if arch(arm)
break
#else
test_codingOf(value: UInt(9000000000000000042), toAndFrom: "9000000000000000042")
#endif
default:
XCTFail("Unexpected UInt size: \(uintSize)")
}
Expand Down
5 changes: 5 additions & 0 deletions TestFoundation/TestNSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ class TestNSAttributedString : XCTestCase {
}

func test_enumerateAttributes() {
#if os(Android)
// Invalid dictionary returned by CFAttributedStringGetAttributesAndLongestEffectiveRange
XCTFail("Intermittent failures on Android")
#else
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."

let attrKey1 = "attribute.placeholder.key1"
Expand Down Expand Up @@ -213,6 +217,7 @@ class TestNSAttributedString : XCTestCase {
}
XCTAssertEqual(rangeDescriptionString, "(0,10)")
XCTAssertEqual(attrsDescriptionString, "[attribute.placeholder.key1:attribute.placeholder.value1]")
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class TestNSData: XCTestCase {

func test_writeToURLOptions() {
let saveData = try! Data(contentsOf: Bundle.main.url(forResource: "Test", withExtension: "plist")!)
let savePath = URL(fileURLWithPath: "/var/tmp/Test.plist")
let savePath = URL(fileURLWithPath: NSTemporaryDirectory() + "Test1.plist")
do {
try saveData.write(to: savePath, options: .atomic)
let fileManager = FileManager.default
Expand Down
4 changes: 4 additions & 0 deletions TestFoundation/TestNSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,16 @@ class TestNSLocale : XCTestCase {
}

func test_localeProperties(){
#if os(Android)
XCTFail("Locale lookup unavailable on Android")
#else
let enUSID = "en_US"
let locale = Locale(identifier: enUSID)
XCTAssertEqual(String(describing: locale.languageCode!), "en")
XCTAssertEqual(String(describing: locale.decimalSeparator!), ".")
XCTAssertEqual(String(describing: locale.currencyCode!), "USD")
XCTAssertEqual(String(describing: locale.collatorIdentifier!), enUSID)
#endif
}

}
68 changes: 58 additions & 10 deletions TestFoundation/TestNSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ class TestNSNumber : XCTestCase {
let uintSize = MemoryLayout<UInt>.size
switch uintSize {
case 4: XCTAssertEqual(NSNumber(value: Int16.min).uintValue, 4294934528)
case 8: XCTAssertEqual(NSNumber(value: Int16.min).uintValue, 18446744073709518848)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int16.min).uintValue, 18446744073709518848)
#endif
default: XCTFail("Unexpected UInt size: \(uintSize)")
}

Expand Down Expand Up @@ -402,7 +407,12 @@ class TestNSNumber : XCTestCase {
let uintSize = MemoryLayout<UInt>.size
switch uintSize {
case 4: XCTAssertEqual(NSNumber(value: Int32.min).uintValue, 2147483648)
case 8: XCTAssertEqual(NSNumber(value: Int32.min).uintValue, 18446744071562067968)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int32.min).uintValue, 18446744071562067968)
#endif
default: XCTFail("Unexpected UInt size: \(uintSize)")
}

Expand Down Expand Up @@ -466,7 +476,12 @@ class TestNSNumber : XCTestCase {
let intSize = MemoryLayout<Int>.size
switch intSize {
case 4: XCTAssertEqual(NSNumber(value: UInt32.max).intValue, -1)
case 8: XCTAssertEqual(NSNumber(value: UInt32.max).intValue, 4294967295)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: UInt32.max).intValue, 4294967295)
#endif
default: XCTFail("Unexpected Int size: \(intSize)")
}
XCTAssertEqual(NSNumber(value: UInt32.max).uintValue, 4294967295)
Expand Down Expand Up @@ -511,14 +526,24 @@ class TestNSNumber : XCTestCase {
let intSize = MemoryLayout<Int>.size
switch intSize {
case 4: XCTAssertEqual(NSNumber(value: Int64.min).intValue, 0)
case 8: XCTAssertEqual(NSNumber(value: Int64.min).intValue, -9223372036854775808)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int64.min).intValue, -9223372036854775808)
#endif
default: XCTFail("Unexpected Int size: \(intSize)")
}

let uintSize = MemoryLayout<UInt>.size
switch uintSize {
case 4: XCTAssertEqual(NSNumber(value: Int64.min).uintValue, 0)
case 8: XCTAssertEqual(NSNumber(value: Int64.min).uintValue, 9223372036854775808)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int64.min).uintValue, 9223372036854775808)
#endif
default: XCTFail("Unexpected UInt size: \(uintSize)")
}

Expand All @@ -541,13 +566,23 @@ class TestNSNumber : XCTestCase {

switch intSize {
case 4: XCTAssertEqual(NSNumber(value: Int64.max).intValue, -1)
case 8: XCTAssertEqual(NSNumber(value: Int64.max).intValue, 9223372036854775807)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int64.max).intValue, 9223372036854775807)
#endif
default: XCTFail("Unexpected Int size: \(intSize)")
}

switch uintSize {
case 4: XCTAssertEqual(NSNumber(value: Int64.max).uintValue, 4294967295)
case 8: XCTAssertEqual(NSNumber(value: Int64.max).uintValue, 9223372036854775807)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int64.max).uintValue, 9223372036854775807)
#endif
default: XCTFail("Unexpected UInt size: \(uintSize)")
}

Expand Down Expand Up @@ -592,7 +627,12 @@ class TestNSNumber : XCTestCase {
let uintSize = MemoryLayout<UInt>.size
switch uintSize {
case 4: XCTAssertEqual(NSNumber(value: UInt64.max).uintValue, 4294967295)
case 8: XCTAssertEqual(NSNumber(value: UInt64.max).uintValue, 18446744073709551615)
case 8:
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: UInt64.max).uintValue, 18446744073709551615)
#endif
default: XCTFail("Unexpected UInt size: \(uintSize)")
}

Expand Down Expand Up @@ -638,8 +678,9 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: Int.min).uint64Value, 18446744071562067968)

XCTAssertEqual(NSNumber(value: Int.min).intValue, -2147483648)
#if !arch(arm)
XCTAssertEqual(NSNumber(value: Int.min).uintValue, 18446744071562067968)

#endif
XCTAssertEqual(NSNumber(value: Int.min).floatValue, Float(Int.min))
XCTAssertEqual(NSNumber(value: Int.min).doubleValue, Double(Int.min))

Expand All @@ -664,6 +705,9 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: Int.max).doubleValue, Double(Int.max))

case (8, 8):
#if arch(arm)
break
#else
XCTAssertEqual(NSNumber(value: Int.min).boolValue, false)

XCTAssertEqual(NSNumber(value: Int.min).int8Value, 0)
Expand Down Expand Up @@ -701,7 +745,7 @@ class TestNSNumber : XCTestCase {

XCTAssertEqual(NSNumber(value: Int.max).floatValue, Float(Int.max))
XCTAssertEqual(NSNumber(value: Int.max).doubleValue, Double(Int.max))

#endif
default: XCTFail("Unexpected mismatched Int & UInt sizes: \(intSize) & \(uintSize)")
}
}
Expand Down Expand Up @@ -743,7 +787,9 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: UInt.max).uint32Value, 4294967295)
XCTAssertEqual(NSNumber(value: UInt.max).uint64Value, 4294967295)

#if !arch(arm)
XCTAssertEqual(NSNumber(value: UInt.max).intValue, 4294967295)
#endif
XCTAssertEqual(NSNumber(value: UInt.max).uintValue, 4294967295)

XCTAssertEqual(NSNumber(value: UInt.max).floatValue, Float(UInt.max))
Expand All @@ -763,7 +809,9 @@ class TestNSNumber : XCTestCase {
XCTAssertEqual(NSNumber(value: UInt.max).uint64Value, 18446744073709551615)

XCTAssertEqual(NSNumber(value: UInt.max).intValue, -1)
#if !arch(arm)
XCTAssertEqual(NSNumber(value: UInt.max).uintValue, 18446744073709551615)
#endif

XCTAssertEqual(NSNumber(value: UInt.max).floatValue, Float(UInt.max))
XCTAssertEqual(NSNumber(value: UInt.max).doubleValue, Double(UInt.max))
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ class TestNSString : XCTestCase {
let path = NSString(string: "~\(userName)/")
let result = path.expandingTildeInPath
// next assert fails in VirtualBox because home directory for unknown user resolved to /var/run/vboxadd
XCTAssert(result == "~\(userName)", "Return copy of reciver if home directory could no be resolved.")
XCTAssertEqual(result, "~\(userName)", "Return copy of receiver if home directory could not be resolved.")
}
}

Expand Down
Loading