Merge pull request 'swaps-enhancements' (#19) from swaps-enhancements into main
All checks were successful
Run unit tests / unit_tests (push) Successful in 3m2s

Reviewed-on: #19
This commit is contained in:
bustikiller 2024-08-01 18:59:56 +00:00
commit 8a4e192385
2 changed files with 84 additions and 0 deletions

View File

@ -7,6 +7,7 @@ module Tables
def initialize(min_per_table:, max_per_table:) def initialize(min_per_table:, max_per_table:)
@min_per_table = min_per_table @min_per_table = min_per_table
@max_per_table = max_per_table @max_per_table = max_per_table
@tables = []
end end
def random_distribution(people) def random_distribution(people)

View File

@ -0,0 +1,83 @@
require 'rails_helper'
module Tables
RSpec.describe Swap do
describe '#each' do
let(:swaps) do
acc = []
described_class.new(initial_solution).each do |solution|
acc << solution.tables.map(&:dup)
end
acc
end
context 'when there are two tables with two people each' do
let(:initial_solution) do
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
distribution.tables << %i[a b]
distribution.tables << %i[c d]
end
end
it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly(
[%i[a d], %i[c b]],
[%i[b c], %i[d a]],
[%i[a c], %i[d b]],
[%i[b d], %i[c a]]
)
end
end
context 'when there are two tables with three people each' do
let(:initial_solution) do
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
distribution.tables << %i[a b c]
distribution.tables << %i[d e f]
end
end
it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly(
[%i[b c d], %i[e f a]],
[%i[b c e], %i[f d a]],
[%i[b c f], %i[d e a]],
[%i[c a d], %i[e f b]],
[%i[c a e], %i[f d b]],
[%i[c a f], %i[d e b]],
[%i[a b d], %i[e f c]],
[%i[a b e], %i[f d c]],
[%i[a b f], %i[d e c]]
)
end
end
context 'when there are three tables with two people each' do
let(:initial_solution) do
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
distribution.tables << %i[a b]
distribution.tables << %i[c d]
distribution.tables << %i[e f]
end
end
it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly(
[%i[b c], %i[d a], %i[e f]],
[%i[b d], %i[c a], %i[e f]],
[%i[a c], %i[d b], %i[e f]],
[%i[a d], %i[c b], %i[e f]],
[%i[b e], %i[c d], %i[f a]],
[%i[b f], %i[c d], %i[e a]],
[%i[a e], %i[c d], %i[f b]],
[%i[a f], %i[c d], %i[e b]],
[%i[a b], %i[d e], %i[f c]],
[%i[a b], %i[d f], %i[e c]],
[%i[a b], %i[c e], %i[f d]],
[%i[a b], %i[c f], %i[e d]]
)
end
end
end
end
end