Sha256: 67a58a31ba1e9e7692e33f172f41e7d8d1c67931ad5cf9672acb4589dcb3c608

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'trie'

TRIE_PATH = 'spec/test-trie'

describe Trie do
  before :each do
    @trie = Trie.new(TRIE_PATH);
    @trie.add('rocket')
    @trie.add('rock')
    @trie.add('frederico')
  end
  
  after :each do
    @trie.close
    File.delete('spec/test-trie/trie.br')
    File.delete('spec/test-trie/trie.tl')
    File.delete('spec/test-trie/trie.sbm')
  end

  describe :path do
    it 'returns the correct path' do
      @trie.path.should == TRIE_PATH
    end
  end

  describe :has_key? do
    it 'returns true for words in the trie' do
      @trie.has_key?('rocket').should be_true
    end

    it 'returns nil for words that are not in the trie' do
      @trie.has_key?('not_in_the_trie').should be_nil
    end
  end

  describe :get do
    it 'returns -1 for words in the trie without a weight' do
      @trie.get('rocket').should == -1
    end

    it 'returns nil if the word is not in the trie' do
      @trie.get('not_in_the_trie').should be_nil
    end
  end

  describe :add do
    it 'adds a word to the trie' do
      @trie.add('forsooth').should == true
      @trie.get('forsooth').should == -1
    end
  end

  describe :delete do
    it 'deletes a word from the trie' do
      @trie.delete('rocket').should == true
      @trie.has_key?('rocket').should be_nil
    end
  end

  describe :children do
    it 'returns all words beginning with a given prefix' do
      children = @trie.children('roc')
      children.size.should == 2
      children.should include('rock')
      children.should include('rocket')
    end

    it 'returns nil if prefix does not exist' do
      @trie.children('ajsodij').should be_nil
    end

    it 'includes the prefix if the prefix is a word' do
      children = @trie.children('rock')
      children.size.should == 2
      children.should include('rock')
      children.should include('rocket')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tyler-trie-0.1.0 spec/trie_spec.rb
tyler-trie-0.1.1 spec/trie_spec.rb
tyler-trie-0.1.2 spec/trie_spec.rb