🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

priority-queue-typescript

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

priority-queue-typescript

Priority queue data structure where you are able to set your own compare function.

2.0.3
latest
Version published
Weekly downloads
5K
8.47%
Maintainers
0
Weekly downloads
 
Created
import { PriorityQueue } from 'priority-queue-typescript';

describe('PriorityQueue', () => {
  it('should create empty priorityQueue', () => {
    const queue = new PriorityQueue<number>();
    expect(queue).not.toBeNull();
  });

  it('should check add and poll function', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => b - a
    );
    queue.add(10);
    queue.add(100);
    queue.add(9);
    queue.add(3);
    queue.add(5000);
    queue.add(4);
    queue.add(40);
    expect(queue.poll()).toBe(5000);
    expect(queue.poll()).toBe(100);
    expect(queue.poll()).toBe(40);
    expect(queue.poll()).toBe(10);
    expect(queue.poll()).toBe(9);
    expect(queue.poll()).toBe(4);
    expect(queue.poll()).toBe(3);
    expect(queue.poll()).toBe(null);
    expect(queue.size()).toBe(0);
    expect(queue.empty()).toBe(true);
  });

  it('should check peek method', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => a - b
    );
    queue.add(10);
    queue.add(100);
    expect(queue.peek()).toBe(10);
    expect(queue.peek()).toBe(10);
  });

  it('should check contains method', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => a - b
    );
    queue.add(10);
    queue.add(100);
    expect(queue.contains(10)).toBe(true);
  });

  it('should check clear method', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => a - b
    );
    queue.add(10);
    queue.add(100);
    expect(queue.empty()).toBe(false);
    queue.clear();
    expect(queue.empty()).toBe(true);
    expect(queue.poll()).toBe(null);
  });

  it('should check toArray method', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => a - b
    );
    queue.add(10);
    queue.add(100);
    const array = queue.toArray();
    expect(array).toContain(10);
    expect(array).toContain(100);
  });

  it('should check iterator ', () => {
    const queue = new PriorityQueue<number>(
      10,
      (a: number, b: number) => a - b
    );
    queue.add(10);
    queue.add(100);
    let count = 0;
    for (const item of queue) {
      count += item;
    }
    expect(count).toBe(110);
  });

  it('should check grow built in', () => {
    const queue = new PriorityQueue<number>(1);
    queue.add(10);
    queue.add(100);
    queue.add(5000);
    queue.add(4);
    expect(queue.size()).toBe(4);
    queue.poll();
    queue.poll();
    queue.poll();
    queue.poll();
    expect(queue.size()).toBe(0);
  });

  it('should check toString', () => {
    const queue = new PriorityQueue<number>(1);
    queue.add(10);
    expect(queue.toString()).toBe('10');
  });
});

FAQs

Package last updated on 11 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts