<commit_before>require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "IO#ioctl" do
  it "raises IOError on closed stream" do
    lambda { IOSpecs.closed_io.ioctl(5, 5) }.should raise_error(IOError)
  end
  
  platform_is :os => :linux do
    it "replaces a String's content with the output buffer" do
      File.open(__FILE__, 'r') do |f|
        buffer = ''
        # FIONREAD in /usr/include/asm-generic/ioctls.h
        f.ioctl 0x541B, buffer
        buffer.unpack('I').first.should be_kind_of(Integer)
      end
    end
    
    it "raises an Errno error when ioctl fails" do
      File.open(__FILE__, 'r') do |f|<commit_msg>Another test for IO.ioctl that verifies String buffer handling.<commit_after>require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "IO#ioctl" do
  it "raises IOError on closed stream" do
    lambda { IOSpecs.closed_io.ioctl(5, 5) }.should raise_error(IOError)
  end
  
  platform_is :os => :linux do
    it "resizes an empty String to match the output size" do
      File.open(__FILE__, 'r') do |f|
        buffer = ''
        # FIONREAD in /usr/include/asm-generic/ioctls.h
        f.ioctl 0x541B, buffer
        buffer.unpack('I').first.should be_kind_of(Integer)
      end
    end
    
    it "passes data from and to a String correctly" do
      s = Socket.new Socket::AF_INET, Socket::SOCK_DGRAM, 0
      # /usr/include/net/if.h, structure ifreq
      # The structure is 32 bytes on x86, 40 bytes on x86_64
      if_name = ['lo'].pack('a16')
      buffer = if_name + 'z' * 24
      # SIOCGIFADDR in /usr/include/bits/ioctls.h
      s.ioctl 0x8915, buffer
      s.close
      
      # Interface name should remain unchanged.
      buffer[0, 16].should == if_name
      # lo should have an IPv4 address of 127.0.0.1
      buffer[16, 1].unpack('C').first.should == Socket::AF_INET
      buffer[20, 4].should == "\x7f\0\0\x01"
    end
    
    it "raises an Errno error when ioctl fails" do
      File.open(__FILE__, 'r') do |f|
{'fraction_non_alphanumeric': 0.12357414448669202, 'fraction_numerical': 0.025190114068441065, 'mean_word_length': 2.6736474694589876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3998262', 'n_tokens_mistral': 757, 'n_tokens_neox': 723, 'n_words': 201}