@@ -342,6 +342,99 @@ func TestEnableTempFile(t *testing.T) {
342342 }
343343}
344344
345+ func TestCopyBuffers (t * testing.T ) {
346+ CopyBuffers (true )
347+ defer CopyBuffers (false )
348+
349+ input := []byte {1 , 2 , 3 , 4 }
350+ buf := testAllocateBytesBuffer (t , input )
351+
352+ output , result := BufferToBytesSafe (& buf )
353+ if result != ERR_NONE {
354+ t .Errorf ("BufferToBytesSafe returned %v" , result )
355+ }
356+
357+ if ! bytes .Equal (input , output ) {
358+ t .Error ("Bytes don't match" )
359+ }
360+
361+ // Verify it's a copy by modifying the output and checking the buffer is unchanged
362+ output [0 ] = 99
363+ output2 , result := BufferToBytesSafe (& buf )
364+ if result != ERR_NONE {
365+ t .Errorf ("BufferToBytesSafe returned %v" , result )
366+ }
367+ if output2 [0 ] != 1 {
368+ t .Error ("Expected copy semantics but buffer was modified" )
369+ }
370+ }
371+
372+ func TestBufferLength (t * testing.T ) {
373+ buf := AllocateBuffer (42 )
374+ if BufferLengthSafe (& buf ) != 42 {
375+ t .Errorf ("Expected 42, got %v" , BufferLengthSafe (& buf ))
376+ }
377+
378+ // After writing data, length should reflect actual data length
379+ input := "hello"
380+ buf2 := testAllocateStringBuffer (t , input )
381+ if BufferLengthSafe (& buf2 ) != int32 (len (input )) {
382+ t .Errorf ("Expected %v, got %v" , len (input ), BufferLengthSafe (& buf2 ))
383+ }
384+
385+ // Nil should return 0
386+ if BufferLength (nil ) != 0 {
387+ t .Error ("Expected BufferLength to return 0 for nil" )
388+ }
389+ if BufferLengthSafe (nil ) != 0 {
390+ t .Error ("Expected BufferLengthSafe to return 0 for nil" )
391+ }
392+ }
393+
394+ func TestIsBufferAllNulls (t * testing.T ) {
395+ // Buffer with all nulls should return true
396+ buf := AllocateBuffer (4 )
397+ if ! IsBufferAllNullsSafe (& buf ) {
398+ t .Error ("Expected IsBufferAllNullsSafe to return true for all-null buffer" )
399+ }
400+
401+ // Buffer with real data should return false
402+ input := []byte {1 , 2 , 3 , 4 }
403+ buf2 := testAllocateBytesBuffer (t , input )
404+ if IsBufferAllNullsSafe (& buf2 ) {
405+ t .Error ("Expected IsBufferAllNullsSafe to return false for non-null buffer" )
406+ }
407+
408+ // Zero-length buffer should return false (not a false positive)
409+ buf3 := AllocateBuffer (0 )
410+ if IsBufferAllNullsSafe (& buf3 ) {
411+ t .Error ("Expected IsBufferAllNullsSafe to return false for zero-length buffer" )
412+ }
413+
414+ // Large buffer with nulls in first 64 bytes but data after should still return true
415+ buf4 := AllocateBuffer (128 )
416+ // Set byte at offset 65 to non-zero (beyond the 64-byte check window)
417+ buf4 [BUFFER_HEADER_SIZE + 65 ] = 0xFF
418+ if ! IsBufferAllNullsSafe (& buf4 ) {
419+ t .Error ("Expected IsBufferAllNullsSafe to return true when only first 64 bytes are checked" )
420+ }
421+
422+ // Buffer with non-null byte within the first 64 bytes should return false
423+ buf5 := AllocateBuffer (128 )
424+ buf5 [BUFFER_HEADER_SIZE + 32 ] = 0xFF
425+ if IsBufferAllNullsSafe (& buf5 ) {
426+ t .Error ("Expected IsBufferAllNullsSafe to return false when non-null byte is within first 64 bytes" )
427+ }
428+
429+ // Nil should return false
430+ if IsBufferAllNulls (nil ) {
431+ t .Error ("Expected IsBufferAllNulls to return false for nil" )
432+ }
433+ if IsBufferAllNullsSafe (nil ) {
434+ t .Error ("Expected IsBufferAllNullsSafe to return false for nil" )
435+ }
436+ }
437+
345438func TestDisableTempFile (t * testing.T ) {
346439 // Disable the use of temp file buffers
347440 AllowTempFileBuffers (false )
0 commit comments