]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/str.rs
std: add test for str::as_c_str
[rust.git] / src / libstd / str.rs
index ab3d362ba792a5d7f5cc79da6bec5dfc7c51e599..5bd084b8796e5bf3ba015497b1a273ecb82dfc4d 100644 (file)
@@ -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";