Skip to content

Property-based tests and contracts #413

Description

@rambip

Like I presented in my previous issue, test coverage is misleading.

It would be a lot more valuable to write property-based tests, for example with fast-check

Another very valuable addition would be to add corresponding contracts (properties you want about a function) directly in the jsdocs. It could look like:

/**
 * @pre array.length > 0
 * @pre array.every((x, i) => i === 0 || x >= array[i - 1])
 * @post (result) => result === -1 || array[result] === key
 */
function binarySearch(array: number[], key: number): number {
  let lo = 0, hi = array.length - 1;
  while (lo <= hi) {
    const mid = (lo + hi) >> 1;
    if (array[mid] === key) return mid;
    if (array[mid] < key) lo = mid + 1;
    else hi = mid - 1;
  }
  return -1;
}

Then, the agent would translate it to a test and back.
I noticed the agent finds bugs a lot faster when invariants are written this way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions