summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/priority_queue_spec.rb
blob: 17361833e509c4c5a548172cbd9f460922fa3208 (plain)
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
93
94
95
# frozen_string_literal: true

require_relative "../spec_helper"

module SyntaxSuggest
  class CurrentIndex
    attr_reader :current_indent

    def initialize(value)
      @current_indent = value
    end

    def <=>(other)
      @current_indent <=> other.current_indent
    end

    def inspect
      @current_indent
    end
  end

  RSpec.describe CodeFrontier do
    it "works" do
      q = PriorityQueue.new
      q << 1
      q << 2
      expect(q.elements).to eq([2, 1])

      q << 3
      expect(q.elements).to eq([3, 1, 2])

      expect(q.pop).to eq(3)
      expect(q.pop).to eq(2)
      expect(q.pop).to eq(1)
      expect(q.pop).to eq(nil)

      array = []
      q = PriorityQueue.new
      array.reverse_each do |v|
        q << v
      end
      expect(q.elements).to eq(array)

      array = [100, 36, 17, 19, 25, 0, 3, 1, 7, 2]
      array.reverse_each do |v|
        q << v
      end

      expect(q.pop).to eq(100)
      expect(q.elements).to eq([36, 25, 19, 17, 0, 1, 7, 2, 3])

      # expected [36, 25, 19, 17, 0, 1, 7, 2, 3]
      expect(q.pop).to eq(36)
      expect(q.pop).to eq(25)
      expect(q.pop).to eq(19)
      expect(q.pop).to eq(17)
      expect(q.pop).to eq(7)
      expect(q.pop).to eq(3)
      expect(q.pop).to eq(2)
      expect(q.pop).to eq(1)
      expect(q.pop).to eq(0)
      expect(q.pop).to eq(nil)
    end

    it "priority queue" do
      frontier = PriorityQueue.new
      frontier << CurrentIndex.new(0)
      frontier << CurrentIndex.new(1)

      expect(frontier.sorted.map(&:current_indent)).to eq([0, 1])

      frontier << CurrentIndex.new(1)
      expect(frontier.sorted.map(&:current_indent)).to eq([0, 1, 1])

      frontier << CurrentIndex.new(0)
      expect(frontier.sorted.map(&:current_indent)).to eq([0, 0, 1, 1])

      frontier << CurrentIndex.new(10)
      expect(frontier.sorted.map(&:current_indent)).to eq([0, 0, 1, 1, 10])

      frontier << CurrentIndex.new(2)
      expect(frontier.sorted.map(&:current_indent)).to eq([0, 0, 1, 1, 2, 10])

      frontier = PriorityQueue.new
      values = [18, 18, 0, 18, 0, 18, 18, 18, 18, 16, 18, 8, 18, 8, 8, 8, 16, 6, 0, 0, 16, 16, 4, 14, 14, 12, 12, 12, 10, 12, 12, 12, 12, 8, 10, 10, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8, 10, 6, 6, 6, 6, 6, 6, 8, 10, 8, 8, 10, 8, 10, 8, 10, 8, 6, 8, 8, 6, 8, 6, 6, 8, 0, 8, 0, 0, 8, 8, 0, 8, 0, 8, 8, 0, 8, 8, 8, 0, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 6, 8, 6, 6, 6, 6, 8, 6, 8, 6, 6, 4, 4, 6, 6, 4, 6, 4, 6, 6, 4, 6, 4, 4, 6, 6, 6, 6, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 6, 6, 2]

      values.each do |v|
        value = CurrentIndex.new(v)
        frontier << value # CurrentIndex.new(v)
      end

      expect(frontier.sorted.map(&:current_indent)).to eq(values.sort)
    end
  end
end