Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created August 7, 2022 15:06
Show Gist options
  • Save DonaldKellett/e053987bbbf5afe895e0d5a7343041ba to your computer and use it in GitHub Desktop.
Save DonaldKellett/e053987bbbf5afe895e0d5a7343041ba to your computer and use it in GitHub Desktop.
docker.io/donaldsebleung/cgreen

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

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
FROM docker.io/library/ubuntu:jammy
RUN set -ex; \
buildDeps='git ca-certificates build-essential cmake'; \
apt-get update; \
apt-get install -y --no-install-recommends gcc $buildDeps; \
cd /tmp; \
git clone --recurse-submodules --branch 1.6.0 https://github.com/cgreen-devs/cgreen.git; \
cd cgreen; \
make; \
make install; \
cd ..; \
apt-get purge -y --auto-remove $buildDeps; \
apt-get clean; \
rm -rvf /var/lib/apt/lists/* /tmp/*;
ENV LD_LIBRARY_PATH=/usr/local/lib
RUN mkdir /app
COPY ./tests.c /app
RUN useradd -m student
USER student
WORKDIR /home/student
#!/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
#include <cgreen/cgreen.h>
TestSuite *solution_tests();
int main(int argc, char **argv) {
TestSuite *suite = create_test_suite();
add_suite(suite, solution_tests());
return run_test_suite(suite, create_text_reporter());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment