Skip to content

Commit 134d6a2

Browse files
committed
Add tests, improve test coverage
1 parent f9dc942 commit 134d6a2

6 files changed

+78
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
unsafe {
9+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
10+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
11+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
12+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
13+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
14+
libc::pthread_mutex_destroy(&mut mutex as *mut _); //~ ERROR destroyed a locked mutex
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
unsafe {
9+
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
10+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
11+
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
12+
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
13+
assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0);
14+
assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0);
15+
libc::pthread_mutex_unlock(&mut mutex as *mut _); //~ ERROR was not locked
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0);
11+
libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0);
11+
libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ignore-windows: No libc on Windows
2+
3+
#![feature(rustc_private)]
4+
5+
extern crate libc;
6+
7+
fn main() {
8+
let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER);
9+
unsafe {
10+
libc::pthread_rwlock_unlock(rw.get()); //~ ERROR was not locked
11+
}
12+
}

tests/run-pass/libc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn tmp() -> PathBuf {
1515
#[cfg(not(target_os = "macos"))]
1616
fn test_posix_fadvise() {
1717
use std::convert::TryInto;
18-
use std::fs::{File, remove_file};
18+
use std::fs::{remove_file, File};
1919
use std::io::Write;
2020
use std::os::unix::io::AsRawFd;
2121

@@ -66,6 +66,7 @@ fn test_mutex_libc_init_recursive() {
6666
fn test_mutex_libc_init_normal() {
6767
unsafe {
6868
let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed();
69+
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678), libc::EINVAL);
6970
assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0);
7071
let mut mutex: libc::pthread_mutex_t = std::mem::zeroed();
7172
assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0);
@@ -133,6 +134,11 @@ fn test_rwlock_libc_static_initializer() {
133134
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
134135
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
135136

137+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0);
138+
assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY);
139+
assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY);
140+
assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0);
141+
136142
assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0);
137143
}
138144
}

0 commit comments

Comments
 (0)