@@ -114,6 +114,43 @@ fn test_from_utf8_lossy() { |
|
|
114 |
114 |
); |
115 |
115 |
} |
116 |
116 |
|
|
117 |
+#[test] |
|
118 |
+fn test_fromutf8error_into_lossy() { |
|
119 |
+fn func(input: &[u8]) -> String { |
|
120 |
+String::from_utf8(input.to_owned()).unwrap_or_else(|e |
|
121 |
+} |
|
122 |
+ |
|
123 |
+let xs = b"hello"; |
|
124 |
+let ys = "hello".to_owned(); |
|
125 |
+assert_eq!(func(xs), ys); |
|
126 |
+ |
|
127 |
+let xs = "ศไทย中华Việt Nam".as_bytes(); |
|
128 |
+let ys = "ศไทย中华Việt Nam".to_owned(); |
|
129 |
+assert_eq!(func(xs), ys); |
|
130 |
+ |
|
131 |
+let xs = b"Hello\xC2 There\xFF Goodbye"; |
|
132 |
+assert_eq!(func(xs), "Hello\u{FFFD} There\u{FFFD} Goodbye".to_owned()); |
|
133 |
+ |
|
134 |
+let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye"; |
|
135 |
+assert_eq!(func(xs), "Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye".to_owned()); |
|
136 |
+ |
|
137 |
+let xs = b"\xF5foo\xF5\x80bar"; |
|
138 |
+assert_eq!(func(xs), "\u{FFFD}foo\u{FFFD}\u{FFFD}bar".to_owned()); |
|
139 |
+ |
|
140 |
+let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz"; |
|
141 |
+assert_eq!(func(xs), "\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz".to_owned()); |
|
142 |
+ |
|
143 |
+let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz"; |
|
144 |
+assert_eq!(func(xs), "\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz".to_owned()); |
|
145 |
+ |
|
146 |
+let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar"; |
|
147 |
+assert_eq!(func(xs), "\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}foo\u{10000}bar".to_owned()); |
|
148 |
+ |
|
149 |
+// surrogates |
|
150 |
+let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar"; |
|
151 |
+assert_eq!(func(xs), "\u{FFFD}\u{FFFD}\u{FFFD}foo\u{FFFD}\u{FFFD}\u{FFFD}bar".to_owned()); |
|
152 |
+} |
|
153 |
+ |
117 |
154 |
#[test] |
118 |
155 |
fn test_from_utf16() { |
119 |
156 |
let pairs = [ |