11#include "fbcu.bi"
2+ #include "file.bi"
23
34namespace fbc_tests.file_.get_
45
@@ -23,6 +24,8 @@ private sub test cdecl( )
2324 dim s as string
2425 dim z6 as zstring * 6
2526 dim w6 as wstring * 6
27+ dim pz6 as zstring ptr = callocate( 6 * sizeof( zstring ) )
28+ dim pw6 as wstring ptr = callocate( 6 * sizeof( wstring ) )
2629 dim array4b( 0 to 3 ) as byte
2730 dim array4l( 0 to 3 ) as long
2831
@@ -54,7 +57,41 @@ private sub test cdecl( )
5457 CU_ASSERT( z6 = "12345" )
5558
5659 CU_ASSERT( get ( #f, 1 , w6 ) = 0 )
57- CU_ASSERT( w6 = wstr( "12345" ) )
60+ # if sizeof(wstring) = 2
61+ CU_ASSERT( w6 [0 ] = cvshort( "12" ) )
62+ CU_ASSERT( w6 [1 ] = cvshort( "34" ) )
63+ CU_ASSERT( w6 [2 ] = cvshort( "56" ) )
64+ CU_ASSERT( w6 [3 ] = cvshort( "78" ) )
65+ CU_ASSERT( w6 [4 ] = cvshort( "90" ) )
66+ # elseif sizeof(wstring) = 4
67+ CU_ASSERT( w6 [0 ] = cvl( "1234" ) )
68+ CU_ASSERT( w6 [1 ] = cvl( "5678" ) )
69+ CU_ASSERT( w6 [2 ] = cvshort( "90" ) )
70+ CU_ASSERT( w6 [3 ] = 0 )
71+ CU_ASSERT( w6 [4 ] = 0 )
72+ # endif
73+ CU_ASSERT( w6 [5 ] = 0 )
74+
75+ *pz6 = "00000"
76+ CU_ASSERT( get ( #f, 1 , *pz6 ) = 0 )
77+ CU_ASSERT( *pz6 = "12345" )
78+
79+ *pw6 = "00000"
80+ CU_ASSERT( get ( #f, 1 , *pw6 ) = 0 )
81+ # if sizeof(wstring) = 2
82+ CU_ASSERT( (*pw6) [0 ] = cvshort( "12" ) )
83+ CU_ASSERT( (*pw6) [1 ] = cvshort( "34" ) )
84+ CU_ASSERT( (*pw6) [2 ] = cvshort( "56" ) )
85+ CU_ASSERT( (*pw6) [3 ] = cvshort( "78" ) )
86+ CU_ASSERT( (*pw6) [4 ] = cvshort( "90" ) )
87+ # elseif sizeof(wstring) = 4
88+ CU_ASSERT( (*pw6) [0 ] = cvl( "1234" ) )
89+ CU_ASSERT( (*pw6) [1 ] = cvl( "5678" ) )
90+ CU_ASSERT( (*pw6) [2 ] = cvshort( "90" ) )
91+ CU_ASSERT( (*pw6) [3 ] = 0 )
92+ CU_ASSERT( (*pw6) [4 ] = 0 )
93+ # endif
94+ CU_ASSERT( (*pw6) [5 ] = 0 )
5895
5996 CU_ASSERT( get ( #f, 1 , array4b() ) = 0 )
6097 CU_ASSERT( array4b( 0 ) = asc( "1" ) )
@@ -65,7 +102,7 @@ private sub test cdecl( )
65102 CU_ASSERT( get ( #f, 1 , array4l() ) = 0 )
66103 CU_ASSERT( array4l( 0 ) = cvl( "1234" ) )
67104 CU_ASSERT( array4l( 1 ) = cvl( "5678" ) )
68- CU_ASSERT( array4l( 2 ) = &h3039 ) '' 90
105+ CU_ASSERT( array4l( 2 ) = cvshort( "90" ) )
69106 CU_ASSERT( array4l( 3 ) = 0 )
70107
71108 ''
@@ -124,8 +161,46 @@ private sub test cdecl( )
124161
125162 bytesread = 0
126163 CU_ASSERT( get ( #f, 1 , w6, , bytesread ) = 0 )
127- CU_ASSERT( w6 = wstr( "12345" ) )
128- CU_ASSERT( bytesread = ( 5 * sizeof( wstring )) )
164+ # if sizeof(wstring) = 2
165+ CU_ASSERT( w6 [0 ] = cvshort( "12" ) )
166+ CU_ASSERT( w6 [1 ] = cvshort( "34" ) )
167+ CU_ASSERT( w6 [2 ] = cvshort( "56" ) )
168+ CU_ASSERT( w6 [3 ] = cvshort( "78" ) )
169+ CU_ASSERT( w6 [4 ] = cvshort( "90" ) )
170+ # elseif sizeof(wstring) = 4
171+ CU_ASSERT( w6 [0 ] = cvl( "1234" ) )
172+ CU_ASSERT( w6 [1 ] = cvl( "5678" ) )
173+ CU_ASSERT( w6 [2 ] = cvshort( "90" ) )
174+ CU_ASSERT( w6 [3 ] = 0 )
175+ CU_ASSERT( w6 [4 ] = 0 )
176+ # endif
177+ CU_ASSERT( w6 [5 ] = 0 )
178+ CU_ASSERT( bytesread = 10 )
179+
180+ bytesread = 0
181+ *pz6 = "00000"
182+ CU_ASSERT( get ( #f, 1 , *pz6, , bytesread ) = 0 )
183+ CU_ASSERT( *pz6 = "12345" )
184+ CU_ASSERT( bytesread = 5 )
185+
186+ bytesread = 0
187+ *pw6 = "00000"
188+ CU_ASSERT( get ( #f, 1 , *pw6, , bytesread ) = 0 )
189+ # if sizeof(wstring) = 2
190+ CU_ASSERT( (*pw6) [0 ] = cvshort( "12" ) )
191+ CU_ASSERT( (*pw6) [1 ] = cvshort( "34" ) )
192+ CU_ASSERT( (*pw6) [2 ] = cvshort( "56" ) )
193+ CU_ASSERT( (*pw6) [3 ] = cvshort( "78" ) )
194+ CU_ASSERT( (*pw6) [4 ] = cvshort( "90" ) )
195+ # elseif sizeof(wstring) = 4
196+ CU_ASSERT( (*pw6) [0 ] = cvl( "1234" ) )
197+ CU_ASSERT( (*pw6) [1 ] = cvl( "5678" ) )
198+ CU_ASSERT( (*pw6) [2 ] = cvshort( "90" ) )
199+ CU_ASSERT( (*pw6) [3 ] = 0 )
200+ CU_ASSERT( (*pw6) [4 ] = 0 )
201+ # endif
202+ CU_ASSERT( (*pw6) [5 ] = 0 )
203+ CU_ASSERT( bytesread = 10 )
129204
130205 bytesread = 0
131206 CU_ASSERT( get ( #f, 1 , array4b(), , bytesread ) = 0 )
@@ -139,7 +214,7 @@ private sub test cdecl( )
139214 CU_ASSERT( get ( #f, 1 , array4l(), , bytesread ) = 0 )
140215 CU_ASSERT( array4l( 0 ) = cvl( "1234" ) )
141216 CU_ASSERT( array4l( 1 ) = cvl( "5678" ) )
142- CU_ASSERT( array4l( 2 ) = &h3039 ) '' 90
217+ CU_ASSERT( array4l( 2 ) = cvshort( "90" ) )
143218 CU_ASSERT( array4l( 3 ) = 0 )
144219 CU_ASSERT( bytesread = 10 )
145220
@@ -193,7 +268,41 @@ private sub test cdecl( )
193268 CU_ASSERT( z6 = "12345" )
194269
195270 CU_ASSERT( get ( #f, 1l l, w6 ) = 0 )
196- CU_ASSERT( w6 = wstr( "12345" ) )
271+ # if sizeof(wstring) = 2
272+ CU_ASSERT( w6 [0 ] = cvshort( "12" ) )
273+ CU_ASSERT( w6 [1 ] = cvshort( "34" ) )
274+ CU_ASSERT( w6 [2 ] = cvshort( "56" ) )
275+ CU_ASSERT( w6 [3 ] = cvshort( "78" ) )
276+ CU_ASSERT( w6 [4 ] = cvshort( "90" ) )
277+ # elseif sizeof(wstring) = 4
278+ CU_ASSERT( w6 [0 ] = cvl( "1234" ) )
279+ CU_ASSERT( w6 [1 ] = cvl( "5678" ) )
280+ CU_ASSERT( w6 [2 ] = cvshort( "90" ) )
281+ CU_ASSERT( w6 [3 ] = 0 )
282+ CU_ASSERT( w6 [4 ] = 0 )
283+ # endif
284+ CU_ASSERT( w6 [5 ] = 0 )
285+
286+ *pz6 = "00000"
287+ CU_ASSERT( get ( #f, 1l l, *pz6 ) = 0 )
288+ CU_ASSERT( *pz6 = "12345" )
289+
290+ *pw6 = "00000"
291+ CU_ASSERT( get ( #f, 1l l, *pw6 ) = 0 )
292+ # if sizeof(wstring) = 2
293+ CU_ASSERT( (*pw6) [0 ] = cvshort( "12" ) )
294+ CU_ASSERT( (*pw6) [1 ] = cvshort( "34" ) )
295+ CU_ASSERT( (*pw6) [2 ] = cvshort( "56" ) )
296+ CU_ASSERT( (*pw6) [3 ] = cvshort( "78" ) )
297+ CU_ASSERT( (*pw6) [4 ] = cvshort( "90" ) )
298+ # elseif sizeof(wstring) = 4
299+ CU_ASSERT( (*pw6) [0 ] = cvl( "1234" ) )
300+ CU_ASSERT( (*pw6) [1 ] = cvl( "5678" ) )
301+ CU_ASSERT( (*pw6) [2 ] = cvshort( "90" ) )
302+ CU_ASSERT( (*pw6) [3 ] = 0 )
303+ CU_ASSERT( (*pw6) [4 ] = 0 )
304+ # endif
305+ CU_ASSERT( (*pw6) [5 ] = 0 )
197306
198307 CU_ASSERT( get ( #f, 1l l, array4b() ) = 0 )
199308 CU_ASSERT( array4b( 0 ) = asc( "1" ) )
@@ -204,7 +313,7 @@ private sub test cdecl( )
204313 CU_ASSERT( get ( #f, 1l l, array4l() ) = 0 )
205314 CU_ASSERT( array4l( 0 ) = cvl( "1234" ) )
206315 CU_ASSERT( array4l( 1 ) = cvl( "5678" ) )
207- CU_ASSERT( array4l( 2 ) = &h3039 ) '' 90
316+ CU_ASSERT( array4l( 2 ) = cvshort( "90" ) )
208317 CU_ASSERT( array4l( 3 ) = 0 )
209318
210319 ''
@@ -262,8 +371,46 @@ private sub test cdecl( )
262371
263372 bytesread = 0
264373 CU_ASSERT( get ( #f, 1l l, w6, , bytesread ) = 0 )
265- CU_ASSERT( w6 = wstr( "12345" ) )
266- CU_ASSERT( bytesread = ( 5 * sizeof( wstring )) )
374+ # if sizeof(wstring) = 2
375+ CU_ASSERT( w6 [0 ] = cvshort( "12" ) )
376+ CU_ASSERT( w6 [1 ] = cvshort( "34" ) )
377+ CU_ASSERT( w6 [2 ] = cvshort( "56" ) )
378+ CU_ASSERT( w6 [3 ] = cvshort( "78" ) )
379+ CU_ASSERT( w6 [4 ] = cvshort( "90" ) )
380+ # elseif sizeof(wstring) = 4
381+ CU_ASSERT( w6 [0 ] = cvl( "1234" ) )
382+ CU_ASSERT( w6 [1 ] = cvl( "5678" ) )
383+ CU_ASSERT( w6 [2 ] = cvshort( "90" ) )
384+ CU_ASSERT( w6 [3 ] = 0 )
385+ CU_ASSERT( w6 [4 ] = 0 )
386+ # endif
387+ CU_ASSERT( w6 [5 ] = 0 )
388+ CU_ASSERT( bytesread = 10 )
389+
390+ bytesread = 0
391+ *pz6 = "00000"
392+ CU_ASSERT( get ( #f, 1l l, *pz6, , bytesread ) = 0 )
393+ CU_ASSERT( *pz6 = "12345" )
394+ CU_ASSERT( bytesread = 5 )
395+
396+ bytesread = 0
397+ *pw6 = "00000"
398+ CU_ASSERT( get ( #f, 1l l, *pw6, , bytesread ) = 0 )
399+ # if sizeof(wstring) = 2
400+ CU_ASSERT( (*pw6) [0 ] = cvshort( "12" ) )
401+ CU_ASSERT( (*pw6) [1 ] = cvshort( "34" ) )
402+ CU_ASSERT( (*pw6) [2 ] = cvshort( "56" ) )
403+ CU_ASSERT( (*pw6) [3 ] = cvshort( "78" ) )
404+ CU_ASSERT( (*pw6) [4 ] = cvshort( "90" ) )
405+ # elseif sizeof(wstring) = 4
406+ CU_ASSERT( (*pw6) [0 ] = cvl( "1234" ) )
407+ CU_ASSERT( (*pw6) [1 ] = cvl( "5678" ) )
408+ CU_ASSERT( (*pw6) [2 ] = cvshort( "90" ) )
409+ CU_ASSERT( (*pw6) [3 ] = 0 )
410+ CU_ASSERT( (*pw6) [4 ] = 0 )
411+ # endif
412+ CU_ASSERT( (*pw6) [5 ] = 0 )
413+ CU_ASSERT( bytesread = 10 )
267414
268415 bytesread = 0
269416 CU_ASSERT( get ( #f, 1l l, array4b(), , bytesread ) = 0 )
@@ -277,16 +424,87 @@ private sub test cdecl( )
277424 CU_ASSERT( get ( #f, 1l l, array4l(), , bytesread ) = 0 )
278425 CU_ASSERT( array4l( 0 ) = cvl( "1234" ) )
279426 CU_ASSERT( array4l( 1 ) = cvl( "5678" ) )
280- CU_ASSERT( array4l( 2 ) = &h3039 ) '' 90
427+ CU_ASSERT( array4l( 2 ) = cvshort( "90" ) )
281428 CU_ASSERT( array4l( 3 ) = 0 )
282429 CU_ASSERT( bytesread = 10 )
283430
431+ deallocate( pw6 )
432+ deallocate( pz6 )
284433 close #f
285434end sub
286435
436+ sub testGetWstrFill cdecl( )
437+ for n as integer = 0 to sizeof(wstring) * 2
438+ const TESTFILE = "data.tmp"
439+
440+ '' Create test file with N bytes
441+ scope
442+ if ( kill( TESTFILE ) ) then
443+ end if
444+ var f = freefile( )
445+ if ( open( TESTFILE, for binary, access write, as #f ) <> 0 ) then
446+ CU_FAIL( "could not create file " & TESTFILE )
447+ end if
448+ for i as integer = 0 to n - 1
449+ var b = cubyte( asc( "a" ) )
450+ put #f, , b
451+ next
452+ close #f
453+ CU_ASSERT( filelen( TESTFILE ) = n )
454+ end scope
455+
456+ scope
457+ var f = freefile( )
458+ if ( open( TESTFILE, for binary, access read, as #f ) <> 0 ) then
459+ CU_FAIL( "could not open file " & TESTFILE )
460+ end if
461+
462+ dim w as wstring * 10
463+ '' Fill all bytes in the wstring buffer with '?'
464+ for i as integer = 0 to sizeof(w) - 1
465+ cptr( ubyte ptr, @w )[i] = asc( "?" )
466+ next
467+
468+ '' Try a Get# wstring. It should read the 'a' bytes from
469+ '' the file, fill the last wchar's remaining bytes with
470+ '' zeroes if needed to round up to sizeof(wstring),
471+ '' and add a null terminator.
472+ dim bytesread as integer
473+ CU_ASSERT( get ( #f, , w, , bytesread ) = 0 )
474+ CU_ASSERT( bytesread = n )
475+
476+ '' Check whether it worked
477+
478+ '' 'a' bytes as read from file
479+ for i as integer = 0 to n - 1
480+ CU_ASSERT( cptr( ubyte ptr, @w )[i] = asc( "a" ) )
481+ next
482+
483+ '' zero bytes padding, if needed
484+ dim as integer extra = bytesread mod sizeof( wstring )
485+ if extra > 0 then
486+ bytesread += sizeof( wstring ) - extra '' round up
487+ for i as integer = n to bytesread - 1
488+ CU_ASSERT( cptr( ubyte ptr, @w )[i] = 0 )
489+ next
490+ end if
491+
492+ '' null terminator
493+ CU_ASSERT( (bytesread mod sizeof( wstring )) = 0 )
494+ CU_ASSERT( w [bytesread \ sizeof( wstring ) ] = 0 )
495+
496+ close #f
497+ end scope
498+
499+ '' Delete test file
500+ CU_ASSERT( kill( TESTFILE ) = 0 )
501+ next
502+ end sub
503+
287504private sub ctor( ) constructor
288505 fbcu.add_suite( "tests/file/get" )
289506 fbcu.add_test( "test" , @test )
507+ fbcu.add_test( "testGetWstrFill" , @testGetWstrFill )
290508end sub
291509
292510end namespace
0 commit comments