]> git.lizzy.rs Git - rust.git/commitdiff
CStr::from_bytes_with_nul tests
authorarcnmx <arcnmx@users.noreply.github.com>
Tue, 23 Feb 2016 06:37:21 +0000 (01:37 -0500)
committerarcnmx <arcnmx@users.noreply.github.com>
Tue, 23 Feb 2016 16:52:19 +0000 (11:52 -0500)
src/libstd/ffi/c_str.rs

index f3561622fa377ad38443343e5e4aee674c54424c..1db45764552392a494217ee3b9d010527b834e58 100644 (file)
@@ -721,4 +721,31 @@ fn equal_hash() {
 
         assert_eq!(cstr_hash, cstring_hash);
     }
+
+    #[test]
+    fn from_bytes_with_nul() {
+        let data = b"123\0";
+        let cstr = CStr::from_bytes_with_nul(data);
+        assert_eq!(cstr.map(CStr::to_bytes), Some(&b"123"[..]));
+        assert_eq!(cstr.map(CStr::to_bytes_with_nul), Some(&b"123\0"[..]));
+
+        unsafe {
+            let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data);
+            assert_eq!(cstr, Some(cstr_unchecked));
+        }
+    }
+
+    #[test]
+    fn from_bytes_with_nul_unterminated() {
+        let data = b"123";
+        let cstr = CStr::from_bytes_with_nul(data);
+        assert!(cstr.is_none());
+    }
+
+    #[test]
+    fn from_bytes_with_nul_interior() {
+        let data = b"1\023\0";
+        let cstr = CStr::from_bytes_with_nul(data);
+        assert!(cstr.is_none());
+    }
 }