From cf75330807ad908a428e9c162a388e367fb07781 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 22 Jul 2013 21:42:51 -0700 Subject: [PATCH] std: add test for str::as_c_str --- src/librustpkg/util.rs | 2 +- src/libstd/run.rs | 1 - src/libstd/str.rs | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index dddf494856e..60367fd9aa6 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -380,7 +380,7 @@ pub fn link_exe(_src: &Path, _dest: &Path) -> bool { #[cfg(target_os = "freebsd")] #[cfg(target_os = "macos")] pub fn link_exe(src: &Path, dest: &Path) -> bool { - use std::{libc, str}; + use std::libc; unsafe { do src.to_str().as_c_str |src_buf| { do dest.to_str().as_c_str |dest_buf| { diff --git a/src/libstd/run.rs b/src/libstd/run.rs index d8fc68d6422..b6cc556af10 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -23,7 +23,6 @@ use os; use prelude::*; use ptr; -use str; use task; use vec::ImmutableVector; diff --git a/src/libstd/str.rs b/src/libstd/str.rs index ab3d362ba79..5bd084b8796 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -3116,6 +3116,28 @@ fn test_as_buf_3() { } } + #[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"; -- 2.44.0