11//! Compare libc's makedev, major, minor functions against the actual C macros, for various
22//! inputs.
33
4- #[ cfg( any( target_os = "solaris" , target_os = "illumos" ) ) ]
5- mod ret {
6- pub type MajorRetType = libc:: major_t ;
7- pub type MinorRetType = libc:: minor_t ;
8- }
9-
10- #[ cfg( any(
11- target_os = "linux" ,
12- target_os = "l4re" ,
13- target_os = "emscripten" ,
14- target_os = "fuchsia" ,
15- target_os = "aix" ,
16- target_os = "nto" ,
17- target_os = "hurd" ,
18- target_os = "openbsd" ,
19- target_os = "cygwin" ,
20- ) ) ]
21- mod ret {
22- pub type MajorRetType = libc:: c_uint ;
23- pub type MinorRetType = libc:: c_uint ;
24- }
25-
26- #[ cfg( any(
27- target_os = "android" ,
28- target_os = "dragonfly" ,
29- target_os = "netbsd" ,
30- target_os = "freebsd" ,
31- ) ) ]
32- mod ret {
33- pub type MajorRetType = libc:: c_int ;
34- pub type MinorRetType = libc:: c_int ;
35- }
36-
37- #[ cfg( any(
38- target_os = "macos" ,
39- target_os = "ios" ,
40- target_os = "tvos" ,
41- target_os = "watchos" ,
42- target_os = "visionos"
43- ) ) ]
44- mod ret {
45- pub type MajorRetType = i32 ;
46- pub type MinorRetType = i32 ;
47- }
48-
49- #[ cfg( any(
4+ #![ cfg( any(
505 target_os = "android" ,
516 target_os = "dragonfly" ,
527 target_os = "emscripten" ,
@@ -57,100 +12,134 @@ mod ret {
5712 target_os = "openbsd" ,
5813 target_os = "cygwin" ,
5914) ) ]
60- mod t {
61- use libc:: { self , c_uint, dev_t} ;
6215
63- use super :: ret :: * ;
16+ use libc :: { self , c_uint , dev_t } ;
6417
65- extern "C" {
66- pub fn makedev_ffi ( major : c_uint , minor : c_uint ) -> dev_t ;
67- pub fn major_ffi ( dev : dev_t ) -> c_uint ;
68- pub fn minor_ffi ( dev : dev_t ) -> c_uint ;
18+ cfg_if:: cfg_if! {
19+ if #[ cfg( any( target_os = "solaris" , target_os = "illumos" ) ) ] {
20+ pub type MajorRetType = libc:: major_t;
21+ pub type MinorRetType = libc:: minor_t;
22+ } else if #[ cfg( any(
23+ target_os = "linux" ,
24+ target_os = "l4re" ,
25+ target_os = "emscripten" ,
26+ target_os = "fuchsia" ,
27+ target_os = "aix" ,
28+ target_os = "nto" ,
29+ target_os = "hurd" ,
30+ target_os = "openbsd" ,
31+ target_os = "cygwin" ,
32+ ) ) ] {
33+ pub type MajorRetType = libc:: c_uint;
34+ pub type MinorRetType = libc:: c_uint;
35+ } else if #[ cfg( any(
36+ target_os = "android" ,
37+ target_os = "dragonfly" ,
38+ target_os = "netbsd" ,
39+ target_os = "freebsd" ,
40+ ) ) ] {
41+ pub type MajorRetType = libc:: c_int;
42+ pub type MinorRetType = libc:: c_int;
43+ } else if #[ cfg( any(
44+ target_os = "macos" ,
45+ target_os = "ios" ,
46+ target_os = "tvos" ,
47+ target_os = "watchos" ,
48+ target_os = "visionos"
49+ ) ) ] {
50+ pub type MajorRetType = i32 ;
51+ pub type MinorRetType = i32 ;
6952 }
53+ }
7054
71- fn compare ( major : c_uint , minor : c_uint ) {
72- let dev = unsafe { makedev_ffi ( major, minor) } ;
73- assert_eq ! ( libc:: makedev( major, minor) , dev) ;
74- let major = unsafe { major_ffi ( dev) } ;
75- assert_eq ! ( libc:: major( dev) , major as MajorRetType ) ;
76- let minor = unsafe { minor_ffi ( dev) } ;
77- assert_eq ! ( libc:: minor( dev) , minor as MinorRetType ) ;
78- }
55+ extern "C" {
56+ pub fn makedev_ffi ( major : c_uint , minor : c_uint ) -> dev_t ;
57+ pub fn major_ffi ( dev : dev_t ) -> c_uint ;
58+ pub fn minor_ffi ( dev : dev_t ) -> c_uint ;
59+ }
7960
80- // Every OS should be able to handle 8 bit major and minor numbers
81- #[ test]
82- fn test_8bits ( ) {
83- for major in 0 ..256 {
84- for minor in 0 ..256 {
85- compare ( major, minor) ;
86- }
61+ fn compare ( major : c_uint , minor : c_uint ) {
62+ let dev = unsafe { makedev_ffi ( major, minor) } ;
63+ assert_eq ! ( libc:: makedev( major, minor) , dev) ;
64+ let major = unsafe { major_ffi ( dev) } ;
65+ assert_eq ! ( libc:: major( dev) , major as MajorRetType ) ;
66+ let minor = unsafe { minor_ffi ( dev) } ;
67+ assert_eq ! ( libc:: minor( dev) , minor as MinorRetType ) ;
68+ }
69+
70+ // Every OS should be able to handle 8 bit major and minor numbers
71+ #[ test]
72+ fn test_8bits ( ) {
73+ for major in 0 ..256 {
74+ for minor in 0 ..256 {
75+ compare ( major, minor) ;
8776 }
8877 }
78+ }
8979
90- // Android allows 12 bits for major and 20 for minor
91- #[ test]
92- #[ cfg( target_os = "android" ) ]
93- fn test_android_like ( ) {
94- for major in [ 0 , 1 , 255 , 256 , 4095 ] {
95- for minor_exp in [ 1 , 8 , 16 ] {
96- for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
97- compare ( major, minor) ;
98- }
80+ // Android allows 12 bits for major and 20 for minor
81+ #[ test]
82+ #[ cfg( target_os = "android" ) ]
83+ fn test_android_like ( ) {
84+ for major in [ 0 , 1 , 255 , 256 , 4095 ] {
85+ for minor_exp in [ 1 , 8 , 16 ] {
86+ for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
87+ compare ( major, minor) ;
9988 }
100- compare ( major, ( 1 << 20 ) - 1 ) ;
10189 }
90+ compare ( major, ( 1 << 20 ) - 1 ) ;
10291 }
92+ }
10393
104- // These OSes allow 32 bits for minor, but only 8 for major
105- #[ test]
106- #[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" , target_os = "netbsd" , ) ) ]
107- fn test_fbsd11_like ( ) {
108- for major in [ 0 , 1 , 255 ] {
109- for minor_exp in [ 1 , 8 , 16 , 24 , 31 ] {
110- for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
111- compare ( major, minor) ;
112- }
94+ // These OSes allow 32 bits for minor, but only 8 for major
95+ #[ test]
96+ #[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" , target_os = "netbsd" , ) ) ]
97+ fn test_fbsd11_like ( ) {
98+ for major in [ 0 , 1 , 255 ] {
99+ for minor_exp in [ 1 , 8 , 16 , 24 , 31 ] {
100+ for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
101+ compare ( major, minor) ;
113102 }
114- compare ( major, c_uint:: MAX ) ;
115103 }
104+ compare ( major, c_uint:: MAX ) ;
116105 }
106+ }
117107
118- // OpenBSD allows 8 bits for major and 24 for minor
119- #[ test]
120- #[ cfg( target_os = "openbsd" ) ]
121- fn test_openbsd_like ( ) {
122- for major in [ 0 , 1 , 255 ] {
123- for minor_exp in [ 1 , 8 , 16 ] {
124- for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
125- compare ( major, minor) ;
126- }
108+ // OpenBSD allows 8 bits for major and 24 for minor
109+ #[ test]
110+ #[ cfg( target_os = "openbsd" ) ]
111+ fn test_openbsd_like ( ) {
112+ for major in [ 0 , 1 , 255 ] {
113+ for minor_exp in [ 1 , 8 , 16 ] {
114+ for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
115+ compare ( major, minor) ;
127116 }
128- compare ( major, ( 1 << 24 ) - 1 ) ;
129117 }
118+ compare ( major, ( 1 << 24 ) - 1 ) ;
130119 }
120+ }
131121
132- // These OSes allow 32 bits for both minor and major
133- #[ cfg( any(
134- target_os = "emscripten" ,
135- target_os = "freebsd" ,
136- target_os = "fuchsia" ,
137- target_os = "linux" ,
138- target_os = "cygwin" ,
139- ) ) ]
140- #[ test]
141- fn test_fbsd12_like ( ) {
142- if std:: mem:: size_of :: < dev_t > ( ) >= 8 {
143- for major_exp in [ 0 , 16 , 24 , 31 ] {
144- for major in [ ( 1 << major_exp) - 1 , ( 1 << major_exp) ] {
145- for minor_exp in [ 1 , 8 , 16 , 24 , 31 ] {
146- for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
147- compare ( major, minor) ;
148- }
122+ // These OSes allow 32 bits for both minor and major
123+ #[ cfg( any(
124+ target_os = "emscripten" ,
125+ target_os = "freebsd" ,
126+ target_os = "fuchsia" ,
127+ target_os = "linux" ,
128+ target_os = "cygwin" ,
129+ ) ) ]
130+ #[ test]
131+ fn test_fbsd12_like ( ) {
132+ if std:: mem:: size_of :: < dev_t > ( ) >= 8 {
133+ for major_exp in [ 0 , 16 , 24 , 31 ] {
134+ for major in [ ( 1 << major_exp) - 1 , ( 1 << major_exp) ] {
135+ for minor_exp in [ 1 , 8 , 16 , 24 , 31 ] {
136+ for minor in [ ( 1 << minor_exp) - 1 , ( 1 << minor_exp) ] {
137+ compare ( major, minor) ;
149138 }
150- compare ( major, c_uint:: MAX ) ;
151139 }
152- compare ( c_uint :: MAX , c_uint:: MAX ) ;
140+ compare ( major , c_uint:: MAX ) ;
153141 }
142+ compare ( c_uint:: MAX , c_uint:: MAX ) ;
154143 }
155144 }
156145}
0 commit comments