Skip to content

Commit

Permalink
Completed Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Cully committed Aug 25, 2015
1 parent 875d548 commit 13577db
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Where 28 is the offset where the token was found, 'platform' is the token itself
The demo code then goes into a console, which will do partial searches within the dictionary. Type a partial word and press enter, and the demo will show all words in the dictionary that start with the partial entered.
Type 'exit' to finish the demo.

## Code of Conduct

The StringTree project is committed to the [Contributor Covenant](http://contributor-covenant.org). Please read [CODE_OF_CONDUCT.md] before making any contributions or comments.

## References

* http://www.ruby-doc.org
Expand Down
8 changes: 5 additions & 3 deletions lib/stringtree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Node
def initialize(char, parent = nil, value = nil)
@char = char
@up = parent
@value = value
end

# Add another node horizontally
Expand Down Expand Up @@ -112,13 +113,14 @@ def find_vertical(str, offset = 0, length = str.length)
node
end


def find_forward(str, offset = 0, length = str.length)
# Find the next match (terminating node with value non-nil) in the String data
# Optionally, set the offset into the data and its length
def find_forward(data, offset = 0, length = data.length)
node = nil
lastvaluenode = nil
i = offset
while (i<offset+length)
c = str[i]
c = data[i]
if (node == nil)
node = self.find_horizontal(c)
elsif (node.down != nil)
Expand Down
20 changes: 15 additions & 5 deletions lib/stringtree/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ def find(key)
(node == nil ? nil : node.value)
end

# Return true if the given key exists
def has_key?(key)
return false if @root == nil
node = @root.find_vertical(key)
return false if node.nil? or node.value.nil?
true
end

# Return an Array of Strings representing all partial matches forward of key in the Tree.
# Please note the key itself, if found, is not included.
# Please note the key itself is not included, even if it exists as a value.
#
# E.g.: A tree containing 'ant','antler','deer','anthropic','beer'
# tree.partials('ant') would return ['antler','anthropic']
Expand All @@ -35,7 +43,7 @@ def partials(key)
end

# Rebalance the tree for faster access.
def optimize
def optimize!
return nil if @root == nil
@root = @root.balance
end
Expand Down Expand Up @@ -66,11 +74,13 @@ def []=(key,value)
add(key,value)
end

def match_count(key,list)
# Return a Hash of terminating nodes to Integer counts for a given String data,
# i.e. Find the count of instances of each String in the tree in the given data.
def match_count(data, list = {})
return nil if @root == nil
i=0
while (i<key.length)
node = @root.find_forward(key, i, key.length-i)
while (i<data.length)
node = @root.find_forward(data, i, data.length-i)
if (node!=nil && node.value!=nil)
if (!list.has_key?(node))
list[node] = 1
Expand Down
13 changes: 13 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@
expect(@nodea.match?).to eq(2)
end
end

describe "#value" do
it "should return @node.value if not nil" do
x = OpenStruct.new(:value => "foo")
@nodea = StringTree::Item.new(1,2,x)
expect(@nodea.value).to eq("foo")
end

it "should return nil if value is nil" do
@nodea = StringTree::Item.new(1,2,nil)
expect(@nodea.value).to be_nil()
end
end
end
53 changes: 53 additions & 0 deletions spec/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,57 @@
inst.all_partials('one')
end
end

describe '#find_forward' do
it 'should return the correct node' do

inst = StringTree::Node.new 'b'
inst.left = StringTree::Node.new 'a'
inst.right = StringTree::Node.new 'c'
inst.down = StringTree::Node.new '2', inst, 'one'
inst.down.left = StringTree::Node.new '1', inst
inst.down.right = StringTree::Node.new '3', inst

expect(inst.find_forward("b2",0,2)).to be(inst.down)
expect(inst.find_forward("asoicbasicn",0,2)).to be_nil
end
end

describe '#to_s' do
it 'should return the correct string' do
inst = StringTree::Node.new 'b'
inst.left = StringTree::Node.new 'a'
inst.right = StringTree::Node.new 'c'
inst.down = StringTree::Node.new '2', inst, 'one'
inst.down.left = StringTree::Node.new '1', inst
inst.down.right = StringTree::Node.new '3', inst

expect(inst.down.left.to_s).to eq("b1")
expect(inst.down.right.to_s).to eq("b3")
expect(inst.down.to_s).to eq("b2")
expect(inst.right.to_s).to eq("c")
expect(inst.left.to_s).to eq("a")
expect(inst.to_s).to eq("b")
end
end

describe '#length' do
it 'should return the correct count' do
inst = StringTree::Node.new 'b'
inst.left = StringTree::Node.new 'a'
inst.right = StringTree::Node.new 'c'
inst.down = StringTree::Node.new '2', inst, 'one'
inst.down.left = StringTree::Node.new '1', inst
inst.down.right = StringTree::Node.new '3', inst
inst.down.left.down = StringTree::Node.new 'z', inst.down.left
inst.down.left.down.left = StringTree::Node.new 'v', inst.down.left

expect(inst.down.left.down.left.length).to eq(3)
expect(inst.down.left.down.length).to eq(3)
expect(inst.down.left.length).to eq(2)
expect(inst.down.length).to eq(2)
expect(inst.length).to eq(1)
end
end

end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
Coveralls.wear!
end

require 'stringtree'
require 'stringtree'
require 'ostruct'
Loading

0 comments on commit 13577db

Please sign in to comment.