Files
swift-mirror/validation-test/StdlibUnittest/Stdin.swift
Daniel Rodríguez Troitiño 5835cebe99 [android] Do not try to turn EOF into an UInt8
Android is one of those platforms that define the symbol EOF as a
negative number. Trying to turn it into an UInt8 will only crash the
test. Move the transformation after we have checked for EOF first. In my
opinion, in other platforms, having the EOF attached to the getline
result was also invalid, and that's probably why the two test that use
EOF were sending an extra newline.

Even with this, the tests do not pass on Android because it deadlocks
waiting for input/output from the child process, which never happens. I
haven't find why this happens, but only happens if the last test of the
suite closes stdin. I will try to fix that in another PR.
2019-04-19 17:24:45 -07:00

81 lines
1.5 KiB
Swift

// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#elseif os(Windows)
import MSVCRT
#else
#error("Unsupported platform")
#endif
func simple_getline() -> [UInt8]? {
var result = [UInt8]()
while true {
let c = getchar()
if c == EOF {
if result.count == 0 {
return nil
}
return result
}
result.append(UInt8(c))
if c == CInt(UnicodeScalar("\n").value) {
return result
}
}
}
var StdinTestSuite = TestSuite("Stdin")
StdinTestSuite.test("Empty")
.stdin("")
.code {
}
StdinTestSuite.test("EmptyLine")
.stdin("\n")
.code {
expectEqual([ 0x0a ], simple_getline())
}
StdinTestSuite.test("Whitespace")
.stdin(" \n")
.code {
expectEqual([ 0x20, 0x0a ], simple_getline())
}
StdinTestSuite.test("NonEmptyLine")
.stdin("abc\n")
.code {
expectEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
StdinTestSuite.test("TwoLines")
.stdin("abc\ndefghi\n")
.code {
expectEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
expectEqual(
[ 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x0a ], simple_getline())
}
StdinTestSuite.test("EOF/1")
.stdin("abc\n", eof: true)
.code {
expectEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
StdinTestSuite.test("EOF/2")
.stdin("abc\n", eof: true)
.code {
expectEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
runAllTests()