Class: RandomTestFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/langa/randomtestfiles.rb

Overview

The class RandomTestFiles creates various files in different languages for

testing purposes. Therefor it scans the language configuration for source
files and, if existing, extracts a portion from random position of it to an
extra file in a testing directory. The files are removed, when object life
cycle ends.

Constant Summary collapse

RTF_DIR =
'rltf.temp'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_count, chunk_size, &block) ⇒ RandomTestFiles Also known as: create

Returns a new instance of RandomTestFiles.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/langa/randomtestfiles.rb', line 44

def initialize(file_count, chunk_size, &block)
  # Store test parameter
  @count = file_count
  @size = chunk_size

  # remember root path
  @root = File.join(File.dirname(__FILE__), '..', '..')
  @rtf_dir = File.join(@root, RTF_DIR)

  # check source files for existance
  @sources = []
  source_file = dest_files = nil
  Languages.new.values_for('source').each do |source, lang|
    source_file = File.join(@root, source)

    if File.exist?(source_file)
      dest_files = []

      # => create number of desired output files
      (1..file_count).each do |i|
        dest_files << 
          File.join(@rtf_dir, source + "-#{@count}-#{@size}")
      end

      # => store filename
      @sources << [lang, source_file, dest_files]
    end
  end

  # => initialize random seed for different results
  srand (Time.new.to_f * 10000).to_i
  
  self.each &block if block_given?
  
  self
end

Instance Method Details

#clearObject



94
95
96
97
# File 'lib/langa/randomtestfiles.rb', line 94

def clear
#    FileUtils.rm(RLTF_DIRECTORY + '/*.txt')
  FileUtils.rm_r(RLTF_DIRECTORY)
end

#copy_random_pieces(filename, file_count, chunk_size) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/langa/randomtestfiles.rb', line 99

def copy_random_pieces(filename, file_count, chunk_size)
  files = []
  
  path = File.join(File.dirname(__FILE__), '..', '..')
  source_file = File.join(path, filename)
  
  # => remember size of source, so that we don't grab past EOF
  file_size = File.exist?(source_file) ? File.size(source_file) : 0
  return nil if chunk_size > file_size

  # => open source file
  File.open(source_file) do |file|
  
    # => create number of desired output files
    (1..file_count).each do |i|
    
      # => grab a randomly piece of fixed size from the source
      file.seek(rand(file_size-chunk_size), IO::SEEK_SET)
      piece = file.read(chunk_size)

      # => fix potentially broken utf-8 sequences at start of piece
      piece = piece[1..-1] while (0x80..0xbf).include?(piece[0])
      # => fix potentially broken utf-8 sequences at end of piece
      piece = piece[0..-2] while (0x80..0xbf).include?(piece[-1])
      piece = piece[0..-2] if (0xc0..0xff).include?(piece[-1])
    
      # => write piece to test files
      source_file =~ /([^\/]+).txt$/i
      files << File.join(path, "#{RLTF_DIRECTORY}/#{$1}.#{chunk_size}.#{i}.txt")
      File.open(files[-1], 'w') do |fout|
        fout.write piece
      end
    end
  end
  files
end

#eachObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/langa/randomtestfiles.rb', line 83

def each
  # => create temp directory
  FileUtils.mkdir_p(RTF_DIR)

  @sources.each do |source| la, lang, filename = source
    yield la, copy_random_pieces(filename, @file_count, @chunk_size)
  end

  self.clear
end