Skip to content

Commit

Permalink
Tooling: add testharnish and a great some first tests (#28)
Browse files Browse the repository at this point in the history
Additions:
* CBuilder JUNIT Tests
* CBuilder JUNIT System-Tests
* C-Runtime Catch2 System-Tests
* Gtihub Action test_suite.yml
  • Loading branch information
0sak authored and cagix committed Jul 29, 2024
1 parent 35744f2 commit 7a8e525
Show file tree
Hide file tree
Showing 131 changed files with 9,433 additions and 554 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/test_suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Testing C-Runtime
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -25,18 +25,18 @@ jobs:
run: make test_clean ; make clean

languagefeatures:
name: Testing CBuilder (Java)
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Build with Gradle
run: ./gradlew test
name: Testing CBuilder (Java)
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Build with Gradle
run: ./gradlew test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ pip-log.txt
*.zip
*.tgz
*.tar.gz

# Catch2 #
##########
c-runtime/test/include/catch_amalgamated.hpp
c-runtime/test/src/catch_amalgamated.cpp
62 changes: 54 additions & 8 deletions c-runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ MKDIR_P := mkdir -p
# make SRCDIR and INCLUDEDIR available to doxygen
export SRCDIR := $(realpath ./src)
export INCLUDEDIR := $(realpath ./include)
DOXYFILE := $(realpath ./Doxyfile)

export TESTDIR := $(realpath ./test)
export INCLUDEDIRTEST := $(realpath ./test/include)
export SRCDIRTEST := $(realpath ./test/src)

CATCHFILES = $(TESTDIR)/include/catch_amalgamated.hpp $(TESTDIR)/src/catch_amalgamated.cpp
export C_FILES_TEST := $(wildcard ./test/src/test/*.c ./test/src/test/*/*.c* ./test/src/*.cpp)
export C_FILES_TEST := $(filter-out ./src/program.c, $(C_FILES_TEST))
export C_FILES := $(wildcard ./src/*.c ./src/*/*.c*)
export C_FILES := $(filter-out ./src/program.c, $(C_FILES))
export DOXYFILE := $(realpath ./Doxyfile)

C_FILES_TEST += $(TESTDIR)/src/catch_amalgamated.cpp
# make DOCDIR available to doxygen
export DOCDIR := $(abspath ./doc)
BINDIR := $(abspath ./bin)
OUTDIR := $(abspath ./out)
export BINDIR := $(abspath ./bin)
export OUTDIR := $(abspath ./out)

#################
# Default Flags #
#################

CFLAGS ?= -pedantic -Wall -Werror
CFLAGS ?= -Wall -Werror -pedantic
CFLAGS += -std=c11 -I$(INCLUDEDIR)
CFLAGS += -ffile-prefix-map=$(SRCDIR)=.
# make getline() (see man getline(3)) available
Expand All @@ -37,6 +47,10 @@ else
# not for now CFLAGS += -DNDEBUG
endif

#####################
# Default g++ Flags #
#####################

###########
# Targets #
###########
Expand All @@ -49,7 +63,6 @@ SRC := $(wildcard $(SRCDIR)/*.c) $(wildcard $(SRCDIR)/*/*.c) $(wildcard $(SRCDIR
OBJECT := $(patsubst $(SRCDIR)/%.c,$(OUTDIR)/%.o,$(SRC))
DEPENDENCY := $(OBJECT:.o=.d)


#########
# Rules #
#########
Expand Down Expand Up @@ -79,6 +92,7 @@ run: $(BIN)
.PHONY: clean
clean:
$(RM) -r $(BIN) $(OBJECT) $(DEPENDENCY) $(wildcard $(DOCDIR)/*)
$(RM) $(TESTDIR)/main

.PHONY: doc
doc:
Expand All @@ -89,11 +103,43 @@ doc:
lint:
clang-tidy --checks=*,-bugprone-reserved-identifier,-cert-dcl37-c,-cert-dcl51-cpp,-altera-struct-pack-align $(SRC)

#https://github.com/catchorg/Catch2/blob/v2.x/include/catch.hpp
.PHONY: curl_catch
$(CATCHFILES):
curl -o $(TESTDIR)/include/catch_amalgamated.hpp https://raw.githubusercontent.com/catchorg/Catch2/devel/extras/catch_amalgamated.hpp
curl -o $(TESTDIR)/src/catch_amalgamated.cpp https://raw.githubusercontent.com/catchorg/Catch2/devel/extras/catch_amalgamated.cpp

.PHONY: test
test: test_clean
test: $(C_FILES_TEST)
d=$$(date +%s)\
; make -f $(TESTDIR)/Systemtests/makefile \
&& echo "Build took $$(($$(date +%s)-d)) seconds"
make test_clean


.PHONY: test_clean
test_clean:
echo "dummy to allow gh workflow to do its job"
make -f $(TESTDIR)/Systemtests/makefile clean

$(RM) -r $(TESTDIR)/include/catch_amalgamated.hpp
$(RM) -r $(TESTDIR)/src/catch_amalgamated.cpp

.PHONY: test_echo
test_echo:
make -f $(TESTDIR)/Systemtests/makefile echo

.PHONY: test_run
test_run:
make -f $(TESTDIR)/Systemtests/makefile run

.PHONY: test_target
test_target: $(C_FILES_TEST)
d=$$(date +%s)\
; make -f $(TESTDIR)/Systemtests/makefile test_declaration_and_assignment \
&& echo "Build took $$(($$(date +%s)-d)) seconds"
make run_target
make test_clean

.PHONY: run_target
run_target:
./test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.out
126 changes: 126 additions & 0 deletions c-runtime/test/Systemtests/ArithmeticOps/TestArithmeticOps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#define CATCH_CONFIG_MAIN

#include <stddef.h>

#include "catch_amalgamated.hpp"
#include "assert.h"
#include "mpy_aliases.h"
#include "mpy_obj.h"
#include "builtins-setup.h"
#include "function-args.h"
#include "literals/tuple.h"
#include "literals/int.h"
#include "literals/boolean.h"
#include "literals/str.h"
#include "type-hierarchy/object.h"
#include "type-hierarchy/type.h"
#include "test/test_helpers.h"

int add_op(int i, int j){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);

printf("[ADD] i : %d --- j : %d\n",i,j);
print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content)) == (i+j) ? 1 : 0;
}

int sub_op(int i, int j){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);

printf("[SUB] i : %d --- j : %d\n",i,j);
print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content)) == (i-j) ? 1 : 0;
}

int mul_op(int i, int j){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__mul__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);

printf("[MUL] i : %d --- j : %d\n",i,j);
print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content)) == (i*j) ? 1 : 0;
}

int div_op(int i, int j){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__div__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);

printf("[DIV] i : %d --- j : %d\n",i,j);
print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content)) == (i/j) ? 1 : 0;
}


TEST_CASE("GENERATE ADD OP"){
auto i = GENERATE(1, 3, 5);
auto j = GENERATE(2, 4, 6);
CHECK(add_op(i, j) == 1);
}

TEST_CASE("GENERATE SUB OP"){
auto i = GENERATE(1, 3, 5);
auto j = GENERATE(2, 4, 6);
CHECK(sub_op(i, j) == 1);
}

TEST_CASE("GENERATE MUL OP"){
auto i = GENERATE(1, 3, 5);
auto j = GENERATE(2, 4, 6);
CHECK(mul_op(i, j) == 1);
}

TEST_CASE("GENERATE DIV OP"){
auto i = GENERATE(3, 8, 12);
auto j = GENERATE(2, 4, 6);
CHECK(div_op(i, j) == 1);
}
103 changes: 103 additions & 0 deletions c-runtime/test/Systemtests/BuiltInFunctions/TestCastInt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#define CATCH_CONFIG_MAIN

#include <stddef.h>

#include "catch_amalgamated.hpp"
#include "assert.h"
#include "mpy_aliases.h"
#include "mpy_obj.h"
#include "builtins-setup.h"
#include "function-args.h"
#include "literals/tuple.h"
#include "literals/int.h"
#include "literals/boolean.h"
#include "literals/str.h"
#include "type-hierarchy/object.h"
#include "type-hierarchy/type.h"
#include "test/test_helpers.h"

void exit(int status) {
throw std::runtime_error("Error");
}

int cast_bool_to_int(bool input){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_obj_init_boolean(input);
__mpy_obj_ref_inc(a);

a = __mpy_call(__mpy_obj_get_attr(a, "__int__"), __mpy_obj_init_tuple(0), NULL);

__mpy_obj_ref_dec(a);

print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content));
}

int cast_string_to_int(const char* input){
__MPyObj *a;

__mpy_builtins_setup();
a = __mpy_obj_init_object();
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_obj_init_str_static(input);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);
a = __mpy_call(__mpy_obj_get_attr(a, "__int__"), __mpy_obj_init_tuple(0), NULL);
__mpy_obj_ref_inc(a);

__mpy_obj_ref_dec(a);

print_mpyobj_int(a);

__mpy_builtins_cleanup();

return (*(int*)(a->content));
}

TEST_CASE("TEST CAST BOOL TO INT")
{
auto [input, expected_output] = GENERATE(table<bool, int>({
{ true, 1 },
{ false, 0 },
{10, 1},
}));

CAPTURE(input);
CHECK(cast_bool_to_int(input) == expected_output);
}

TEST_CASE("TEST CAST STRING TO INT")
{
auto [input, expected_output] = GENERATE(table<const char*, int>({
{ "1", 1 },
{ "0", 0 },
{ "10", 10},
}));

CAPTURE(input);
CHECK(cast_string_to_int(input) == expected_output);
}

TEST_CASE("TEST CAST STRING TO INT ERROR") {
REQUIRE_THROWS_AS(cast_string_to_int("false"), std::runtime_error);
}

/*
And
int cast_int_to_int(int i){
}
*/
Loading

0 comments on commit 7a8e525

Please sign in to comment.