Sha256: e2ca3771a65aea74585690ebcf33f43d811dd269a05103115e2102037799db9a

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/constraint_helper'

class ChannelSampleProblem < Gecode::Model
  attr :elements
  attr :positions
  
  def initialize
    @elements = int_var_array(4, 0..3)
    @elements.must_be.distinct
    @positions = int_var_array(4, 0..3)
    @positions.must_be.distinct
    branch_on @elements
  end
end

describe Gecode::Constraints::IntEnum::Channel do
  before do
    @model = ChannelSampleProblem.new
    @positions = @model.positions
    @elements = @model.elements
    @invoke_options = lambda do |hash| 
      @positions.must.channel @elements, hash
      @model.solve!
    end
    @expect_options = lambda do |strength, reif_var|
      Gecode::Raw.should_receive(:channel).once.with(@model.active_space, 
        an_instance_of(Gecode::Raw::IntVarArray), 
        an_instance_of(Gecode::Raw::IntVarArray), strength)
    end
  end

  it 'should translate into a channel constraint' do
    Gecode::Raw.should_receive(:channel).once.with(@model.active_space, 
      anything, anything, Gecode::Raw::ICL_DEF)
    @invoke_options.call({})
  end

  it 'should constrain variables to be channelled' do
    @elements.must.channel @positions
    @model.solve!
    elements = @model.elements.map{ |e| e.val }
    positions = @model.elements.map{ |p| p.val }
    elements.each_with_index do |element, i|
      element.should equal(positions.index(i))
    end
  end

  it 'should not allow negation' do
    lambda{ @elements.must_not.channel @positions }.should raise_error(
      Gecode::MissingConstraintError) 
  end
  
  it_should_behave_like 'constraint with strength option'
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gecoder-0.4.0 specs/constraints/channel.rb
gecoder-0.5.0 specs/constraints/channel.rb