-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathStdin.swift
76 lines (62 loc) · 1.5 KB
/
Stdin.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
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Windows)
import Glibc
#endif
func simple_getline() -> [UInt8]? {
var result = [UInt8]()
while true {
let c = getchar()
result.append(UInt8(c))
if c == EOF {
if result.count == 0 {
return nil
}
return result
}
if c == CInt(UnicodeScalar("\n").value) {
return result
}
}
}
var StdinTestSuite = TestSuite("Stdin")
StdinTestSuite.test("Empty")
.stdin("")
.code {
}
StdinTestSuite.test("EmptyLine")
.stdin("\n")
.code {
expectOptionalEqual([ 0x0a ], simple_getline())
}
StdinTestSuite.test("Whitespace")
.stdin(" \n")
.code {
expectOptionalEqual([ 0x20, 0x0a ], simple_getline())
}
StdinTestSuite.test("NonEmptyLine")
.stdin("abc\n")
.code {
expectOptionalEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
StdinTestSuite.test("TwoLines")
.stdin("abc\ndefghi\n")
.code {
expectOptionalEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
expectOptionalEqual(
[ 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x0a ], simple_getline())
}
StdinTestSuite.test("EOF/1")
.stdin("abc\n", eof: true)
.code {
expectOptionalEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
StdinTestSuite.test("EOF/2")
.stdin("abc\n", eof: true)
.code {
expectOptionalEqual([ 0x61, 0x62, 0x63, 0x0a ], simple_getline())
}
runAllTests()