File: trie_node_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (92 lines) | stat: -rw-r--r-- 2,544 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespaces::Traversal::TrieNode, feature_category: :global_search do
  describe '.new' do
    it 'initializes with empty children and end set to false' do
      trie = described_class.new

      expect(trie.children).to be_empty
      expect(trie.end).to be false
    end
  end

  describe '.build' do
    it 'creates a trie from traversal IDs' do
      traversal_ids = [[1, 2], [1, 3], [2, 4]]
      trie = described_class.build(traversal_ids)

      expect(trie).to be_a(described_class)
      expect(trie.children.keys).to contain_exactly(1, 2)
      expect(trie.children[1].children.keys).to contain_exactly(2, 3)
      expect(trie.children[2].children.keys).to contain_exactly(4)
    end

    it 'does not create duplicate branches' do
      traversal_ids = [[1, 2], [1, 2]]
      trie = described_class.build(traversal_ids)

      expect(trie.children[1].children[2].end).to be true
      expect(trie.children[1].children[2].children).to be_empty
    end
  end

  describe '#prefix_search' do
    let(:trie) { described_class.build([[1, 2, 3], [1, 2, 4], [1, 3]]) }

    it 'returns all matching traversal IDs' do
      result = trie.prefix_search([1, 2])

      expect(result).to contain_exactly([1, 2, 3], [1, 2, 4])
    end

    it 'returns an empty array for non-existent prefix' do
      result = trie.prefix_search([4])

      expect(result).to be_empty
    end
  end

  describe '#covered?' do
    let(:trie) { described_class.build([[1, 2], [3, 4]]) }

    it 'returns true for covered traversal ID' do
      expect(trie.covered?([1, 2, 3])).to be true
    end

    it 'returns true for included traversal ID' do
      expect(trie.covered?([1, 2])).to be true
    end

    it 'returns false for non-covered traversal ID' do
      expect(trie.covered?([1, 3])).to be false
    end
  end

  describe '#to_a' do
    it 'returns an array of all traversal IDs in the trie' do
      trie = described_class.build([[1, 2, 3], [1, 2, 4], [1, 3], [4, 5]])

      result = trie.to_a

      expect(result).to contain_exactly([1, 2, 3], [1, 2, 4], [1, 3], [4, 5])
    end

    it 'returns an empty array for an empty trie' do
      trie = described_class.new

      result = trie.to_a

      expect(result).to be_empty
    end

    it 'handles nested branches correctly' do
      trie = described_class.build([[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4]])

      result = trie.to_a

      expect(result).to contain_exactly([1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4])
    end
  end
end