require File.expand_path('../../spec_helper.rb', __FILE__) describe Dither do it 'mipog does not support constraints' do expect { Dither.mipog([[1,1],[1,2]], 2, :constraints => []) }.to raise_error(Dither::Error, 'mipog does not support constraints') end it 't must be >= 2' do expect { Dither.all_pairs([], 0) }.to raise_error(Dither::Error, 't must be >= 2') end it 't must be <= params.length' do expect { Dither.all_pairs([(0...3).to_a], 4) }.to raise_error(Dither::Error,'t must be <= params.length') end it 'param length must be > 1' do expect { Dither.all_pairs([[], []], 2) }.to raise_error(Dither::Error,'param length must be > 1') end it 'can compute 2-way ipog using symbols' do params = [[:a, :b, :c], [:d, :e, :f], [:h, :i]] expect(Dither.all_pairs(params)).to eq([[:a, :d, :h], [:a, :e, :i], [:a, :f, :h], [:b, :d, :i], [:b, :e, :h], [:b, :f, :i], [:c, :d, :h], [:c, :e, :i], [:c, :f, :h]]) end it 'can compute 3-way mipog' do params = [(0...2).to_a, (0...2).to_a, (0..3).to_a] expect(Dither.mipog(params, 3)).to eq([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1], [0, 0, 2], [1, 0, 2], [0, 1, 2], [1, 1, 2], [0, 0, 3], [1, 0, 3], [0, 1, 3], [1, 1, 3], ]) end it 'can compute 2-way mipog using symbols' do params = [[:a, :b, :c], [:d, :e, :f], [:h, :i]] expect(Dither.mipog(params).to_set).to eq([[:a, :d, :h], [:a, :e, :i], [:a, :f, :h], [:b, :d, :i], [:b, :e, :h], [:b, :f, :i], [:c, :d, :h], [:c, :e, :i], [:c, :f, :h]].to_set) end it 'can compute 2-way mipog' do params = [(0...2).to_a, (0..3).to_a] expect(Dither.mipog(params)).to eq([ [0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2], [0, 3], [1, 3], ]) end it 'can compute 2-way ipog' do params = [(0...2).to_a, (0..3).to_a] expect(Dither.all_pairs(params)).to eq([ [0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2], [0, 3], [1, 3], ]) end it 'can compute 3-way ipog' do params = [(0...2).to_a, (0...2).to_a, (0..3).to_a] expect(Dither.all_pairs(params, 3).to_set).to eq([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1], [0, 0, 2], [1, 0, 2], [0, 1, 2], [1, 1, 2], [0, 0, 3], [1, 0, 3], [0, 1, 3], [1, 1, 3], ].to_set) end it 'can compute 3-way ipog with constraints' do params = [(0...2).to_a, (0...2).to_a, (0..3).to_a] expect(Dither.all_pairs(params, 3, :constraints => [ {0 => 0, 2 => 2}, {0 => 0, 1 => 1, 2 => 0} ]).to_set).to eq([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1], [1, 0, 2], [1, 1, 2], [0, 0, 3], [1, 0, 3], [0, 1, 3], [1, 1, 3], ].to_set) end it 'another 3-way ipog with constraints' do params = [(0...2).to_a, (0...2).to_a, (0...2).to_a, (0..3).to_a] expect(Dither.all_pairs(params, 3, :constraints => [ {0 => 0, 1 => 1, 2 => 0} ]).to_set).to eq([[0, 0, 0, 0], [1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1], [0, 0, 1, 1], [1, 1, 1, 1], [0, 0, 0, 2], [1, 1, 0, 2], [1, 0, 1, 2], [0, 1, 1, 2], [0, 0, 0, 3], [1, 1, 0, 3], [1, 0, 1, 3], [0, 1, 1, 3], [0, 0, 0, 1], [0, 1, 1, 1]].to_set) # results = Dither.all_pairs([ # (0...10).to_a, # (0...10).to_a, # (0...4).to_a, # (0...3).to_a, # (0...3).to_a, # (0...2).to_a, # (0...2).to_a, # (0...2).to_a, # (0...2).to_a, # (0...2).to_a, # (0...2).to_a, # (0...2).to_a, # ], 4) # results = Dither.all_pairs([ # (0...4).to_a, # (0...4).to_a, # (0...4).to_a, # (0...4).to_a, # (0...4).to_a, # (0...3).to_a, # (0...3).to_a, # (0...3).to_a, # (0...3).to_a, # ], 2) results = Dither.all_pairs([ (0...2).to_a, # user type (0...2).to_a, # exists? (0...2).to_a, # seats (0...2).to_a, # seats (0...3).to_a, # user region (0...3).to_a, # currency (0...3).to_a, (0...4).to_a, # payment (0...6).to_a, # delivery (0...6).to_a, ], 3) # mipog 2-way 36... ipog 36 # mipog 3-way 156... ipog 151 # mipog 4-way 592... ipog 511 # results = Dither.all_pairs( [[1,2], # ['1','2'], # [1.0,2.0], # [true, false, 3]],3) require 'csv' results.each { |a| puts a.to_csv } puts results.count end end