ruby-tdb.git  about / heads / tags
Trivial Database bindings for Ruby
blob cd4332631d16dfd4c73c234acc4800ca9cbb5979 1057 bytes (raw)
$ git show HEAD:test/test_tdb_hash_functions.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
# -*- encoding: binary -*-
require 'test/unit'
$-w = true
require 'tdb'

class TestHashFunctions < Test::Unit::TestCase
  include TDB::HashFunctions

  def test_hashes
    TDB::HASHES.each do |name,_|
      next if :default == name
      assert_kind_of Integer, __send__("tdb_hash_#{name}", "hello")
    end
  end

  def test_distro_sequential
    TDB::HASHES.each do |name,_|
      next if :default == name
      m = method "tdb_hash_#{name}"
      tmp = Hash.new { |h,k| h[k] = 0 }
      (1..100000).each do |s|
        tmp[m.call(s.to_s) % 7] += 1
      end
      assert_equal 7, tmp.size
      tmp.each_value do |v|
        assert_in_delta 100000/7.0, v, 1000
      end
    end
  end

  def test_distro_random
    TDB::HASHES.each do |name,_|
      next if :default == name
      m = method "tdb_hash_#{name}"
      tmp = Hash.new { |h,k| h[k] = 0 }
      100000.times do
        tmp[m.call(rand.to_s) % 7] += 1
      end
      assert_equal 7, tmp.size
      tmp.each_value do |v|
        assert_in_delta 100000/7.0, v, 1000
      end
    end
  end
end

git clone https://yhbt.net/ruby-tdb.git