Class: Marmalade::Puzzle

Inherits:
Object
  • Object
show all
Defined in:
lib/marmalade/puzzle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_reader, options = {}) ⇒ Puzzle

Returns a new instance of Puzzle.



7
8
9
10
11
12
13
14
# File 'lib/marmalade/puzzle.rb', line 7

def initialize(file_reader, options = {})
  self.reader = file_reader
  @options = options.dup
  @debug = (@options[:debug] == true)
  if @options[:processes].to_i > 1 && !@options[:parallel]
    @options[:parallel] = true
  end
end

Instance Attribute Details

#readerObject

Returns the value of attribute reader.



5
6
7
# File 'lib/marmalade/puzzle.rb', line 5

def reader
  @reader
end

Instance Method Details

#read(assignments, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/marmalade/puzzle.rb', line 20

def read(assignments, options = {})
  assigns = reader.read(assignments, options)
  assigns.each do |k, v|
    # Set an ivar on both the puzzle and the test case so we can support using them in
    # subsequent calls to 'read' in the same block evaluation.
    instance_variable_set("@#{k.to_s}", v)
    if @current_case
      @current_case.instance_variable_set("@#{k.to_s}", v)
    end
  end
end

#read_num_casesObject



16
17
18
# File 'lib/marmalade/puzzle.rb', line 16

def read_num_cases
  read :num_cases, :type => :int
end

#run_case(&block) ⇒ Object



91
92
93
# File 'lib/marmalade/puzzle.rb', line 91

def run_case(&block)
  @current_case.run_block = block
end

#test_cases(options = {}, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
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
80
81
82
83
84
85
86
87
88
89
# File 'lib/marmalade/puzzle.rb', line 32

def test_cases(options = {}, &block)
  unless @num_cases.is_a?(Fixnum)
    raise MarmaladeError.new("@num_cases has not been set or is not an integer")
  end
  options = options.merge(@options)

  # Set up all test cases and read each one's info from the file
  test_cases = []
  1.upto(@num_cases).each do |case_num|
    test_case = TestCase.new(case_num, options)
    test_case.debug = @debug

    @current_case = test_case
    instance_eval(&block)
    @current_case = nil

    test_cases << test_case
  end

  # If we are to run in more than one process, we'll run all of the tests without
  # regard to stepping or finding a single case to run
  if options[:parallel]
    # Setup pipes for the output of each test case
    reader, writer = IO.pipe
    test_cases.each do |test_case|
      test_case.output = writer
    end
    # Run them
    processes = options[:processes] || Parallel.processor_count
    Parallel.each(test_cases, :in_processes => processes) do |test_case|
      test_case.run
    end
    # Now output the buffers from each case
    writer.close
    outputs = []
    # TODO: This could probably be done with a stable sort instead...
    while message = reader.gets
      match = message.match(/Case #(\d+)/)
      if match
        case_num = match[1].to_i
        case_output = outputs[case_num] ||= []
        case_output << message
      else
        outputs << message
      end
    end
    outputs.flatten.compact.each { |msg| puts msg }
  else
    test_cases.each do |test_case|
      if options[:case].nil? || options[:case] == test_case.case_num
        test_case.run
        if options[:step] == true
          STDIN.getc if options[:step]
        end
      end
    end
  end
end