diff options
| author | bors <bors@rust-lang.org> | 2016-09-29 11:20:02 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-09-29 11:20:02 -0700 |
| commit | 289f3a4ca79916d6445b452fc19a18a1e42a879a (patch) | |
| tree | ff6191e3e2f803fa54875567235f301ed5a762bf /src/libcoretest | |
| parent | ff67da63ea4ca9b19e1e8ee97de002a64a2a0473 (diff) | |
| parent | 13a2dd96fe824cc5d61e94ed380db0114efdd014 (diff) | |
| download | rust-289f3a4ca79916d6445b452fc19a18a1e42a879a.tar.gz rust-289f3a4ca79916d6445b452fc19a18a1e42a879a.zip | |
Auto merge of #36377 - tormol:encode_utf, r=alexcrichton
Change encode_utf{8,16}() to write to a buffer and panic if it's too small
cc #27784
Should the "A buffer that's too small" examples be removed and replaced by tests?
Diffstat (limited to 'src/libcoretest')
| -rw-r--r-- | src/libcoretest/char.rs | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/libcoretest/char.rs b/src/libcoretest/char.rs index 199437a431e..7da0b6902f2 100644 --- a/src/libcoretest/char.rs +++ b/src/libcoretest/char.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::char; +use std::{char,str}; use std::convert::TryFrom; #[test] @@ -248,10 +248,12 @@ fn test_escape_unicode() { #[test] fn test_encode_utf8() { fn check(input: char, expect: &[u8]) { - assert_eq!(input.encode_utf8().as_slice(), expect); - for (a, b) in input.encode_utf8().zip(expect) { - assert_eq!(a, *b); - } + let mut buf = [0; 4]; + let ptr = buf.as_ptr(); + let s = input.encode_utf8(&mut buf); + assert_eq!(s.as_ptr() as usize, ptr as usize); + assert!(str::from_utf8(s.as_bytes()).is_ok()); + assert_eq!(s.as_bytes(), expect); } check('x', &[0x78]); @@ -263,10 +265,11 @@ fn test_encode_utf8() { #[test] fn test_encode_utf16() { fn check(input: char, expect: &[u16]) { - assert_eq!(input.encode_utf16().as_slice(), expect); - for (a, b) in input.encode_utf16().zip(expect) { - assert_eq!(a, *b); - } + let mut buf = [0; 2]; + let ptr = buf.as_mut_ptr(); + let b = input.encode_utf16(&mut buf); + assert_eq!(b.as_mut_ptr() as usize, ptr as usize); + assert_eq!(b, expect); } check('x', &[0x0078]); |
