digital_trees_and_sets 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ # this performs all judy test
2
+
3
+ libdir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
5
+ libdir = File.expand_path(File.join(File.dirname(__FILE__), '../ext'))
6
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
7
+
8
+ require 'test/judy_set'
9
+ require 'test/judy_hash'
@@ -0,0 +1,64 @@
1
+ require "rubygems"
2
+ require 'digital_trees_and_sets'
3
+
4
+ def bench
5
+ GC.disable
6
+ start = Time.now
7
+ yield
8
+ t = Time.now - start
9
+ GC.enable
10
+ t
11
+ end
12
+ # just to get a uniform interface with sets
13
+ Hash.class_eval do
14
+ def add i
15
+ self[i] = i + 1
16
+ end
17
+ #we know this is just used to get the first, so i is always 0
18
+ def by_count i
19
+ first
20
+ end
21
+ end
22
+ JudyHash.class_eval do
23
+ def add i
24
+ self[i] = i + 1
25
+ end
26
+ end
27
+ def put_row row
28
+ puts "| " + row.collect{|o|o.to_s.center(12)}.join("|") + "|"
29
+ end
30
+ def test clazz , dens
31
+ puts ""
32
+ puts "Testing #{clazz} with a key density of 1 per #{dens}"
33
+ puts ""
34
+ rows = [["2**", "Insert","Iterate","Get","Delete","mem/key","no.keys"]]
35
+ 10.upto(20) do |power|
36
+ max = 2**power
37
+ judy = clazz.new
38
+ const = bench {1.upto(max) {|i| i } }
39
+ insert = bench {1.upto(max) {|i| judy.add( (i*dens).to_i ) } }
40
+ memory = judy.mem_used
41
+ iterate = bench {judy.each {|k,v| k } }
42
+ get = bench {1.upto(max) {|i| judy[ (i*dens).to_i ] }}
43
+ delete = bench {1.upto(max) {|i| judy.delete( (i*dens).to_i) } }
44
+ result = [power] + [insert,iterate,get,delete].collect{|n| (1000000*(n-const)/max).round(3)} + [ (memory.to_f/max).round(3) , max]
45
+ rows << result
46
+ end
47
+ # for the web it is better to transpose, but on the screen not
48
+ rows = rows.transpose
49
+ header = rows.delete_at(0)
50
+ put_row header
51
+ put_row ["-"*12]*header.length
52
+ rows.each do |row|
53
+ put_row row
54
+ end
55
+ end
56
+
57
+ puts "Times in microseconds per action , memory in bytes/insert"
58
+
59
+ density = 1000
60
+ test Hash , density
61
+ test JudyHash , density
62
+ test JudySet , density
63
+
64
+
@@ -0,0 +1,129 @@
1
+ require 'test/unit'
2
+ require 'digital_trees_and_sets.so'
3
+
4
+ class JudyHashLTest < Test::Unit::TestCase
5
+ def setup
6
+ @array = JudyHash.new
7
+ end
8
+
9
+ def fill(array)
10
+ 0.upto(2**12 - 1) { |i| @array[i] = i }
11
+ end
12
+
13
+ def test_set
14
+ assert_equal(222, (@array[1] = 222))
15
+ assert_equal(333, (@array.set(:some_sym , 333)))
16
+ assert_equal(:sym, (@array.set(2 , :sym)))
17
+ assert_raise ArgumentError do @array["string"] = "not allowed" end
18
+ end
19
+
20
+ def test_delete
21
+ @array[1] = 222
22
+ assert( ! @array.delete(0))
23
+ assert( @array.delete(1))
24
+ end
25
+
26
+ def test_get
27
+ assert_nil(@array[0])
28
+ assert_nil(@array.get(22))
29
+ @array[0] = 42
30
+ assert_equal(42, @array[0])
31
+ assert_equal(42, @array.get(0))
32
+ end
33
+
34
+ def test_count
35
+ assert_equal(0, @array.count)
36
+ fill(@array)
37
+ assert_equal(2**12, @array.count)
38
+ end
39
+
40
+ def test_by_count
41
+ @array[1] = 333
42
+ @array[3] = 444
43
+ @array[5] = 555
44
+ @array[7] = 666
45
+ assert_equal(333, @array.by_count(1))
46
+ assert_equal(444, @array.by_count(2))
47
+ assert_equal(555, @array.by_count(3))
48
+ assert_equal(666, @array.by_count(4))
49
+ assert_nil(@array.by_count(5))
50
+ assert_nil(@array.by_count(0))
51
+ end
52
+
53
+ def test_free_array
54
+ assert_equal(JudyHash, @array.clear.class)
55
+ 0.upto(100) { |i| @array[i] = i }
56
+ GC.start
57
+ assert(@array.clear)
58
+ assert(@array.mem_used == 0)
59
+ end
60
+
61
+ def test_mem_used
62
+ assert_equal(0, @array.mem_used)
63
+ 1.upto(2**12) { |i| @array[i] = i }
64
+ assert(0 < @array.mem_used)
65
+ @array.clear
66
+ assert_equal(0, @array.mem_used)
67
+ end
68
+
69
+ def test_indexes
70
+ assert_nil(@array.last_index)
71
+ assert_nil(@array.first_index)
72
+ @array[1] = 333
73
+ @array[3] = 444
74
+ assert_equal(1, @array.first_index)
75
+ assert_equal(3, @array.last_index)
76
+ end
77
+
78
+ def test_each_key
79
+ 1.upto(100) { |i| @array[i] = i }
80
+ sum = 0
81
+ @array.each_key { |v| sum += v }
82
+ assert_equal(5050, sum)
83
+ end
84
+
85
+ def test_each
86
+ 1.upto(100) { |i| @array[i] = i }
87
+ keys = 0
88
+ vals = 0
89
+ @array.each { |key , val |
90
+ keys += key
91
+ vals += val
92
+ assert_equal(key, val)
93
+ }
94
+ assert_equal(5050, keys)
95
+ assert_equal(5050, vals)
96
+ end
97
+
98
+ def test_clear
99
+ 0.upto(100) { |i| @array[i] = i }
100
+ assert_equal(101, @array.length)
101
+ @array.clear
102
+ assert_equal(0, @array.length)
103
+ end
104
+
105
+ def test_empty?
106
+ assert(@array.empty?)
107
+ @array[0] = 0
108
+ assert(!@array.empty?)
109
+ end
110
+
111
+ def test_length
112
+ 0.upto(100) { |i| @array[i] = i }
113
+ assert_equal(101, @array.length)
114
+ end
115
+
116
+ def test_include?
117
+ 1.upto(99) { |i| @array[i] = i }
118
+ assert(@array.include?(99))
119
+ assert(!@array.include?(0))
120
+ assert(!@array.include?(100))
121
+ end
122
+
123
+ def ttest_to_a
124
+ @array[55] = 55
125
+ ar = @array.to_a
126
+ assert_equal 1 , ar.length
127
+ assert_equal [[55 , 55 ]] , ar
128
+ end
129
+ end
@@ -0,0 +1,91 @@
1
+ require 'test/unit'
2
+ require 'digital_trees_and_sets.so'
3
+
4
+ class JudySetTest < Test::Unit::TestCase
5
+ def setup
6
+ @array = JudySet.new
7
+ end
8
+
9
+ def load
10
+ 1.upto(100) { |i| @array.add(i)}
11
+ assert_equal(100, @array.length)
12
+ end
13
+ def test_test
14
+ assert_equal(true, @array.add(0))
15
+ assert_equal(true, @array.get(0))
16
+ assert_equal(true, @array.unset(0))
17
+ assert_equal(false, @array.get(0))
18
+ end
19
+
20
+ def test_del_with_each
21
+ load
22
+ 1.upto(100) { |i| @array.delete(i)}
23
+ assert_equal(0, @array.length)
24
+ @array.each do |i|
25
+ assert false
26
+ end
27
+ end
28
+
29
+ def test_length
30
+ load
31
+ # 1.upto(100) { |i| @array.add(i)}
32
+ assert_equal(100, @array.length)
33
+ end
34
+
35
+ def test_by_count
36
+ load
37
+ assert_equal(2, @array.by_count(2))
38
+ assert_equal(42, @array.by_count(42))
39
+ end
40
+
41
+ def test_clear
42
+ load
43
+ assert_equal(100, @array.clear)
44
+ assert_equal(0, @array.length)
45
+ end
46
+
47
+ def test_mem_used
48
+ assert_equal(0, @array.mem_used)
49
+ 0.upto(10) do |i|
50
+ @array.add(i)
51
+ end
52
+ assert(@array.mem_used > 0)
53
+ end
54
+
55
+ def test_indexes
56
+ load
57
+ assert_equal(1, @array.first_index)
58
+ assert_equal(44, @array.next_index(43))
59
+ assert_equal(66, @array.next_index(65))
60
+ assert_equal false , @array.next_index(400)
61
+ assert_equal(100, @array.last_index)
62
+ end
63
+
64
+ def test_each_index
65
+ load
66
+ sum = 0
67
+ @array.each_index do |i|
68
+ sum = sum + i
69
+ assert @array.get(i)
70
+ end
71
+ assert_equal(5050, sum)
72
+ end
73
+
74
+ def test_each
75
+ load
76
+ sum = 0
77
+ @array.each do |i|
78
+ assert @array.get(i)
79
+ sum = sum + i
80
+ end
81
+ assert_equal(5050, sum)
82
+ end
83
+
84
+ def test_to_a
85
+ assert_equal([], @array.to_a)
86
+ test_each
87
+ assert @array.get(55)
88
+ assert_equal(1.upto(100).to_a, @array.to_a)
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digital_trees_and_sets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tosten Ruger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Fast and simple. A tree is an int to object , a set an int to bit mapping.
15
+ Highly scalable, very fast and incredibly small footprint
16
+ email: torsten@villataika.fi
17
+ executables: []
18
+ extensions:
19
+ - ext/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - README.md
23
+ - ext/extconf.rb
24
+ - ext/judy.h
25
+ - ext/judy1.c
26
+ - ext/judy_compat.h
27
+ - ext/judy_hash.c
28
+ - ext/judy_set.c
29
+ - ext/judyl.c
30
+ - test/all.rb
31
+ - test/judy_bench.rb
32
+ - test/judy_hash.rb
33
+ - test/judy_set.rb
34
+ homepage: https://github.com/dancinglightning/digital_trees_and_sets/
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ - ext/
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.9.3
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '1.6'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Digital trees and sets for ruby
59
+ test_files: []