From d5110854f76eac307e7a1729f3003e09d0277b60 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 30 Jun 2013 08:29:38 -0700 Subject: [PATCH] std: add test for str::as_c_str --- src/libstd/str.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 2a24351fff4e..e520c4d24766 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -3032,6 +3032,29 @@ mod tests { assert_eq!(s.to_c_str(), v); } + #[test] + fn test_as_c_str() { + let a = ~""; + do a.as_c_str |buf| { + unsafe { + assert_eq!(*ptr::offset(buf, 0), 0); + } + } + + let a = ~"hello"; + do a.as_c_str |buf| { + unsafe { + assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char); + assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char); + assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char); + assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char); + assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char); + assert_eq!(*ptr::offset(buf, 5), 0); + } + } + } + + #[test] fn test_subslice_offset() { let a = "kernelsprite"; let b = a.slice(7, a.len());