Sign inSign up

donaldsebleung/cgreen

By donaldsebleung

•Updated about 4 years ago

Cgreen testing framework in C packaged by donaldsebleung

0

10K+

donaldsebleung/cgreen repository overview

⁠docker.io/donaldsebleung/cgreen

Cgreen⁠ testing framework in C packaged by donaldsebleung, for solving Codewars⁠ Kata locally and more

This is an unofficial build not endorsed or supported by upstream in any way.

Current supported architectures are:

  • amd64
  • arm64
  • riscv64

⁠Usage

Pull the image:

$ docker pull docker.io/donaldsebleung/cgreen

You can also pull the image for an architecture other than the native one for your device (default), if you are using Docker Desktop, or have otherwise enabled QEMU user-mode emulation⁠ on the container host. For example, to pull the image for riscv64 architecture:

$ docker pull --platform linux/riscv64 docker.io/donaldsebleung/cgreen

Place the following files in your working directory:

  • preloaded.h: interface
  • solution.c: implementation
  • solution_tests.c: Cgreen unit tests

With run.sh:

#!/bin/bash
set -eu

if [[ "$#" -eq 0 ]]; then
  CMD="gcc -O2 preloaded.h solution.c solution_tests.c -lcgreen /app/tests.c -o tests && ./tests"
elif [[ "$#" -eq 1 && "$1" == --with-asm ]]; then
  CMD="gcc -O2 preloaded.h solution.s solution_tests.c -lcgreen /app/tests.c -o tests && ./tests"
else
  >&2 echo "Usage: ./run.sh [--with-asm]"
  exit 1
fi

if [ -z "${CONTAINER_ENGINE:+x}" ]; then
  CONTAINER_ENGINE=docker
fi

if [ -z "${IMAGE_TAG:+x}" ]; then
  IMAGE_TAG=docker.io/donaldsebleung/cgreen:latest
fi

if [ -z "${ARCH:+x}" ]; then
  ARCH=amd64
fi

W=/home/student

# Create container
C=$($CONTAINER_ENGINE container create --platform linux/$ARCH --rm -w $W $IMAGE_TAG sh -c "$CMD")

# Copy files from the current directory
$CONTAINER_ENGINE container cp ./. $C:$W

# Run tests
$CONTAINER_ENGINE container start --attach $C

For solution_tests.c, you should not define a main() function at all - instead, you need to define a function with signature TestSuite *solution_tests(), e.g.

#include <cgreen/cgreen.h>

Describe(Trivial);
BeforeEach(Trivial) {}
AfterEach(Trivial) {}

Ensure(Trivial, should_work_for_a_trivial_test) {
  assert_that(1);
}

TestSuite *solution_tests() {
  TestSuite *suite = create_test_suite();
  add_test_with_context(suite, Trivial, should_work_for_a_trivial_test);
  return suite;
}

Then, to compile and execute the code:

$ ./run.sh

By default, the script assumes you have Docker installed. If you have another container engine such as Podman installed, specify it through CONTAINER_ENGINE:

$ CONTAINER_ENGINE=podman ./run.sh

To specify a particular architecture (default: amd64), set the ARCH variable. For example, to compile and execute the code on riscv64 architecture:

$ ARCH=riscv64 ./run.sh

If your implementation is in assembly instead of C, replace solution.c with an equivalent assembly program solution.s, and pass the --with-asm flag to run.sh:

$ ./run.sh --with-asm

Tag summary

Content type

-

Digest

sha256:ee5813345…

Size

91.4 MB

Last updated

about 4 years ago

docker pull donaldsebleung/cgreen