Giter Site home page Giter Site logo

oderjunkie / foobarbaz Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 80 KB

foobarbaz - a single-header property-based testing library in C99

License: GNU General Public License v3.0

C 100.00%
c c99 fuzzing header-only macros property-based-testing tdd test-framework testing testing-library

foobarbaz's Introduction

foobarbaz

Discord Lines of code GitHub all releases GitHub release (latest SemVer) Minimum C version: 99 GitHub

foobarbaz is my attempt to create a unit testing library for C that is simple, type-agnostic1, and readable. its pseudo-syntax is borrowed heavily from mocha, and it achieves it through many macros from chax.

usage

// change the text displayed next to a passing test
#define CHECKMARK "[pass]"

// note: by default, CROSSMARK is *not* defined, and
// instead of displaying a fixed piece of text it will
// display the ordinal corresponding to which test failed
#define CROSSMARK "[fail]"

// if the test takes longer than SLOW/2, then it will be
// marked yellow. if it takes longer than SLOW, it will be red.
// note that you can use minutes, seconds, and milliseconds as
// units of measure (ex. #define SLOW 1s500ms) but they must be
// whole numbers
#define SLOW 10ms

// requires POSIX. if defined, will catch segmentation faults.
#define CATCH_SEGFAULTS

#include "foobarbaz.h"

// function under test
int add(int lhs, int rhs) {
  return lhs + rhs;
}

#include <stdio.h>

int main(void) {
  printf("you forgot to compile this file with `-DTEST`\n");
  printf("

  // use `describe` to categorize tests.
  describe ("add") {
    // use `it` to describe individual tests
    it ("can calculate 1+1") {
      assert(add(1, 1) == 2);
    }
    
    it ("has an identity element of 0") {
      // use `given` to test some property that holds true
      // no matter what the given value is. if you're familiar
      // with formal logic, think of `given` as `for_all`
      int x;
      given (x) {
        assert(add(x, 0) == x);
      }
    }
    
    it ("is commutative") {
      // note that you can't write `given (int x, int y)`
      // because of the limitations of C macros
      int x, y;
      given (x, y) {
        assert(add(x, y) == add(y, x));
      }
    }
    
    it ("is associative") {
      int x, y, z;
      given (x, y, z) {
        assert(add(x, add(y, z)) == add(add(x, y), z));
      }
    }
  }
  
}

Footnotes

  1. it is not completely type-agnostic, because if a type has an "invalid" state (ex. a tagged union) then the input may be invalid. โ†ฉ

foobarbaz's People

Contributors

oderjunkie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.