|
2 | 2 | // |
3 | 3 | // This source file is part of the Swift.org open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception |
7 | 7 | // |
8 | 8 | // See https://swift.org/LICENSE.txt for license information |
@@ -110,4 +110,81 @@ UnicodeAPIs.test("UTF-8 and UTF-16 queries") { |
110 | 110 | } |
111 | 111 | } |
112 | 112 |
|
| 113 | +if #available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *) { |
| 114 | + var UnicodeLatin1 = TestSuite("UnicodeLatin1") |
| 115 | + |
| 116 | + UnicodeLatin1.test("Encoding") { |
| 117 | + let unicodeScalars = (UInt8.min ... UInt8.max).map { Unicode.Scalar($0) } |
| 118 | + let encodedScalars = unicodeScalars.compactMap { Unicode.Latin1.encode($0) } |
| 119 | + let decodedScalars = encodedScalars.map { Unicode.Latin1.decode($0) } |
| 120 | + expectEqualSequence(unicodeScalars, decodedScalars) |
| 121 | + expectNil(Unicode.Latin1.encode("\u{0100}")) |
| 122 | + expectNil(Unicode.Latin1.encode("\u{10FFFF}")) |
| 123 | + } |
| 124 | + |
| 125 | + UnicodeLatin1.test("Parser") { |
| 126 | + let codeUnits = UInt8.min ... UInt8.max |
| 127 | + var codeUnitsIterator = codeUnits.makeIterator() |
| 128 | + var encodedScalars: [Unicode.Latin1.EncodedScalar] = [] |
| 129 | + var forwardParser = Unicode.Latin1.ForwardParser() |
| 130 | + loop: while true { |
| 131 | + switch forwardParser.parseScalar(from: &codeUnitsIterator) { |
| 132 | + case .valid(let encodedScalar): |
| 133 | + encodedScalars.append(encodedScalar) |
| 134 | + case .emptyInput: |
| 135 | + expectEqualSequence(codeUnits, encodedScalars.joined()) |
| 136 | + break loop |
| 137 | + case .error: |
| 138 | + expectUnreachable() |
| 139 | + break loop |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + UnicodeLatin1.test("Transcode") { |
| 145 | + let codeUnitsAndText: [ClosedRange<UInt8>: String] = [ |
| 146 | + UInt8(0x20) ... UInt8(0x7E): |
| 147 | + """ |
| 148 | + \u{20}!"#$%&'()*+,-./0123456789:;<=>?\ |
| 149 | + @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\ |
| 150 | + `abcdefghijklmnopqrstuvwxyz{|}~ |
| 151 | + """, |
| 152 | + UInt8(0xA0) ... UInt8(0xFF): |
| 153 | + """ |
| 154 | + \u{A0}¡¢£¤¥¦§¨©ª«¬\u{AD}®¯°±²³´µ¶·¸¹º»¼½¾¿\ |
| 155 | + ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞß\ |
| 156 | + àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ |
| 157 | + """, |
| 158 | + ] |
| 159 | + for (codeUnits, expectedText) in codeUnitsAndText { |
| 160 | + let actualText = String(decoding: codeUnits, as: Unicode.Latin1.self) |
| 161 | + expectEqual(expectedText, actualText) |
| 162 | + } |
| 163 | + for (expectedCodeUnits, text) in codeUnitsAndText { |
| 164 | + var actualCodeUnits: [UInt8] = [] |
| 165 | + let hadError = transcode( |
| 166 | + text.utf8.makeIterator(), |
| 167 | + from: Unicode.UTF8.self, |
| 168 | + to: Unicode.Latin1.self, |
| 169 | + stoppingOnError: false, |
| 170 | + into: { actualCodeUnits.append($0) } |
| 171 | + ) |
| 172 | + expectEqualSequence(expectedCodeUnits, actualCodeUnits) |
| 173 | + expectFalse(hadError) |
| 174 | + } |
| 175 | + do { |
| 176 | + var actualCodeUnits: [UInt8] = [] |
| 177 | + let hadError = transcode( |
| 178 | + "A\u{0100}B\u{10FFFF}C".utf8.makeIterator(), |
| 179 | + from: Unicode.UTF8.self, |
| 180 | + to: Unicode.Latin1.self, |
| 181 | + stoppingOnError: false, |
| 182 | + into: { actualCodeUnits.append($0) } |
| 183 | + ) |
| 184 | + expectEqualSequence([0x41, 0x1A, 0x42, 0x1A, 0x43], actualCodeUnits) |
| 185 | + expectFalse(hadError) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
113 | 190 | runAllTests() |
0 commit comments