@@ -65,6 +65,122 @@ pub unsafe fn test_fputs_1() {
6565 Err ( _) => -1 ,
6666 } ;
6767}
68+ pub unsafe fn test_puts_2 ( ) {
69+ {
70+ let bytes = std:: ffi:: CStr :: from_ptr (
71+ ( b"puts hello\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8
72+ )
73+ . to_bytes ( ) ;
74+ let stdout = libcc2rs:: cout_unsafe ( ) ;
75+ let r1 = ( * stdout) . write_all ( bytes) ;
76+ let r2 = ( * stdout) . write_all ( b"\n " ) ;
77+ if r1. is_ok ( ) && r2. is_ok ( ) {
78+ 0
79+ } else {
80+ -1
81+ }
82+ } ;
83+ let mut s: * const u8 = ( b"puts variable\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) ;
84+ {
85+ let bytes = std:: ffi:: CStr :: from_ptr ( s as * const i8 ) . to_bytes ( ) ;
86+ let stdout = libcc2rs:: cout_unsafe ( ) ;
87+ let r1 = ( * stdout) . write_all ( bytes) ;
88+ let r2 = ( * stdout) . write_all ( b"\n " ) ;
89+ if r1. is_ok ( ) && r2. is_ok ( ) {
90+ 0
91+ } else {
92+ -1
93+ }
94+ } ;
95+ }
96+ pub unsafe fn test_fileno_3 ( ) {
97+ assert ! (
98+ ( ( ( ( if libcc2rs:: cin_unsafe( ) == libcc2rs:: cin_unsafe( ) {
99+ 0
100+ } else if libcc2rs:: cin_unsafe( ) == libcc2rs:: cout_unsafe( ) {
101+ 1
102+ } else if libcc2rs:: cin_unsafe( ) == libcc2rs:: cerr_unsafe( ) {
103+ 2
104+ } else {
105+ :: std:: os:: fd:: AsRawFd :: as_raw_fd( & * libcc2rs:: cin_unsafe( ) )
106+ } ) == ( 0 ) ) as i32 )
107+ != 0 )
108+ ) ;
109+ assert ! (
110+ ( ( ( ( if libcc2rs:: cout_unsafe( ) == libcc2rs:: cin_unsafe( ) {
111+ 0
112+ } else if libcc2rs:: cout_unsafe( ) == libcc2rs:: cout_unsafe( ) {
113+ 1
114+ } else if libcc2rs:: cout_unsafe( ) == libcc2rs:: cerr_unsafe( ) {
115+ 2
116+ } else {
117+ :: std:: os:: fd:: AsRawFd :: as_raw_fd( & * libcc2rs:: cout_unsafe( ) )
118+ } ) == ( 1 ) ) as i32 )
119+ != 0 )
120+ ) ;
121+ assert ! (
122+ ( ( ( ( if libcc2rs:: cerr_unsafe( ) == libcc2rs:: cin_unsafe( ) {
123+ 0
124+ } else if libcc2rs:: cerr_unsafe( ) == libcc2rs:: cout_unsafe( ) {
125+ 1
126+ } else if libcc2rs:: cerr_unsafe( ) == libcc2rs:: cerr_unsafe( ) {
127+ 2
128+ } else {
129+ :: std:: os:: fd:: AsRawFd :: as_raw_fd( & * libcc2rs:: cerr_unsafe( ) )
130+ } ) == ( 2 ) ) as i32 )
131+ != 0 )
132+ ) ;
133+ let mut fp: * mut :: std:: fs:: File =
134+ match std:: ffi:: CStr :: from_ptr ( ( b"wb\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8 )
135+ . to_str ( )
136+ . expect ( "invalid c-string" )
137+ {
138+ v if v == "rb" => std:: fs:: OpenOptions :: new ( )
139+ . read ( true )
140+ . open (
141+ std:: ffi:: CStr :: from_ptr (
142+ ( b"/tmp/cpp2rust_fileno_test.tmp\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( )
143+ as * const i8 ,
144+ )
145+ . to_str ( )
146+ . expect ( "invalid c-string" ) ,
147+ )
148+ . ok ( )
149+ . map_or ( std:: ptr:: null_mut ( ) , |f| Box :: into_raw ( Box :: new ( f) ) ) ,
150+ v if v == "wb" => std:: fs:: OpenOptions :: new ( )
151+ . write ( true )
152+ . create ( true )
153+ . truncate ( true )
154+ . open (
155+ std:: ffi:: CStr :: from_ptr (
156+ ( b"/tmp/cpp2rust_fileno_test.tmp\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( )
157+ as * const i8 ,
158+ )
159+ . to_str ( )
160+ . expect ( "invalid c-string" ) ,
161+ )
162+ . ok ( )
163+ . map_or ( std:: ptr:: null_mut ( ) , |f| Box :: into_raw ( Box :: new ( f) ) ) ,
164+ _ => panic ! ( "unsupported mode" ) ,
165+ } ;
166+ assert ! ( ( ( ( !( ( fp) . is_null( ) ) ) as i32 ) != 0 ) ) ;
167+ assert ! (
168+ ( ( ( ( if fp == libcc2rs:: cin_unsafe( ) {
169+ 0
170+ } else if fp == libcc2rs:: cout_unsafe( ) {
171+ 1
172+ } else if fp == libcc2rs:: cerr_unsafe( ) {
173+ 2
174+ } else {
175+ :: std:: os:: fd:: AsRawFd :: as_raw_fd( & * fp)
176+ } ) > ( 2 ) ) as i32 )
177+ != 0 )
178+ ) ;
179+ {
180+ Box :: from_raw ( fp) ;
181+ 0
182+ } ;
183+ }
68184pub fn main ( ) {
69185 unsafe {
70186 std:: process:: exit ( main_0 ( ) as i32 ) ;
@@ -73,5 +189,7 @@ pub fn main() {
73189unsafe fn main_0 ( ) -> i32 {
74190 ( unsafe { test_fputc_0 ( ) } ) ;
75191 ( unsafe { test_fputs_1 ( ) } ) ;
192+ ( unsafe { test_puts_2 ( ) } ) ;
193+ ( unsafe { test_fileno_3 ( ) } ) ;
76194 return 0 ;
77195}
0 commit comments