@@ -15,10 +15,10 @@ fn test_read_write() {
15
15
let fd = unsafe { libc:: eventfd ( 0 , flags) } ;
16
16
let sized_8_data: [ u8 ; 8 ] ;
17
17
if cfg ! ( target_endian = "big" ) {
18
- // Adjust the data based on the endianess of host system.
19
- sized_8_data = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
18
+ // Adjust the data based on the endianness of host system.
19
+ sized_8_data = 1_i64 . to_be_bytes ( ) ;
20
20
} else {
21
- sized_8_data = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
21
+ sized_8_data = 1_i64 . to_le_bytes ( ) ;
22
22
}
23
23
// Write 1 to the counter.
24
24
let res: i64 = unsafe {
@@ -33,6 +33,7 @@ fn test_read_write() {
33
33
} ;
34
34
// Read returns number of bytes has been read, which is always 8.
35
35
assert_eq ! ( res, 8 ) ;
36
+ // Check the value of counter read.
36
37
let counter: u64 ;
37
38
if cfg ! ( target_endian = "big" ) {
38
39
// Read will store the bytes based on the endianess of the host system.
@@ -42,47 +43,43 @@ fn test_read_write() {
42
43
}
43
44
assert_eq ! ( counter, 1 ) ;
44
45
45
- // Read when counter == 0 should fail.
46
+ // After read, the counter is currently 0, read counter 0 should fail with return
47
+ // value -1.
46
48
let mut buf: [ u8 ; 8 ] = [ 0 ; 8 ] ;
47
49
let res: i32 = unsafe {
48
50
libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
49
51
} ;
50
- // Read returns number of bytes has been read, which is always 8.
51
52
assert_eq ! ( res, -1 ) ;
52
53
53
- // Write with buffer size > 8 bytes.
54
+ // Write with supplied buffer that > 8 bytes should be allowed.
55
+ let sized_9_data: [ u8 ; 9 ] ;
56
+ if cfg ! ( target_endian = "big" ) {
57
+ // Adjust the data based on the endianess of host system.
58
+ sized_9_data = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
59
+ } else {
60
+ sized_9_data = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
61
+ }
54
62
let res: i64 = unsafe {
55
- libc:: write ( fd, sized_8_data . as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
63
+ libc:: write ( fd, sized_9_data . as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
56
64
} ;
57
65
assert_eq ! ( res, 8 ) ;
58
66
59
- // Read with supplied buffer that < 8 bytes.
67
+ // Read with supplied buffer that < 8 bytes should fail with return
68
+ // value -1.
60
69
let mut buf: [ u8 ; 7 ] = [ 1 ; 7 ] ;
61
70
let res: i32 = unsafe {
62
71
libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
63
72
} ;
64
73
assert_eq ! ( res, -1 ) ;
65
74
66
- // Write with supplied buffer that < 8 bytes
75
+ // Write with supplied buffer that < 8 bytes should fail with return
76
+ // value -1.
67
77
let res: i64 = unsafe {
68
78
libc:: write ( fd, sized_8_data[ 0 ..7 ] . as_ptr ( ) as * const libc:: c_void , 7 ) . try_into ( ) . unwrap ( )
69
79
} ;
70
80
assert_eq ! ( res, -1 ) ;
71
81
72
- // Write with supplied buffer that > 8 bytes
73
- let sized_9_data: [ u8 ; 9 ] ;
74
- if cfg ! ( target_endian = "big" ) {
75
- // Adjust the data based on the endianess of host system.
76
- sized_9_data = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] ;
77
- } else {
78
- sized_9_data = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
79
- }
80
- let res: i64 = unsafe {
81
- libc:: write ( fd, sized_9_data. as_ptr ( ) as * const libc:: c_void , 8 ) . try_into ( ) . unwrap ( )
82
- } ;
83
- assert_eq ! ( res, 8 ) ;
84
-
85
- // Read with supplied buffer > 8 bytes.
82
+ // Read with supplied buffer > 8 bytes should be allowed.
86
83
let mut buf: [ u8 ; 9 ] = [ 1 ; 9 ] ;
87
84
let res: i32 = unsafe {
88
85
libc:: read ( fd, buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) as libc:: size_t ) . try_into ( ) . unwrap ( )
0 commit comments