npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toolbuilder/ring-buffer-tests

v0.1.3

Published

Simple test harness for testing ring buffer implementations.

Downloads

5

Readme

Ring-Buffer-Tests

This is a simple test harness for testing ring buffers (aka circular queues). Multiple ring buffer implementations are required to support different optimization requirements, so this package provides testing for the core functionality.

The interface being tested is defined by SimpleRingBuffer.

Installation

npm install --save @toolbuilder/ring-buffer-tests

Use

import { RingBufferDriver } from '@toolbuilder/ring-buffer-tests'
import { test } from 'zora' // or use your favorite test suite
import { YourRingBuffer } from '../src/your-ring-buffer'

test('ring buffer interface test', assert => {
  const driver = new RingBufferDriver() // taking default options

  // Since the driver does not test the ctor, you need to provide a factory function
  const factory = (capacity) => new YourRingBuffer(capacity)

  // Test the ring buffer with a capacity of 100
  const [actual, expected] = driver.testRingBuffer(100, factory)

  // If the test fails, the 'actual' field will hold what YourRingBuffer provided.
  // And the 'expected' field will hold what the exemplar (SimpleRingBuffer) provided.
  // Both will indicate whether it was a return value or a buffer state check that failed.
  assert.deepEqual(actual, expected, 'ring buffer passed test')
})

API

SimpleRingBuffer

This is the default ring buffer implementation that RingBufferDriver uses to compare your ring buffer implementation with. The implementation is at src/simple-ring-buffer.js. It has full JSDocs.

RingBufferDriver

The class that runs the tests.

constructor

  • options - an optional options object
    • options.exemplarFactory - factory function, fn(capacity), for exemplar ring buffer. Defaults to SimpleRingBuffer.
    • options.getState - method to get state from both ring buffers, returns a simple Object with the field values you want to check. Defaults keys are: capacity, length, front, back, contents which uses the iterator to collect all buffer values.
    • options.methodPairs - method name pairs for grow/shrink operations. Defaults to [['push', 'shift'], ['unshift', 'pop']]. Other method names are not currently supported.
    • options.dataGenerator - a function that takes no parameters that returns a function that returns a data element to insert into the test buffer. The default dataGenerator repeats a sequence of 1000 random numbers forever.

testRingBuffer

Tests a ring buffer implementation by:

  • grow buffer to capacity - both grow and shrink but with a bias toward growth
  • push into a buffer at capacity - repeatedly push into a buffer at capacity to verify behavior
  • shrink a buffer to zero - both shrink and grow with bias toward shrinking

Every buffer operation is tested. The return value and buffer state is compared with the exemplar implementation. The first difference is returned by the method. However, testing will continue.

  • capacity [Number] - capacity value to test ring buffer with
  • ringBufferFactory [Function] - given capacity, fn(capacity), returns the ring buffer to test.
  • Return value [Array] - returns a pair of test results. The first element is the 'actual' result, and the second element is the 'expected' result. If both values are the same, the test passes. If they are different, the test fails, and the values represent the difference between actual and expected values.

Contributing

For the most part, I threw this package together as an experiement in generic testing. So the flexibility required to test any ring buffer is not quite there. As such, I expect that this package is of limited utility to others. If you think it might be useful, feel free to contribute! Also, write up issues, submit pull requests, etc.