Sha256: 31cb68c1bf3943e9348c6b6b3e0f2662673f6743ad59988d3979b82946d40dfe

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require_relative 'test_helper'

module Dynflow
  module UtilsTest
    describe ::Dynflow::Utils::PriorityQueue do
      let(:queue) { Utils::PriorityQueue.new }

      it 'can insert elements' do
        queue.push 1
        _(queue.top).must_equal 1
        queue.push 2
        _(queue.top).must_equal 2
        queue.push 3
        _(queue.top).must_equal 3
        _(queue.to_a).must_equal [1, 2, 3]
      end

      it 'can override the comparator' do
        queue = Utils::PriorityQueue.new { |a, b| b <=> a }
        queue.push 1
        _(queue.top).must_equal 1
        queue.push 2
        _(queue.top).must_equal 1
        queue.push 3
        _(queue.top).must_equal 1
        _(queue.to_a).must_equal [3, 2, 1]
      end

      it 'can inspect top element without removing it' do
        assert_nil queue.top
        queue.push(1)
        _(queue.top).must_equal 1
        queue.push(3)
        _(queue.top).must_equal 3
        queue.push(2)
        _(queue.top).must_equal 3
      end

      it 'can report size' do
        count = 5
        count.times { queue.push 1 }
        _(queue.size).must_equal count
      end

      it 'pops elements in correct order' do
        queue.push 1
        queue.push 3
        queue.push 2
        _(queue.pop).must_equal 3
        _(queue.pop).must_equal 2
        _(queue.pop).must_equal 1
        assert_nil queue.pop
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynflow-1.9.0 test/utils_test.rb
dynflow-1.8.3 test/utils_test.rb