class RSpec::Core::Ordering::Random

@private Orders items randomly.

Constants

MAX_32_BIT

Public Class Methods

new(configuration) click to toggle source
# File lib/rspec/core/ordering.rb, line 16
def initialize(configuration)
  @configuration = configuration
  @used = false
end

Public Instance Methods

order(items) click to toggle source
# File lib/rspec/core/ordering.rb, line 25
def order(items)
  @used = true

  seed = @configuration.seed.to_s
  items.sort_by { |item| jenkins_hash_digest(seed + item.id) }
end
used?() click to toggle source
# File lib/rspec/core/ordering.rb, line 21
def used?
  @used
end

Private Instance Methods

jenkins_hash_digest(string) click to toggle source

en.wikipedia.org/wiki/Jenkins_hash_function Jenkins provides a good distribution and is simpler than MD5. It’s a bit slower than MD5 (primarily because ‘Digest::MD5` is implemented in C) but has the advantage of not requiring us to load another part of stdlib, which we try to minimize.

# File lib/rspec/core/ordering.rb, line 39
def jenkins_hash_digest(string)
  hash = 0

  string.each_byte do |byte|
    hash += byte
    hash &= MAX_32_BIT
    hash += ((hash << 10) & MAX_32_BIT)
    hash &= MAX_32_BIT
    hash ^= hash >> 6
  end

  hash += ((hash << 3) & MAX_32_BIT)
  hash &= MAX_32_BIT
  hash ^= hash >> 11
  hash += ((hash << 15) & MAX_32_BIT)
  hash &= MAX_32_BIT
  hash
end