From 7a8e5253cbd6b6d57981588c7900bba5f89ed4b2 Mon Sep 17 00:00:00 2001 From: f0gel <64742664+0sak@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:07:21 +0200 Subject: [PATCH] Tooling: add testharnish and a great some first tests (#28) Additions: * CBuilder JUNIT Tests * CBuilder JUNIT System-Tests * C-Runtime Catch2 System-Tests * Gtihub Action test_suite.yml --- .github/workflows/test_suite.yml | 32 +- .gitignore | 5 + c-runtime/Makefile | 62 +++- .../ArithmeticOps/TestArithmeticOps.cpp | 126 +++++++ .../BuiltInFunctions/TestCastInt.cpp | 103 ++++++ .../BuiltInFunctions/TestCastString.cpp | 127 +++++++ .../Systemtests/BuiltInFunctions/TestId.cpp | 140 ++++++++ .../BuiltInFunctions/TestPrint.cpp | 72 ++++ .../Systemtests/BuiltInFunctions/TestType.cpp | 44 +++ .../Classes/TestClassInheritance.cpp | 57 ++++ .../Systemtests/Classes/TestClassInit.cpp | 121 +++++++ .../Classes/TestClassInitThrows.cpp | 128 ++++++++ .../Systemtests/Classes/TestClassMembers.cpp | 130 ++++++++ .../ComparisonOps/TestComparisonOps.cpp | 73 +++++ .../ComparisonOps/TestGreaterEqualOp.cpp | 45 +++ .../ComparisonOps/TestGreaterOp.cpp | 46 +++ .../ComparisonOps/TestLessEqualOp.cpp | 45 +++ .../Systemtests/ComparisonOps/TestLessOp.cpp | 46 +++ .../Conditions/TestElifStatement.cpp | 61 ++++ .../Conditions/TestElseStatement.cpp | 57 ++++ .../Conditions/TestIfStatement.cpp | 59 ++++ .../Conditions/TestWhileStatement.cpp | 50 +++ .../TestDeclarationAndAssignment.cpp | 144 ++++++++ .../FunctionCalls/TestFunctionBody.cpp | 174 ++++++++++ .../FunctionCalls/TestFunctionParams.cpp | 207 ++++++++++++ .../TestFunctionParamsThrows.cpp | 142 ++++++++ .../FunctionCalls/TestFunctionRecursion.cpp | 37 +++ .../FunctionCalls/TestFunctionReturn.cpp | 91 +++++ .../Systemtests/LogicalOps/TestLogicalOps.cpp | 101 ++++++ c-runtime/test/Systemtests/makefile | 165 ++++++++++ c-runtime/test/Systemtests/wuppie.cpp | 46 +++ .../Classes/TestClassInheritanceHelpers.h | 22 ++ .../test/Classes/TestClassInitHelpers.h | 24 ++ .../test/Classes/TestClassMembersHelpers.h | 24 ++ .../FunctionCalls/TestFunctionBodyHelpers.h | 30 ++ .../FunctionCalls/TestFunctionParamsHelpers.h | 36 ++ .../TestFunctionRecursionHelpers.h | 22 ++ .../FunctionCalls/TestFunctionReturnHelpers.h | 24 ++ c-runtime/test/include/test/test_helpers.h | 44 +++ .../Classes/TestClassInheritanceHelpers.c | 45 +++ .../src/test/Classes/TestClassInitHelpers.c | 74 +++++ .../test/Classes/TestClassMembersHelpers.c | 70 ++++ .../FunctionCalls/TestFunctionBodyHelpers.c | 138 ++++++++ .../FunctionCalls/TestFunctionParamsHelpers.c | 199 +++++++++++ .../TestFunctionRecursionHelpers.c | 32 ++ .../FunctionCalls/TestFunctionReturnHelpers.c | 61 ++++ c-runtime/test/src/test/test_helpers.c | 30 ++ src/main/java/CBuilder/Expression.java | 4 +- src/main/java/CBuilder/ManualTest.java | 283 +++++++++------- src/main/java/CBuilder/ProgramBuilder.java | 149 +++++---- src/main/java/CBuilder/Reference.java | 13 +- src/main/java/CBuilder/Statement.java | 4 +- .../conditions/IfThenElseStatement.java | 22 +- .../ConditionalStatement.java | 21 +- .../conditionalStatement/ElifStatement.java | 4 +- .../conditionalStatement/ElseStatement.java | 5 +- .../conditionalStatement/IfStatement.java | 5 +- .../conditionalStatement/WhileStatement.java | 9 +- .../CBuilder/keywords/bool/AndKeyword.java | 1 - .../keywords/bool/BinaryBoolKeyword.java | 24 +- .../CBuilder/keywords/bool/NotKeyword.java | 17 +- .../CBuilder/keywords/bool/OrKeyword.java | 1 - .../java/CBuilder/literals/BoolLiteral.java | 9 +- .../java/CBuilder/literals/IntLiteral.java | 8 +- src/main/java/CBuilder/literals/Literal.java | 8 +- .../java/CBuilder/literals/StringLiteral.java | 8 +- .../java/CBuilder/literals/TupleLiteral.java | 13 +- .../CBuilder/manualTests/BooleanTest.java | 85 ++--- .../java/CBuilder/manualTests/ClassTest.java | 41 ++- .../java/CBuilder/manualTests/IntOps.java | 21 +- .../java/CBuilder/manualTests/StrOps.java | 22 +- .../CBuilder/objects/AttributeAssignment.java | 20 +- .../CBuilder/objects/AttributeReference.java | 23 +- src/main/java/CBuilder/objects/Call.java | 28 +- src/main/java/CBuilder/objects/MPyClass.java | 70 ++-- src/main/java/CBuilder/objects/SuperCall.java | 13 +- .../CBuilder/objects/functions/Argument.java | 24 +- .../CBuilder/objects/functions/Function.java | 63 ++-- .../objects/functions/ReturnStatement.java | 7 +- .../java/CBuilder/variables/Assignment.java | 51 +-- .../variables/VariableDeclaration.java | 16 +- src/test/java/CBuilder/TestReference.java | 46 +++ .../java/CBuilder/TestWithCToolchains.java | 32 +- .../java/CBuilder/bool/TestAndKeyword.java | 44 +++ .../java/CBuilder/bool/TestNotKeyword.java | 55 ++++ .../java/CBuilder/bool/TestOrKeyword.java | 43 +++ .../conditions/TestIfThenElseStatement.java | 62 ++++ .../TestElifStatement.java | 72 ++++ .../TestElseStatement.java | 54 +++ .../conditionalStatement/TestIfStatement.java | 72 ++++ .../TestWhileStatement.java | 94 ++++++ .../CBuilder/literals/TestBoolLiteral.java | 51 +++ .../CBuilder/literals/TestIntLiteral.java | 52 +++ .../CBuilder/literals/TestStringLiteral.java | 51 +++ .../CBuilder/literals/TestTupleLiteral.java | 73 +++++ .../objects/TestAttributeAssignment.java | 46 +++ .../objects/TestAttributeReference.java | 119 +++++++ src/test/java/CBuilder/objects/TestCall.java | 65 ++++ .../java/CBuilder/objects/TestMPyClass.java | 310 ++++++++++++++++++ .../java/CBuilder/objects/TestSuperCall.java | 63 ++++ .../objects/functions/TestArgument.java | 59 ++++ .../objects/functions/TestFunction.java | 230 +++++++++++++ .../functions/TestReturnStatement.java | 49 +++ .../CBuilder/variables/TestAssignment.java | 126 +++++++ .../variables/TestVariableDeclaration.java | 102 ++++++ .../ArithmeticOps/TestArithmeticOps.java | 83 +++++ .../BuiltInFunctions/TestCastInt.java | 104 ++++++ .../BuiltInFunctions/TestCastStr.java | 82 +++++ .../TestDifferentialExamplePrint.java | 83 +++++ .../BuiltInFunctions/TestId.java | 77 +++++ .../BuiltInFunctions/TestInput.java | 84 +++++ .../TestMetamorphicExamplePrint.java | 114 +++++++ .../BuiltInFunctions/TestPrint.java | 69 ++++ .../BuiltInFunctions/TestType.java | 87 +++++ .../Classes/TestClassInheritance.java | 129 ++++++++ .../Classes/TestClassInit.java | 169 ++++++++++ .../Classes/TestClassMembers.java | 209 ++++++++++++ .../ComparisonOps/TestComparisonOps.java | 82 +++++ .../Conditions/TestElifStatement.java | 130 ++++++++ .../Conditions/TestElseStatement.java | 126 +++++++ .../Conditions/TestIfStatement.java | 114 +++++++ .../Conditions/TestWhileStatement.java | 91 +++++ .../TestDeclarationAndAssignment.java | 283 ++++++++++++++++ .../FunctionCalls/TestFunctionBody.java | 189 +++++++++++ .../FunctionCalls/TestFunctionParams.java | 214 ++++++++++++ .../FunctionCalls/TestFunctionRecursion.java | 138 ++++++++ .../FunctionCalls/TestFunctionReturn.java | 190 +++++++++++ .../LogicalOps/TestLogicalOps.java | 83 +++++ .../LanguageFeatures/TestEmpty.java | 13 + src/test/java/Systemtests/TestHelpers.java | 81 +++++ .../TestLanguageFeaturesCombined.java | 290 ++++++++++++++++ 131 files changed, 9433 insertions(+), 554 deletions(-) create mode 100644 c-runtime/test/Systemtests/ArithmeticOps/TestArithmeticOps.cpp create mode 100644 c-runtime/test/Systemtests/BuiltInFunctions/TestCastInt.cpp create mode 100644 c-runtime/test/Systemtests/BuiltInFunctions/TestCastString.cpp create mode 100644 c-runtime/test/Systemtests/BuiltInFunctions/TestId.cpp create mode 100644 c-runtime/test/Systemtests/BuiltInFunctions/TestPrint.cpp create mode 100644 c-runtime/test/Systemtests/BuiltInFunctions/TestType.cpp create mode 100644 c-runtime/test/Systemtests/Classes/TestClassInheritance.cpp create mode 100644 c-runtime/test/Systemtests/Classes/TestClassInit.cpp create mode 100644 c-runtime/test/Systemtests/Classes/TestClassInitThrows.cpp create mode 100644 c-runtime/test/Systemtests/Classes/TestClassMembers.cpp create mode 100644 c-runtime/test/Systemtests/ComparisonOps/TestComparisonOps.cpp create mode 100644 c-runtime/test/Systemtests/ComparisonOps/TestGreaterEqualOp.cpp create mode 100644 c-runtime/test/Systemtests/ComparisonOps/TestGreaterOp.cpp create mode 100644 c-runtime/test/Systemtests/ComparisonOps/TestLessEqualOp.cpp create mode 100644 c-runtime/test/Systemtests/ComparisonOps/TestLessOp.cpp create mode 100644 c-runtime/test/Systemtests/Conditions/TestElifStatement.cpp create mode 100644 c-runtime/test/Systemtests/Conditions/TestElseStatement.cpp create mode 100644 c-runtime/test/Systemtests/Conditions/TestIfStatement.cpp create mode 100644 c-runtime/test/Systemtests/Conditions/TestWhileStatement.cpp create mode 100644 c-runtime/test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.cpp create mode 100644 c-runtime/test/Systemtests/FunctionCalls/TestFunctionBody.cpp create mode 100644 c-runtime/test/Systemtests/FunctionCalls/TestFunctionParams.cpp create mode 100644 c-runtime/test/Systemtests/FunctionCalls/TestFunctionParamsThrows.cpp create mode 100644 c-runtime/test/Systemtests/FunctionCalls/TestFunctionRecursion.cpp create mode 100644 c-runtime/test/Systemtests/FunctionCalls/TestFunctionReturn.cpp create mode 100644 c-runtime/test/Systemtests/LogicalOps/TestLogicalOps.cpp create mode 100644 c-runtime/test/Systemtests/makefile create mode 100644 c-runtime/test/Systemtests/wuppie.cpp create mode 100644 c-runtime/test/include/test/Classes/TestClassInheritanceHelpers.h create mode 100644 c-runtime/test/include/test/Classes/TestClassInitHelpers.h create mode 100644 c-runtime/test/include/test/Classes/TestClassMembersHelpers.h create mode 100644 c-runtime/test/include/test/FunctionCalls/TestFunctionBodyHelpers.h create mode 100644 c-runtime/test/include/test/FunctionCalls/TestFunctionParamsHelpers.h create mode 100644 c-runtime/test/include/test/FunctionCalls/TestFunctionRecursionHelpers.h create mode 100644 c-runtime/test/include/test/FunctionCalls/TestFunctionReturnHelpers.h create mode 100644 c-runtime/test/include/test/test_helpers.h create mode 100644 c-runtime/test/src/test/Classes/TestClassInheritanceHelpers.c create mode 100644 c-runtime/test/src/test/Classes/TestClassInitHelpers.c create mode 100644 c-runtime/test/src/test/Classes/TestClassMembersHelpers.c create mode 100644 c-runtime/test/src/test/FunctionCalls/TestFunctionBodyHelpers.c create mode 100644 c-runtime/test/src/test/FunctionCalls/TestFunctionParamsHelpers.c create mode 100644 c-runtime/test/src/test/FunctionCalls/TestFunctionRecursionHelpers.c create mode 100644 c-runtime/test/src/test/FunctionCalls/TestFunctionReturnHelpers.c create mode 100644 c-runtime/test/src/test/test_helpers.c create mode 100644 src/test/java/CBuilder/TestReference.java create mode 100644 src/test/java/CBuilder/bool/TestAndKeyword.java create mode 100644 src/test/java/CBuilder/bool/TestNotKeyword.java create mode 100644 src/test/java/CBuilder/bool/TestOrKeyword.java create mode 100644 src/test/java/CBuilder/conditions/TestIfThenElseStatement.java create mode 100644 src/test/java/CBuilder/conditions/conditionalStatement/TestElifStatement.java create mode 100644 src/test/java/CBuilder/conditions/conditionalStatement/TestElseStatement.java create mode 100644 src/test/java/CBuilder/conditions/conditionalStatement/TestIfStatement.java create mode 100644 src/test/java/CBuilder/conditions/conditionalStatement/TestWhileStatement.java create mode 100644 src/test/java/CBuilder/literals/TestBoolLiteral.java create mode 100644 src/test/java/CBuilder/literals/TestIntLiteral.java create mode 100644 src/test/java/CBuilder/literals/TestStringLiteral.java create mode 100644 src/test/java/CBuilder/literals/TestTupleLiteral.java create mode 100644 src/test/java/CBuilder/objects/TestAttributeAssignment.java create mode 100644 src/test/java/CBuilder/objects/TestAttributeReference.java create mode 100644 src/test/java/CBuilder/objects/TestCall.java create mode 100644 src/test/java/CBuilder/objects/TestMPyClass.java create mode 100644 src/test/java/CBuilder/objects/TestSuperCall.java create mode 100644 src/test/java/CBuilder/objects/functions/TestArgument.java create mode 100644 src/test/java/CBuilder/objects/functions/TestFunction.java create mode 100644 src/test/java/CBuilder/objects/functions/TestReturnStatement.java create mode 100644 src/test/java/CBuilder/variables/TestAssignment.java create mode 100644 src/test/java/CBuilder/variables/TestVariableDeclaration.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/ArithmeticOps/TestArithmeticOps.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastInt.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastStr.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestDifferentialExamplePrint.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestId.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestInput.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestMetamorphicExamplePrint.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestPrint.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestType.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInheritance.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInit.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Classes/TestClassMembers.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/ComparisonOps/TestComparisonOps.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Conditions/TestElifStatement.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Conditions/TestElseStatement.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Conditions/TestIfStatement.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/Conditions/TestWhileStatement.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/DeclarationAndAssignment/TestDeclarationAndAssignment.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionBody.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionParams.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionRecursion.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionReturn.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/LogicalOps/TestLogicalOps.java create mode 100644 src/test/java/Systemtests/LanguageFeatures/TestEmpty.java create mode 100644 src/test/java/Systemtests/TestHelpers.java create mode 100644 src/test/java/Systemtests/TestLanguageFeaturesCombined.java diff --git a/.github/workflows/test_suite.yml b/.github/workflows/test_suite.yml index ff488c6..4ba97e0 100644 --- a/.github/workflows/test_suite.yml +++ b/.github/workflows/test_suite.yml @@ -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 @@ -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 diff --git a/.gitignore b/.gitignore index 4450517..c21a97f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/c-runtime/Makefile b/c-runtime/Makefile index 8e7e88a..e059453 100644 --- a/c-runtime/Makefile +++ b/c-runtime/Makefile @@ -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 @@ -37,6 +47,10 @@ else # not for now CFLAGS += -DNDEBUG endif +##################### +# Default g++ Flags # +##################### + ########### # Targets # ########### @@ -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 # ######### @@ -79,6 +92,7 @@ run: $(BIN) .PHONY: clean clean: $(RM) -r $(BIN) $(OBJECT) $(DEPENDENCY) $(wildcard $(DOCDIR)/*) + $(RM) $(TESTDIR)/main .PHONY: doc doc: @@ -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 diff --git a/c-runtime/test/Systemtests/ArithmeticOps/TestArithmeticOps.cpp b/c-runtime/test/Systemtests/ArithmeticOps/TestArithmeticOps.cpp new file mode 100644 index 0000000..f9f0bb5 --- /dev/null +++ b/c-runtime/test/Systemtests/ArithmeticOps/TestArithmeticOps.cpp @@ -0,0 +1,126 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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); +} diff --git a/c-runtime/test/Systemtests/BuiltInFunctions/TestCastInt.cpp b/c-runtime/test/Systemtests/BuiltInFunctions/TestCastInt.cpp new file mode 100644 index 0000000..d003211 --- /dev/null +++ b/c-runtime/test/Systemtests/BuiltInFunctions/TestCastInt.cpp @@ -0,0 +1,103 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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({ + { 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({ + { "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){ +} + +*/ diff --git a/c-runtime/test/Systemtests/BuiltInFunctions/TestCastString.cpp b/c-runtime/test/Systemtests/BuiltInFunctions/TestCastString.cpp new file mode 100644 index 0000000..da9b742 --- /dev/null +++ b/c-runtime/test/Systemtests/BuiltInFunctions/TestCastString.cpp @@ -0,0 +1,127 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 cast_bool_to_string(bool input, const char* expected){ + __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); + + __mpy_obj_ref_dec(a); + a = __mpy_call(__mpy_obj_get_attr(a, "__str__"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +int cast_int_to_string(int input, const char* expected){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_int(input); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_call(__mpy_obj_get_attr(a, "__str__"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +int cast_string_to_string(const char* input, const char* expected){ + __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, "__str__"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +TEST_CASE("TEST CAST BOOL TO STRING") +{ + auto [input, expected_output] = GENERATE(table({ + { true, "True" }, + { false, "False" }, + { 10, "True" }, + { 0, "False" }, + })); + + CAPTURE(input); + CHECK(cast_bool_to_string(input, expected_output) == 0); +} + +TEST_CASE("TEST CAST INT TO STRING") +{ + auto [input, expected_output] = GENERATE(table({ + { 123, "123" }, + { 0, "0" }, + { -10, "-10" }, + { 1236789, "1236789" }, + })); + + CAPTURE(input); + CHECK(cast_int_to_string(input, expected_output) == 0); +} + +TEST_CASE("TEST CAST STRING TO STRING") +{ + auto [input, expected_output] = GENERATE(table({ + { "123", "123" }, + { "0", "0" }, + { "-10", "-10" }, + { "1236789", "1236789" }, + })); + + CAPTURE(input); + CHECK(cast_string_to_string(input, expected_output) == 0); +} diff --git a/c-runtime/test/Systemtests/BuiltInFunctions/TestId.cpp b/c-runtime/test/Systemtests/BuiltInFunctions/TestId.cpp new file mode 100644 index 0000000..a9fc1ab --- /dev/null +++ b/c-runtime/test/Systemtests/BuiltInFunctions/TestId.cpp @@ -0,0 +1,140 @@ +/* + The Tests defined here only work if the assignment of the id to an mpyobj is not random. +*/ +#define CATCH_CONFIG_MAIN + +#include + +#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 test_id_string(){ + __MPyObj *a; + __MPyObj *b; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + b = __mpy_obj_init_object(); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("123"); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(b); + b = __mpy_call(id, __mpy_tuple_assign(0, a, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + __mpy_obj_ref_dec(b); + + print_mpyobj_int(b); + + __mpy_builtins_cleanup(); + + return (*(int*)(b->content) == 63 ? 1 : 0); +} + +int test_id_int(){ + __MPyObj *a; + __MPyObj *b; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + b = __mpy_obj_init_object(); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_int(123); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(b); + b = __mpy_call(id, __mpy_tuple_assign(0, a, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + __mpy_obj_ref_dec(b); + + print_mpyobj_int(b); + + __mpy_builtins_cleanup(); + + return (*(int*)(b->content) == 158 ? 1 : 0); +} + +int test_id_bool(){ + __MPyObj *a; + __MPyObj *b; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + b = __mpy_obj_init_object(); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_boolean(true); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(b); + b = __mpy_call(id, __mpy_tuple_assign(0, a, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(b); + + __mpy_obj_ref_dec(a); + __mpy_obj_ref_dec(b); + + print_mpyobj_int(b); + + __mpy_builtins_cleanup(); + + return (*(int*)(b->content) == 260 ? 1 : 0); +} + +int test_id_call(){ + __MPyObj *a; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_call(id, __mpy_tuple_assign(0, __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content) == 390 ? 1 : 0); +} + +TEST_CASE("TEST ID OF mpy_obj") +{ + CHECK(test_id_string() == 1); + CHECK(test_id_int() == 1); + CHECK(test_id_bool() == 1); + CHECK(test_id_call() == 1); + +} diff --git a/c-runtime/test/Systemtests/BuiltInFunctions/TestPrint.cpp b/c-runtime/test/Systemtests/BuiltInFunctions/TestPrint.cpp new file mode 100644 index 0000000..81ec3cd --- /dev/null +++ b/c-runtime/test/Systemtests/BuiltInFunctions/TestPrint.cpp @@ -0,0 +1,72 @@ +/* + Since print returns void only the return value of the program can be checked. + A successful test indicates that calling print with an literal/function call worked. + Calling print(type(mpyobj)) should result in a crash for instance. +*/ +#define CATCH_CONFIG_MAIN + +#include + +#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 print_int(){ + + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_int(133), __mpy_obj_init_tuple(1)), NULL)); + + return 0; +} + +int print_string(){ + + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("Test"), __mpy_obj_init_tuple(1)), NULL)); + + return 0; +} + +int print_bool(){ + + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_boolean(true), __mpy_obj_init_tuple(1)), NULL)); + + return 0; +} + +int print_expression(){ + + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL)); + + return 0; +} + +// Throws error see TODO(./src/function-args.c:36) +int print_type(){ + + //__mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_call(type, __mpy_tuple_assign(0, __mpy_obj_init_int(123), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_call(type, __mpy_tuple_assign(0, print, __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL)); + return 0; +} + + +TEST_CASE("TEST PRINT") +{ + __mpy_builtins_setup(); + + CHECK(print_int() == 0); + CHECK(print_string() == 0); + CHECK(print_bool() == 0); + CHECK(print_expression() == 0); + //CHECK(print_type() == 0); + + __mpy_builtins_cleanup(); +} diff --git a/c-runtime/test/Systemtests/BuiltInFunctions/TestType.cpp b/c-runtime/test/Systemtests/BuiltInFunctions/TestType.cpp new file mode 100644 index 0000000..f543d05 --- /dev/null +++ b/c-runtime/test/Systemtests/BuiltInFunctions/TestType.cpp @@ -0,0 +1,44 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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" + +// Throws error see TODO(./src/function-args.c:36) + +int test_type(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_call(type, __mpy_tuple_assign(0, a, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE("TEST PRINT") +{ + //CHECK(test_type() == 0); +} diff --git a/c-runtime/test/Systemtests/Classes/TestClassInheritance.cpp b/c-runtime/test/Systemtests/Classes/TestClassInheritance.cpp new file mode 100644 index 0000000..2af92e0 --- /dev/null +++ b/c-runtime/test/Systemtests/Classes/TestClassInheritance.cpp @@ -0,0 +1,57 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/Classes/TestClassInheritanceHelpers.h" + +__MPyObj *A; + +__MPyObj *B; + +int inheritance(){ + __MPyObj *child; + + __mpy_builtins_setup(); + child = __mpy_obj_init_object(); + __mpy_obj_ref_inc(child); + + + A = __mpy_obj_init_type("A", __MPyType_Object); + __mpy_obj_ref_inc(A); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_A___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(A, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + B = __mpy_obj_init_type("B", A); + __mpy_obj_ref_inc(B); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_B___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(B, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(child); + child = __mpy_call(B, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(child); + + __mpy_obj_ref_dec(child); + + + __mpy_obj_ref_dec(A); + __mpy_obj_ref_dec(B); + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE("CLASS INHERITANCE"){ + CHECK_NOTHROW(inheritance()); +} diff --git a/c-runtime/test/Systemtests/Classes/TestClassInit.cpp b/c-runtime/test/Systemtests/Classes/TestClassInit.cpp new file mode 100644 index 0000000..81c60ce --- /dev/null +++ b/c-runtime/test/Systemtests/Classes/TestClassInit.cpp @@ -0,0 +1,121 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/Classes/TestClassInitHelpers.h" + +__MPyObj *init_no_params; + +__MPyObj *init_one_param; + +__MPyObj *init_multiple_params; + +int init_class_no_params(){ + __MPyObj *x; + + __mpy_builtins_setup(); + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_no_params = __mpy_obj_init_type("init_no_params", __MPyType_Object); + __mpy_obj_ref_inc(init_no_params); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_no_params___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_no_params, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + x = __mpy_call(init_no_params, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + print_mpyobj(x); + + __mpy_obj_ref_dec(init_no_params); + + __mpy_builtins_cleanup(); + + return 0; +} + +int init_class_one_param(){ + __MPyObj *x; + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_one_param = __mpy_obj_init_type("init_one_param", __MPyType_Object); + __mpy_obj_ref_inc(init_one_param); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_one_param___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_one_param, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + x = __mpy_call(init_one_param, __mpy_tuple_assign(0, __mpy_obj_init_str_static("test"), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + + __mpy_obj_ref_dec(init_one_param); + + print_mpyobj(x); + + __mpy_builtins_cleanup(); + + return 0; +} + +int init_class_multiple_params(){ + __MPyObj *x; + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_multiple_params = __mpy_obj_init_type("init_multiple_params", __MPyType_Object); + __mpy_obj_ref_inc(init_multiple_params); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_multiple_params___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_multiple_params, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + x = __mpy_call(init_multiple_params, __mpy_tuple_assign(0, __mpy_obj_init_str_static("test"), __mpy_tuple_assign(1, __mpy_obj_init_int(123), __mpy_obj_init_tuple(2))), NULL); + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + __mpy_obj_ref_dec(init_multiple_params); + + print_mpyobj(x); + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE("CLASS INIT"){ + CHECK_NOTHROW(init_class_no_params()); + CHECK_NOTHROW(init_class_one_param()); + CHECK_NOTHROW(init_class_multiple_params()); +} diff --git a/c-runtime/test/Systemtests/Classes/TestClassInitThrows.cpp b/c-runtime/test/Systemtests/Classes/TestClassInitThrows.cpp new file mode 100644 index 0000000..4200215 --- /dev/null +++ b/c-runtime/test/Systemtests/Classes/TestClassInitThrows.cpp @@ -0,0 +1,128 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/Classes/TestClassInitHelpers.h" + +__MPyObj *init_no_params; + +__MPyObj *init_one_param; + +__MPyObj *init_multiple_params; + +void exit(int status); + +int init_class_no_params_but_one_provided(){ + __MPyObj *x; + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_no_params = __mpy_obj_init_type("init_no_params", __MPyType_Object); + __mpy_obj_ref_inc(init_no_params); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_no_params___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_no_params, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + + x = __mpy_call(init_no_params, __mpy_tuple_assign(0, __mpy_obj_init_str_static("test"), __mpy_obj_init_tuple(1)), NULL); + + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + print_mpyobj(x); + + __mpy_obj_ref_dec(init_no_params); + + __mpy_builtins_cleanup(); + + return 0; +} + +int init_class_one_param_but_none_provided(){ + __MPyObj *x; + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_one_param = __mpy_obj_init_type("init_one_param", __MPyType_Object); + __mpy_obj_ref_inc(init_one_param); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_one_param___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_one_param, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + x = __mpy_call(init_one_param, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + + __mpy_obj_ref_dec(init_one_param); + + print_mpyobj(x); + + __mpy_builtins_cleanup(); + + return 0; +} + +int init_class_multiple_params_but_less_provided(){ + __MPyObj *x; + + __mpy_builtins_setup(); + x = __mpy_obj_init_object(); + __mpy_obj_ref_inc(x); + + + init_multiple_params = __mpy_obj_init_type("init_multiple_params", __MPyType_Object); + __mpy_obj_ref_inc(init_multiple_params); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_init_multiple_params___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(init_multiple_params, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + + __mpy_obj_ref_dec(x); + x = __mpy_call(init_multiple_params, __mpy_tuple_assign(0, __mpy_obj_init_str_static("test"), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(x); + + __mpy_obj_ref_dec(x); + + __mpy_obj_ref_dec(init_multiple_params); + + print_mpyobj(x); + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE("CLASS INIT THROWS"){ + CHECK_THROWS_AS(init_class_no_params_but_one_provided(), std::runtime_error); // Actually this is the same as testing if function throw... + CHECK_THROWS_AS(init_class_one_param_but_none_provided(), std::runtime_error); // "" + CHECK_THROWS_AS(init_class_multiple_params_but_less_provided(), std::runtime_error); // "" +} + +void exit(int status) { + throw std::runtime_error(""); +} + diff --git a/c-runtime/test/Systemtests/Classes/TestClassMembers.cpp b/c-runtime/test/Systemtests/Classes/TestClassMembers.cpp new file mode 100644 index 0000000..97a0926 --- /dev/null +++ b/c-runtime/test/Systemtests/Classes/TestClassMembers.cpp @@ -0,0 +1,130 @@ +#define CATCH_CONFIG_MAIN + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/Classes/TestClassMembersHelpers.h" + +__MPyObj *getter_setter; + +int getX_from_class(){ + __MPyObj *myClass; + __MPyObj *returnValueGetter; + + __mpy_builtins_setup(); + myClass = __mpy_obj_init_object(); + __mpy_obj_ref_inc(myClass); + + returnValueGetter = __mpy_obj_init_object(); + __mpy_obj_ref_inc(returnValueGetter); + + getter_setter = __mpy_obj_init_type("getter_setter", __MPyType_Object); + __mpy_obj_ref_inc(getter_setter); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_getter_setter___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(getter_setter, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + { + __MPyObj *getX; + getX = __mpy_obj_init_func(&func_getter_setter_getX); + __mpy_obj_ref_inc(getX); + __mpy_obj_set_attr(getter_setter, "getX", getX); + __mpy_obj_ref_dec(getX); + } + { + __MPyObj *setX; + setX = __mpy_obj_init_func(&func_getter_setter_setX); + __mpy_obj_ref_inc(setX); + __mpy_obj_set_attr(getter_setter, "setX", setX); + __mpy_obj_ref_dec(setX); + } + + __mpy_obj_ref_dec(myClass); + myClass = __mpy_call(getter_setter, __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(myClass); + + __mpy_obj_ref_dec(returnValueGetter); + returnValueGetter = __mpy_call(__mpy_obj_get_attr(myClass, "getX"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(returnValueGetter); + + __mpy_obj_ref_dec(myClass); + __mpy_obj_ref_dec(returnValueGetter); + + __mpy_obj_ref_dec(getter_setter); + + print_mpyobj_int(returnValueGetter); + + __mpy_builtins_cleanup(); + + return (*(int*)(returnValueGetter->content)); +} + +int setX_from_class(){ + __MPyObj *myClass; + __MPyObj *returnValueGetter; + + __mpy_builtins_setup(); + myClass = __mpy_obj_init_object(); + __mpy_obj_ref_inc(myClass); + + returnValueGetter = __mpy_obj_init_object(); + __mpy_obj_ref_inc(returnValueGetter); + + getter_setter = __mpy_obj_init_type("getter_setter", __MPyType_Object); + __mpy_obj_ref_inc(getter_setter); + { + __MPyObj *__init__; + __init__ = __mpy_obj_init_func(&func_getter_setter___init__); + __mpy_obj_ref_inc(__init__); + __mpy_obj_set_attr(getter_setter, "__init__", __init__); + __mpy_obj_ref_dec(__init__); + } + { + __MPyObj *getX; + getX = __mpy_obj_init_func(&func_getter_setter_getX); + __mpy_obj_ref_inc(getX); + __mpy_obj_set_attr(getter_setter, "getX", getX); + __mpy_obj_ref_dec(getX); + } + { + __MPyObj *setX; + setX = __mpy_obj_init_func(&func_getter_setter_setX); + __mpy_obj_ref_inc(setX); + __mpy_obj_set_attr(getter_setter, "setX", setX); + __mpy_obj_ref_dec(setX); + } + + __mpy_obj_ref_dec(myClass); + myClass = __mpy_call(getter_setter, __mpy_tuple_assign(0, __mpy_obj_init_int(133), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(myClass); + + __mpy_obj_ref_dec(returnValueGetter); + returnValueGetter = __mpy_call(__mpy_obj_get_attr(myClass, "getX"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(returnValueGetter); + + print_mpyobj_int(returnValueGetter); + + __mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(myClass, "setX"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL)); + + __mpy_obj_ref_dec(returnValueGetter); + returnValueGetter = __mpy_call(__mpy_obj_get_attr(myClass, "getX"), __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(returnValueGetter); + + __mpy_obj_ref_dec(myClass); + __mpy_obj_ref_dec(returnValueGetter); + + __mpy_obj_ref_dec(getter_setter); + + print_mpyobj_int(returnValueGetter); + + __mpy_builtins_cleanup(); + + return (*(int*)(returnValueGetter->content)); +} + +TEST_CASE("CLASS MEMBERS"){ + CHECK(getX_from_class() == 1); + CHECK(setX_from_class() == 1); +} diff --git a/c-runtime/test/Systemtests/ComparisonOps/TestComparisonOps.cpp b/c-runtime/test/Systemtests/ComparisonOps/TestComparisonOps.cpp new file mode 100644 index 0000000..a26c520 --- /dev/null +++ b/c-runtime/test/Systemtests/ComparisonOps/TestComparisonOps.cpp @@ -0,0 +1,73 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 eq_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), "__eq__"), __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("[EQ] i : %d --- j : %d\n",i,j); + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (i == j) ? 1 : 0; +} + +int ne_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), "__ne__"), __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("[NE] 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 EQ OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 5); + CHECK(eq_op(i, j) == 1); +} + +TEST_CASE("GENERATE NE OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 5); + CHECK(ne_op(i, j) == 1); +} diff --git a/c-runtime/test/Systemtests/ComparisonOps/TestGreaterEqualOp.cpp b/c-runtime/test/Systemtests/ComparisonOps/TestGreaterEqualOp.cpp new file mode 100644 index 0000000..3c2e1ce --- /dev/null +++ b/c-runtime/test/Systemtests/ComparisonOps/TestGreaterEqualOp.cpp @@ -0,0 +1,45 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 ge_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), "__ge__"), __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("[GE] 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 GE OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 5); + CHECK(ge_op(i, j) == 1); +} diff --git a/c-runtime/test/Systemtests/ComparisonOps/TestGreaterOp.cpp b/c-runtime/test/Systemtests/ComparisonOps/TestGreaterOp.cpp new file mode 100644 index 0000000..ace2583 --- /dev/null +++ b/c-runtime/test/Systemtests/ComparisonOps/TestGreaterOp.cpp @@ -0,0 +1,46 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 gt_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), "__gt__"), __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("[GT] 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 GT OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 6); + CHECK(gt_op(i, j) == 1); +} diff --git a/c-runtime/test/Systemtests/ComparisonOps/TestLessEqualOp.cpp b/c-runtime/test/Systemtests/ComparisonOps/TestLessEqualOp.cpp new file mode 100644 index 0000000..18a2402 --- /dev/null +++ b/c-runtime/test/Systemtests/ComparisonOps/TestLessEqualOp.cpp @@ -0,0 +1,45 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 le_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), "__le__"), __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("[LE] 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 LE OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 5); + CHECK(le_op(i, j) == 1); +} diff --git a/c-runtime/test/Systemtests/ComparisonOps/TestLessOp.cpp b/c-runtime/test/Systemtests/ComparisonOps/TestLessOp.cpp new file mode 100644 index 0000000..93609a4 --- /dev/null +++ b/c-runtime/test/Systemtests/ComparisonOps/TestLessOp.cpp @@ -0,0 +1,46 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 lt_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), "__lt__"), __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("[LT] 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 LT OP"){ + auto i = GENERATE(1, 3, 5); + auto j = GENERATE(2, 4, 5); + CHECK(lt_op(i, j) == 1); +} diff --git a/c-runtime/test/Systemtests/Conditions/TestElifStatement.cpp b/c-runtime/test/Systemtests/Conditions/TestElifStatement.cpp new file mode 100644 index 0000000..c347e5c --- /dev/null +++ b/c-runtime/test/Systemtests/Conditions/TestElifStatement.cpp @@ -0,0 +1,61 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 elif_statement(int i, const char* expected){ + __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("Skipped if!"); + __mpy_obj_ref_inc(a); + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside if!"); + __mpy_obj_ref_inc(a); + }else if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside elif!"); + __mpy_obj_ref_inc(a); + }else { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside else!"); + __mpy_obj_ref_inc(a); + } + __mpy_obj_ref_dec(a); + + printf("[ELIF STATEMENT] if condition : %d, expected : %s\n",i, expected); + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +TEST_CASE("ELIF CONDITION") { + auto i = 0; + SECTION("elif(false)") { + CHECK(elif_statement(i,"Went inside else!") == 0); + } + i = 1; + SECTION("elif(true)") { + CHECK(elif_statement(i,"Went inside elif!") == 0); + } +} diff --git a/c-runtime/test/Systemtests/Conditions/TestElseStatement.cpp b/c-runtime/test/Systemtests/Conditions/TestElseStatement.cpp new file mode 100644 index 0000000..0fbea33 --- /dev/null +++ b/c-runtime/test/Systemtests/Conditions/TestElseStatement.cpp @@ -0,0 +1,57 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 else_statement(int i, const char* expected){ + __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("Skipped if!"); + __mpy_obj_ref_inc(a); + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside if!"); + __mpy_obj_ref_inc(a); + }else { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside else!"); + __mpy_obj_ref_inc(a); + } + __mpy_obj_ref_dec(a); + + printf("[ELSE STATEMENT] if condition : %d, expected : %s\n",i, expected); + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +TEST_CASE("ELSE CONDITION") { + auto i = 0; + SECTION("else") { + CHECK(else_statement(i,"Went inside else!") == 0); + } + i = 1; + SECTION("else") { + CHECK(else_statement(i,"Went inside if!") == 0); + } +} diff --git a/c-runtime/test/Systemtests/Conditions/TestIfStatement.cpp b/c-runtime/test/Systemtests/Conditions/TestIfStatement.cpp new file mode 100644 index 0000000..12e7fa2 --- /dev/null +++ b/c-runtime/test/Systemtests/Conditions/TestIfStatement.cpp @@ -0,0 +1,59 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 if_statement(int i, const char* expected){ + __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("Skipped if!"); + __mpy_obj_ref_inc(a); + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_str_static("Went inside if!"); + __mpy_obj_ref_inc(a); + } + __mpy_obj_ref_dec(a); + + printf("[IF STATEMENT] If condition : %d, expected : %s\n",i, expected); + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} +/* +TEST_CASE("GENERATE IF CONDITION"){ + auto i = GENERATE(0, 1); + auto j = GENERATE("Skipped if!", "Went inside if!"); + CHECK(if_statement(i, j) == 0); +}*/ + +TEST_CASE("IF CONDITION") { + auto i = 0; + SECTION("if(false)") { + CHECK(if_statement(i, "Skipped if!") == 0); + } + i = 1; + SECTION("if(true)") { + CHECK(if_statement(i,"Went inside if!") == 0); + } +} diff --git a/c-runtime/test/Systemtests/Conditions/TestWhileStatement.cpp b/c-runtime/test/Systemtests/Conditions/TestWhileStatement.cpp new file mode 100644 index 0000000..6fa6a84 --- /dev/null +++ b/c-runtime/test/Systemtests/Conditions/TestWhileStatement.cpp @@ -0,0 +1,50 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 while_statement(){ + __MPyObj *a; + int counter = 0; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_int(3); + __mpy_obj_ref_inc(a); + + while (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_call(__mpy_obj_get_attr(a, "__gt__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(a); + a = __mpy_call(__mpy_obj_get_attr(a, "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + counter = counter +1; + } + __mpy_obj_ref_dec(a); + + printf("Visited while loop : %d times", counter); + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (counter == 2) ? 1 : 0; +} + +TEST_CASE( "TEST WHILE STATEMENT" ){ + REQUIRE(while_statement() == 1); +} diff --git a/c-runtime/test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.cpp b/c-runtime/test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.cpp new file mode 100644 index 0000000..53b81c2 --- /dev/null +++ b/c-runtime/test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.cpp @@ -0,0 +1,144 @@ +// 010-TestCase.cpp + +// Let Catch provide main(): +#define CATCH_CONFIG_MAIN +//#define CATCH_CONFING_RUNNER + +#include + +#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" +#include "literals/str.h" + +int declaration_checked_malloc() { + __MPyObj *a; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + print_mpyobj(a); + + __mpy_builtins_cleanup(); + + return a != NULL; // This will never be the case since checked_malloc will terminate the program if malloc fails. +} + +int declaration_assigned_id() { + __MPyObj *a; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + print_mpyobj(a); + + __mpy_builtins_cleanup(); + + return (a->id == 123) ? 1 : 0; +} + +int assignment_int_literal(int i){ + __MPyObj *a; + + __mpy_builtins_setup(); + + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_int(i); + __mpy_obj_ref_inc(a); + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == i ? 1 : 0; +} + +int assignment_string_literal(){ + __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("stringVal"); + __mpy_obj_ref_inc(a); + + print_mpyobj_str(a); + + __mpy_obj_ref_dec(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, "stringVal"); +} + +int assignment_bool_literal(){ + __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(true); + __mpy_obj_ref_inc(a); + + print_mpyobj_bool(a); + + __mpy_obj_ref_dec(a); + + __mpy_builtins_cleanup(); + return (((MPyBooleanContent*)a->content)->value) ? 1 : 0; +} + +int assignment_tuple_literal(){ +// see literals/tuple.c + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + a = __mpy_tuple_assign(0, __mpy_obj_init_int(123), __mpy_tuple_assign(1, __mpy_obj_init_str_static("stringVal"), __mpy_tuple_assign(2, __mpy_obj_init_boolean(true), __mpy_obj_init_tuple(3)))); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE( "TEST" ){ + REQUIRE(declaration_checked_malloc() == 1); + CHECK(declaration_assigned_id() == 1); + CHECK(assignment_int_literal(133) == 1); + CHECK(assignment_string_literal() == 0); // 0 means the const char* are the same + CHECK(assignment_bool_literal() == 1); +} + +TEST_CASE("GENERATE"){ + auto i = GENERATE(1, 3, 5); + CHECK(assignment_int_literal(i) == 1); +} diff --git a/c-runtime/test/Systemtests/FunctionCalls/TestFunctionBody.cpp b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionBody.cpp new file mode 100644 index 0000000..044b61b --- /dev/null +++ b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionBody.cpp @@ -0,0 +1,174 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/FunctionCalls/TestFunctionBodyHelpers.h" + +__MPyObj *minimalistic_body; + +__MPyObj *print_from_func; + +__MPyObj *return_local_var; + +__MPyObj *return_param_body; + +__MPyObj *return_add; + +__MPyObj *return_add_conditional; + +int minimal_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + minimalistic_body = __mpy_obj_init_func(&func_minimalistic_body); + __mpy_obj_ref_inc(minimalistic_body); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(minimalistic_body, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(minimalistic_body); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)); +} + +int print_from_func_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + print_from_func = __mpy_obj_init_func(&func_print_from_func); + __mpy_obj_ref_inc(print_from_func); + + __mpy_obj_ref_dec(a); + a = __mpy_call(print_from_func, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(print_from_func); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)); +} + +int local_var_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_local_var = __mpy_obj_init_func(&func_return_local_var); + __mpy_obj_ref_inc(return_local_var); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_local_var, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(return_local_var); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)); +} + +int param_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_param_body = __mpy_obj_init_func(&func_return_param_body); + __mpy_obj_ref_inc(return_param_body); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_param_body, __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(return_param_body); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)); +} + +int return_add_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_add = __mpy_obj_init_func(&func_return_add); + __mpy_obj_ref_inc(return_add); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_add, __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_tuple_assign(1, __mpy_obj_init_int(3), __mpy_obj_init_tuple(2))), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(return_add); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == 4 ? 1 : 0; +} + +int return_add_conditional_body(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_add_conditional = __mpy_obj_init_func(&func_return_add_conditional); + __mpy_obj_ref_inc(return_add_conditional); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_add_conditional, __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_tuple_assign(1, __mpy_obj_init_int(3), __mpy_obj_init_tuple(2))), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(return_add_conditional); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == 4 ? 1 : 0; +} + +TEST_CASE("DIFFERENT FUNCTION BODIES"){ + CAPTURE(minimal_body()); + CAPTURE(print_from_func_body()); // CAPTURE prints one additional time. + + CHECK(minimal_body() == 1); + CHECK(print_from_func_body() == 1); + CHECK(local_var_body() == 1); + CHECK(param_body() == 1); + CHECK(return_add_body() == 1); + CHECK(return_add_conditional_body() == 1); +} diff --git a/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParams.cpp b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParams.cpp new file mode 100644 index 0000000..2488a59 --- /dev/null +++ b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParams.cpp @@ -0,0 +1,207 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/FunctionCalls/TestFunctionParamsHelpers.h" + +__MPyObj *func1; + +__MPyObj *func2; + +__MPyObj *func3; + +__MPyObj *func4; + +int empty_params(const char* expected){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + func1 = __mpy_obj_init_func(&func_func1); + __mpy_obj_ref_inc(func1); + + __mpy_obj_ref_dec(a); + a = __mpy_call(func1, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(func1); + + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, expected); +} + +int one_param(__MPyObj *param1, __MPyObj *expected, int isNumeric){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + func2 = __mpy_obj_init_func(&func_func2); + __mpy_obj_ref_inc(func2); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(func2, __mpy_tuple_assign(0, param1, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(func2); + + if(!isNumeric){ + print_mpyobj_str(a); + return strcmp(((__MPyStrContent*)a->content)->string, ((__MPyStrContent*)expected->content)->string); + } + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (*(int*)(expected->content)) ? 1 : 0; +} + +int one_param_print_call(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + func4 = __mpy_obj_init_func(&func_func4); + __mpy_obj_ref_inc(func4); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(func4, __mpy_tuple_assign(0, __mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("Wuppie"), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(func4); + + __mpy_builtins_cleanup(); + + return 0; +} + +int multiple_params(__MPyObj *param1, __MPyObj *param2, __MPyObj *expected, int isNumeric){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + func3 = __mpy_obj_init_func(&func_func3); + __mpy_obj_ref_inc(func3); + + __mpy_obj_ref_dec(a); + a = __mpy_call(func3, __mpy_tuple_assign(0, param1, __mpy_tuple_assign(1, param2, __mpy_obj_init_tuple(2))), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(func3); + + if(!isNumeric){ + print_mpyobj_str(a); + + __mpy_builtins_cleanup(); + + return strcmp(((__MPyStrContent*)a->content)->string, ((__MPyStrContent*)expected->content)->string); + } + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (*(int*)(expected->content)) ? 1 : 0; +} + +TEST_CASE("TEST FUNCTION EMPTY PARAMS") +{ + CHECK(empty_params("Function called !") == 0); + CHECK(one_param_print_call() == 0); // This does not crash return value is irrelevant. +} + +TEST_CASE("TEST FUNCTION ONE PARAM") +{ + auto [input, expected_output, isNumeric] = GENERATE(table<__MPyObj *, __MPyObj *, int>({ + { __mpy_obj_init_int(123), __mpy_obj_init_int(123), 1}, + { __mpy_obj_init_boolean(true), __mpy_obj_init_boolean(true), 1}, + { __mpy_obj_init_str_static("String"), __mpy_obj_init_str_static("String"), 0}, + })); + + __MPyObj *param1; + __MPyObj *expected; + + param1 = __mpy_obj_init_object(); + __mpy_obj_ref_inc(param1); + + __mpy_obj_ref_dec(param1); + param1 = input; + __mpy_obj_ref_inc(param1); + + expected = __mpy_obj_init_object(); + __mpy_obj_ref_inc(expected); + + __mpy_obj_ref_dec(expected); + expected = expected_output; + __mpy_obj_ref_inc(expected); + + CAPTURE(input); + if(!isNumeric) + CHECK(one_param(param1, expected, isNumeric) == 0); + else + CHECK(one_param(param1, expected, isNumeric) == 1); +} + +TEST_CASE("TEST FUNCTION MULTIPLE PARAMS") +{ + auto [input1, input2, expected_output, isNumeric] = GENERATE(table<__MPyObj *, __MPyObj *, __MPyObj *, int>({ + { __mpy_obj_init_int(1), __mpy_obj_init_int(2), __mpy_obj_init_int(3), 1}, + { __mpy_obj_init_str_static("Hello"), __mpy_obj_init_str_static(" World !"), __mpy_obj_init_str_static("Hello World !"), 0}, + })); + + __MPyObj *param1; + __MPyObj *param2; + __MPyObj *expected; + + param1 = __mpy_obj_init_object(); + __mpy_obj_ref_inc(param1); + + __mpy_obj_ref_dec(param1); + param1 = input1; + __mpy_obj_ref_inc(param1); + + param2 = __mpy_obj_init_object(); + __mpy_obj_ref_inc(param2); + + __mpy_obj_ref_dec(param2); + param2 = input2; + __mpy_obj_ref_inc(param2); + + expected = __mpy_obj_init_object(); + __mpy_obj_ref_inc(expected); + + __mpy_obj_ref_dec(expected); + expected = expected_output; + __mpy_obj_ref_inc(expected); + + CAPTURE(input1); + CAPTURE(input2); + + if(!isNumeric) + CHECK(multiple_params(param1, param2, expected, isNumeric) == 0); + else + CHECK(multiple_params(param1, param2, expected, isNumeric) == 1); +} diff --git a/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParamsThrows.cpp b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParamsThrows.cpp new file mode 100644 index 0000000..29776ec --- /dev/null +++ b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionParamsThrows.cpp @@ -0,0 +1,142 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/FunctionCalls/TestFunctionParamsHelpers.h" + +__MPyObj *func_throws1; +__MPyObj *throws2; +__MPyObj *throws3; +__MPyObj *throws4; +__MPyObj *throws5; + +void exit(int status); + +int no_param_but_provided(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + func_throws1 = __mpy_obj_init_func(&func_func_throws1); + __mpy_obj_ref_inc(func_throws1); + + __mpy_obj_ref_dec(a); + a = __mpy_call(func_throws1, __mpy_tuple_assign(0, __mpy_obj_init_int(123), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(func_throws1); + + __mpy_builtins_cleanup(); + + return 0; +} + +int one_param_but_none_provided(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + throws2 = __mpy_obj_init_func(&func_throws2); + __mpy_obj_ref_inc(throws2); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(throws2, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(throws2); + + __mpy_builtins_cleanup(); + + return 0; +} + +int one_param_but_two_provided(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + throws3 = __mpy_obj_init_func(&func_throws3); + __mpy_obj_ref_inc(throws3); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(throws3, __mpy_tuple_assign(0, __mpy_obj_init_str_static("String1"), __mpy_tuple_assign(1, __mpy_obj_init_str_static("String2"), __mpy_obj_init_tuple(2))), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(throws3); + + __mpy_builtins_cleanup(); + return 0; +} + +int one_param_but_void_function_provided(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + throws4 = __mpy_obj_init_func(&func_throws4); + __mpy_obj_ref_inc(throws4); + + __mpy_obj_ref_dec(a); + a = __mpy_call(print, __mpy_tuple_assign(0, __mpy_call(throws4, __mpy_tuple_assign(0, __mpy_call(print, __mpy_obj_init_tuple(0), NULL), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(throws4); + + __mpy_builtins_cleanup(); + return 0; +} + +int two_params_but_one_provided(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + throws5 = __mpy_obj_init_func(&func_throws5); + __mpy_obj_ref_inc(throws5); + + __mpy_obj_ref_dec(a); + a = __mpy_call(throws5, __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(throws5); + + __mpy_builtins_cleanup(); + return 0; +} + +TEST_CASE("TEST FUNCTION THROWS") +{ + CHECK_THROWS_AS(no_param_but_provided(), std::runtime_error); + CHECK_THROWS_AS(one_param_but_none_provided(), std::runtime_error); + CHECK_THROWS_AS(one_param_but_two_provided(), std::runtime_error); + CHECK_THROWS_AS(one_param_but_void_function_provided(), std::runtime_error); // This belongs to built in print testing! + CHECK_THROWS_AS(two_params_but_one_provided(), std::runtime_error); +} + +void exit(int status) { + throw std::runtime_error(""); +} diff --git a/c-runtime/test/Systemtests/FunctionCalls/TestFunctionRecursion.cpp b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionRecursion.cpp new file mode 100644 index 0000000..f474a98 --- /dev/null +++ b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionRecursion.cpp @@ -0,0 +1,37 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/FunctionCalls/TestFunctionRecursionHelpers.h" + +int rec(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + recursion = __mpy_obj_init_func(&func_recursion); + __mpy_obj_ref_inc(recursion); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(recursion, __mpy_tuple_assign(0, __mpy_obj_init_int(5), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(recursion); + + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == 0 ? 1 : 0; +} + +TEST_CASE("FUNCTION RECURSION"){ + CHECK(rec() == 1); +} diff --git a/c-runtime/test/Systemtests/FunctionCalls/TestFunctionReturn.cpp b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionReturn.cpp new file mode 100644 index 0000000..756e796 --- /dev/null +++ b/c-runtime/test/Systemtests/FunctionCalls/TestFunctionReturn.cpp @@ -0,0 +1,91 @@ +#define CATCH_CONFIG_MAIN + +#include + +#include "catch_amalgamated.hpp" +#include "test/test_helpers.h" +#include "test/FunctionCalls/TestFunctionReturnHelpers.h" + +__MPyObj *return_expression; + +__MPyObj *return_tuple_literal_throws; + +__MPyObj *return_void_function_call_print; + +int return_expr(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_expression = __mpy_obj_init_func(&func_return_expression); + __mpy_obj_ref_inc(return_expression); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_expression, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + __mpy_obj_ref_dec(return_expression); + + __mpy_builtins_cleanup(); + return (*(int*)(a->content)); +} + +int return_tuple_literal(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_tuple_literal_throws = __mpy_obj_init_func(&func_return_tuple_literal_throws); + __mpy_obj_ref_inc(return_tuple_literal_throws); + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_tuple_literal_throws, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj(a); + + __mpy_obj_ref_dec(return_tuple_literal_throws); + + __mpy_builtins_cleanup(); + return 1; +} + +int return_print(){ + __MPyObj *a; + + __mpy_builtins_setup(); + a = __mpy_obj_init_object(); + __mpy_obj_ref_inc(a); + + return_void_function_call_print = __mpy_obj_init_func(&func_return_void_function_call_print); + __mpy_obj_ref_inc(return_void_function_call_print); + + + __mpy_obj_ref_dec(a); + a = __mpy_call(return_void_function_call_print, __mpy_obj_init_tuple(0), NULL); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + print_mpyobj(a); + + __mpy_obj_ref_dec(return_void_function_call_print); + + __mpy_builtins_cleanup(); + return 1; +} + +TEST_CASE("RETURN VALUES"){ + CHECK(return_expr() == 1); //This is actually return_mpyobj when the expression is evaluated... Also if you call func or return string, bool, int literal ... + CHECK(return_tuple_literal() == 1); // Returning does not throw an error but trying to print the "returned" values will result in a crashing program. See TestFunctionReturn.java + CHECK(return_print() == 1); // "" +} // Testing return of literals is already done in TestFunctionParams.cpp diff --git a/c-runtime/test/Systemtests/LogicalOps/TestLogicalOps.cpp b/c-runtime/test/Systemtests/LogicalOps/TestLogicalOps.cpp new file mode 100644 index 0000000..510c04b --- /dev/null +++ b/c-runtime/test/Systemtests/LogicalOps/TestLogicalOps.cpp @@ -0,0 +1,101 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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 and_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_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL)) && __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(j), "__bool__"), __mpy_obj_init_tuple(0), NULL))); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + printf("[AND] i : %d --- j : %d\n",i,j); + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (i && j) ? 1 : 0; + +} + +int or_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_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL)) || __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(j), "__bool__"), __mpy_obj_init_tuple(0), NULL))); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + printf("[OR] i : %d --- j : %d\n",i,j); + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (i || j) ? 1 : 0; + +} + +int not_op(int i){ + __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(!__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(i), "__bool__"), __mpy_obj_init_tuple(0), NULL))); + __mpy_obj_ref_inc(a); + + __mpy_obj_ref_dec(a); + + printf("[NOT] i : %d\n",i); + print_mpyobj_int(a); + + __mpy_builtins_cleanup(); + + return (*(int*)(a->content)) == (!i) ? 1 : 0; +} + +TEST_CASE("GENERATE AND OP"){ + auto i = GENERATE(0, 1); + auto j = GENERATE(0, 1); + CHECK(and_op(i, j) == 1); +} + +TEST_CASE("GENERATE OR OP"){ + auto i = GENERATE(0, 1); + auto j = GENERATE(0, 1); + CHECK(or_op(i, j) == 1); +} + +/*TEST_CASE("GENERATE NOT OP"){ + auto i = GENERATE(0,1); + CHECK(not_op(i) == 1); +}*/ diff --git a/c-runtime/test/Systemtests/makefile b/c-runtime/test/Systemtests/makefile new file mode 100644 index 0000000..9060736 --- /dev/null +++ b/c-runtime/test/Systemtests/makefile @@ -0,0 +1,165 @@ +UNAME := $(shell uname) + +CC = test + +ifeq ($(UNAME), Darwin) + CC = g++-13 +endif +ifeq ($(UNAME), Linux) + CC = g++ +endif + +RUNDECLA := /test/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.out +RUNARITOPS := /test/Systemtests/ArithmeticOps/TestArithmeticOps.out +RUNCOMPOPS := /test/Systemtests/ComparisonOps/TestComparisonOps.out +RUNGEOP := /test/Systemtests/ComparisonOps/TestGreaterEqualOp.out +RUNGTOP := /test/Systemtests/ComparisonOps/TestGreaterOp.out +RUNLEOP := /test/Systemtests/ComparisonOps/TestLessEqualOp.out +RUNLTOP := /test/Systemtests/ComparisonOps/TestLessOp.out +RUNLOGOPS := /test/Systemtests/LogicalOps/TestLogicalOps.out +RUNIFST := /test/Systemtests/Conditions/TestIfStatement.out +RUNELST := /test/Systemtests/Conditions/TestElseStatement.out +RUNELIFST := /test/Systemtests/Conditions/TestElifStatement.out +RUNWHILEST := /test/Systemtests/Conditions/TestWhileStatement.out +RUNCASTINT := /test/Systemtests/BuiltInFunctions/TestCastInt.out +RUNCASTSTRING := /test/Systemtests/BuiltInFunctions/TestCastString.out +RUNID := /test/Systemtests/BuiltInFunctions/TestId.out +RUNPRINT := /test/Systemtests/BuiltInFunctions/TestPrint.out +RUNTYPE := /test/Systemtests/BuiltInFunctions/TestType.out +RUNFUNPARAMS := /test/Systemtests/FunctionCalls/TestFunctionParams.out +RUNFUNPARAMSTHROWS := /test/Systemtests/FunctionCalls/TestFunctionParamsThrows.out +RUNFUNBODY := /test/Systemtests/FunctionCalls/TestFunctionBody.out +RUNFUNRET := /test/Systemtests/FunctionCalls/TestFunctionReturn.out +RUNFUNREC := /test/Systemtests/FunctionCalls/TestFunctionRecursion.out +RUNCLASSINIT := /test/Systemtests/Classes/TestClassInit.out +RUNCLASSINITTHROWS := /test/Systemtests/Classes/TestClassInitThrows.out +RUNCLASSMEMBERS := /test/Systemtests/Classes/TestClassMembers.out +RUNCLASSINHERITANCE := /test/Systemtests/Classes/TestClassInheritance.out + +.PHONY : all run echo test_declaration_and_assignment test_arithmetic_ops test_comparison_ops test_greater_equal_op test_greater_op test_less_equal_op test_less_op test_logical_ops test_if_statement test_else_statement test_elif_statement test_while_statement test_cast_int test_cast_string test_id test_print test_type test_function_body test_function_return test_function_recursion test_class_init test_class_members test_class_inheritance clean +#Disabled tests: test_function_params, test_function_params_throws, test_class_init_throws +#To Enable add to all target, .PHONY and remove comment from run target! +all: test_declaration_and_assignment test_arithmetic_ops test_comparison_ops test_greater_equal_op test_greater_op test_less_equal_op test_less_op test_logical_ops test_if_statement test_else_statement test_elif_statement test_while_statement test_cast_int test_cast_string test_print test_type test_function_body test_function_return test_function_recursion test_class_init test_class_members test_class_inheritance run + +run: + .$(RUNDECLA) + .$(RUNARITOPS) + .$(RUNCOMPOPS) + .$(RUNGEOP) + .$(RUNGTOP) + .$(RUNLEOP) + .$(RUNLTOP) + .$(RUNLOGOPS) + .$(RUNIFST) + .$(RUNELST) + .$(RUNELIFST) + .$(RUNWHILEST) + .$(RUNCASTINT) + .$(RUNCASTSTRING) + .$(RUNPRINT) + .$(RUNTYPE) + #.$(RUNFUNPARAMS) + #.$(RUNFUNPARAMSTHROWS) + .$(RUNFUNBODY) + .$(RUNFUNRET) + .$(RUNFUNREC) + .$(RUNCLASSINIT) + #.$(RUNCLASSINITTHROWS) + .$(RUNCLASSMEMBERS) + .$(RUNCLASSINHERITANCE) + +echo: + @echo $(C_FILES_TEST) + @echo $(C_FILES) + @echo $(TESTDIR) + @echo $(RUN) + @echo $(UNAME) + @echo $(CC) + @echo $(INCLUDEDIRTEST) + +test_declaration_and_assignment: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.out -w -fpermissive $(TESTDIR)/Systemtests/DeclarationAndAssignment/TestDeclarationAndAssignment.c* $(C_FILES_TEST) $(C_FILES) + +test_arithmetic_ops: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ArithmeticOps/TestArithmeticOps.out -w -fpermissive $(TESTDIR)/Systemtests/ArithmeticOps/TestArithmeticOps.c* $(C_FILES_TEST) $(C_FILES) + +test_comparison_ops: + $(CC) --version + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ComparisonOps/TestComparisonOps.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestComparisonOps.c* $(C_FILES_TEST) $(C_FILES) + +test_greater_equal_op: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ComparisonOps/TestGreaterEqualOp.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestGreaterEqualOp.c* $(C_FILES_TEST) $(C_FILES) + +test_greater_op: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ComparisonOps/TestGreaterOp.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestGreaterOp.c* $(C_FILES_TEST) $(C_FILES) + +test_less_equal_op: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ComparisonOps/TestLessEqualOp.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestLessEqualOp.c* $(C_FILES_TEST) $(C_FILES) + +test_less_op: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/ComparisonOps/TestLessOp.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestLessOp.c* $(C_FILES_TEST) $(C_FILES) + +test_logical_ops: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/LogicalOps/TestLogicalOps.out -w -fpermissive $(TESTDIR)/Systemtests/LogicalOps/TestLogicalOps.c* $(C_FILES_TEST) $(C_FILES) + +test_if_statement: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Conditions/TestIfStatement.out -w -fpermissive $(TESTDIR)/Systemtests/Conditions/TestIfStatement.c* $(C_FILES_TEST) $(C_FILES) + +test_else_statement: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Conditions/TestElseStatement.out -w -fpermissive $(TESTDIR)/Systemtests/Conditions/TestElseStatement.c* $(C_FILES_TEST) $(C_FILES) + +test_elif_statement: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Conditions/TestElifStatement.out -w -fpermissive $(TESTDIR)/Systemtests/Conditions/TestElifStatement.c* $(C_FILES_TEST) $(C_FILES) + +test_while_statement: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Conditions/TestWhileStatement.out -w -fpermissive $(TESTDIR)/Systemtests/Conditions/TestWhileStatement.c* $(C_FILES_TEST) $(C_FILES) + +test_cast_int: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/BuiltInFunctions/TestCastInt.out -w -fpermissive $(TESTDIR)/Systemtests/BuiltInFunctions/TestCastInt.c* $(C_FILES_TEST) $(C_FILES) + +test_cast_string: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/BuiltInFunctions/TestCastString.out -w -fpermissive $(TESTDIR)/Systemtests/BuiltInFunctions/TestCastString.c* $(C_FILES_TEST) $(C_FILES) + +test_id: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/BuiltInFunctions/TestId.out -w -fpermissive $(TESTDIR)/Systemtests/BuiltInFunctions/TestId.c* $(C_FILES_TEST) $(C_FILES) + +test_print: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/BuiltInFunctions/TestPrint.out -w -fpermissive $(TESTDIR)/Systemtests/BuiltInFunctions/TestPrint.c* $(C_FILES_TEST) $(C_FILES) + +test_type: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/BuiltInFunctions/TestType.out -w -fpermissive $(TESTDIR)/Systemtests/BuiltInFunctions/TestType.c* $(C_FILES_TEST) $(C_FILES) + +test_function_params: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionParams.out -w -fpermissive $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionParams.c* $(C_FILES_TEST) $(C_FILES) + +test_function_params_throws: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionParamsThrows.out -w -fpermissive $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionParamsThrows.c* $(C_FILES_TEST) $(C_FILES) + +test_function_body: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionBody.out -w -fpermissive $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionBody.c* $(C_FILES_TEST) $(C_FILES) + +test_function_return: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionReturn.out -w -fpermissive $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionReturn.c* $(C_FILES_TEST) $(C_FILES) + +test_function_recursion: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionRecursion.out -w -fpermissive $(TESTDIR)/Systemtests/FunctionCalls/TestFunctionRecursion.c* $(C_FILES_TEST) $(C_FILES) + +test_class_init: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Classes/TestClassInit.out -w -fpermissive $(TESTDIR)/Systemtests/Classes/TestClassInit.c* $(C_FILES_TEST) $(C_FILES) + +test_class_init_throws: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Classes/TestClassInitThrows.out -w -fpermissive $(TESTDIR)/Systemtests/Classes/TestClassInitThrows.c* $(C_FILES_TEST) $(C_FILES) + +test_class_members: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Classes/TestClassMembers.out -w -fpermissive $(TESTDIR)/Systemtests/Classes/TestClassMembers.c* $(C_FILES_TEST) $(C_FILES) + +test_class_inheritance: + $(CC) --version + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/Classes/TestClassInheritance.out -w -fpermissive $(TESTDIR)/Systemtests/Classes/TestClassInheritance.c* $(C_FILES_TEST) $(C_FILES) + +test_wuppie: + $(CC) -I $(INCLUDEDIR) -I $(INCLUDEDIRTEST) -o $(TESTDIR)/Systemtests/wuppie.out -w -fpermissive $(TESTDIR)/Systemtests/wuppie.c* $(C_FILES_TEST) $(C_FILES) + +clean: + $(RM) -r $(TESTDIR)/*.out + $(RM) -r $(TESTDIR)/*/*/*.out diff --git a/c-runtime/test/Systemtests/wuppie.cpp b/c-runtime/test/Systemtests/wuppie.cpp new file mode 100644 index 0000000..a322426 --- /dev/null +++ b/c-runtime/test/Systemtests/wuppie.cpp @@ -0,0 +1,46 @@ +#define CATCH_CONFIG_MAIN + +#include + +#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" + +/* +test_comparison_ops: + g++-13 --version + g++-13 -I $(INCLUDEDIR) -o $(TESTDIR)/Systemtests/ComparisonOps/TestComparisonOps.out -w -fpermissive $(TESTDIR)/Systemtests/ComparisonOps/TestComparisonOps.c* $(C_FILES) +*/ +int test(){ + __MPyObj *a; + __MPyObj *b; + + __mpy_builtins_setup(); + + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(0), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("Somehow went here"), __mpy_obj_init_tuple(1)), NULL)); + }else if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("elif!"), __mpy_obj_init_tuple(1)), NULL)); + }else { + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_call(__mpy_obj_get_attr(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(123), "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(22), __mpy_obj_init_tuple(1)), NULL), "__lt__"), __mpy_tuple_assign(0, __mpy_obj_init_int(0), __mpy_obj_init_tuple(1)), NULL), __mpy_obj_init_tuple(1)), NULL)); + } + + + __mpy_builtins_cleanup(); + + return 0; +} + +TEST_CASE("wuppie"){ + CHECK(test() == 0); +} diff --git a/c-runtime/test/include/test/Classes/TestClassInheritanceHelpers.h b/c-runtime/test/include/test/Classes/TestClassInheritanceHelpers.h new file mode 100644 index 0000000..2a7ff72 --- /dev/null +++ b/c-runtime/test/include/test/Classes/TestClassInheritanceHelpers.h @@ -0,0 +1,22 @@ +#ifndef TEST_CLASS_INHERITANCE_HELPERS_H +#define TEST_CLASS_INHERITANCE_HELPERS_H + +#include + +#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" + +__MPyObj* func_A___init__(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_B___init__(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/Classes/TestClassInitHelpers.h b/c-runtime/test/include/test/Classes/TestClassInitHelpers.h new file mode 100644 index 0000000..5f21efb --- /dev/null +++ b/c-runtime/test/include/test/Classes/TestClassInitHelpers.h @@ -0,0 +1,24 @@ +#ifndef TEST_CLASS_INIT_HELPERS_H +#define TEST_CLASS_INIT_HELPERS_H + +#include + +#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" + +__MPyObj* func_init_no_params___init__(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_init_one_param___init__(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_init_multiple_params___init__(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/Classes/TestClassMembersHelpers.h b/c-runtime/test/include/test/Classes/TestClassMembersHelpers.h new file mode 100644 index 0000000..b093414 --- /dev/null +++ b/c-runtime/test/include/test/Classes/TestClassMembersHelpers.h @@ -0,0 +1,24 @@ +#ifndef TEST_CLASS_MEMBERS_HELPERS_H +#define TEST_CLASS_MEMBERS_HELPERS_H + +#include + +#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" + +__MPyObj* func_getter_setter___init__(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_getter_setter_getX(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_getter_setter_setX(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/FunctionCalls/TestFunctionBodyHelpers.h b/c-runtime/test/include/test/FunctionCalls/TestFunctionBodyHelpers.h new file mode 100644 index 0000000..f080bf4 --- /dev/null +++ b/c-runtime/test/include/test/FunctionCalls/TestFunctionBodyHelpers.h @@ -0,0 +1,30 @@ +#ifndef TEST_FUNCTION_BODY_HELPERS_H +#define TEST_FUNCTION_BODY_HELPERS_H + +#include + +#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" + +__MPyObj* func_minimalistic_body(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_print_from_func(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_local_var(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_param_body(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_add(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_add_conditional(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/FunctionCalls/TestFunctionParamsHelpers.h b/c-runtime/test/include/test/FunctionCalls/TestFunctionParamsHelpers.h new file mode 100644 index 0000000..5de28a6 --- /dev/null +++ b/c-runtime/test/include/test/FunctionCalls/TestFunctionParamsHelpers.h @@ -0,0 +1,36 @@ +#ifndef TEST_FUNCTION_PARAMS_HELPERS_H +#define TEST_FUNCTION_PARAMS_HELPERS_H + +#include + +#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" + +__MPyObj* func_func1(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_func2(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_func3(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_func4(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_func_throws1(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_throws2(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_throws3(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_throws4(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_throws5(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/FunctionCalls/TestFunctionRecursionHelpers.h b/c-runtime/test/include/test/FunctionCalls/TestFunctionRecursionHelpers.h new file mode 100644 index 0000000..588cb16 --- /dev/null +++ b/c-runtime/test/include/test/FunctionCalls/TestFunctionRecursionHelpers.h @@ -0,0 +1,22 @@ +#ifndef TEST_FUNCTION_RECURSION_HELPERS_H +#define TEST_FUNCTION_RECURSION_HELPERS_H + +#include + +#include "assert.h" +#include "mpy_aliases.h" +#include "mpy_obj.h" +#include "builtins-setup.h" +#include "literals/tuple.h" +#include "literals/int.h" +#include "function-args.h" +#include "literals/boolean.h" +#include "literals/str.h" +#include "type-hierarchy/object.h" +#include "type-hierarchy/type.h" + +extern __MPyObj *recursion; + +__MPyObj* func_recursion(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/FunctionCalls/TestFunctionReturnHelpers.h b/c-runtime/test/include/test/FunctionCalls/TestFunctionReturnHelpers.h new file mode 100644 index 0000000..0b83e53 --- /dev/null +++ b/c-runtime/test/include/test/FunctionCalls/TestFunctionReturnHelpers.h @@ -0,0 +1,24 @@ +#ifndef TEST_FUNCTION_RETURN_HELPERS_H +#define TEST_FUNCTION_RETURN_HELPERS_H + +#include + +#include "assert.h" +#include "mpy_aliases.h" +#include "mpy_obj.h" +#include "builtins-setup.h" +#include "literals/tuple.h" +#include "literals/int.h" +#include "function-args.h" +#include "literals/boolean.h" +#include "literals/str.h" +#include "type-hierarchy/object.h" +#include "type-hierarchy/type.h" + +__MPyObj* func_return_expression(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_tuple_literal_throws(__MPyObj *args, __MPyObj *kwargs); + +__MPyObj* func_return_void_function_call_print(__MPyObj *args, __MPyObj *kwargs); + +#endif diff --git a/c-runtime/test/include/test/test_helpers.h b/c-runtime/test/include/test/test_helpers.h new file mode 100644 index 0000000..f01d84f --- /dev/null +++ b/c-runtime/test/include/test/test_helpers.h @@ -0,0 +1,44 @@ +#ifndef TEST_HELPERS_H +#define TEST_HELPERS_H + +#include "type-hierarchy/object.h" +#include "literals/int.h" +#include "mpy_obj.h" +#include "checks.h" +#include "type-hierarchy/type.h" + +typedef bool __mpy_boolean_c_type; + +typedef struct __MPyStrContent { + bool isStatic; + const char *string; + __MPyObj *strMethod; + __MPyObj *boolMethod; + __MPyObj *addMethod; + __MPyObj *intMethod; + __MPyObj *eqMethod; + __MPyObj *neMethod; + __MPyObj *geMethod; + __MPyObj *leMethod; + __MPyObj *gtMethod; + __MPyObj *ltMethod; +} __MPyStrContent; + +typedef struct MPyBooleanContent { + __mpy_boolean_c_type value; + __MPyObj *strMethod; + __MPyObj *boolMethod; + __MPyObj *intMethod; + __MPyObj *eqMethod; + __MPyObj *neMethod; +} MPyBooleanContent; + +extern void print_mpyobj(__MPyObj *mpyobj); + +extern void print_mpyobj_int(__MPyObj *mpyobj); + +extern void print_mpyobj_str(__MPyObj *mpyobj); + +extern void print_mpyobj_bool(__MPyObj *mpyobj); + +#endif diff --git a/c-runtime/test/src/test/Classes/TestClassInheritanceHelpers.c b/c-runtime/test/src/test/Classes/TestClassInheritanceHelpers.c new file mode 100644 index 0000000..add70e3 --- /dev/null +++ b/c-runtime/test/src/test/Classes/TestClassInheritanceHelpers.c @@ -0,0 +1,45 @@ +#include "test/Classes/TestClassInheritanceHelpers.h" + +__MPyObj* func_A___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 1); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[A] Print from __init__"), __mpy_obj_init_tuple(1)), NULL)); + + __mpy_obj_ref_dec(self); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_B___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 1); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[B] Print from __init__"), __mpy_obj_init_tuple(1)), NULL)); + + __mpy_obj_ref_dec(self); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/Classes/TestClassInitHelpers.c b/c-runtime/test/src/test/Classes/TestClassInitHelpers.c new file mode 100644 index 0000000..ca681c7 --- /dev/null +++ b/c-runtime/test/src/test/Classes/TestClassInitHelpers.c @@ -0,0 +1,74 @@ +#include "test/Classes/TestClassInitHelpers.h" + + +__MPyObj* func_init_no_params___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 1); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[INIT_NO_PARAMS] Print from __init__"), __mpy_obj_init_tuple(1)), NULL)); + + __mpy_obj_ref_dec(self); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_init_one_param___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 2); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 1, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[INIT_ONE_PARAM] Print from __init__ Param Value : "), __mpy_tuple_assign(1, param1, __mpy_obj_init_tuple(2))), NULL)); + + __mpy_obj_ref_dec(self); + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_init_multiple_params___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 3); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 1, "param1"); + __MPyObj *param2 = __mpy_args_get_positional(&argHelper, 2, "param2"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[INIT_MULTIPLE_PARAMS] Print from __init__ Param1 : "), __mpy_tuple_assign(1, param1, __mpy_tuple_assign(2, __mpy_obj_init_str_static(" Param2 : "), __mpy_tuple_assign(3, param2, __mpy_obj_init_tuple(4))))), NULL)); + + __mpy_obj_ref_dec(self); + __mpy_obj_ref_dec(param1); + __mpy_obj_ref_dec(param2); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/Classes/TestClassMembersHelpers.c b/c-runtime/test/src/test/Classes/TestClassMembersHelpers.c new file mode 100644 index 0000000..205dd13 --- /dev/null +++ b/c-runtime/test/src/test/Classes/TestClassMembersHelpers.c @@ -0,0 +1,70 @@ +#include "test/Classes/TestClassMembersHelpers.h" + + +__MPyObj* func_getter_setter___init__(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 2); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __MPyObj *val = __mpy_args_get_positional(&argHelper, 1, "val"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("[B] Print from __init__"), __mpy_obj_init_tuple(1)), NULL)); + __mpy_obj_set_attr(self, "x", val); + __mpy_obj_ref_dec(self); + __mpy_obj_ref_dec(val); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_getter_setter_getX(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("getX", args, kwargs, 1); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_get_attr(self, "x"); + goto ret; + + __mpy_obj_ref_dec(self); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_getter_setter_setX(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("setX", args, kwargs, 2); + __MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + __MPyObj *newVal = __mpy_args_get_positional(&argHelper, 1, "newVal"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_set_attr(self, "x", newVal); + __mpy_obj_ref_dec(self); + __mpy_obj_ref_dec(newVal); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/FunctionCalls/TestFunctionBodyHelpers.c b/c-runtime/test/src/test/FunctionCalls/TestFunctionBodyHelpers.c new file mode 100644 index 0000000..48a1264 --- /dev/null +++ b/c-runtime/test/src/test/FunctionCalls/TestFunctionBodyHelpers.c @@ -0,0 +1,138 @@ +#include "test/FunctionCalls/TestFunctionBodyHelpers.h" + +__MPyObj* func_minimalistic_body(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("minimalistic_body", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_init_int(1); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_print_from_func(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("print_from_func", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_str_static("Print from function body"), __mpy_obj_init_tuple(1)), NULL)); + retValue = __mpy_obj_init_int(1); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_local_var(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_local_var", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + __MPyObj *y = __mpy_obj_init_object(); + __mpy_obj_ref_inc(y); + __mpy_obj_ref_dec(y); + y = __mpy_obj_init_int(1); + __mpy_obj_ref_inc(y); + retValue = y; + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_param_body(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_param_body", args, kwargs, 1); + __MPyObj *x = __mpy_args_get_positional(&argHelper, 0, "x"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = x; + goto ret; + + __mpy_obj_ref_dec(x); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_add(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_add", args, kwargs, 2); + __MPyObj *x = __mpy_args_get_positional(&argHelper, 0, "x"); + __MPyObj *y = __mpy_args_get_positional(&argHelper, 1, "y"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_call(__mpy_obj_get_attr(x, "__add__"), __mpy_tuple_assign(0, y, __mpy_obj_init_tuple(1)), NULL); + goto ret; + + __mpy_obj_ref_dec(x); + __mpy_obj_ref_dec(y); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_add_conditional(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_add_conditional", args, kwargs, 2); + __MPyObj *x = __mpy_args_get_positional(&argHelper, 0, "x"); + __MPyObj *y = __mpy_args_get_positional(&argHelper, 1, "y"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_call(__mpy_obj_get_attr(x, "__ge__"), __mpy_tuple_assign(0, __mpy_obj_init_int(0), __mpy_obj_init_tuple(1)), NULL), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + retValue = __mpy_call(__mpy_obj_get_attr(x, "__add__"), __mpy_tuple_assign(0, y, __mpy_obj_init_tuple(1)), NULL); + goto ret; + } + __mpy_obj_ref_dec(x); + __mpy_obj_ref_dec(y); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/FunctionCalls/TestFunctionParamsHelpers.c b/c-runtime/test/src/test/FunctionCalls/TestFunctionParamsHelpers.c new file mode 100644 index 0000000..692d435 --- /dev/null +++ b/c-runtime/test/src/test/FunctionCalls/TestFunctionParamsHelpers.c @@ -0,0 +1,199 @@ +#include "test/FunctionCalls/TestFunctionParamsHelpers.h" + +__MPyObj* func_func1(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("func1", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_init_str_static("Function called !"); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_func2(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("func2", args, kwargs, 1); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = param1; + goto ret; + + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_func3(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("func3", args, kwargs, 2); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __MPyObj *param2 = __mpy_args_get_positional(&argHelper, 1, "param2"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_call(__mpy_obj_get_attr(param1, "__add__"), __mpy_tuple_assign(0, param2, __mpy_obj_init_tuple(1)), NULL); + goto ret; + + __mpy_obj_ref_dec(param1); + __mpy_obj_ref_dec(param2); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_func4(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("func4", args, kwargs, 1); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = param1; + goto ret; + + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_func_throws1(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("func_throws1", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_init_str_static(""); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_throws2(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("throws2", args, kwargs, 1); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = param1; + goto ret; + + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_throws3(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("throws3", args, kwargs, 1); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = param1; + goto ret; + + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_throws4(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("throws4", args, kwargs, 1); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = param1; + goto ret; + + __mpy_obj_ref_dec(param1); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_throws5(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("throws5", args, kwargs, 2); + __MPyObj *param1 = __mpy_args_get_positional(&argHelper, 0, "param1"); + __MPyObj *param2 = __mpy_args_get_positional(&argHelper, 1, "param2"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_call(__mpy_obj_get_attr(param1, "__add__"), __mpy_tuple_assign(0, param2, __mpy_obj_init_tuple(1)), NULL); + goto ret; + + __mpy_obj_ref_dec(param1); + __mpy_obj_ref_dec(param2); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/FunctionCalls/TestFunctionRecursionHelpers.c b/c-runtime/test/src/test/FunctionCalls/TestFunctionRecursionHelpers.c new file mode 100644 index 0000000..4af2c3b --- /dev/null +++ b/c-runtime/test/src/test/FunctionCalls/TestFunctionRecursionHelpers.c @@ -0,0 +1,32 @@ +#include "test/FunctionCalls/TestFunctionRecursionHelpers.h" + +__MPyObj *recursion; + +__MPyObj* func_recursion(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("recursion", args, kwargs, 1); + __MPyObj *x = __mpy_args_get_positional(&argHelper, 0, "x"); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_call(__mpy_obj_get_attr(x, "__gt__"), __mpy_tuple_assign(0, __mpy_obj_init_int(0), __mpy_obj_init_tuple(1)), NULL), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + __mpy_obj_ref_dec(x); + x = __mpy_call(__mpy_obj_get_attr(x, "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(x); + __mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, x, __mpy_obj_init_tuple(1)), NULL)); + retValue = __mpy_call(recursion, __mpy_tuple_assign(0, x, __mpy_obj_init_tuple(1)), NULL); + goto ret; + }retValue = x; + goto ret; + + __mpy_obj_ref_dec(x); + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/FunctionCalls/TestFunctionReturnHelpers.c b/c-runtime/test/src/test/FunctionCalls/TestFunctionReturnHelpers.c new file mode 100644 index 0000000..c920573 --- /dev/null +++ b/c-runtime/test/src/test/FunctionCalls/TestFunctionReturnHelpers.c @@ -0,0 +1,61 @@ +#include "test/FunctionCalls/TestFunctionReturnHelpers.h" + +__MPyObj* func_return_expression(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_expression", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL)) && __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_tuple_literal_throws(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_tuple_literal_throws", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_obj_init_tuple(0); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} + +__MPyObj* func_return_void_function_call_print(__MPyObj *args, __MPyObj *kwargs) { + assert(args != NULL && kwargs != NULL); + + __MPyGetArgsState argHelper = __mpy_args_init("return_void_function_call_print", args, kwargs, 0); + __mpy_args_finish(&argHelper); + + __MPyObj *retValue = NULL; + + retValue = __mpy_call(print, __mpy_obj_init_tuple(0), NULL); + goto ret; + + + goto ret; + ret: + if (retValue == NULL) { + retValue = __mpy_obj_init_object(); + } + return __mpy_obj_return(retValue); +} diff --git a/c-runtime/test/src/test/test_helpers.c b/c-runtime/test/src/test/test_helpers.c new file mode 100644 index 0000000..21ea1f8 --- /dev/null +++ b/c-runtime/test/src/test/test_helpers.c @@ -0,0 +1,30 @@ +#include "test/test_helpers.h" + +#include + +extern void print_mpyobj(__MPyObj *mpyobj){ + printf("mpyobj->mpyobj %p\n", (void*)mpyobj); + printf("mpyobj->refCount %d\n", mpyobj->refCount); + printf("mpyobj->id %d\n\n", mpyobj->id); +} + +extern void print_mpyobj_int(__MPyObj *mpyobj){ + printf("mpyobj %p\n", (void*)mpyobj); + printf("mpyobj->refCount %d\n", mpyobj->refCount); + printf("mpyobj->id %d\n", mpyobj->id); + printf("mpyobj->content value : %d\n\n", (*(int*)(mpyobj->content))); +} + +extern void print_mpyobj_str(__MPyObj *mpyobj){ + printf("mpyobj %p\n", (void*)mpyobj); + printf("mpyobj->refCount %d\n", mpyobj->refCount); + printf("mpyobj->id %d\n", mpyobj->id); + printf("mpyobj->content->string : %s\n\n", (((__MPyStrContent*)mpyobj->content)->string)); +} + +extern void print_mpyobj_bool(__MPyObj *mpyobj){ + printf("mpyobj %p\n", (void*)mpyobj); + printf("mpyobj->refCount %d\n", mpyobj->refCount); + printf("mpyobj->id %d\n", mpyobj->id); + printf("mpyobj->content->bool : %d\n\n", ((MPyBooleanContent*)mpyobj->content)->value); +} diff --git a/src/main/java/CBuilder/Expression.java b/src/main/java/CBuilder/Expression.java index e9a8eed..b8462a2 100644 --- a/src/main/java/CBuilder/Expression.java +++ b/src/main/java/CBuilder/Expression.java @@ -1,7 +1,8 @@ package CBuilder; /** - * Something that returns the C representation of a MiniPython object, e. g. a function call or a literal. + * Something that returns the C representation of a MiniPython object, e. g. a function call or a + * literal. */ public interface Expression extends Statement { @@ -11,5 +12,4 @@ public interface Expression extends Statement { * @return A String with the c-code that evaluates to the containing MiniPython object. */ String buildExpression(); - } diff --git a/src/main/java/CBuilder/ManualTest.java b/src/main/java/CBuilder/ManualTest.java index 2bf512f..fff7b70 100644 --- a/src/main/java/CBuilder/ManualTest.java +++ b/src/main/java/CBuilder/ManualTest.java @@ -6,7 +6,6 @@ import CBuilder.objects.functions.Function; import CBuilder.variables.Assignment; import CBuilder.variables.VariableDeclaration; - import java.nio.file.Path; import java.util.HashMap; import java.util.List; @@ -14,32 +13,22 @@ /** * Allows to test the Builder without involving the ast or anything. * - * TODO remove this when manual testing of this is not needed anymore + *

TODO remove this when manual testing of this is not needed anymore */ public class ManualTest { /** - * Mini-Python code of program generated: - * ```python - * a - * b + * Mini-Python code of program generated: ```python a b * - * a = b - * d = -30 + *

a = b d = -30 * - * type(a) - * type(d) + *

type(a) type(d) * - * idA = id(a) - * print(idA) + *

idA = id(a) print(idA) * - * print(id(a)) - * id(print(d)) - * id(type(a)) + *

print(id(a)) id(print(d)) id(type(a)) * - * e = id(type(print(d))) - * e = print(-50) - * ``` + *

e = id(type(print(d))) e = print(-50) ``` */ static void generateProgram(Path output) { ProgramBuilder builder = new ProgramBuilder(); @@ -50,130 +39,190 @@ static void generateProgram(Path output) { // does cause build errors with -Wall since the assignment is indeed quite useless // (and there's no good solution to ignore the specific error on assignment) -// builder.addVariable(new VariableDeclaration("c")); -// builder.addStatement(new Assignment(new VariableReference("c"), new VariableReference("c"))); + // builder.addVariable(new VariableDeclaration("c")); + // builder.addStatement(new Assignment(new VariableReference("c"), new + // VariableReference("c"))); builder.addVariable(new VariableDeclaration("d")); builder.addStatement(new Assignment(new Reference("d"), new IntLiteral(-30))); - builder.addStatement(new Call(new Reference("type"), List.of(new Expression[]{ - new Reference("a") - }))); - builder.addStatement(new Call(new Reference("type"), List.of(new Expression[]{ - new Reference("d") - }))); + builder.addStatement( + new Call(new Reference("type"), List.of(new Expression[] {new Reference("a")}))); + builder.addStatement( + new Call(new Reference("type"), List.of(new Expression[] {new Reference("d")}))); builder.addVariable(new VariableDeclaration("idA")); - builder.addStatement(new Assignment(new Reference("idA"), - new Call(new Reference("id"), - List.of(new Expression[]{ - new Reference("a") - })))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[]{ - new Reference("idA") - }))); - -// call each built-in function in a nested context to make sure it correctly implements refCounts - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[]{ - new Call(new Reference("id"), List.of(new Expression[]{ - new Reference("a") - })) - }))); - builder.addStatement(new Call(new Reference("id"), List.of(new Expression[]{ - new Call(new Reference("print"), List.of(new Expression[]{ - new Reference("d") - })) - }))); - builder.addStatement(new Call(new Reference("id"), List.of(new Expression[]{ - new Call(new Reference("type"), List.of(new Expression[]{ - new Reference("a") - })) - }))); + builder.addStatement( + new Assignment( + new Reference("idA"), + new Call( + new Reference("id"), + List.of(new Expression[] {new Reference("a")})))); + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("idA")}))); + + // call each built-in function in a nested context to make sure it correctly + // implements refCounts + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new Reference("id"), + List.of(new Expression[] {new Reference("a")})) + }))); + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("d")})) + }))); + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("a")})) + }))); // assign a nested function call to a variable builder.addVariable(new VariableDeclaration("e")); - builder.addStatement(new Call(new Reference("id"), List.of(new Expression[]{ - new Call(new Reference("type"), List.of(new Expression[]{ - new Call(new Reference("print"), List.of(new Expression[]{ - new Reference("d") - })) - })) - }))); + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("type"), + List.of( + new Expression[] { + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("d") + })) + })) + }))); // call a function on a literal and assign that to a variable - builder.addStatement(new Assignment(new Reference("e"), - new Call(new Reference("print"), - List.of(new Expression[]{ - new IntLiteral(-50) - })))); - - builder.addStatement(new Call(new Reference("type"), - List.of(new Expression[]{ - new Reference("type") - }))); - - builder.addFunction(new Function("printA", List.of(new Statement[]{ - new Call(new Reference("print"), - List.of(new Expression[]{ - new Reference("a") - })) - }), List.of(new Argument[]{ - new Argument("a", 0) - }), List.of())); - - builder.addStatement(new Call(new Reference("printA"), List.of(new Expression[]{ - new Reference("idA") - }))); - - builder.addClass(new MPyClass("B", new Reference("__MPyType_Object"), List.of(new Function[]{ - new Function("print", List.of(new Statement[]{ - new Call(new Reference("print"), - List.of(new Expression[]{ - new Reference("self") - })) - }), List.of(new Argument[]{ - new Argument("self", 0) - }), List.of()), - new Function("__init__", List.of(new Statement[] { - new SuperCall(List.of()), - new AttributeAssignment(new AttributeReference("b", new Reference("self")), new IntLiteral(100)) - }), List.of(new Argument[]{ - new Argument("self", 0) - }), List.of()) - }), new HashMap<>())); - - builder.addClass(new MPyClass("C", new Reference("B"), List.of(new Function[]{ - new Function("__init__", List.of(new Statement[] { - new SuperCall(List.of()) - }), List.of(new Argument[]{ - new Argument("self", 0) - }), List.of()) - }), new HashMap<>())); + builder.addStatement( + new Assignment( + new Reference("e"), + new Call( + new Reference("print"), + List.of(new Expression[] {new IntLiteral(-50)})))); + + builder.addStatement( + new Call(new Reference("type"), List.of(new Expression[] {new Reference("type")}))); + + builder.addFunction( + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of())); + + builder.addStatement( + new Call( + new Reference("printA"), List.of(new Expression[] {new Reference("idA")}))); + + builder.addClass( + new MPyClass( + "B", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "print", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("self") + })) + }), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()), + new Function( + "__init__", + List.of( + new Statement[] { + new SuperCall(List.of()), + new AttributeAssignment( + new AttributeReference( + "b", new Reference("self")), + new IntLiteral(100)) + }), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>())); + + builder.addClass( + new MPyClass( + "C", + new Reference("B"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>())); builder.addVariable(new VariableDeclaration("bObj")); - builder.addStatement(new Assignment(new Reference("bObj"), new Call(new Reference("B"), List.of()))); + builder.addStatement( + new Assignment(new Reference("bObj"), new Call(new Reference("B"), List.of()))); - builder.addStatement(new Call(new AttributeReference("print", new Reference("bObj")), List.of())); + builder.addStatement( + new Call(new AttributeReference("print", new Reference("bObj")), List.of())); // print(bObj.b) - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[]{ - new AttributeReference("b", new Reference("bObj")) - }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new AttributeReference("b", new Reference("bObj")) + }))); builder.addVariable(new VariableDeclaration("cObj")); - builder.addStatement(new Assignment(new Reference("cObj"), new Call(new Reference("C"), List.of()))); + builder.addStatement( + new Assignment(new Reference("cObj"), new Call(new Reference("C"), List.of()))); - builder.addStatement(new Call(new AttributeReference("print", new Reference("cObj")), List.of())); + builder.addStatement( + new Call(new AttributeReference("print", new Reference("cObj")), List.of())); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[]{ - new AttributeReference("b", new Reference("cObj")) - }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new AttributeReference("b", new Reference("cObj")) + }))); builder.writeProgram(output); } public static void main(String[] args) { - java.nio.file.Path fileOutput = java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/ManualTest/"); + java.nio.file.Path fileOutput = + java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/ManualTest/"); generateProgram(fileOutput); } } diff --git a/src/main/java/CBuilder/ProgramBuilder.java b/src/main/java/CBuilder/ProgramBuilder.java index 9647b5a..646a267 100644 --- a/src/main/java/CBuilder/ProgramBuilder.java +++ b/src/main/java/CBuilder/ProgramBuilder.java @@ -1,10 +1,11 @@ package CBuilder; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + import CBuilder.objects.MPyClass; import CBuilder.objects.functions.Function; import CBuilder.objects.functions.ReturnStatement; import CBuilder.variables.VariableDeclaration; - import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -16,59 +17,45 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; - -/** - * Entrypoint to creating C code from a MiniPython AST. - */ +/** Entrypoint to creating C code from a MiniPython AST. */ public class ProgramBuilder { - /** - * Definition of the c-runtime header files. - */ + /** Definition of the c-runtime header files. */ private static final String[] headers = { - // allow usage of assertions - "assert", - // aliases mapping python names to the internal c names - "mpy_aliases", - // ref-counting - "mpy_obj", - // initialisation - "builtins-setup", - // building function args - "function-args", - "literals/tuple", - "literals/int", - "literals/boolean", - "literals/str", - "type-hierarchy/object", - // custom objects - "type-hierarchy/type", + // allow usage of assertions + "assert", + // aliases mapping python names to the internal c names + "mpy_aliases", + // ref-counting + "mpy_obj", + // initialisation + "builtins-setup", + // building function args + "function-args", + "literals/tuple", + "literals/int", + "literals/boolean", + "literals/str", + "type-hierarchy/object", + // custom objects + "type-hierarchy/type", }; - /** - * A list of all statements in the global scope. - */ + /** A list of all statements in the global scope. */ private final List statements; - /** - * A list of all defined variables in the global scope. - */ + /** A list of all defined variables in the global scope. */ private final List globalVariables; - /** - * A list of all defined functions in the global scope. - */ + /** A list of all defined functions in the global scope. */ private final List globalFunctions; - /** - * A list of all defined classes in th global scope. - */ + /** A list of all defined classes in th global scope. */ private final List classes; /** - * Create a new program with empty global scope. - * Allows to manually specify options regarding c-code generation. + * Create a new program with empty global scope. Allows to manually specify options regarding + * c-code generation. */ public ProgramBuilder() { statements = new LinkedList<>(); @@ -78,14 +65,15 @@ public ProgramBuilder() { } /** - * Add a new statement to the global scope. - * The resulting statement list more or less represents the program's main method. + * Add a new statement to the global scope. The resulting statement list more or less represents + * the program's main method. * * @param statement The statement to add. */ public void addStatement(Statement statement) { if (statement instanceof ReturnStatement) { - // I'd prefer to have this type safe (i. e. different statement interfaces for global/non-global scope, + // I'd prefer to have this type safe (i. e. different statement interfaces for + // global/non-global scope, // but this is just a prototype after all and shouldn't happen too often anyway) throw new IllegalArgumentException("Cannot return from global scope"); } @@ -204,8 +192,11 @@ public String buildProgram() { body.append("return 0;\n"); - program.append(body.toString().lines().map(string -> "\t" + string + "\n").collect( - Collectors.joining())); + program.append( + body.toString() + .lines() + .map(string -> "\t" + string + "\n") + .collect(Collectors.joining())); program.append("}\n"); return program.toString(); @@ -235,7 +226,12 @@ public void writeProgram(Path destDir) { try { Path output = Path.of(destDir.toString(), "src/program.c"); - java.nio.file.Files.writeString(output, this.buildProgram(), java.nio.file.StandardOpenOption.CREATE, java.nio.file.StandardOpenOption.WRITE, java.nio.file.StandardOpenOption.TRUNCATE_EXISTING); + java.nio.file.Files.writeString( + output, + this.buildProgram(), + java.nio.file.StandardOpenOption.CREATE, + java.nio.file.StandardOpenOption.WRITE, + java.nio.file.StandardOpenOption.TRUNCATE_EXISTING); } catch (java.io.IOException e) { throw new RuntimeException("Failed to write 'program.c'!", e); } @@ -250,13 +246,18 @@ public void writeProgram(Path destDir) { */ public void copyFolder(Path srcDir, Path destDir) throws IOException { try (Stream stream = Files.walk(srcDir)) { - stream.forEach(source -> { - try { - Files.copy(source, destDir.resolve(srcDir.relativize(source)), REPLACE_EXISTING); - } catch (IOException e) { - throw new RuntimeException("Failed to copy template folder to output directory", e); - } - }); + stream.forEach( + source -> { + try { + Files.copy( + source, + destDir.resolve(srcDir.relativize(source)), + REPLACE_EXISTING); + } catch (IOException e) { + throw new RuntimeException( + "Failed to copy template folder to output directory", e); + } + }); } } @@ -265,29 +266,37 @@ public void copyFolder(Path srcDir, Path destDir) throws IOException { * * @param resourcePath The Path to the jar archive. * @param targetLocation The output directory. - * @throws URISyntaxException Will be thrown if the c-runtime can not be found in the application resources. + * @throws URISyntaxException Will be thrown if the c-runtime can not be found in the + * application resources. * @throws IOException Will be thrown if a needed file or folder is not accessible. */ - public void copyFolderFromJar(String resourcePath, final Path targetLocation) throws URISyntaxException, IOException { + public void copyFolderFromJar(String resourcePath, final Path targetLocation) + throws URISyntaxException, IOException { URI resource = getClass().getResource("/c-runtime").toURI(); try (FileSystem fileSystem = FileSystems.newFileSystem(resource, Map.of())) { final Path jarArchive = fileSystem.getPath(resourcePath); - Files.walkFileTree(jarArchive, new SimpleFileVisitor<>() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - Files.createDirectories(targetLocation.resolve(jarArchive.relativize(dir).toString())); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.copy(file, targetLocation.resolve(jarArchive.relativize(file).toString()), REPLACE_EXISTING); - return FileVisitResult.CONTINUE; - } - - }); + Files.walkFileTree( + jarArchive, + new SimpleFileVisitor<>() { + @Override + public FileVisitResult preVisitDirectory( + Path dir, BasicFileAttributes attrs) throws IOException { + Files.createDirectories( + targetLocation.resolve(jarArchive.relativize(dir).toString())); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { + Files.copy( + file, + targetLocation.resolve(jarArchive.relativize(file).toString()), + REPLACE_EXISTING); + return FileVisitResult.CONTINUE; + } + }); } } - } diff --git a/src/main/java/CBuilder/Reference.java b/src/main/java/CBuilder/Reference.java index df37e9f..c7a3c6b 100644 --- a/src/main/java/CBuilder/Reference.java +++ b/src/main/java/CBuilder/Reference.java @@ -1,13 +1,9 @@ package CBuilder; -/** - * Representation of a reference to a MiniPython variable, i. e. a variable name. - */ +/** Representation of a reference to a MiniPython variable, i. e. a variable name. */ public class Reference implements Expression { - /** - * The name of the reference. - */ + /** The name of the reference. */ protected String name; /** @@ -24,7 +20,9 @@ public Reference(String name) { * * @return The name of the reference. */ - public String getName() { return this.name; } + public String getName() { + return this.name; + } @Override public String buildExpression() { @@ -35,5 +33,4 @@ public String buildExpression() { public String buildStatement() { return name + ";\n"; } - } diff --git a/src/main/java/CBuilder/Statement.java b/src/main/java/CBuilder/Statement.java index e7af15a..66cc55f 100644 --- a/src/main/java/CBuilder/Statement.java +++ b/src/main/java/CBuilder/Statement.java @@ -1,7 +1,8 @@ package CBuilder; /** - * C representation of anything that does something in MiniPython, e. g. assignments, control structures, function calls. + * C representation of anything that does something in MiniPython, e. g. assignments, control + * structures, function calls. */ public interface Statement { @@ -11,5 +12,4 @@ public interface Statement { * @return A String which represents the c-code of the statement. */ String buildStatement(); - } diff --git a/src/main/java/CBuilder/conditions/IfThenElseStatement.java b/src/main/java/CBuilder/conditions/IfThenElseStatement.java index 6c7578a..ea6a04e 100644 --- a/src/main/java/CBuilder/conditions/IfThenElseStatement.java +++ b/src/main/java/CBuilder/conditions/IfThenElseStatement.java @@ -4,28 +4,19 @@ import CBuilder.conditions.conditionalStatement.ElifStatement; import CBuilder.conditions.conditionalStatement.ElseStatement; import CBuilder.conditions.conditionalStatement.IfStatement; - import java.util.List; import java.util.Optional; -/** - * A MiniPython if (/else if/else) block. - */ +/** A MiniPython if (/else if/else) block. */ public class IfThenElseStatement implements Statement { - /** - * The containing if block. - */ + /** The containing if block. */ private IfStatement ifStatement; - /** - * A list of the containing elif blocks. - */ + /** A list of the containing elif blocks. */ private Optional> elifStatements; - /** - * The containing else block. - */ + /** The containing else block. */ private Optional elseStatement; /** @@ -35,7 +26,10 @@ public class IfThenElseStatement implements Statement { * @param elifStatements An optional list of elif statements. * @param elseStatement An optional else statement. */ - public IfThenElseStatement(IfStatement ifStatement, Optional> elifStatements, Optional elseStatement) { + public IfThenElseStatement( + IfStatement ifStatement, + Optional> elifStatements, + Optional elseStatement) { this.ifStatement = ifStatement; this.elifStatements = elifStatements; this.elseStatement = elseStatement; diff --git a/src/main/java/CBuilder/conditions/conditionalStatement/ConditionalStatement.java b/src/main/java/CBuilder/conditions/conditionalStatement/ConditionalStatement.java index bfc61e2..f7ff687 100644 --- a/src/main/java/CBuilder/conditions/conditionalStatement/ConditionalStatement.java +++ b/src/main/java/CBuilder/conditions/conditionalStatement/ConditionalStatement.java @@ -4,28 +4,21 @@ import CBuilder.Statement; import CBuilder.objects.AttributeReference; import CBuilder.objects.Call; - import java.util.List; -import java.util.Map; /** - * Internal base for all conditional control structures that consist of a condition and a body following that condition. + * Internal base for all conditional control structures that consist of a condition and a body + * following that condition. */ abstract class ConditionalStatement implements Statement { - /** - * The keyword of the representing conditional statement. - */ + /** The keyword of the representing conditional statement. */ private String keyword; - /** - * The condition of the representing conditional statement. - */ + /** The condition of the representing conditional statement. */ private Expression condition; - /** - * A List of all statements in the body of the conditional statement. - */ + /** A List of all statements in the body of the conditional statement. */ private List body; /** @@ -77,5 +70,7 @@ public String build() { } @Override - public String buildStatement() { return null; } + public String buildStatement() { + return null; + } } diff --git a/src/main/java/CBuilder/conditions/conditionalStatement/ElifStatement.java b/src/main/java/CBuilder/conditions/conditionalStatement/ElifStatement.java index 4aa0359..1c2170b 100644 --- a/src/main/java/CBuilder/conditions/conditionalStatement/ElifStatement.java +++ b/src/main/java/CBuilder/conditions/conditionalStatement/ElifStatement.java @@ -3,7 +3,6 @@ import CBuilder.Expression; import CBuilder.Statement; import CBuilder.conditions.IfThenElseStatement; - import java.util.List; /** @@ -19,8 +18,7 @@ public class ElifStatement extends ConditionalStatement { * @param condition The condition of the elif block. * @param body The list of statements in the body of the elif block. */ - public ElifStatement(Expression condition, - List body) { + public ElifStatement(Expression condition, List body) { super("else if", condition, body); } } diff --git a/src/main/java/CBuilder/conditions/conditionalStatement/ElseStatement.java b/src/main/java/CBuilder/conditions/conditionalStatement/ElseStatement.java index ad3f830..1846443 100644 --- a/src/main/java/CBuilder/conditions/conditionalStatement/ElseStatement.java +++ b/src/main/java/CBuilder/conditions/conditionalStatement/ElseStatement.java @@ -2,7 +2,6 @@ import CBuilder.Statement; import CBuilder.conditions.IfThenElseStatement; - import java.util.List; /** @@ -38,5 +37,7 @@ public String build() { } @Override - public String buildStatement() { return null; } + public String buildStatement() { + return null; + } } diff --git a/src/main/java/CBuilder/conditions/conditionalStatement/IfStatement.java b/src/main/java/CBuilder/conditions/conditionalStatement/IfStatement.java index 30bb2aa..30780d8 100644 --- a/src/main/java/CBuilder/conditions/conditionalStatement/IfStatement.java +++ b/src/main/java/CBuilder/conditions/conditionalStatement/IfStatement.java @@ -3,7 +3,6 @@ import CBuilder.Expression; import CBuilder.Statement; import CBuilder.conditions.IfThenElseStatement; - import java.util.List; /** @@ -19,9 +18,7 @@ public class IfStatement extends ConditionalStatement { * @param condition The condition of the if block. * @param body The list of statements in the body of the if block. */ - public IfStatement(Expression condition, - List body) { + public IfStatement(Expression condition, List body) { super("if", condition, body); } - } diff --git a/src/main/java/CBuilder/conditions/conditionalStatement/WhileStatement.java b/src/main/java/CBuilder/conditions/conditionalStatement/WhileStatement.java index 56205e8..06128bc 100644 --- a/src/main/java/CBuilder/conditions/conditionalStatement/WhileStatement.java +++ b/src/main/java/CBuilder/conditions/conditionalStatement/WhileStatement.java @@ -2,13 +2,9 @@ import CBuilder.Expression; import CBuilder.Statement; -import CBuilder.conditions.IfThenElseStatement; - import java.util.List; -/** - * An while loop with condition and statements. - */ +/** An while loop with condition and statements. */ public class WhileStatement extends ConditionalStatement implements Statement { /** @@ -17,8 +13,7 @@ public class WhileStatement extends ConditionalStatement implements Statement { * @param condition The condition of the while loop. * @param body The list of statements in the body of the while loop. */ - public WhileStatement(Expression condition, - List body) { + public WhileStatement(Expression condition, List body) { super("while", condition, body); } diff --git a/src/main/java/CBuilder/keywords/bool/AndKeyword.java b/src/main/java/CBuilder/keywords/bool/AndKeyword.java index dbebbe6..40023d1 100644 --- a/src/main/java/CBuilder/keywords/bool/AndKeyword.java +++ b/src/main/java/CBuilder/keywords/bool/AndKeyword.java @@ -18,5 +18,4 @@ public class AndKeyword extends BinaryBoolKeyword { public AndKeyword(Expression x, Expression y) { super("&&", x, y); } - } diff --git a/src/main/java/CBuilder/keywords/bool/BinaryBoolKeyword.java b/src/main/java/CBuilder/keywords/bool/BinaryBoolKeyword.java index 9bd8bdc..9319fc0 100644 --- a/src/main/java/CBuilder/keywords/bool/BinaryBoolKeyword.java +++ b/src/main/java/CBuilder/keywords/bool/BinaryBoolKeyword.java @@ -3,28 +3,18 @@ import CBuilder.Expression; import CBuilder.objects.AttributeReference; import CBuilder.objects.Call; - import java.util.List; -import java.util.Map; -/** - * Represents a boolean operation with a left and a right expression. - */ +/** Represents a boolean operation with a left and a right expression. */ abstract class BinaryBoolKeyword implements Expression { - /** - * The representing operator in c. - */ + /** The representing operator in c. */ private String cOp; - /** - * The operations left expression. - */ + /** The operations left expression. */ private Expression x; - /** - * The operations right expression. - */ + /** The operations right expression. */ private Expression y; /** @@ -55,8 +45,10 @@ public String buildExpression() { @Override public String buildStatement() { - // allow cleanup of returned object (which would otherwise simply vanish and leak the allocated memory) - // note: not needed for expressions, since as an expression the returned object is used (e. g. for assignment) + // allow cleanup of returned object (which would otherwise simply vanish and leak the + // allocated memory) + // note: not needed for expressions, since as an expression the returned object is used (e. + // g. for assignment) return "__mpy_obj_ref_dec(" + buildExpression() + ");\n"; } } diff --git a/src/main/java/CBuilder/keywords/bool/NotKeyword.java b/src/main/java/CBuilder/keywords/bool/NotKeyword.java index 134a6ef..ddbd3ca 100644 --- a/src/main/java/CBuilder/keywords/bool/NotKeyword.java +++ b/src/main/java/CBuilder/keywords/bool/NotKeyword.java @@ -3,18 +3,12 @@ import CBuilder.Expression; import CBuilder.objects.AttributeReference; import CBuilder.objects.Call; - import java.util.List; -import java.util.Map; -/** - * A boolean not operation (Python/MiniPython). - */ +/** A boolean not operation (Python/MiniPython). */ public class NotKeyword implements Expression { - /** - * The boolean expression to negate. - */ + /** The boolean expression to negate. */ private Expression x; /** @@ -37,9 +31,10 @@ public String buildExpression() { @Override public String buildStatement() { - // allow cleanup of returned object (which would otherwise simply vanish and leak the allocated memory) - // note: not needed for expressions, since as an expression the returned object is used (e. g. for assignment) + // allow cleanup of returned object (which would otherwise simply vanish and leak the + // allocated memory) + // note: not needed for expressions, since as an expression the returned object is used (e. + // g. for assignment) return "__mpy_obj_ref_dec(" + buildExpression() + ");\n"; } - } diff --git a/src/main/java/CBuilder/keywords/bool/OrKeyword.java b/src/main/java/CBuilder/keywords/bool/OrKeyword.java index 131017e..af4ff51 100644 --- a/src/main/java/CBuilder/keywords/bool/OrKeyword.java +++ b/src/main/java/CBuilder/keywords/bool/OrKeyword.java @@ -18,5 +18,4 @@ public class OrKeyword extends BinaryBoolKeyword { public OrKeyword(Expression x, Expression y) { super("||", x, y); } - } diff --git a/src/main/java/CBuilder/literals/BoolLiteral.java b/src/main/java/CBuilder/literals/BoolLiteral.java index 710e1a2..ad1dc54 100644 --- a/src/main/java/CBuilder/literals/BoolLiteral.java +++ b/src/main/java/CBuilder/literals/BoolLiteral.java @@ -1,13 +1,9 @@ package CBuilder.literals; -/** - * A simple boolean literal. - */ +/** A simple boolean literal. */ public class BoolLiteral implements Literal { - /** - * The containing boolean value. - */ + /** The containing boolean value. */ boolean value; /** @@ -28,5 +24,4 @@ public String buildExpression() { public String buildStatement() { return buildExpression() + ";\n"; } - } diff --git a/src/main/java/CBuilder/literals/IntLiteral.java b/src/main/java/CBuilder/literals/IntLiteral.java index cbf608f..4439abb 100644 --- a/src/main/java/CBuilder/literals/IntLiteral.java +++ b/src/main/java/CBuilder/literals/IntLiteral.java @@ -1,13 +1,9 @@ package CBuilder.literals; -/** - * A simple integer literal. - */ +/** A simple integer literal. */ public class IntLiteral implements Literal { - /** - * The containing integer value. - */ + /** The containing integer value. */ long value; /** diff --git a/src/main/java/CBuilder/literals/Literal.java b/src/main/java/CBuilder/literals/Literal.java index 35b9f80..6e143ff 100644 --- a/src/main/java/CBuilder/literals/Literal.java +++ b/src/main/java/CBuilder/literals/Literal.java @@ -2,9 +2,5 @@ import CBuilder.Expression; -/** - * Anything that is a static value, such as numbers, chars. - */ -public interface Literal extends Expression { - -} +/** Anything that is a static value, such as numbers, chars. */ +public interface Literal extends Expression {} diff --git a/src/main/java/CBuilder/literals/StringLiteral.java b/src/main/java/CBuilder/literals/StringLiteral.java index 3cae29a..a0e6992 100644 --- a/src/main/java/CBuilder/literals/StringLiteral.java +++ b/src/main/java/CBuilder/literals/StringLiteral.java @@ -1,13 +1,9 @@ package CBuilder.literals; -/** - * A simple string literal. - */ +/** A simple string literal. */ public class StringLiteral implements Literal { - /** - * The containing string value. - */ + /** The containing string value. */ String value; /** diff --git a/src/main/java/CBuilder/literals/TupleLiteral.java b/src/main/java/CBuilder/literals/TupleLiteral.java index aade2a1..a0c3f29 100644 --- a/src/main/java/CBuilder/literals/TupleLiteral.java +++ b/src/main/java/CBuilder/literals/TupleLiteral.java @@ -1,17 +1,12 @@ package CBuilder.literals; import CBuilder.Expression; - import java.util.List; -/** - * A simple tuple literal. - */ +/** A simple tuple literal. */ public class TupleLiteral implements Literal { - /** - * The list of values the tuple is containing. - */ + /** The list of values the tuple is containing. */ private final List elems; /** @@ -28,7 +23,8 @@ public String buildExpression() { StringBuilder tupleInit = new StringBuilder(); for (int i = 0; i < elems.size(); i++) { - tupleInit.append("__mpy_tuple_assign(" + i + ", " + elems.get(i).buildExpression() + ", "); + tupleInit.append( + "__mpy_tuple_assign(" + i + ", " + elems.get(i).buildExpression() + ", "); } tupleInit.append("__mpy_obj_init_tuple(" + elems.size() + ")"); tupleInit.append(")".repeat(elems.size())); @@ -40,5 +36,4 @@ public String buildExpression() { public String buildStatement() { return "__mpy_obj_ref_dec(" + buildExpression() + ");\n"; } - } diff --git a/src/main/java/CBuilder/manualTests/BooleanTest.java b/src/main/java/CBuilder/manualTests/BooleanTest.java index 7b61fe5..fbf9f3a 100644 --- a/src/main/java/CBuilder/manualTests/BooleanTest.java +++ b/src/main/java/CBuilder/manualTests/BooleanTest.java @@ -3,49 +3,32 @@ import CBuilder.Expression; import CBuilder.ProgramBuilder; import CBuilder.Reference; -import CBuilder.Statement; import CBuilder.literals.BoolLiteral; -import CBuilder.literals.IntLiteral; import CBuilder.objects.*; -import CBuilder.objects.functions.Argument; -import CBuilder.objects.functions.Function; import CBuilder.variables.Assignment; import CBuilder.variables.VariableDeclaration; - import java.nio.file.Path; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * Allows to test the Builder without involving the ast or anything. * - * TODO remove this when manual testing of this is not needed anymore + *

TODO remove this when manual testing of this is not needed anymore */ public class BooleanTest { /** - * Mini-Python code of program generated: - * ```python - * a - * b + * Mini-Python code of program generated: ```python a b * - * a = b - * d = -30 + *

a = b d = -30 * - * type(a) - * type(d) + *

type(a) type(d) * - * idA = id(a) - * print(idA) + *

idA = id(a) print(idA) * - * print(id(a)) - * id(print(d)) - * id(type(a)) + *

print(id(a)) id(print(d)) id(type(a)) * - * e = id(type(print(d))) - * e = print(-50) - * ``` + *

e = id(type(print(d))) e = print(-50) ``` */ static void generateProgram(Path output) { ProgramBuilder builder = new ProgramBuilder(); @@ -56,28 +39,48 @@ static void generateProgram(Path output) { builder.addVariable(new VariableDeclaration("b")); builder.addStatement(new Assignment(new Reference("b"), new BoolLiteral(false))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] { - new Reference("a"), - new Reference("b"), - }))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] { - new Reference("a"), - }))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] { - new Reference("b"), - }))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] { - new Reference("a"), - }))); - builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] { - new Reference("b"), - }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("a"), new Reference("b"), + }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("a"), + }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("b"), + }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("a"), + }))); + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("b"), + }))); builder.writeProgram(output); } public static void main(String[] args) { - Path fileOutput = java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/BooleanTest/"); + Path fileOutput = + java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/BooleanTest/"); generateProgram(fileOutput); } } diff --git a/src/main/java/CBuilder/manualTests/ClassTest.java b/src/main/java/CBuilder/manualTests/ClassTest.java index feab198..8474068 100644 --- a/src/main/java/CBuilder/manualTests/ClassTest.java +++ b/src/main/java/CBuilder/manualTests/ClassTest.java @@ -12,7 +12,6 @@ import CBuilder.objects.functions.Function; import CBuilder.variables.Assignment; import CBuilder.variables.VariableDeclaration; - import java.nio.file.Path; import java.util.List; import java.util.Map; @@ -27,26 +26,42 @@ static void generateProgram(Path output) { Reference funcPrint = new Reference("print"); Reference object = new Reference("__MPyType_Object"); - MPyClass clazz = new MPyClass("A", object, List.of(new Function[]{ - new Function("foo", List.of(new Expression[]{ - new Call(funcPrint, List.of(new Expression[]{ - new StringLiteral("foo") - })) - }), List.of(new Argument[]{new Argument("self", 0)}), List.of()), - new Function("__init__", List.of(new Expression[]{ - new SuperCall(List.of()) - }), List.of(new Argument[]{new Argument("self", 0)}), List.of()) - }), Map.of()); + MPyClass clazz = + new MPyClass( + "A", + object, + List.of( + new Function[] { + new Function( + "foo", + List.of( + new Expression[] { + new Call( + funcPrint, + List.of( + new Expression[] { + new StringLiteral("foo") + })) + }), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()), + new Function( + "__init__", + List.of(new Expression[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + Map.of()); builder.addClass(clazz); builder.addStatement(new Assignment(varB, new Call(new Reference("A"), List.of()))); builder.addStatement(new Call(new AttributeReference("foo", varB), List.of())); - builder.writeProgram(output); } public static void main(String[] args) { - Path fileOutput = java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/ClassTest/"); + Path fileOutput = + java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/ClassTest/"); generateProgram(fileOutput); } } diff --git a/src/main/java/CBuilder/manualTests/IntOps.java b/src/main/java/CBuilder/manualTests/IntOps.java index 6b8c52a..1d5c694 100644 --- a/src/main/java/CBuilder/manualTests/IntOps.java +++ b/src/main/java/CBuilder/manualTests/IntOps.java @@ -8,7 +8,6 @@ import CBuilder.objects.Call; import CBuilder.variables.Assignment; import CBuilder.variables.VariableDeclaration; - import java.nio.file.Path; import java.util.List; @@ -23,17 +22,25 @@ static void generateProgram(Path output) { builder.addVariable(varADecl); builder.addStatement(new Assignment(varA, new IntLiteral(10))); - builder.addStatement(new Call(funcPrint, List.of(new Expression[]{ - new Call(new AttributeReference("__xor__", varA), List.of(new Expression[]{ - new IntLiteral(12), - })) - }))); + builder.addStatement( + new Call( + funcPrint, + List.of( + new Expression[] { + new Call( + new AttributeReference("__xor__", varA), + List.of( + new Expression[] { + new IntLiteral(12), + })) + }))); builder.writeProgram(output); } public static void main(String[] args) { - Path fileOutput = java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/IntOps/"); + Path fileOutput = + java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/IntOps/"); generateProgram(fileOutput); } } diff --git a/src/main/java/CBuilder/manualTests/StrOps.java b/src/main/java/CBuilder/manualTests/StrOps.java index aae4011..aef21db 100644 --- a/src/main/java/CBuilder/manualTests/StrOps.java +++ b/src/main/java/CBuilder/manualTests/StrOps.java @@ -3,13 +3,11 @@ import CBuilder.Expression; import CBuilder.ProgramBuilder; import CBuilder.Reference; -import CBuilder.literals.IntLiteral; import CBuilder.literals.StringLiteral; import CBuilder.objects.AttributeReference; import CBuilder.objects.Call; import CBuilder.variables.Assignment; import CBuilder.variables.VariableDeclaration; - import java.nio.file.Path; import java.util.List; @@ -24,17 +22,25 @@ static void generateProgram(Path output) { builder.addVariable(varADecl); builder.addStatement(new Assignment(varA, new StringLiteral("abc"))); - builder.addStatement(new Call(funcPrint, List.of(new Expression[]{ - new Call(new AttributeReference("__eq__", varA), List.of(new Expression[]{ - new StringLiteral("abc"), - })) - }))); + builder.addStatement( + new Call( + funcPrint, + List.of( + new Expression[] { + new Call( + new AttributeReference("__eq__", varA), + List.of( + new Expression[] { + new StringLiteral("abc"), + })) + }))); builder.writeProgram(output); } public static void main(String[] args) { - Path fileOutput = java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/StrOps/"); + Path fileOutput = + java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/StrOps/"); generateProgram(fileOutput); } } diff --git a/src/main/java/CBuilder/objects/AttributeAssignment.java b/src/main/java/CBuilder/objects/AttributeAssignment.java index c0d8f55..a7bb436 100644 --- a/src/main/java/CBuilder/objects/AttributeAssignment.java +++ b/src/main/java/CBuilder/objects/AttributeAssignment.java @@ -3,19 +3,13 @@ import CBuilder.Expression; import CBuilder.Statement; -/** - * Assign a value to the attribute of an object. - */ +/** Assign a value to the attribute of an object. */ public class AttributeAssignment implements Statement { - /** - * The reference of the attribute in the object scope. - */ + /** The reference of the attribute in the object scope. */ private AttributeReference attribute; - /** - * The value to assign to the attribute. - */ + /** The value to assign to the attribute. */ private Expression value; /** @@ -31,6 +25,12 @@ public AttributeAssignment(AttributeReference attribute, Expression value) { @Override public String buildStatement() { - return "__mpy_obj_set_attr(" + attribute.buildObject() + ", " + attribute.buildName() + ", " + value.buildExpression() + ");"; + return "__mpy_obj_set_attr(" + + attribute.buildObject() + + ", " + + attribute.buildName() + + ", " + + value.buildExpression() + + ");"; } } diff --git a/src/main/java/CBuilder/objects/AttributeReference.java b/src/main/java/CBuilder/objects/AttributeReference.java index aebe1a0..f28f17e 100644 --- a/src/main/java/CBuilder/objects/AttributeReference.java +++ b/src/main/java/CBuilder/objects/AttributeReference.java @@ -1,31 +1,20 @@ package CBuilder.objects; import CBuilder.Expression; -import CBuilder.Reference; -/** - * Mini-Python attribute access, i. e. `a.b`. - */ +/** Mini-Python attribute access, i. e. `a.b`. */ public class AttributeReference implements Expression { - /** - * The name of the attribute the reference should point to. - */ + /** The name of the attribute the reference should point to. */ private String name; - /** - * The object which containing the referenced attribute. - */ + /** The object which containing the referenced attribute. */ private Expression object; /** * Create a new attribute reference for the given object. * - * Example: - * ``` - * a = 1 - * a.__str__ # AttributeReference("__str__", new Reference("a")) - * ``` + *

Example: ``` a = 1 a.__str__ # AttributeReference("__str__", new Reference("a")) ``` * * @param name The name of the reference. * @param object The object to retrieve the reference from. @@ -68,5 +57,7 @@ public String buildStatement() { * * @return The name of the attribute. */ - public String getName() { return this.name; } + public String getName() { + return this.name; + } } diff --git a/src/main/java/CBuilder/objects/Call.java b/src/main/java/CBuilder/objects/Call.java index b19f0be..79097bc 100644 --- a/src/main/java/CBuilder/objects/Call.java +++ b/src/main/java/CBuilder/objects/Call.java @@ -2,30 +2,23 @@ import CBuilder.Expression; import CBuilder.literals.TupleLiteral; - import java.util.List; import java.util.Map; /** * A Mini-Python call, i. e. a function call or object initialisation. * - * Samples: `object()`, `a.b()`. + *

Samples: `object()`, `a.b()`. */ public class Call implements Expression { - /** - * The positional arguments of the call. - */ + /** The positional arguments of the call. */ private List args; - /** - * The keyword arguments of the call. - */ + /** The keyword arguments of the call. */ private Map kwargs; - /** - * The callable object, i. e. a type or a function. - */ + /** The callable object, i. e. a type or a function. */ private Expression callable; /** @@ -51,14 +44,19 @@ public String buildExpression() { // finally: call // FIXME replace NULL with keyword arguments - return "__mpy_call(" + callable.buildExpression() + ", " + packedArguments.buildExpression() + ", NULL)"; + return "__mpy_call(" + + callable.buildExpression() + + ", " + + packedArguments.buildExpression() + + ", NULL)"; } @Override public String buildStatement() { - // allow cleanup of returned object (which would otherwise simply vanish and leak the allocated memory) - // note: not needed for expressions, since as an expression the returned object is used (e. g. for assignment) + // allow cleanup of returned object (which would otherwise simply vanish and leak the + // allocated memory) + // note: not needed for expressions, since as an expression the returned object is used (e. + // g. for assignment) return "__mpy_obj_ref_dec(" + buildExpression() + ");\n"; } - } diff --git a/src/main/java/CBuilder/objects/MPyClass.java b/src/main/java/CBuilder/objects/MPyClass.java index 17e1c23..d5ab140 100644 --- a/src/main/java/CBuilder/objects/MPyClass.java +++ b/src/main/java/CBuilder/objects/MPyClass.java @@ -3,29 +3,20 @@ import CBuilder.Expression; import CBuilder.Reference; import CBuilder.objects.functions.Function; - import java.util.List; import java.util.Map; import java.util.stream.Collectors; -/** - * Mini-Python class declaration. - */ +/** Mini-Python class declaration. */ public class MPyClass extends Reference { - /** - * The reference to the parent class. - */ + /** The reference to the parent class. */ private final Reference parent; - /** - * The list of methods the class contains. - */ + /** The list of methods the class contains. */ private final List functions; - /** - * The list of static attributes the class contains. - */ + /** The list of static attributes the class contains. */ private final Map classAttributes; /** @@ -36,7 +27,11 @@ public class MPyClass extends Reference { * @param functions The associated functions and methods. * @param classAttributes The classes attributes. */ - public MPyClass(String name, Reference parent, List functions, Map classAttributes) { + public MPyClass( + String name, + Reference parent, + List functions, + Map classAttributes) { super(name); this.parent = parent; this.functions = functions; @@ -72,12 +67,26 @@ public String buildInitialisation() { StringBuilder init = new StringBuilder(); // first create the class object - init.append(name + " = __mpy_obj_init_type(\"" + name + "\"" + ", " + parent.buildExpression() + ");\n"); + init.append( + name + + " = __mpy_obj_init_type(\"" + + name + + "\"" + + ", " + + parent.buildExpression() + + ");\n"); init.append("__mpy_obj_ref_inc(" + name + ");\n"); // then bind attributes for (Map.Entry attribute : classAttributes.entrySet()) { - init.append("__mpy_obj_set_attr(" + name + ", \"" + attribute.getKey().buildExpression() + "\", " + attribute.getValue().buildExpression() + ");"); + init.append( + "__mpy_obj_set_attr(" + + name + + ", \"" + + attribute.getKey().buildExpression() + + "\", " + + attribute.getValue().buildExpression() + + ");"); } // and bind functions @@ -87,15 +96,28 @@ public String buildInitialisation() { funcBind.append(function.buildFuncObjectDeclaration()); funcBind.append(function.buildInitialisation()); - funcBind.append("__mpy_obj_set_attr(" + name + ", \"" + function.buildExpression() + "\", " + function.buildExpression() + ");\n"); - funcBind.append(function.buildRefDec()); // this may look surprising, but keep in mind the function object - // is essentially a temporary variable here, which then naturally needs to be correctly refCounted - - // perform init inside temporary scope to prevent pollution of the current scope - // with the function names of the class + funcBind.append( + "__mpy_obj_set_attr(" + + name + + ", \"" + + function.buildExpression() + + "\", " + + function.buildExpression() + + ");\n"); + funcBind.append( + function.buildRefDec()); // this may look surprising, but keep in mind the + // function object + // is essentially a temporary variable here, which then naturally needs to be correctly + // refCounted + + // perform init inside temporary scope to prevent pollution of the current scope + // with the function names of the class init.append("{\n"); - init.append(funcBind.toString().lines().map(string -> "\t" + string + "\n").collect( - Collectors.joining())); + init.append( + funcBind.toString() + .lines() + .map(string -> "\t" + string + "\n") + .collect(Collectors.joining())); init.append("}\n"); } diff --git a/src/main/java/CBuilder/objects/SuperCall.java b/src/main/java/CBuilder/objects/SuperCall.java index b7d64c1..9607d58 100644 --- a/src/main/java/CBuilder/objects/SuperCall.java +++ b/src/main/java/CBuilder/objects/SuperCall.java @@ -2,22 +2,19 @@ import CBuilder.Expression; import CBuilder.Reference; -import CBuilder.Statement; - import java.util.LinkedList; import java.util.List; -import java.util.Map; /** - * Create a new super call for a class definition. - * All classes need to call super since they inherit from '__MPyType_Object'. - * The super call need to be the first statement of '__init__' method. + * Create a new super call for a class definition. All classes need to call super since they inherit + * from '__MPyType_Object'. The super call need to be the first statement of '__init__' method. */ public class SuperCall extends Call { // addAll does not return `this`, // and `super()` needs to be called first... - // so we need an extra method simply to be able to have a default element in the front of the list.... + // so we need an extra method simply to be able to have a default element in the front of the + // list.... private static List chainedAddAll(List thisInstance, List appended) { // thisInstance may very well be immutable (who needs rust's ownership model anyway)... List result = new LinkedList<>(); @@ -29,7 +26,7 @@ private static List chainedAddAll(List thisInstance, List appended) /** * Create a new super call. * - * @param args The positional arguments of the parent class constructor. + * @param args The positional arguments of the parent class constructor. */ public SuperCall(List args) { super(new Reference("__mpy_super"), chainedAddAll(List.of(new Reference("self")), args)); diff --git a/src/main/java/CBuilder/objects/functions/Argument.java b/src/main/java/CBuilder/objects/functions/Argument.java index 0b06e59..b962e5e 100644 --- a/src/main/java/CBuilder/objects/functions/Argument.java +++ b/src/main/java/CBuilder/objects/functions/Argument.java @@ -1,18 +1,12 @@ package CBuilder.objects.functions; -/** - * A mini-python (positional) function argument. - */ +/** A mini-python (positional) function argument. */ public class Argument { - /** - * The name of the argument. - */ + /** The name of the argument. */ private final String name; - /** - * The position of the argument. - */ + /** The position of the argument. */ private final int position; /** @@ -32,16 +26,22 @@ public Argument(String name, int position) { * @return Code extracting this argument from a function's argument extractor. */ public String buildArgExtraction() { - return "__MPyObj *" + name + " = __mpy_args_get_positional(&argHelper, " + position + ", \"" + name + "\");\n"; + return "__MPyObj *" + + name + + " = __mpy_args_get_positional(&argHelper, " + + position + + ", \"" + + name + + "\");\n"; } /** * Create the c-code for cleaning up the argument object. * - * @return A string which contains the c-code for cleaning up the mini-python object of this argument. + * @return A string which contains the c-code for cleaning up the mini-python object of this + * argument. */ public String buildArgCleanup() { return "__mpy_obj_ref_dec(" + name + ");\n"; } - } diff --git a/src/main/java/CBuilder/objects/functions/Function.java b/src/main/java/CBuilder/objects/functions/Function.java index bc6b29f..a895fc1 100644 --- a/src/main/java/CBuilder/objects/functions/Function.java +++ b/src/main/java/CBuilder/objects/functions/Function.java @@ -3,14 +3,14 @@ import CBuilder.Reference; import CBuilder.Statement; import CBuilder.variables.VariableDeclaration; - import java.util.List; import java.util.stream.Collectors; /** * Mini-Python function declaration. * - * Implements the Reference interface to allow using instances for referring to the declaration too. + *

Implements the Reference interface to allow using instances for referring to the declaration + * too. */ public class Function extends Reference { @@ -23,23 +23,25 @@ public class Function extends Reference { private final List localVariables; - /** * Create a new function (or method). * - * Receives a funcName to fulfill requirements of both mini-python's duck typing - * and the declaration of the native C function containing the functions' statements. - * Since there are no namespaces in C, a globally unique name is required for the - * declaration of the function itself. For functions, funcName is used for this purpose; - * for methods, the class name is automatically added as a prefix to funcName once the - * class is created (MPyClass). + *

Receives a funcName to fulfill requirements of both mini-python's duck typing and the + * declaration of the native C function containing the functions' statements. Since there are no + * namespaces in C, a globally unique name is required for the declaration of the function + * itself. For functions, funcName is used for this purpose; for methods, the class name is + * automatically added as a prefix to funcName once the class is created (MPyClass). * * @param funcName The mini-python name of the function. * @param body The body of the function. * @param positionalArgs The arguments of the function. * @param localVariables Variables declared inside the function. */ - public Function(String funcName, List body, List positionalArgs, List localVariables) { + public Function( + String funcName, + List body, + List positionalArgs, + List localVariables) { super(funcName); this.cName = funcName; this.body = body; @@ -52,15 +54,15 @@ public Function(String funcName, List body, List positional /** * Creates a unique method name used in emitted C code (auxiliary method, called by MPyClass) * - * A method is a function that can only be called in connection with its class. However, - * since a method is emitted just as a normal function in the C code, the method name must - * be extended with a prefix at C level and thus be made unique. The constructor of MPyClass + *

A method is a function that can only be called in connection with its class. However, + * since a method is emitted just as a normal function in the C code, the method name must be + * extended with a prefix at C level and thus be made unique. The constructor of MPyClass * handles this by calling this auxiliary method. * - *

To call a method, use its name (funcName) and not its unique C name. + *

To call a method, use its name (funcName) and not its unique C name. * - *

Warning: Do not use this helper method for functions, as functions are not allowed to - * have a different C name. + *

Warning: Do not use this helper method for functions, as functions are not allowed to have + * a different C name. * * @param prefix The prefix string which should be adde to the C name. */ @@ -71,7 +73,7 @@ public void createUniqueCName(String prefix) { /** * Create the C code representation for the function. * - * @return The C code of the function implementing this mini-python function. + * @return The C code of the function implementing this mini-python function. */ public String buildCFunction() { StringBuilder declaration = new StringBuilder(); @@ -84,7 +86,12 @@ public String buildCFunction() { body.append("assert(args != NULL && kwargs != NULL);\n\n"); - body.append("__MPyGetArgsState argHelper = __mpy_args_init(\"" + name + "\", args, kwargs, " + positionalArgs.size() + ");\n"); + body.append( + "__MPyGetArgsState argHelper = __mpy_args_init(\"" + + name + + "\", args, kwargs, " + + positionalArgs.size() + + ");\n"); for (Argument arg : positionalArgs) { body.append(arg.buildArgExtraction()); @@ -93,7 +100,8 @@ public String buildCFunction() { if (!receivesPackedPositionalArgs && !receivesPackedKeywordArgs) { body.append("__mpy_args_finish(&argHelper);\n"); } else { - // FIXME leave out check for unused arguments only partially (not implemented on the c side too) + // FIXME leave out check for unused arguments only partially (not implemented on the c + // side too) body.append("__mpy_obj_ref_dec(args);\n"); body.append("__mpy_obj_ref_dec(kwargs);\n"); } @@ -124,8 +132,11 @@ public String buildCFunction() { body.append("}\n"); body.append("return __mpy_obj_return(retValue);"); - declaration.append(body.toString().lines().map(string -> "\t" + string + "\n").collect( - Collectors.joining())); + declaration.append( + body.toString() + .lines() + .map(string -> "\t" + string + "\n") + .collect(Collectors.joining())); declaration.append("}\n"); return declaration.toString(); @@ -146,8 +157,13 @@ public String buildFuncObjectDeclaration() { * @return Initialisation code for this function's mini-python object. */ public String buildInitialisation() { - return name + " = __mpy_obj_init_func(&func_" + cName + ");\n" + - "__mpy_obj_ref_inc(" + name + ");\n"; + return name + + " = __mpy_obj_init_func(&func_" + + cName + + ");\n" + + "__mpy_obj_ref_inc(" + + name + + ");\n"; } /** @@ -158,5 +174,4 @@ public String buildInitialisation() { public String buildRefDec() { return "__mpy_obj_ref_dec(" + name + ");\n"; } - } diff --git a/src/main/java/CBuilder/objects/functions/ReturnStatement.java b/src/main/java/CBuilder/objects/functions/ReturnStatement.java index 427d51f..96e49ae 100644 --- a/src/main/java/CBuilder/objects/functions/ReturnStatement.java +++ b/src/main/java/CBuilder/objects/functions/ReturnStatement.java @@ -3,9 +3,7 @@ import CBuilder.Expression; import CBuilder.Statement; -/** - * A return statement inside a function. - */ +/** A return statement inside a function. */ public class ReturnStatement implements Statement { private Expression returnValue; @@ -24,7 +22,6 @@ public String buildStatement() { // Note: retVal and goto ret depend on the implementation of Function#buildCFunction // I'm sorry for all the hidden interdependencies in this stuff, but I have no idea // how this could have been solved better - return "retValue = " + returnValue.buildExpression() + ";\n" + - "goto ret;\n"; + return "retValue = " + returnValue.buildExpression() + ";\n" + "goto ret;\n"; } } diff --git a/src/main/java/CBuilder/variables/Assignment.java b/src/main/java/CBuilder/variables/Assignment.java index 98fbde1..6a0dc11 100644 --- a/src/main/java/CBuilder/variables/Assignment.java +++ b/src/main/java/CBuilder/variables/Assignment.java @@ -4,19 +4,13 @@ import CBuilder.Reference; import CBuilder.Statement; -/** - * Assign a value to a variable. - */ +/** Assign a value to a variable. */ public class Assignment implements Statement { - /** - * The name of the variable. - */ + /** The name of the variable. */ private final Reference lhs; - /** - * The new value of the variable. - */ + /** The new value of the variable. */ private final Expression rhs; /** @@ -37,22 +31,39 @@ public String buildStatement() { if (this.rhs instanceof Reference) { // first increment reference count of object we assign since it's now used once more - // then decrement reference count of object in variable that is reassigned, since it's now used once less. + // then decrement reference count of object in variable that is reassigned, since it's + // now used once less. // Afterwards perform assignment. // - // Note: Order is important, since for example for the case of `a=a` the variable could reach a count of 0 even + // Note: Order is important, since for example for the case of `a=a` the variable could + // reach a count of 0 even // if it's still in use afterwards. - return "__mpy_obj_ref_inc(" + rhs + ");\n" + - "__mpy_obj_ref_dec(" + lhs + ");\n" + - lhs + " = " + rhs + ";\n"; + return "__mpy_obj_ref_inc(" + + rhs + + ");\n" + + "__mpy_obj_ref_dec(" + + lhs + + ");\n" + + lhs + + " = " + + rhs + + ";\n"; } else { // Note: Order is not important here, since returned values (literals or function calls) - // have the temporary mechanism and therefore a refCount of +1 until the _inc call, so calling _dec first - // is not problematic (e. g. def returnSelf(x): return x; x = 10; x = returnSelf(x); works even if decrementing first) - return "__mpy_obj_ref_dec(" + lhs + ");\n" + - lhs + " = " + rhs + ";\n" + - "__mpy_obj_ref_inc(" + lhs + ");\n"; + // have the temporary mechanism and therefore a refCount of +1 until the _inc call, so + // calling _dec first + // is not problematic (e. g. def returnSelf(x): return x; x = 10; x = returnSelf(x); + // works even if decrementing first) + return "__mpy_obj_ref_dec(" + + lhs + + ");\n" + + lhs + + " = " + + rhs + + ";\n" + + "__mpy_obj_ref_inc(" + + lhs + + ");\n"; } } - } diff --git a/src/main/java/CBuilder/variables/VariableDeclaration.java b/src/main/java/CBuilder/variables/VariableDeclaration.java index 5b4249e..1daffcc 100644 --- a/src/main/java/CBuilder/variables/VariableDeclaration.java +++ b/src/main/java/CBuilder/variables/VariableDeclaration.java @@ -1,14 +1,9 @@ package CBuilder.variables; -/** - * Declaration of a variable. - * This makes the variable known to the c compiler. - */ +/** Declaration of a variable. This makes the variable known to the c compiler. */ public class VariableDeclaration { - /** - * The name of the variable. - */ + /** The name of the variable. */ protected String name; /** @@ -27,8 +22,7 @@ public VariableDeclaration(String name) { */ private String initialisation() { // FIXME: init none instead of object - return " = __mpy_obj_init_object();\n" + - "__mpy_obj_ref_inc(" + name + ")"; + return " = __mpy_obj_init_object();\n" + "__mpy_obj_ref_inc(" + name + ")"; } /** @@ -57,10 +51,10 @@ public String buildInitialisation() { /** * Create the c-code to decrement the reference counter of the variable. * - * @return A string which represents the c-code to decrement the reference counter of the variable + * @return A string which represents the c-code to decrement the reference counter of the + * variable */ public String buildRefDec() { return "__mpy_obj_ref_dec(" + name + ");\n"; } - } diff --git a/src/test/java/CBuilder/TestReference.java b/src/test/java/CBuilder/TestReference.java new file mode 100644 index 0000000..9855339 --- /dev/null +++ b/src/test/java/CBuilder/TestReference.java @@ -0,0 +1,46 @@ +/* (C)2024 */ +package CBuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestReference { + String testClass = "[REFERENCE]\n"; + + private static Stream sources() { + return Stream.of("ref1", "ref2"); + } + + @ParameterizedTest + @MethodSource("sources") + void constructor(String name) { + Reference ref = new Reference(name); + + System.out.println(testClass + ref.getName()); + + assertEquals(name, ref.getName()); + } + + @ParameterizedTest + @MethodSource("sources") + void build_expression(String name) { + Reference ref = new Reference(name); + + System.out.println(testClass + ref.buildExpression()); + + assertEquals(name, ref.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(String name) { + Reference ref = new Reference(name); + + System.out.println(testClass + ref.buildStatement()); + + assertEquals(name + ";\n", ref.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/TestWithCToolchains.java b/src/test/java/CBuilder/TestWithCToolchains.java index b58ca95..d7a9e88 100644 --- a/src/test/java/CBuilder/TestWithCToolchains.java +++ b/src/test/java/CBuilder/TestWithCToolchains.java @@ -1,19 +1,21 @@ package CBuilder; +import static CBuilder.ManualTest.generateProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.nio.file.Path; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.io.IOException; -import java.nio.file.Path; - -import static CBuilder.ManualTest.generateProgram; -import static org.junit.jupiter.api.Assertions.assertEquals; - +@Disabled public class TestWithCToolchains { - private void generateAndCompileProgram(Path outputDirectory) throws IOException, InterruptedException { + private void generateAndCompileProgram(Path outputDirectory) + throws IOException, InterruptedException { generateProgram(outputDirectory); ProcessBuilder builder = new ProcessBuilder("make"); @@ -28,8 +30,9 @@ private void generateAndCompileProgram(Path outputDirectory) throws IOException, } @ParameterizedTest - @ValueSource(strings = { "gcc", "clang" }) - void compile(String compiler, @TempDir Path outputDirectory) throws IOException, InterruptedException { + @ValueSource(strings = {"gcc", "clang"}) + void compile(String compiler, @TempDir Path outputDirectory) + throws IOException, InterruptedException { generateProgram(outputDirectory); ProcessBuilder builder = new ProcessBuilder("make"); @@ -47,7 +50,14 @@ void compile(String compiler, @TempDir Path outputDirectory) throws IOException, void valgrind(@TempDir Path workDirectory) throws IOException, InterruptedException { generateAndCompileProgram(workDirectory); - ProcessBuilder builder = new ProcessBuilder("valgrind", "--show-error-list=yes", "--leak-check=full", "--show-leak-kinds=all", "--error-exitcode=1", "./bin/program"); + ProcessBuilder builder = + new ProcessBuilder( + "valgrind", + "--show-error-list=yes", + "--leak-check=full", + "--show-leak-kinds=all", + "--error-exitcode=1", + "./bin/program"); builder.directory(workDirectory.toFile()); builder.inheritIO(); @@ -61,7 +71,7 @@ void clang_tidy(@TempDir Path workDirectory) throws IOException, InterruptedExce generateProgram(workDirectory); // this does not error on warnings, but those are too broad anyway - ProcessBuilder builder = new ProcessBuilder("make", "lint"); + ProcessBuilder builder = new ProcessBuilder("make", "lint"); builder.directory(workDirectory.toFile()); builder.inheritIO(); diff --git a/src/test/java/CBuilder/bool/TestAndKeyword.java b/src/test/java/CBuilder/bool/TestAndKeyword.java new file mode 100644 index 0000000..d4e839e --- /dev/null +++ b/src/test/java/CBuilder/bool/TestAndKeyword.java @@ -0,0 +1,44 @@ +/* (C)2024 */ +package CBuilder.bool; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.keywords.bool.AndKeyword; +import CBuilder.literals.IntLiteral; +import org.junit.jupiter.api.Test; + +/** Snapshot 23.02.2024 */ +public class TestAndKeyword { + String testClass = "[ANDKEYWORD]\n"; + + /** 1 && 1 */ + @Test + void build_expression() { + String expected = + "__mpy_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)) &&" + + " __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)))"; + + AndKeyword andK = new AndKeyword(new IntLiteral(1), new IntLiteral(1)); + + System.out.println(testClass + andK.buildExpression()); + + assertEquals(expected, andK.buildExpression()); + } + + @Test + void build_statement() { + String expected = + "__mpy_obj_ref_dec(__mpy_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)) &&" + + " __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))));\n"; + + AndKeyword andK = new AndKeyword(new IntLiteral(1), new IntLiteral(1)); + + System.out.println(testClass + andK.buildStatement()); + + assertEquals(expected, andK.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/bool/TestNotKeyword.java b/src/test/java/CBuilder/bool/TestNotKeyword.java new file mode 100644 index 0000000..d284dbc --- /dev/null +++ b/src/test/java/CBuilder/bool/TestNotKeyword.java @@ -0,0 +1,55 @@ +/* (C)2024 */ +package CBuilder.bool; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.keywords.bool.NotKeyword; +import CBuilder.literals.BoolLiteral; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** Parameterized tests not really useful here. Snapshot 22.02.2024 */ +public class TestNotKeyword { + String testClass = "[NOTKEYWORD]\n"; + + private static Stream sources() { + return Stream.of(true, false); + } + + /** + * @param b_value - Boolean + */ + @ParameterizedTest + @MethodSource("sources") + void build_expression(boolean b_value) { + String expected = + "__mpy_obj_init_boolean(!__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(" + + b_value + + "), \"__bool__\"), __mpy_obj_init_tuple(0), NULL)))"; + + NotKeyword notK = new NotKeyword(new BoolLiteral(b_value)); + + System.out.println(testClass + notK.buildExpression()); + + assertEquals(expected, notK.buildExpression()); + } + + /** + * @param b_value - Boolean + */ + @ParameterizedTest + @MethodSource("sources") + void build_statement(Boolean b_value) { + String expected = + "__mpy_obj_ref_dec(__mpy_obj_init_boolean(!__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(" + + b_value + + "), \"__bool__\"), __mpy_obj_init_tuple(0), NULL))));\n"; + + NotKeyword notK = new NotKeyword(new BoolLiteral(b_value)); + + System.out.println(testClass + notK.buildStatement()); + + assertEquals(expected, notK.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/bool/TestOrKeyword.java b/src/test/java/CBuilder/bool/TestOrKeyword.java new file mode 100644 index 0000000..e1112a8 --- /dev/null +++ b/src/test/java/CBuilder/bool/TestOrKeyword.java @@ -0,0 +1,43 @@ +/* (C)2024 */ +package CBuilder.bool; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.keywords.bool.OrKeyword; +import CBuilder.literals.BoolLiteral; +import org.junit.jupiter.api.Test; + +/** Snapshot 23.02.2024 */ +public class TestOrKeyword { + String testClass = "[ORKEYWORD]\n"; + + @Test + void build_expression() { + String expected = + "__mpy_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)) ||" + + " __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)))"; + + OrKeyword orK = new OrKeyword(new BoolLiteral(true), new BoolLiteral(false)); + + System.out.println(testClass + orK.buildExpression()); + + assertEquals(expected, orK.buildExpression()); + } + + @Test + void build_statement() { + String expected = + "__mpy_obj_ref_dec(__mpy_obj_init_boolean(__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL)) ||" + + " __mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))));\n"; + + OrKeyword orK = new OrKeyword(new BoolLiteral(true), new BoolLiteral(false)); + + System.out.println(testClass + orK.buildStatement()); + + assertEquals(expected, orK.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/conditions/TestIfThenElseStatement.java b/src/test/java/CBuilder/conditions/TestIfThenElseStatement.java new file mode 100644 index 0000000..a734a25 --- /dev/null +++ b/src/test/java/CBuilder/conditions/TestIfThenElseStatement.java @@ -0,0 +1,62 @@ +/* (C)2024 */ +package CBuilder.conditions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.conditions.conditionalStatement.ElifStatement; +import CBuilder.conditions.conditionalStatement.ElseStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.BoolLiteral; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestIfThenElseStatement { + String testClass = "[IFTHENELSESTATEMENT]\n"; + + private static Stream sources_build_statement() { + return Stream.of( + Arguments.of( + new IfStatement(new BoolLiteral(true), List.of()), + Optional.of(List.of(new ElifStatement(new BoolLiteral(true), List.of()))), + Optional.of(new ElseStatement(List.of())), + """ + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + }else if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + }else { + }"""), + Arguments.of( + new IfStatement(new BoolLiteral(true), List.of()), + Optional.empty(), + Optional.of(new ElseStatement(List.of())), + """ + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + }else { + }"""), + Arguments.of( + new IfStatement(new BoolLiteral(true), List.of()), + Optional.empty(), + Optional.empty(), + "if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))) {\n" + + "}")); + } + + @ParameterizedTest + @MethodSource("sources_build_statement") + void build_statement( + IfStatement ifStatement, + Optional> elifStatementList, + Optional elseStatement, + String expected) { + IfThenElseStatement ifThenElseS = + new IfThenElseStatement(ifStatement, elifStatementList, elseStatement); + + System.out.println(testClass + ifThenElseS.buildStatement()); + + assertEquals(expected, ifThenElseS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/conditions/conditionalStatement/TestElifStatement.java b/src/test/java/CBuilder/conditions/conditionalStatement/TestElifStatement.java new file mode 100644 index 0000000..f8a24f7 --- /dev/null +++ b/src/test/java/CBuilder/conditions/conditionalStatement/TestElifStatement.java @@ -0,0 +1,72 @@ +/* (C)2024 */ +package CBuilder.conditions.conditionalStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import CBuilder.Expression; +import CBuilder.Statement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestElifStatement { + String testClass = "[ELIFSTATEMENT]\n"; + + private static Stream sources_build() { + return Stream.of( + Arguments.of( + new BoolLiteral(true), + List.of(), + "else if" + + " (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))) {\n" + + "}"), + Arguments.of( + new BoolLiteral(true), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + else if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }"""), + Arguments.of( + new BoolLiteral(false), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + else if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }""")); + } + + @ParameterizedTest + @MethodSource("sources_build") + void build(Expression condition, List statementList, String expected) { + ElifStatement elifS = new ElifStatement(condition, statementList); + + System.out.println(testClass + elifS.build()); + + assertEquals(expected, elifS.build()); + } + + @Test + void build_statement() { + ElifStatement elifS = new ElifStatement(new BoolLiteral(true), List.of()); + + System.out.println(testClass + elifS.buildStatement()); + + assertNull(elifS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/conditions/conditionalStatement/TestElseStatement.java b/src/test/java/CBuilder/conditions/conditionalStatement/TestElseStatement.java new file mode 100644 index 0000000..1368e1f --- /dev/null +++ b/src/test/java/CBuilder/conditions/conditionalStatement/TestElseStatement.java @@ -0,0 +1,54 @@ +/* (C)2024 */ +package CBuilder.conditions.conditionalStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import CBuilder.Expression; +import CBuilder.Statement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestElseStatement { + String testClass = "[ELSESTATEMENT]\n"; + + private static Stream sources_build() { + return Stream.of( + Arguments.of(List.of(), "else {\n" + "}"), + Arguments.of( + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + else { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }""")); + } + + @ParameterizedTest + @MethodSource("sources_build") + void build(List statementList, String expected) { + ElseStatement elseS = new ElseStatement(statementList); + + System.out.println(testClass + elseS.build()); + + assertEquals(expected, elseS.build()); + } + + @Test + void build_statement() { + ElseStatement elseS = new ElseStatement(List.of()); + + System.out.println(testClass + elseS.buildStatement()); + + assertNull(elseS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/conditions/conditionalStatement/TestIfStatement.java b/src/test/java/CBuilder/conditions/conditionalStatement/TestIfStatement.java new file mode 100644 index 0000000..d357bae --- /dev/null +++ b/src/test/java/CBuilder/conditions/conditionalStatement/TestIfStatement.java @@ -0,0 +1,72 @@ +/* (C)2024 */ +package CBuilder.conditions.conditionalStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import CBuilder.Expression; +import CBuilder.Statement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** Snapshot 23.02.2024 */ +public class TestIfStatement { + String testClass = "[IFSTATEMENT]\n"; + + private static Stream sources_build() { + return Stream.of( + Arguments.of( + new BoolLiteral(true), + List.of(), + "if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))) {\n" + + "}"), + Arguments.of( + new BoolLiteral(true), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }"""), + Arguments.of( + new BoolLiteral(false), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + if (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }""")); + } + + @ParameterizedTest + @MethodSource("sources_build") + void build(Expression condition, List statementList, String expected) { + IfStatement ifS = new IfStatement(condition, statementList); + + System.out.println(testClass + ifS.build()); + + assertEquals(expected, ifS.build()); + } + + @Test + void build_statement() { + IfStatement ifS = new IfStatement(new BoolLiteral(false), List.of()); + + System.out.println(testClass + ifS.buildStatement()); + + assertNull(ifS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/conditions/conditionalStatement/TestWhileStatement.java b/src/test/java/CBuilder/conditions/conditionalStatement/TestWhileStatement.java new file mode 100644 index 0000000..770c86f --- /dev/null +++ b/src/test/java/CBuilder/conditions/conditionalStatement/TestWhileStatement.java @@ -0,0 +1,94 @@ +/* (C)2024 */ +package CBuilder.conditions.conditionalStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.variables.Assignment; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestWhileStatement { + String testClass = "[WHILESTATEMENT]\n"; + + private static Stream sources_build() { + return Stream.of( + Arguments.of( + new BoolLiteral(true), + List.of(), + "while" + + " (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true)," + + " \"__bool__\"), __mpy_obj_init_tuple(0), NULL))) {\n" + + "}"), + Arguments.of( + new BoolLiteral(true), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + while (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(true), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }"""), + Arguments.of( + new BoolLiteral(false), + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + """ + while (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_boolean(false), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + }"""), + Arguments.of( + new Call( + new AttributeReference("__gt__", new Reference("a")), + List.of(new Expression[] {new IntLiteral(1)})), + List.of( + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})), + new Assignment( + new Reference("a"), + new Call( + new AttributeReference( + "__sub__", new Reference("a")), + List.of(new Expression[] {new IntLiteral(1)})))), + """ + while (__mpy_boolean_raw(__mpy_call(__mpy_obj_get_attr(__mpy_call(__mpy_obj_get_attr(a, "__gt__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL), "__bool__"), __mpy_obj_init_tuple(0), NULL))) { + \t__mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0, a, __mpy_obj_init_tuple(1)), NULL)); + \t__mpy_obj_ref_dec(a); + a = __mpy_call(__mpy_obj_get_attr(a, "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(a); + }""")); + } + + @ParameterizedTest + @MethodSource("sources_build") + void build(Expression condition, List statementList, String expected) { + WhileStatement whileS = new WhileStatement(condition, statementList); + + System.out.println(testClass + whileS.build()); + + assertEquals(expected, whileS.build()); + } + + @ParameterizedTest + @MethodSource("sources_build") + void build_statement(Expression condition, List statementList, String expected) { + WhileStatement whileS = new WhileStatement(condition, statementList); + + System.out.println(testClass + whileS.buildStatement()); + + assertEquals(expected, whileS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/literals/TestBoolLiteral.java b/src/test/java/CBuilder/literals/TestBoolLiteral.java new file mode 100644 index 0000000..4f258a9 --- /dev/null +++ b/src/test/java/CBuilder/literals/TestBoolLiteral.java @@ -0,0 +1,51 @@ +/* (C)2024 */ +package CBuilder.literals; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestBoolLiteral { + String testClass = "[BOOLLITERAL]\n"; + + private static Stream sources() { + return Stream.of(true, false); + } + + @ParameterizedTest + @MethodSource("sources") + void constructor(boolean b_value) { + + BoolLiteral boolL = new BoolLiteral(b_value); + + System.out.println(testClass + boolL.value); + + assertEquals(b_value, boolL.value); + } + + @ParameterizedTest + @MethodSource("sources") + void build_expression(boolean b_value) { + String expected = "__mpy_obj_init_boolean(" + b_value + ")"; + + BoolLiteral boolL = new BoolLiteral(b_value); + + System.out.println(testClass + boolL.buildExpression()); + + assertEquals(expected, boolL.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(boolean b_value) { + String expected = "__mpy_obj_init_boolean(" + b_value + ");\n"; + + BoolLiteral boolL = new BoolLiteral(b_value); + + System.out.println(testClass + boolL.buildStatement()); + + assertEquals(expected, boolL.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/literals/TestIntLiteral.java b/src/test/java/CBuilder/literals/TestIntLiteral.java new file mode 100644 index 0000000..d1341db --- /dev/null +++ b/src/test/java/CBuilder/literals/TestIntLiteral.java @@ -0,0 +1,52 @@ +/* (C)2024 */ +package CBuilder.literals; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestIntLiteral { + String testClass = "[INTLITERAL]\n"; + + private static Stream sources() { + return Stream.of(Integer.MAX_VALUE, Integer.MIN_VALUE); + } + + @ParameterizedTest + @MethodSource("sources") + void constructor(Integer i_value) { + long expected = i_value; + + IntLiteral intL = new IntLiteral(i_value); + + System.out.println(testClass + intL.value); + + assertEquals(expected, intL.value); + } + + @ParameterizedTest + @MethodSource("sources") + void build_expression(Integer i_value) { + String expected = "__mpy_obj_init_int(" + i_value + ")"; + + IntLiteral intL = new IntLiteral(i_value); + + System.out.println(testClass + intL.buildExpression()); + + assertEquals(expected, intL.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(Integer i_value) { + String expected = "__mpy_obj_init_int(" + i_value + ");\n"; + + IntLiteral intL = new IntLiteral(i_value); + + System.out.println(testClass + intL.buildStatement()); + + assertEquals(expected, intL.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/literals/TestStringLiteral.java b/src/test/java/CBuilder/literals/TestStringLiteral.java new file mode 100644 index 0000000..0ffac75 --- /dev/null +++ b/src/test/java/CBuilder/literals/TestStringLiteral.java @@ -0,0 +1,51 @@ +/* (C)2024 */ +package CBuilder.literals; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestStringLiteral { + String testClass = "[STRINGLITERAL]\n"; + + private static Stream sources() { + return Stream.of("true", "false"); + } + + @ParameterizedTest + @MethodSource("sources") + void constructor(String name) { + + StringLiteral stringL = new StringLiteral(name); + + System.out.println(testClass + stringL.value); + + assertEquals(name, stringL.value); + } + + @ParameterizedTest + @MethodSource("sources") + void build_expression(String name) { + String expected = "__mpy_obj_init_str_static(\"" + name + "\")"; + + StringLiteral stringL = new StringLiteral(name); + + System.out.println(testClass + stringL.buildExpression()); + + assertEquals(expected, stringL.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(String name) { + String expected = "__mpy_obj_ref_dec__mpy_obj_init_str_static(\"" + name + "\"));\n"; + + StringLiteral stringL = new StringLiteral(name); + + System.out.println(testClass + stringL.buildStatement()); + + assertEquals(expected, stringL.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/literals/TestTupleLiteral.java b/src/test/java/CBuilder/literals/TestTupleLiteral.java new file mode 100644 index 0000000..3ee875e --- /dev/null +++ b/src/test/java/CBuilder/literals/TestTupleLiteral.java @@ -0,0 +1,73 @@ +/* (C)2024 */ +package CBuilder.literals; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** Snapshot 23.02.2024 print(tupleLiteral) does not work will look into */ +public class TestTupleLiteral { + String testClass = "[TUPLELITERAL]\n"; + + private static Stream sources_expression() { + return Stream.of( + Arguments.of( + List.of(new IntLiteral(1)), + "__mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_obj_init_tuple(1))"), + Arguments.of( + List.of(new IntLiteral(1), new IntLiteral(2), new IntLiteral(3)), + "__mpy_tuple_assign(0, __mpy_obj_init_int(1), __mpy_tuple_assign(1," + + " __mpy_obj_init_int(2), __mpy_tuple_assign(2, __mpy_obj_init_int(3)," + + " __mpy_obj_init_tuple(3))))"), + Arguments.of( + List.of(new StringLiteral("Test1"), new StringLiteral("Test2")), + "__mpy_tuple_assign(0, __mpy_obj_init_str_static(\"Test1\")," + + " __mpy_tuple_assign(1, __mpy_obj_init_str_static(\"Test2\")," + + " __mpy_obj_init_tuple(2)))")); + } + + private static Stream sources_statement() { + return Stream.of( + Arguments.of( + List.of(new IntLiteral(1)), + "__mpy_obj_ref_dec(__mpy_tuple_assign(0, __mpy_obj_init_int(1)," + + " __mpy_obj_init_tuple(1)));\n"), + Arguments.of( + List.of(new IntLiteral(1), new IntLiteral(2), new IntLiteral(3)), + "__mpy_obj_ref_dec(__mpy_tuple_assign(0, __mpy_obj_init_int(1)," + + " __mpy_tuple_assign(1, __mpy_obj_init_int(2), __mpy_tuple_assign(2," + + " __mpy_obj_init_int(3), __mpy_obj_init_tuple(3)))));\n"), + Arguments.of( + List.of(new StringLiteral("Test1"), new StringLiteral("Test2")), + "__mpy_obj_ref_dec(__mpy_tuple_assign(0," + + " __mpy_obj_init_str_static(\"Test1\"), __mpy_tuple_assign(1," + + " __mpy_obj_init_str_static(\"Test2\")," + + " __mpy_obj_init_tuple(2))));\n")); + } + + @ParameterizedTest + @MethodSource("sources_expression") + void build_expression(List expressionList, String expected) { + + TupleLiteral tupleL = new TupleLiteral(expressionList); + + System.out.println(testClass + tupleL.buildExpression()); + + assertEquals(expected, tupleL.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_statement") + void build_statement(List expressionList, String expected) { + TupleLiteral tupleL = new TupleLiteral(expressionList); + + System.out.println(testClass + tupleL.buildStatement()); + + assertEquals(expected, tupleL.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/objects/TestAttributeAssignment.java b/src/test/java/CBuilder/objects/TestAttributeAssignment.java new file mode 100644 index 0000000..3b76275 --- /dev/null +++ b/src/test/java/CBuilder/objects/TestAttributeAssignment.java @@ -0,0 +1,46 @@ +/* (C)2024 */ +package CBuilder.objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestAttributeAssignment { + String testClass = "[ATTRIBUTEASSIGNMENT]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new AttributeReference("referenceName1", new IntLiteral(23)), + new IntLiteral(34), + "__mpy_obj_set_attr(__mpy_obj_init_int(23), \"referenceName1\"," + + " __mpy_obj_init_int(34));"), + Arguments.of( + new AttributeReference("referenceName2", new BoolLiteral(false)), + new BoolLiteral(true), + "__mpy_obj_set_attr(__mpy_obj_init_boolean(false), \"referenceName2\"," + + " __mpy_obj_init_boolean(true));"), + Arguments.of( + new AttributeReference("referenceName3", new StringLiteral("stringL")), + new StringLiteral("34"), + "__mpy_obj_set_attr(__mpy_obj_init_str_static(\"stringL\")," + + " \"referenceName3\", __mpy_obj_init_str_static(\"34\"));")); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(AttributeReference attributeR, Expression value, String expected) { + AttributeAssignment attributeA = new AttributeAssignment(attributeR, value); + + System.out.println(testClass + attributeA.buildStatement()); + + assertEquals(expected, attributeA.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/objects/TestAttributeReference.java b/src/test/java/CBuilder/objects/TestAttributeReference.java new file mode 100644 index 0000000..4dc7160 --- /dev/null +++ b/src/test/java/CBuilder/objects/TestAttributeReference.java @@ -0,0 +1,119 @@ +/* (C)2024 */ +package CBuilder.objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestAttributeReference { + String testClass = "[ATTRIBUTEREFERENCE]\n"; + + private static Stream sources_get_name() { + return Stream.of( + Arguments.of("referenceName", new IntLiteral(23), "referenceName"), + Arguments.of("referenceName1", new BoolLiteral(false), "referenceName1"), + Arguments.of("referenceName2", new StringLiteral("stringL"), "referenceName2")); + } + + private static Stream sources_build_expression() { + return Stream.of( + Arguments.of( + "referenceName", + new IntLiteral(23), + "__mpy_obj_get_attr(__mpy_obj_init_int(23), \"referenceName\")"), + Arguments.of( + "referenceName1", + new BoolLiteral(true), + "__mpy_obj_get_attr(__mpy_obj_init_boolean(true), \"referenceName1\")"), + Arguments.of( + "referenceName2", + new StringLiteral("stringL"), + "__mpy_obj_get_attr(__mpy_obj_init_str_static(\"stringL\")," + + " \"referenceName2\")")); + } + + private static Stream sources_build_statement() { + return Stream.of( + Arguments.of( + "referenceName", + new IntLiteral(23), + "__mpy_obj_get_attr(__mpy_obj_init_int(23), \"referenceName\");\n"), + Arguments.of( + "referenceName1", + new BoolLiteral(true), + "__mpy_obj_get_attr(__mpy_obj_init_boolean(true), \"referenceName1\");\n"), + Arguments.of( + "referenceName2", + new StringLiteral("stringL"), + "__mpy_obj_get_attr(__mpy_obj_init_str_static(\"stringL\")," + + " \"referenceName2\");\n")); + } + + private static Stream sources_build_object() { + return Stream.of( + Arguments.of("referenceName", new IntLiteral(23), "__mpy_obj_init_int(23)"), + Arguments.of( + "referenceName1", new BoolLiteral(true), "__mpy_obj_init_boolean(true)"), + Arguments.of( + "referenceName2", + new StringLiteral("stringL"), + "__mpy_obj_init_str_static(\"stringL\")")); + } + + @ParameterizedTest + @MethodSource("sources_get_name") + void get_name(String name, Expression expression, String expected) { + AttributeReference attributeR = new AttributeReference(name, expression); + + System.out.println(testClass + attributeR.getName()); + + assertEquals(expected, attributeR.getName()); + } + + @ParameterizedTest + @MethodSource("sources_build_expression") + void build_expression(String name, Expression expression, String expected) { + AttributeReference attributeR = new AttributeReference(name, expression); + + System.out.println(testClass + attributeR.buildExpression()); + + assertEquals(expected, attributeR.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_build_statement") + void build_statement(String name, Expression expression, String expected) { + AttributeReference attributeR = new AttributeReference(name, expression); + + System.out.println(testClass + attributeR.buildStatement()); + + assertEquals(expected, attributeR.buildStatement()); + } + + @ParameterizedTest + @MethodSource("sources_build_object") + void build_object(String name, Expression expression, String expected) { + AttributeReference attributeR = new AttributeReference(name, expression); + + System.out.println(attributeR.buildObject()); + + assertEquals(expected, attributeR.buildObject()); + } + + @Test + void build_name() { + AttributeReference attributeR = new AttributeReference("attributeR", new IntLiteral(23)); + + System.out.println(testClass + attributeR.buildName()); + + assertEquals("\"attributeR\"", attributeR.buildName()); + } +} diff --git a/src/test/java/CBuilder/objects/TestCall.java b/src/test/java/CBuilder/objects/TestCall.java new file mode 100644 index 0000000..fc18f7a --- /dev/null +++ b/src/test/java/CBuilder/objects/TestCall.java @@ -0,0 +1,65 @@ +/* (C)2024 */ +package CBuilder.objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestCall { + String testClass = "[CALL]\n"; + + private static Stream sources_build_expression() { + return Stream.of( + Arguments.of( + new Reference("print"), + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_call(print, __mpy_tuple_assign(0, __mpy_obj_init_int(23)," + + " __mpy_obj_init_tuple(1)), NULL)"), + Arguments.of( + new Reference("type"), + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_call(type, __mpy_tuple_assign(0, __mpy_obj_init_int(23)," + + " __mpy_obj_init_tuple(1)), NULL)")); + } + + private static Stream sources_build_statement() { + return Stream.of( + Arguments.of( + new Reference("print"), + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_obj_ref_dec(__mpy_call(print, __mpy_tuple_assign(0," + + " __mpy_obj_init_int(23), __mpy_obj_init_tuple(1)), NULL));\n"), + Arguments.of( + new Reference("type"), + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_obj_ref_dec(__mpy_call(type, __mpy_tuple_assign(0," + + " __mpy_obj_init_int(23), __mpy_obj_init_tuple(1)), NULL));\n")); + } + + @ParameterizedTest + @MethodSource("sources_build_expression") + void build_expression(Expression callable, List args, String expected) { + Call call = new Call(callable, args); + + System.out.println(testClass + call.buildExpression()); + + assertEquals(expected, call.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_build_statement") + void build_statement(Expression callable, List args, String expected) { + Call call = new Call(callable, args); + + System.out.println(testClass + call.buildStatement()); + + assertEquals(expected, call.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/objects/TestMPyClass.java b/src/test/java/CBuilder/objects/TestMPyClass.java new file mode 100644 index 0000000..f96aeab --- /dev/null +++ b/src/test/java/CBuilder/objects/TestMPyClass.java @@ -0,0 +1,310 @@ +/* (C)2024 */ +package CBuilder.objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import CBuilder.Expression; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestMPyClass { + String testClass = "[TESTMPYCLASS]\n"; + + @ParameterizedTest + @MethodSource("sources_build_declaration") + void build_declaration( + String name, + Reference parent, + List functions, + Map classAttributes, + String expected) { + MPyClass mpyC = new MPyClass(name, parent, functions, classAttributes); + + System.out.println(testClass + mpyC.buildDeclaration()); + + assertEquals(expected, mpyC.buildDeclaration()); + } + + @ParameterizedTest + @MethodSource("sources_build_initialisation") + void build_initialisation( + String name, + Reference parent, + List functions, + Map classAttributes, + String expected) { + MPyClass mpyC = new MPyClass(name, parent, functions, classAttributes); + + System.out.println(testClass + mpyC.buildInitialisation()); + + assertEquals(expected, mpyC.buildInitialisation()); + } + + @ParameterizedTest + @MethodSource("sources_build_refdec") + void build_refdec( + String name, + Reference parent, + List functions, + Map classAttributes, + String expected) { + MPyClass mpyC = new MPyClass(name, parent, functions, classAttributes); + + System.out.println(testClass + mpyC.buildRefDec()); + + assertEquals(expected, mpyC.buildRefDec()); + } + + @ParameterizedTest + @MethodSource("sources_build_expression") + void build_expression( + String name, + Reference parent, + List functions, + Map classAttributes, + String expected) { + MPyClass mpyC = new MPyClass(name, parent, functions, classAttributes); + + System.out.println(testClass + mpyC.buildExpression()); + + assertEquals(expected, mpyC.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_build_statement") + void build_statement( + String name, + Reference parent, + List functions, + Map classAttributes) { + MPyClass mpyC = new MPyClass(name, parent, functions, classAttributes); + + System.out.println(testClass + mpyC.buildStatement()); + + assertNull(mpyC.buildStatement()); + } + + private static Stream sources_build_declaration() { + return Stream.of( + Arguments.of( + "MyClass1", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + """ + __MPyObj *MyClass1; + __MPyObj* func_MyClass1___init__(__MPyObj *args, __MPyObj *kwargs) { + \tassert(args != NULL && kwargs != NULL); + \t + \t__MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 1); + \t__MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + \t__mpy_args_finish(&argHelper); + \t + \t__MPyObj *retValue = NULL; + \t + \t__mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + \t + \t__mpy_obj_ref_dec(self); + \t + \tgoto ret; + \tret: + \tif (retValue == NULL) { + \t\tretValue = __mpy_obj_init_object(); + \t} + \treturn __mpy_obj_return(retValue); + } + """), + Arguments.of( + "MyClass2", + new Reference("MyClass1"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + """ + __MPyObj *MyClass2; + __MPyObj* func_MyClass2___init__(__MPyObj *args, __MPyObj *kwargs) { + \tassert(args != NULL && kwargs != NULL); + \t + \t__MPyGetArgsState argHelper = __mpy_args_init("__init__", args, kwargs, 1); + \t__MPyObj *self = __mpy_args_get_positional(&argHelper, 0, "self"); + \t__mpy_args_finish(&argHelper); + \t + \t__MPyObj *retValue = NULL; + \t + \t__mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_obj_init_tuple(1)), NULL)); + \t + \t__mpy_obj_ref_dec(self); + \t + \tgoto ret; + \tret: + \tif (retValue == NULL) { + \t\tretValue = __mpy_obj_init_object(); + \t} + \treturn __mpy_obj_return(retValue); + } + """)); + } + + private static Stream sources_build_initialisation() { + return Stream.of( + Arguments.of( + "MyClass1", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + """ + MyClass1 = __mpy_obj_init_type("MyClass1", __MPyType_Object); + __mpy_obj_ref_inc(MyClass1); + { + \t__MPyObj *__init__; + \t__init__ = __mpy_obj_init_func(&func_MyClass1___init__); + \t__mpy_obj_ref_inc(__init__); + \t__mpy_obj_set_attr(MyClass1, "__init__", __init__); + \t__mpy_obj_ref_dec(__init__); + } + """), + Arguments.of( + "MyClass2", + new Reference("MyClass1"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + """ + MyClass2 = __mpy_obj_init_type("MyClass2", MyClass1); + __mpy_obj_ref_inc(MyClass2); + { + \t__MPyObj *__init__; + \t__init__ = __mpy_obj_init_func(&func_MyClass2___init__); + \t__mpy_obj_ref_inc(__init__); + \t__mpy_obj_set_attr(MyClass2, "__init__", __init__); + \t__mpy_obj_ref_dec(__init__); + } + """)); + } + + private static Stream sources_build_refdec() { + return Stream.of( + Arguments.of( + "MyClass1", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "__mpy_obj_ref_dec(MyClass1);\n"), + Arguments.of( + "MyClass2", + new Reference("MyClass1"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "__mpy_obj_ref_dec(MyClass2);\n")); + } + + private static Stream sources_build_expression() { + return Stream.of( + Arguments.of( + "MyClass1", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "MyClass1"), + Arguments.of( + "MyClass2", + new Reference("MyClass1"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "MyClass2")); + } + + private static Stream sources_build_statement() { + return Stream.of( + Arguments.of( + "MyClass1", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "__mpy_obj_ref_dec(MyClass1);\n"), + Arguments.of( + "MyClass2", + new Reference("MyClass1"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>(), + "__mpy_obj_ref_dec(MyClass2);\n")); + } +} diff --git a/src/test/java/CBuilder/objects/TestSuperCall.java b/src/test/java/CBuilder/objects/TestSuperCall.java new file mode 100644 index 0000000..1c13d1f --- /dev/null +++ b/src/test/java/CBuilder/objects/TestSuperCall.java @@ -0,0 +1,63 @@ +/* (C)2024 */ +package CBuilder.objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestSuperCall { + String testClass = "[SUPERCALL]\n"; + + private static Stream sources_expression() { + return Stream.of( + Arguments.of( + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_tuple_assign(1," + + " __mpy_obj_init_int(23), __mpy_obj_init_tuple(2))), NULL)"), + Arguments.of( + List.of(new Expression[] {new BoolLiteral(true)}), + "__mpy_call(__mpy_super, __mpy_tuple_assign(0, self, __mpy_tuple_assign(1," + + " __mpy_obj_init_boolean(true), __mpy_obj_init_tuple(2))), NULL)")); + } + + private static Stream sources_statement() { + return Stream.of( + Arguments.of( + List.of(new Expression[] {new IntLiteral(23)}), + "__mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self," + + " __mpy_tuple_assign(1, __mpy_obj_init_int(23)," + + " __mpy_obj_init_tuple(2))), NULL));\n"), + Arguments.of( + List.of(new Expression[] {new BoolLiteral(true)}), + "__mpy_obj_ref_dec(__mpy_call(__mpy_super, __mpy_tuple_assign(0, self," + + " __mpy_tuple_assign(1, __mpy_obj_init_boolean(true)," + + " __mpy_obj_init_tuple(2))), NULL));\n")); + } + + @ParameterizedTest + @MethodSource("sources_expression") + void build_expression(List args, String expected) { + SuperCall superC = new SuperCall(args); + + System.out.println(testClass + superC.buildExpression()); + + assertEquals(expected, superC.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_statement") + void build_statement(List args, String expected) { + SuperCall superC = new SuperCall(args); + + System.out.println(testClass + superC.buildStatement()); + + assertEquals(expected, superC.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/objects/functions/TestArgument.java b/src/test/java/CBuilder/objects/functions/TestArgument.java new file mode 100644 index 0000000..77b2050 --- /dev/null +++ b/src/test/java/CBuilder/objects/functions/TestArgument.java @@ -0,0 +1,59 @@ +/* (C)2024 */ +package CBuilder.objects.functions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestArgument { + String testClass = "[ARGUMENT]\n"; + + private static Stream sources_arg_extraction() { + return Stream.of( + Arguments.of( + "argName1", + 1, + "__MPyObj *argName1 = __mpy_args_get_positional(&argHelper, 1," + + " \"argName1\");\n"), + Arguments.of( + "argName2", + 2, + "__MPyObj *argName2 = __mpy_args_get_positional(&argHelper, 2," + + " \"argName2\");\n"), + Arguments.of( + "argName3", + 3, + "__MPyObj *argName3 = __mpy_args_get_positional(&argHelper, 3," + + " \"argName3\");\n")); + } + + private static Stream sources_build_arg_cleanup() { + return Stream.of( + Arguments.of("argName1", 1, "__mpy_obj_ref_dec(argName1);\n"), + Arguments.of("argName2", 2, "__mpy_obj_ref_dec(argName2);\n"), + Arguments.of("argName3", 3, "__mpy_obj_ref_dec(argName3);\n")); + } + + @ParameterizedTest + @MethodSource("sources_arg_extraction") + void build_arg_extraction(String name, int position, String expected) { + Argument argument = new Argument(name, position); + + System.out.println(testClass + argument.buildArgExtraction()); + + assertEquals(expected, argument.buildArgExtraction()); + } + + @ParameterizedTest + @MethodSource("sources_build_arg_cleanup") + void build_arg_cleanup(String name, int position, String expected) { + Argument argument = new Argument(name, position); + + System.out.println(testClass + argument.buildArgCleanup()); + + assertEquals(expected, argument.buildArgCleanup()); + } +} diff --git a/src/test/java/CBuilder/objects/functions/TestFunction.java b/src/test/java/CBuilder/objects/functions/TestFunction.java new file mode 100644 index 0000000..bd80c20 --- /dev/null +++ b/src/test/java/CBuilder/objects/functions/TestFunction.java @@ -0,0 +1,230 @@ +/* (C)2024 */ +package CBuilder.objects.functions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.Statement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.variables.VariableDeclaration; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestFunction { + String testClass = "[FUNCTION]\n"; + + @ParameterizedTest + @MethodSource("sources_build_cfunction") + void build_cfunction( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.buildCFunction()); + + assertEquals(expected, function.buildCFunction()); + } + + @ParameterizedTest + @MethodSource("sources_build_func_object_declaration") + void build_func_object_declaration( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.buildFuncObjectDeclaration()); + + assertEquals(expected, function.buildFuncObjectDeclaration()); + } + + @ParameterizedTest + @MethodSource("sources_build_initialisation") + void build_initialisation( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.buildInitialisation()); + + assertEquals(expected, function.buildInitialisation()); + } + + @ParameterizedTest + @MethodSource("sources_build_refdec") + void build_refdec( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.buildRefDec()); + + assertEquals(expected, function.buildRefDec()); + } + + @ParameterizedTest + @MethodSource("sources_build_expression") + void build_expression( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.buildExpression()); + + assertEquals(expected, function.buildExpression()); + } + + @ParameterizedTest + @MethodSource("sources_get_name") + void get_name( + String funcName, + List body, + List positionalArgs, + List localVariables, + String expected) { + Function function = new Function(funcName, body, positionalArgs, localVariables); + + System.out.println(testClass + function.getName()); + + assertEquals(expected, function.getName()); + } + + private static Stream sources_build_cfunction() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + """ + __MPyObj* func_funcName1(__MPyObj *args, __MPyObj *kwargs) { + \tassert(args != NULL && kwargs != NULL); + \t + \t__MPyGetArgsState argHelper = __mpy_args_init("funcName1", args, kwargs, 2); + \t__MPyObj *argName1 = __mpy_args_get_positional(&argHelper, 0, "argName1"); + \t__MPyObj *argName2 = __mpy_args_get_positional(&argHelper, 1, "argName2"); + \t__mpy_args_finish(&argHelper); + \t + \t__MPyObj *retValue = NULL; + \t + \t__MPyObj *local1 = __mpy_obj_init_object(); + \t__mpy_obj_ref_inc(local1); + \t__MPyObj *local2 = __mpy_obj_init_object(); + \t__mpy_obj_ref_inc(local2); + \t__mpy_obj_ref_dec(__mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(1), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(3), __mpy_obj_init_tuple(1)), NULL)); + \t + \t__mpy_obj_ref_dec(argName1); + \t__mpy_obj_ref_dec(argName2); + \t + \tgoto ret; + \tret: + \tif (retValue == NULL) { + \t\tretValue = __mpy_obj_init_object(); + \t} + \treturn __mpy_obj_return(retValue); + } + """)); + } + + private static Stream sources_build_func_object_declaration() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + "__MPyObj *funcName1;\n")); + } + + private static Stream sources_build_initialisation() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + """ + funcName1 = __mpy_obj_init_func(&func_funcName1); + __mpy_obj_ref_inc(funcName1); + """)); + } + + private static Stream sources_build_refdec() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + "__mpy_obj_ref_dec(funcName1);\n")); + } + + private static Stream sources_build_expression() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + "funcName1")); + } + + private static Stream sources_get_name() { + return Stream.of( + Arguments.of( + "funcName1", + List.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(3)}))), + List.of(new Argument("argName1", 0), new Argument("argName2", 1)), + List.of( + new VariableDeclaration("local1"), + new VariableDeclaration("local2")), + "funcName1")); + } +} diff --git a/src/test/java/CBuilder/objects/functions/TestReturnStatement.java b/src/test/java/CBuilder/objects/functions/TestReturnStatement.java new file mode 100644 index 0000000..f192451 --- /dev/null +++ b/src/test/java/CBuilder/objects/functions/TestReturnStatement.java @@ -0,0 +1,49 @@ +/* (C)2024 */ +package CBuilder.objects.functions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestReturnStatement { + String testClass = "[RETURNSTATEMENT]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new IntLiteral(34), + """ + retValue = __mpy_obj_init_int(34); + goto ret; + """), + Arguments.of( + new BoolLiteral(true), + """ + retValue = __mpy_obj_init_boolean(true); + goto ret; + """), + Arguments.of( + new StringLiteral("34"), + """ + retValue = __mpy_obj_init_str_static("34"); + goto ret; + """)); + } + + @ParameterizedTest + @MethodSource("sources") + void build_statement(Expression returnValue, String expected) { + ReturnStatement returnS = new ReturnStatement(returnValue); + + System.out.println(testClass + returnS.buildStatement()); + + assertEquals(expected, returnS.buildStatement()); + } +} diff --git a/src/test/java/CBuilder/variables/TestAssignment.java b/src/test/java/CBuilder/variables/TestAssignment.java new file mode 100644 index 0000000..d692763 --- /dev/null +++ b/src/test/java/CBuilder/variables/TestAssignment.java @@ -0,0 +1,126 @@ +/* (C)2024 */ +package CBuilder.variables; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import java.util.List; +import org.junit.jupiter.api.Test; + +/** Snapshot 22.02.2024 */ +public class TestAssignment { + String testClass = "[ASSIGNMENT]\n"; + + /** + * Test right hand side reference
+ * a = b + */ + @Test + void assignment_rhs_ref() { + String expected = + """ + __mpy_obj_ref_inc(b); + __mpy_obj_ref_dec(a); + a = b; + """; + + Assignment assignment = new Assignment(new Reference("a"), new Reference("b")); + + String actual = assignment.buildStatement(); + + System.out.println(testClass + actual); + + assertEquals(expected, actual); + } + + /** + * Test right hand side literal (no reference)
+ * a = 133 + */ + @Test + void assignment_rhs_int_literal() { + String expected = + """ + __mpy_obj_ref_dec(a); + a = __mpy_obj_init_int(133); + __mpy_obj_ref_inc(a); + """; + + Assignment assignment = new Assignment(new Reference("a"), new IntLiteral(133)); + + String actual = assignment.buildStatement(); + + System.out.println(testClass + actual); + + assertEquals(expected, actual); + } + + /** + * Test right hand side function (which is also reference)
+ * a = printA + */ + @Test + void assignment_rhs_function() { + String expected = + """ + __mpy_obj_ref_inc(printA); + __mpy_obj_ref_dec(a); + a = printA; + """; + + Function printA = + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of()); + + Assignment assignment = new Assignment(new Reference("a"), printA); + + String actual = assignment.buildStatement(); + + System.out.println(testClass + actual); + + assertEquals(expected, actual); + } + + // d = a+b + + /** + * Test right hand side expression (no reference)
+ * d = a + b + */ + @Test + void assignment_rhs_expression() { + String expected = + """ + __mpy_obj_ref_dec(d); + d = __mpy_call(__mpy_obj_get_attr(a, "__add__"), __mpy_tuple_assign(0, b, __mpy_obj_init_tuple(1)), NULL); + __mpy_obj_ref_inc(d); + """; + + AttributeReference addA = new AttributeReference("__add__", new Reference("a")); + Assignment assignD = + new Assignment( + new Reference("d"), + new Call(addA, List.of(new Expression[] {new Reference("b")}))); + + String actual = assignD.buildStatement(); + + System.out.println(testClass + actual); + + assertEquals(expected, actual); + } +} diff --git a/src/test/java/CBuilder/variables/TestVariableDeclaration.java b/src/test/java/CBuilder/variables/TestVariableDeclaration.java new file mode 100644 index 0000000..716119b --- /dev/null +++ b/src/test/java/CBuilder/variables/TestVariableDeclaration.java @@ -0,0 +1,102 @@ +/* (C)2024 */ +package CBuilder.variables; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** Snapshot 22.02.2024 */ +public class TestVariableDeclaration { + private static Stream sources() { + return Stream.of("someVar", "anotherOne", "", "'"); + } + + /** + * var_name # just init + * + * @param var_name - variable name + */ + @ParameterizedTest + @MethodSource("sources") + void build_initialisation_parameterized(String var_name) { + String expected = + var_name + + " = __mpy_obj_init_object();\n" + + "__mpy_obj_ref_inc(" + + var_name + + ");\n"; + + VariableDeclaration var = new VariableDeclaration(var_name); + + String actual = var.buildInitialisation(); + + System.out.println(actual); + + assertEquals(expected, actual); + } + + /** + * var_name # with init + * + * @param var_name - variable name + */ + @ParameterizedTest + @MethodSource("sources") + void build_true_parameterized(String var_name) { + String expected = + "__MPyObj *" + + var_name + + " = __mpy_obj_init_object();\n" + + "__mpy_obj_ref_inc(" + + var_name + + ");\n"; + + VariableDeclaration var = new VariableDeclaration(var_name); + + String actual = var.build(true); + + System.out.println(actual); + + assertEquals(expected, actual); + } + + /** + * var_name # without init + * + * @param var_name - variable name + */ + @ParameterizedTest + @MethodSource("sources") + void build_false_parameterized(String var_name) { + String expected = "__MPyObj *" + var_name + ";\n"; + + VariableDeclaration var = new VariableDeclaration(var_name); + + String actual = var.build(false); + + System.out.println(actual); + + assertEquals(expected, actual); + } + + /** + * Test decrementing reference counter of the variable + * + * @param var_name - variable name + */ + @ParameterizedTest + @MethodSource("sources") + void build_ref_dec_parameterized(String var_name) { + String expected = "__mpy_obj_ref_dec(" + var_name + ");\n"; + + VariableDeclaration var = new VariableDeclaration(var_name); + + String actual = var.buildRefDec(); + + System.out.println(actual); + + assertEquals(expected, actual); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/ArithmeticOps/TestArithmeticOps.java b/src/test/java/Systemtests/LanguageFeatures/ArithmeticOps/TestArithmeticOps.java new file mode 100644 index 0000000..f7a7a98 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/ArithmeticOps/TestArithmeticOps.java @@ -0,0 +1,83 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.ArithmeticOps; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestArithmeticOps { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of("__add__", 1, 2, "3\n"), + Arguments.of("__add__", Integer.MAX_VALUE, 0, Integer.MAX_VALUE + "\n"), + Arguments.of("__add__", Integer.MIN_VALUE, 0, Integer.MIN_VALUE + "\n"), + Arguments.of("__sub__", 2, 1, "1\n"), + Arguments.of("__sub__", Integer.MAX_VALUE, 0, Integer.MAX_VALUE + "\n"), + Arguments.of("__sub__", Integer.MIN_VALUE, 0, Integer.MIN_VALUE + "\n"), + Arguments.of("__mul__", 2, 2, "4\n"), + Arguments.of("__mul__", Integer.MAX_VALUE, 0, "0\n"), + Arguments.of("__mul__", Integer.MIN_VALUE, 0, "0\n"), + Arguments.of("__div__", 5, 2, "2\n"), + Arguments.of("__div__", 0, Integer.MAX_VALUE, "0\n"), + Arguments.of("__div__", 0, Integer.MIN_VALUE, "0\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void arithmetic_operation( + String operation, Integer a, Integer b, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_arithmetic_operation(workDirectory, operation, a, b); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Operation : " + operation + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * print(a (+,-,*,/) b) + * + * @param a Summand + * @param b Summand + */ + void generate_arithmetic_operation(Path output, String operation, Integer a, Integer b) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference(operation, new IntLiteral(a)), + List.of(new Expression[] {new IntLiteral(b)})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastInt.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastInt.java new file mode 100644 index 0000000..f7b6c25 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastInt.java @@ -0,0 +1,104 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.Literal; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestCastInt { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + @ParameterizedTest + @MethodSource("sources_equals") + void cast_int_equals(Literal value, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_cast_int(workDirectory, value); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Value : " + value + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + @ParameterizedTest + @MethodSource("sources_throws") + void cast_int_throws(Literal value, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result = ""; + + generate_cast_int(workDirectory, value); + + makeProgram(workDirectory); + + assertThrows(RuntimeException.class, () -> getProgramOutput(workDirectory)); + + System.out.println(testClass + "Value : " + value + " Result : " + result); + + // Thread.sleep(5000); + } + + /** + * Mini Python source code :
+ * print(int( True, False, "23", "0", "-23", 23[error], "false"[error])) + */ + void generate_cast_int(Path output, Literal value) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + // new Assignment(new Reference("a"), new Call(new AttributeReference("__int__", new + // Reference("a")), List.of(new Expression[]{}))); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference("__int__", value), + List.of(new Expression[] {})) + }))); + + builder.writeProgram(output); + } + + private static Stream sources_equals() { + return Stream.of( + Arguments.of(new BoolLiteral(true), "1\n"), + Arguments.of(new BoolLiteral(false), "0\n"), + Arguments.of(new StringLiteral("23"), "23\n"), + Arguments.of(new StringLiteral("0"), "0\n"), + Arguments.of(new StringLiteral("-23"), "-23\n")); + } + + private static Stream sources_throws() { + return Stream.of( + Arguments.of(new IntLiteral(23)), Arguments.of(new StringLiteral("false"))); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastStr.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastStr.java new file mode 100644 index 0000000..86aaf92 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestCastStr.java @@ -0,0 +1,82 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.Literal; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestCastStr { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of(new BoolLiteral(true), "True\n"), + Arguments.of(new BoolLiteral(false), "False\n"), + Arguments.of(new StringLiteral("testString"), "testString\n"), + Arguments.of(new IntLiteral(0), "0\n"), + Arguments.of(new IntLiteral(-23), "-23\n"), + Arguments.of(new IntLiteral(23), "23\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void cast_str(Literal value, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_cast_str(workDirectory, value); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Value : " + value + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * print(str( True, False, 23, 0, -23, "testString")) + */ + void generate_cast_str(Path output, Literal value) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + // new Assignment(new Reference("a"), new Call(new AttributeReference("__int__", new + // Reference("a")), List.of(new Expression[]{}))); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference("__str__", value), + List.of(new Expression[] {})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestDifferentialExamplePrint.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestDifferentialExamplePrint.java new file mode 100644 index 0000000..86191e0 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestDifferentialExamplePrint.java @@ -0,0 +1,83 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.Call; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.*; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TestDifferentialExamplePrint { + + @Test + void call_print(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + + generate_call_print(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(result); + + ProcessBuilder builder = + new ProcessBuilder( + "bash", "-c", "printf 'a=133\nprint(a)' >> test.py ; python3 test.py"); + builder.directory(workDirectory.toFile()); + // builder.inheritIO(); + + Process p = builder.start(); + + StringBuilder python3Output = new StringBuilder(); + String line; // https://github.com/actions/runner-images + + InputStream processStdOutput = p.getInputStream(); + Reader r = new InputStreamReader(processStdOutput); + BufferedReader br = new BufferedReader(r); + + while ((line = br.readLine()) != null) { + python3Output.append(line).append("\n"); + } + + if (p.waitFor() != 0) { + System.out.println("Could not create file test.py or run python3 test.py !"); + throw new RuntimeException("Could not create file test.py !"); + } + + System.out.println(python3Output); + + // Thread.sleep(5000); + assertEquals(python3Output.toString(), result); + + // After Test + } + + /** + * Mini Python source code :
+ * a = 133
+ * print(a) + */ + static void generate_call_print(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("a")}))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestId.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestId.java new file mode 100644 index 0000000..a876b67 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestId.java @@ -0,0 +1,77 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +@Disabled +public class TestId { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of(new IntLiteral(133), "61\n"), + Arguments.of(new BoolLiteral(true), "61\n"), + Arguments.of(new StringLiteral("toPrint"), "61\n"), + Arguments.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(1)})), + "102\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void id(Expression toId, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_id(workDirectory, toId); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "toId : " + toId + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * print(id(133, True, "toPrint", 1+1)) + */ + void generate_id(Path output, Expression toId) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call(new Reference("id"), List.of(new Expression[] {toId})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestInput.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestInput.java new file mode 100644 index 0000000..50d8518 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestInput.java @@ -0,0 +1,84 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutputBasedOnInput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.time.Duration; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +@Disabled +public class TestInput { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of("61\n", "61\n"), + Arguments.of("True\n", "True\n"), + Arguments.of("!!\n", "!!\n"), + Arguments.of("\n", "\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void input(String input, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result = ""; + + generate_input(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutputBasedOnInput(workDirectory, result, input); + + System.out.println(testClass + "input : " + input + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + @Test + void input_timeout(@TempDir Path workDirectory) throws IOException, InterruptedException { + String input = ""; + String result = ""; + + generate_input(workDirectory); + + makeProgram(workDirectory); + + System.out.println(testClass + "input : " + input + " Result : " + result); + + assertTimeoutPreemptively( + Duration.ofSeconds(5), + () -> getProgramOutputBasedOnInput(workDirectory, result, input)); + } + + void generate_input(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call(new Reference("input"), List.of(new Expression[] {})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestMetamorphicExamplePrint.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestMetamorphicExamplePrint.java new file mode 100644 index 0000000..656e6cc --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestMetamorphicExamplePrint.java @@ -0,0 +1,114 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TestMetamorphicExamplePrint { + + @Test + void call_print(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + String expected = "133\n"; + + generate_call_print(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + @Test + void print_through_function(@TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + String expected = "133\n"; + + generate_print_through_function(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + /** + * Mini Python source code :
+ * a = 133
+ * print(a) + */ + static void generate_call_print(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("a")}))); + + builder.writeProgram(output); + } + + /** + * Mini Python source code :
+ * a = 133
+ *
+ * def printA(a):
+ * print(a)
+ *
+ * printA(a) + */ + static void generate_print_through_function(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); + + builder.addFunction( + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of())); + + builder.addStatement( + new Call(new Reference("printA"), List.of(new Expression[] {new Reference("a")}))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestPrint.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestPrint.java new file mode 100644 index 0000000..9fbcc5a --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestPrint.java @@ -0,0 +1,69 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestPrint { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of(new IntLiteral(133), "133\n"), + Arguments.of(new BoolLiteral(true), "True\n"), + Arguments.of(new StringLiteral("toPrint"), "toPrint\n"), + Arguments.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(1)})), + "2\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void print(Expression toPrint, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_print(workDirectory, toPrint); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "toPrint : " + toPrint + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * print(133, True, False, "toPrint", 1+1) + */ + void generate_print(Path output, Expression toPrint) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement(new Call(new Reference("print"), List.of(new Expression[] {toPrint}))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestType.java b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestType.java new file mode 100644 index 0000000..bfc9732 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/BuiltInFunctions/TestType.java @@ -0,0 +1,87 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.BuiltInFunctions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.Literal; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +@Disabled +public class TestType { + // TODO print(type(a)) seems not to be working see TODO(./src/function-args.c:36) + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of(new IntLiteral(133), "61\n"), + Arguments.of(new BoolLiteral(true), "61\n"), + Arguments.of(new StringLiteral("toPrint"), "61\n"), + Arguments.of( + new Call( + new AttributeReference("__add__", new IntLiteral(1)), + List.of(new Expression[] {new IntLiteral(1)})), + "102\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void type(Literal value, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_type(workDirectory, value); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "toType : " + value + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * a = (133, True, "toPrint", 1+1)
+ * print(type(a)) + */ + void generate_type(Path output, Expression value) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), value)); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("a")})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInheritance.java b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInheritance.java new file mode 100644 index 0000000..c66b854 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInheritance.java @@ -0,0 +1,129 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Classes; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import CBuilder.objects.MPyClass; +import CBuilder.objects.SuperCall; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestClassInheritance { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + new MPyClass( + "ClassA", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassA] Print" + + " from" + + " __init__") + }))), + List.of(new Argument("self", 0)), + List.of())), + Map.of()), + new MPyClass( + "ClassB", + new Reference("ClassA"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassB] Print" + + " from" + + " __init__") + }))), + List.of(new Argument("self", 0)), + List.of())), + Map.of()), + """ + [ClassA] Print from __init__ + [ClassB] Print from __init__ + """)); + } + + @ParameterizedTest + @MethodSource("sources_equals") + void inheritance(MPyClass A, MPyClass B, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_inheritance(workDirectory, A, B); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * class ClassA(__MPyType_Object):
+ *
+ * def __init__(self, val):
+ * super()
+ * print("[ClassA] Print from __init__\n")
+ *
+ * class ClassB(A):
+ *
+ * def __init__(self, val):
+ * super()
+ * print("[ClassB] Print from __init__\n")
+ *
+ * x = ClassB(123) + */ + void generate_inheritance(Path output, MPyClass parent, MPyClass child) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addClass(parent); + builder.addClass(child); + + builder.addVariable(new VariableDeclaration("x")); + + builder.addStatement( + new Assignment(new Reference("x"), new Call(new Reference("ClassB"), List.of()))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInit.java b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInit.java new file mode 100644 index 0000000..332938a --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassInit.java @@ -0,0 +1,169 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Classes; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import CBuilder.objects.MPyClass; +import CBuilder.objects.SuperCall; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestClassInit { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + "ClassA", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassA] Print from" + + " __init__") + }))), + List.of(new Argument("self", 0)), + List.of())), + Map.of(), + List.of(), + "[ClassA] Print from __init__\n"), + Arguments.of( + "ClassB", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassB] Print from" + + " __init__"), + new Reference("x") + }))), + List.of(new Argument("self", 0), new Argument("x", 1)), + List.of())), + Map.of(), + List.of(new StringLiteral("test")), + "[ClassB] Print from __init__ test\n"), + Arguments.of( + "ClassB", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassB] Print from" + + " __init__ Param1" + + " :"), + new Reference("x"), + new StringLiteral(" Param2 : "), + new Reference("y") + }))), + List.of( + new Argument("self", 0), + new Argument("x", 1), + new Argument("y", 2)), + List.of())), + Map.of(), + List.of(new StringLiteral("test"), new IntLiteral(123)), + "[ClassB] Print from __init__ Param1 : test Param2 : 123\n")); + } + + @ParameterizedTest + @MethodSource("sources_equals") + void class_definition( + String className, + Reference parent, + List functions, + Map classAttributes, + List initArgs, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_class(workDirectory, className, parent, functions, classAttributes, initArgs); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * class ClassA(__MPyType_Object):
+ *
+ * def __init__(self):
+ * print("[ClassA] Print from __init__\n")
+ *
+ * class ClassB(__MPyType_Object):
+ * def __init__(self, x):
+ * print("[ClassB] Print from __init__" + str(param1))
+ *
+ * class ClassB(__MPyTypeObject):
+ * def __init__(self, x, y):
+ * print("[ClassB] Print from __init__ Param1 : " + str(x) + " Param2 : " + str(y))
+ *
+ * x = className(( , param1, (param1,param2))) + */ + void generate_class( + Path output, + String className, + Reference parent, + List functions, + Map classAttributes, + List initArgs) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addClass(new MPyClass(className, parent, functions, classAttributes)); + + builder.addVariable(new VariableDeclaration("x")); + + builder.addStatement( + new Assignment(new Reference("x"), new Call(new Reference(className), initArgs))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassMembers.java b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassMembers.java new file mode 100644 index 0000000..7855c9b --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Classes/TestClassMembers.java @@ -0,0 +1,209 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Classes; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.*; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.objects.functions.ReturnStatement; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TestClassMembers { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + @Test + void getter_setter(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + + generate_getter_and_setter( + workDirectory, + new MPyClass( + "ClassB", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassB] Print from" + + " __init__") + })), + new AttributeAssignment( + new AttributeReference( + "x", new Reference("self")), + new Reference("val"))), + List.of(new Argument("self", 0), new Argument("val", 1)), + List.of()), + new Function( + "getX", + List.of( + new ReturnStatement( + new AttributeReference( + "x", new Reference("self")))), + List.of(new Argument("self", 0)), + List.of()), + new Function( + "setX", + List.of( + new AttributeAssignment( + new AttributeReference( + "x", new Reference("self")), + new Reference("newVal"))), + List.of(new Argument("self", 0), new Argument("newVal", 1)), + List.of())), + Map.of()), + List.of(new IntLiteral(133))); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals("[ClassB] Print from __init__\n23\n", result); + } + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * class A(__MPyType_Object):
+ *
+ * def __init__(self, val):
+ * super()
+ * print("[ClassB] Print from __init__\n")
+ * self.x = val
+ *
+ * def getX(self):
+ * return self.x
+ *
+ * def setX(self, newVal):
+ * self.x = newVal
+ *
+ *
+ * x = A(123)
+ * x.setX(23)
+ * print(x.getX()) + */ + void generate_getter_and_setter(Path output, MPyClass mpyClass, List initArgs) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addClass(mpyClass); + + builder.addVariable(new VariableDeclaration("x")); + + builder.addStatement( + new Assignment( + new Reference("x"), new Call(new Reference(mpyClass.getName()), initArgs))); + + builder.addStatement( + new Call( + new AttributeReference("setX", new Reference("x")), + List.of(new IntLiteral(23)))); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference("getX", new Reference("x")), + List.of()) + }))); + + builder.writeProgram(output); + } + + @Test + void getter(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + + generate_getter( + workDirectory, + new MPyClass( + "ClassA", + new Reference("__MPyType_Object"), + List.of( + new Function( + "__init__", + List.of( + new SuperCall(List.of()), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "[ClassA] Print from" + + " __init__") + })), + new AttributeAssignment( + new AttributeReference( + "x", new Reference("self")), + new Reference("val"))), + List.of(new Argument("self", 0), new Argument("val", 1)), + List.of()), + new Function( + "getX", + List.of( + new ReturnStatement( + new AttributeReference( + "x", new Reference("self")))), + List.of(new Argument("self", 0)), + List.of())), + Map.of()), + List.of(new IntLiteral(133))); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals("[ClassA] Print from __init__\n133\n", result); + } + + void generate_getter(Path output, MPyClass mpyClass, List initArgs) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addClass(mpyClass); + + builder.addVariable(new VariableDeclaration("x")); + + builder.addStatement( + new Assignment( + new Reference("x"), new Call(new Reference(mpyClass.getName()), initArgs))); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference("getX", new Reference("x")), + List.of()) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/ComparisonOps/TestComparisonOps.java b/src/test/java/Systemtests/LanguageFeatures/ComparisonOps/TestComparisonOps.java new file mode 100644 index 0000000..9187857 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/ComparisonOps/TestComparisonOps.java @@ -0,0 +1,82 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.ComparisonOps; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.Literal; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestComparisonOps { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of("__eq__", new IntLiteral(1), new IntLiteral(1), "True\n"), + Arguments.of("__eq__", new IntLiteral(1), new IntLiteral(0), "False\n"), + Arguments.of("__ne__", new IntLiteral(1), new IntLiteral(1), "False\n"), + Arguments.of("__ne__", new IntLiteral(1), new IntLiteral(0), "True\n"), + Arguments.of("__ge__", new IntLiteral(1), new IntLiteral(2), "False\n"), + Arguments.of("__ge__", new IntLiteral(2), new IntLiteral(1), "True\n"), + Arguments.of("__gt__", new IntLiteral(1), new IntLiteral(2), "False\n"), + Arguments.of("__gt__", new IntLiteral(2), new IntLiteral(1), "True\n"), + Arguments.of("__le__", new IntLiteral(1), new IntLiteral(2), "True\n"), + Arguments.of("__le__", new IntLiteral(2), new IntLiteral(1), "False\n"), + Arguments.of("__lt__", new IntLiteral(1), new IntLiteral(2), "True\n"), + Arguments.of("__lt__", new IntLiteral(2), new IntLiteral(1), "False\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void comparison_operation( + String operation, Literal a, Literal b, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_comparison_operation(workDirectory, operation, a, b); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Operation : " + operation + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * a = 133
+ * print(a (==, !=, <, <=, >, >=) b) + */ + void generate_comparison_operation(Path output, String operation, Literal a, Literal b) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new AttributeReference(operation, a), + List.of(new Expression[] {b})) + }))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElifStatement.java b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElifStatement.java new file mode 100644 index 0000000..26e424d --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElifStatement.java @@ -0,0 +1,130 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Conditions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.conditions.IfThenElseStatement; +import CBuilder.conditions.conditionalStatement.ElifStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestElifStatement { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new IfStatement( + new BoolLiteral(false), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into if Statement") + })))), + Optional.of( + List.of( + new ElifStatement( + new BoolLiteral(true), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into Elif" + + " Statement") + })))))), + "Went into Elif Statement\n"), + Arguments.of( + new IfStatement( + new BoolLiteral(true), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into if Statement") + })))), + Optional.of( + List.of( + new ElifStatement( + new BoolLiteral(false), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into Elif" + + " Statement") + })))))), + "Went into if Statement\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void elif_statement( + IfStatement ifStatement, + Optional> elifStatement, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_elif_statement(workDirectory, ifStatement, elifStatement); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println( + testClass + + "If Statement : " + + ifStatement + + " Elif Statement : " + + elifStatement + + " Result : " + + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ *
+ * if(false/true):
+ * print("Went inside if!")
+ *
+ * elif(true/false):
+ * print("Went inside elif!") + */ + void generate_elif_statement( + Path output, IfStatement ifStatement, Optional> elifStatement) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement(new IfThenElseStatement(ifStatement, elifStatement, Optional.empty())); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElseStatement.java b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElseStatement.java new file mode 100644 index 0000000..3553826 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestElseStatement.java @@ -0,0 +1,126 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Conditions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.conditions.IfThenElseStatement; +import CBuilder.conditions.conditionalStatement.ElseStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestElseStatement { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new IfStatement( + new BoolLiteral(false), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into if Statement") + })))), + Optional.of( + new ElseStatement( + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into else" + + " Statement") + }))))), + "Went into else Statement\n"), + Arguments.of( + new IfStatement( + new BoolLiteral(true), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into if Statement") + })))), + Optional.of( + new ElseStatement( + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral( + "Went into else" + + " Statement") + }))))), + "Went into if Statement\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void else_statement( + IfStatement ifStatement, + Optional elseStatement, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_else_statement(workDirectory, ifStatement, elseStatement); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println( + testClass + + "If Statement : " + + ifStatement + + " Elif Statement : " + + elseStatement + + " Result : " + + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ *
+ * if(false/true):
+ * print("Went inside if!")
+ *
+ * else:
+ * print("Went inside else!") + */ + void generate_else_statement( + Path output, IfStatement ifStatement, Optional elseStatement) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement(new IfThenElseStatement(ifStatement, Optional.empty(), elseStatement)); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Conditions/TestIfStatement.java b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestIfStatement.java new file mode 100644 index 0000000..70834d2 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestIfStatement.java @@ -0,0 +1,114 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Conditions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.conditions.IfThenElseStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestIfStatement { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new BoolLiteral(false), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral("Went into if Statement") + }))), + ""), + Arguments.of( + new BoolLiteral(true), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral("Went into if Statement") + }))), + "Went into if Statement\n"), + Arguments.of( + new IntLiteral(0), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral("Went into if Statement") + }))), + ""), + Arguments.of( + new IntLiteral(1), + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral("Went into if Statement") + }))), + "Went into if Statement\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void if_statement( + Expression condition, + List statementList, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_if_statement(workDirectory, condition, statementList); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Condition : " + condition + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ *
+ * if(false/true, 0, 1):
+ * print("Went into if Statement")
+ */ + void generate_if_statement(Path output, Expression condition, List statementList) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new IfThenElseStatement( + new IfStatement(condition, statementList), + Optional.empty(), + Optional.empty())); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/Conditions/TestWhileStatement.java b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestWhileStatement.java new file mode 100644 index 0000000..75e7c2c --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/Conditions/TestWhileStatement.java @@ -0,0 +1,91 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.Conditions; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.conditions.conditionalStatement.WhileStatement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestWhileStatement { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new Call( + new AttributeReference("__gt__", new Reference("a")), + List.of(new Expression[] {new IntLiteral(1)})), + List.of( + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})), + new Assignment( + new Reference("a"), + new Call( + new AttributeReference( + "__sub__", new Reference("a")), + List.of(new Expression[] {new IntLiteral(1)})))), + "3\n2\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void while_statement( + Expression condition, + List statementList, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_while_statement(workDirectory, condition, statementList); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Condition : " + condition + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ *
+ * a = 3
+ *
+ * while(a > 1):
+ * print(a)
+ * a = a - 1 + */ + void generate_while_statement( + Path output, Expression condition, List statementList) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(3))); + + builder.addStatement(new WhileStatement(condition, statementList)); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/DeclarationAndAssignment/TestDeclarationAndAssignment.java b/src/test/java/Systemtests/LanguageFeatures/DeclarationAndAssignment/TestDeclarationAndAssignment.java new file mode 100644 index 0000000..890c7ba --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/DeclarationAndAssignment/TestDeclarationAndAssignment.java @@ -0,0 +1,283 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.DeclarationAndAssignment; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.Literal; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestDeclarationAndAssignment { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream source_literals() { + return Stream.of( + Arguments.of(new IntLiteral(133), "133\n"), + Arguments.of(new StringLiteral("stringVal"), "stringVal\n"), + Arguments.of(new BoolLiteral(true), "True\n")); + } + + @Test + void declaration_and_assignment(@TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + String expected = + """ + 133 + 133 + 133 + 133 + """; + + generate_declaration_and_assignment(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + @ParameterizedTest + @MethodSource("source_literals") + void assignment_literal(Literal literal, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_assignment_literal(workDirectory, literal); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + @Test + void assignment_reference(@TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + String expected = "133\n"; + + generate_assignment_reference(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + @Test + void assignment_function_call(@TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + String expected = "133\n"; + + generate_assignment_function_call(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + @Test + void assignment_function(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + String expected = "133\n"; + + generate_assignment_function_call(workDirectory); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + + // After Test + } + + /** + * Mini-Python code of program generated : a a = 133 + * + *

b b = a + * + *

c = print(a) + * + *

+ * + *

d def printA(a): print(a) d = printA + * + *

print(a) print(b) c d(a) + */ + static void generate_declaration_and_assignment(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); // a = 133 + + builder.addVariable(new VariableDeclaration("b")); // b + builder.addStatement(new Assignment(new Reference("b"), new Reference("a"))); // b = a + + builder.addVariable(new VariableDeclaration("c")); + builder.addStatement( + new Assignment( + new Reference("c"), + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})))); // c = print(a) + + builder.addVariable(new VariableDeclaration("d")); + builder.addFunction( + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of())); // d def printA(a): printa(a) + + builder.addStatement( + new Assignment(new Reference("d"), new Reference("printA"))); // d = printA + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("a")}))); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("b")}))); + + builder.addStatement( + new Call( + new Reference("d"), + List.of(new Expression[] {new Reference("a")}))); // d(a) + + builder.writeProgram(output); + } + + /** + * Mini Python source code :
+ * a = 133
+ * print(a) + * + * @param output writeProgram to here + */ + static void generate_assignment_literal(Path output, Literal literal) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), literal)); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("a")}))); + + builder.writeProgram(output); + } + + /** + * Mini python source code :
+ * a = 133
+ * b = a
+ * print(b) + * + * @param output writeProgram to here + */ + static void generate_assignment_reference(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); + + builder.addVariable(new VariableDeclaration("b")); + builder.addStatement(new Assignment(new Reference("b"), new Reference("a"))); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {new Reference("b")}))); + + builder.writeProgram(output); + } + + /** + * Mini python source code :
+ * a = 133
+ * b
+ * def printA(a):
+ * print(a)
+ * b = printA
+ * b(a) + * + * @param output writeProgram to here + */ + static void generate_assignment_function_call(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); + builder.addStatement(new Assignment(new Reference("a"), new IntLiteral(133))); + + builder.addVariable(new VariableDeclaration("b")); + builder.addFunction( + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of())); + + builder.addStatement(new Assignment(new Reference("b"), new Reference("printA"))); + builder.addStatement( + new Call(new Reference("b"), List.of(new Expression[] {new Reference("a")}))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionBody.java b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionBody.java new file mode 100644 index 0000000..11a3c46 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionBody.java @@ -0,0 +1,189 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.FunctionCalls; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.conditions.IfThenElseStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.objects.functions.ReturnStatement; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestFunctionBody { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * def function1():
+ * return ""
+ *
+ * def function1():
+ * print("Print from function body")
+ *
+ * def function1():
+ * y = 133
+ * return y
+ *
+ * def function1(x):
+ * return x
+ *
+ * def function1(x, y):
+ * return x + y
+ *
+ * def function1(x, y):
+ * if(x >= 0):
+ * return x + y
+ *
+ * print(function1()) + */ + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + "function1", + List.of(new ReturnStatement(new StringLiteral(""))), + List.of(), + List.of(), + List.of(), + "\n"), + Arguments.of( + "function1", + List.of( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new StringLiteral("Print from function body") + })), + new ReturnStatement(new StringLiteral(""))), + List.of(), + List.of(), + List.of(), + "Print from function body\n\n"), + Arguments.of( + "function1", + List.of( + new Assignment(new Reference("y"), new IntLiteral(133)), + new ReturnStatement(new Reference("y"))), + List.of(), + List.of(new VariableDeclaration("y")), + List.of(), + "133\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("x"))), + List.of(new Argument("x", 0)), + List.of(), + List.of(new IntLiteral(133)), + "133\n"), + Arguments.of( + "function1", + List.of( + new ReturnStatement( + new Call( + new AttributeReference( + "__add__", new Reference("x")), + List.of(new Expression[] {new Reference("y")})))), + List.of(new Argument("x", 0), new Argument("y", 1)), + List.of(), + List.of(new IntLiteral(1), new IntLiteral(3)), + "4\n"), + Arguments.of( + "function1", + List.of( + new IfThenElseStatement( + new IfStatement( + new Call( + new AttributeReference( + "__ge__", new Reference("x")), + List.of( + new Expression[] { + new IntLiteral(0) + })), + List.of( + new ReturnStatement( + new Call( + new AttributeReference( + "__add__", + new Reference("x")), + List.of( + new Expression[] { + new Reference( + "y") + }))))), + Optional.empty(), + Optional.empty())), + List.of(new Argument("x", 0), new Argument("y", 1)), + List.of(), + List.of(new IntLiteral(1), new IntLiteral(3)), + "4\n")); + } + + @ParameterizedTest + @MethodSource("sources_equals") + void function( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + void generate_function( + Path output, + String funcName, + List body, + List positionalArgs, + List localVariables, + List args) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + builder.addFunction(new Function(funcName, body, positionalArgs, localVariables)); + + builder.addStatement( + new Call( + new Reference("print"), + List.of(new Expression[] {new Call(new Reference("function1"), args)}))); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionParams.java b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionParams.java new file mode 100644 index 0000000..df232c5 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionParams.java @@ -0,0 +1,214 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.FunctionCalls; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.objects.functions.ReturnStatement; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestFunctionParams { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + @ParameterizedTest + @MethodSource("sources_equals") + void function_equals( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + @ParameterizedTest + @MethodSource("sources_fails") + void function_throws( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result = ""; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + // result = getProgramOutput(workDirectory); + assertThrows(RuntimeException.class, () -> getProgramOutput(workDirectory)); + + System.out.println(testClass + " Result from throws : " + result); + // Thread.sleep(5000); + + } + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * def function1():
+ * return ""
+ *
+ * def function1(param1):
+ * return param1
+ *
+ * def function1(param1, param2):
+ * return param1 + param2
+ * Calls :
+ *
+ * print(function1())
+ * print(function1(123))
+ * print(function1(123,123))
+ *
+ * Functions will Throw runtime error if more or less parameters are passed.
+ * Also if a print called is passed as parameter and something is done that cant be done with a + * function pointer. + */ + void generate_function( + Path output, + String funcName, + List body, + List positionalArgs, + List localVariables, + List args) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + builder.addFunction(new Function(funcName, body, positionalArgs, localVariables)); + + builder.addStatement( + new Call( + new Reference("print"), + List.of(new Expression[] {new Call(new Reference("function1"), args)}))); + + builder.writeProgram(output); + } + + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + "function1", + List.of(new ReturnStatement(new StringLiteral(""))), + List.of(), + List.of(), + List.of(), + "\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new IntLiteral(0)), + "0\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new StringLiteral("String")), + "String\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new BoolLiteral(true)), + "True\n"), + Arguments.of( + "function1", + List.of( + new ReturnStatement( + new Call( + new AttributeReference( + "__add__", new Reference("param1")), + List.of( + new Expression[] { + new Reference("param2") + })))), + List.of(new Argument("param1", 0), new Argument("param2", 1)), + List.of(), + List.of(new IntLiteral(1), new IntLiteral(2)), + "3\n")); + } + + private static Stream sources_fails() { + return Stream.of( + Arguments.of( + "function1", + List.of(new ReturnStatement(new StringLiteral(""))), + List.of(), + List.of(), + List.of(new IntLiteral(133))), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of()), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new StringLiteral("String1"), new StringLiteral("String2"))), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new Call(new Reference("print"), List.of(new Expression[] {})))), + Arguments.of( + "function1", + List.of( + new ReturnStatement( + new Call( + new AttributeReference( + "__add__", new Reference("param1")), + List.of( + new Expression[] { + new Reference("param2") + })))), + List.of(new Argument("param1", 0), new Argument("param2", 1)), + List.of(), + List.of(new IntLiteral(1)))); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionRecursion.java b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionRecursion.java new file mode 100644 index 0000000..434dc52 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionRecursion.java @@ -0,0 +1,138 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.FunctionCalls; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.conditions.IfThenElseStatement; +import CBuilder.conditions.conditionalStatement.IfStatement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.AttributeReference; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.objects.functions.ReturnStatement; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestFunctionRecursion { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + "function1", + List.of( + new IfThenElseStatement( + new IfStatement( + new Call( + new AttributeReference( + "__gt__", new Reference("x")), + List.of( + new Expression[] { + new IntLiteral(0) + })), + List.of( + new Assignment( + new Reference("x"), + new Call( + new AttributeReference( + "__sub__", + new Reference("x")), + List.of( + new Expression[] { + new IntLiteral( + 1) + }))), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("x") + })), + new ReturnStatement( + new Call( + new Reference("function1"), + List.of( + new Expression[] { + new Reference( + "x") + }))))), + Optional.empty(), + Optional.empty()), + new ReturnStatement(new Reference("x"))), + List.of(new Argument("x", 0)), + List.of(), + List.of(new IntLiteral(5)), + "4\n3\n2\n1\n0\n")); + } + + @ParameterizedTest + @MethodSource("sources_equals") + void function( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ *
+ * def function1(x):
+ * if(x > 0):
+ * x = x - 1
+ * print(x)
+ * return function1(x)
+ *
+ * function1(5) + */ + void generate_function( + Path output, + String funcName, + List body, + List positionalArgs, + List localVariables, + List args) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + builder.addFunction(new Function(funcName, body, positionalArgs, localVariables)); + + builder.addStatement(new Call(new Reference("function1"), args)); + + builder.writeProgram(output); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionReturn.java b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionReturn.java new file mode 100644 index 0000000..1b41061 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/FunctionCalls/TestFunctionReturn.java @@ -0,0 +1,190 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.FunctionCalls; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.keywords.bool.AndKeyword; +import CBuilder.literals.BoolLiteral; +import CBuilder.literals.IntLiteral; +import CBuilder.literals.StringLiteral; +import CBuilder.literals.TupleLiteral; +import CBuilder.objects.Call; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.objects.functions.ReturnStatement; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestFunctionReturn { + // TODO cannot return tuple literal from function without runtime exception + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + @ParameterizedTest + @MethodSource("sources_equals") + void function_equals( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + String expected, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + @ParameterizedTest + @MethodSource("sources_fails") + void function_fails( + String funcName, + List body, + List positionalArgs, + List localVariables, + List args, + @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result = ""; + + generate_function(workDirectory, funcName, body, positionalArgs, localVariables, args); + + makeProgram(workDirectory); + + assertThrows(RuntimeException.class, () -> getProgramOutput(workDirectory)); + + System.out.println(testClass + " Result : " + result); + } + + void generate_function( + Path output, + String funcName, + List body, + List positionalArgs, + List localVariables, + List args) { + ProgramBuilder builder = new ProgramBuilder(); + + // builder.addVariable(new VariableDeclaration("a")); + // builder.addStatement(new Assignment(new Reference("a"), value)); + + builder.addFunction(new Function(funcName, body, positionalArgs, localVariables)); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new Reference("function1"), + args) // This call works, printing the value crashes! + }))); + + builder.writeProgram(output); + } + + /** + * Mini Python source code for tests from top to bottom :
+ *
+ * def function1():
+ * return ""
+ *
+ * def function1():
+ * return 123
+ *
+ * def function1():
+ * return False
+ *
+ * def function1(param1):
+ * return param1
+ *
+ * def function1():
+ * return (True and True)
+ *
+ * print(function1()) + */ + private static Stream sources_equals() { + return Stream.of( + Arguments.of( + "function1", + List.of(new ReturnStatement(new StringLiteral(""))), + List.of(), + List.of(), + List.of(), + "\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new IntLiteral(123))), + List.of(), + List.of(), + List.of(), + "123\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new BoolLiteral(false))), + List.of(), + List.of(), + List.of(), + "False\n"), + Arguments.of( + "function1", + List.of(new ReturnStatement(new Reference("param1"))), + List.of(new Argument("param1", 0)), + List.of(), + List.of(new BoolLiteral(true)), + "True\n"), + Arguments.of( + "function1", + List.of( + new ReturnStatement( + new AndKeyword( + new BoolLiteral(true), new BoolLiteral(true)))), + List.of(), + List.of(), + List.of(), + "True\n")); + } + + private static Stream sources_fails() { + return Stream.of( + Arguments.of( + "function1", + List.of(new ReturnStatement(new TupleLiteral(List.of()))), + List.of(), + List.of(), + List.of()), + Arguments.of( + "function1", + List.of( + new ReturnStatement( + new Call( + new Reference("print"), + List.of(new Expression[] {})))), + List.of(), + List.of(), + List.of())); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/LogicalOps/TestLogicalOps.java b/src/test/java/Systemtests/LanguageFeatures/LogicalOps/TestLogicalOps.java new file mode 100644 index 0000000..21ea504 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/LogicalOps/TestLogicalOps.java @@ -0,0 +1,83 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures.LogicalOps; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.keywords.bool.AndKeyword; +import CBuilder.keywords.bool.NotKeyword; +import CBuilder.keywords.bool.OrKeyword; +import CBuilder.literals.BoolLiteral; +import CBuilder.objects.Call; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class TestLogicalOps { + String testClass = '[' + this.getClass().getSimpleName().toUpperCase() + "]\n"; + + private static Stream sources() { + return Stream.of( + Arguments.of( + new AndKeyword(new BoolLiteral(true), new BoolLiteral(true)), "True\n"), + Arguments.of( + new AndKeyword(new BoolLiteral(true), new BoolLiteral(false)), "False\n"), + Arguments.of( + new AndKeyword(new BoolLiteral(false), new BoolLiteral(false)), "False\n"), + Arguments.of( + new AndKeyword(new BoolLiteral(false), new BoolLiteral(true)), "False\n"), + Arguments.of(new OrKeyword(new BoolLiteral(true), new BoolLiteral(true)), "True\n"), + Arguments.of( + new OrKeyword(new BoolLiteral(true), new BoolLiteral(false)), "True\n"), + Arguments.of( + new OrKeyword(new BoolLiteral(false), new BoolLiteral(false)), "False\n"), + Arguments.of( + new OrKeyword(new BoolLiteral(false), new BoolLiteral(true)), "True\n"), + Arguments.of(new NotKeyword(new BoolLiteral(true)), "False\n"), + Arguments.of(new NotKeyword(new BoolLiteral(false)), "True\n")); + } + + @ParameterizedTest + @MethodSource("sources") + void logical_operation(Expression operation, String expected, @TempDir Path workDirectory) + throws IOException, InterruptedException { + String result; + + generate_logical_operation(workDirectory, operation); + + makeProgram(workDirectory); + + result = getProgramOutput(workDirectory); + + System.out.println(testClass + "Operation : " + operation + " Result : " + result); + + // Thread.sleep(5000); + assertEquals(expected, result); + } + + /** + * Mini Python source code :
+ * print(a &&, ||, not b) + * + * @param output writeProgram to here + * @param operation logical operation to test for + */ + void generate_logical_operation(Path output, Expression operation) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addStatement( + new Call(new Reference("print"), List.of(new Expression[] {operation}))); + + builder.writeProgram(output); + System.out.println(builder.buildProgram()); + } +} diff --git a/src/test/java/Systemtests/LanguageFeatures/TestEmpty.java b/src/test/java/Systemtests/LanguageFeatures/TestEmpty.java new file mode 100644 index 0000000..41bce67 --- /dev/null +++ b/src/test/java/Systemtests/LanguageFeatures/TestEmpty.java @@ -0,0 +1,13 @@ +/* (C)2024 */ +package Systemtests.LanguageFeatures; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TestEmpty { + @Test + void testempty() { + assertEquals(1, 1); + } +} diff --git a/src/test/java/Systemtests/TestHelpers.java b/src/test/java/Systemtests/TestHelpers.java new file mode 100644 index 0000000..275cc28 --- /dev/null +++ b/src/test/java/Systemtests/TestHelpers.java @@ -0,0 +1,81 @@ +/* (C)2024 */ +package Systemtests; + +import java.io.*; +import java.nio.file.Path; + +public class TestHelpers { + + public static String getProgramOutput(Path workDirectory) + throws IOException, InterruptedException { + String line; + StringBuilder result = new StringBuilder(); + ProcessBuilder runner = new ProcessBuilder("./bin/program"); + runner.directory(workDirectory.toFile()); + // runner.inheritIO(); //You can not redirect the stdout/stderr because the Reader will not + + Process rp = runner.start(); + + InputStream processStdOutput = rp.getInputStream(); + Reader r = new InputStreamReader(processStdOutput); + BufferedReader br = new BufferedReader(r); + + while ((line = br.readLine()) != null) { + result.append(line).append("\n"); + } + + // String result = new String(rp.getInputStream().readAllBytes()); + + if (rp.waitFor() != 0) { + throw new RuntimeException("Program returned with value != 0"); + } + return result.toString(); + } + + public static String getProgramOutputBasedOnInput( + Path workDirectory, String result, String input) + throws IOException, InterruptedException { + String line; + ProcessBuilder runner = new ProcessBuilder("./bin/program"); + runner.directory(workDirectory.toFile()); + // runner.inheritIO(); //You can not redirect the stdout/stderr because the Reader will not + // be able to get the output. :) + + Process rp = runner.start(); + + OutputStream os = rp.getOutputStream(); + PrintWriter pw = new PrintWriter(os); + pw.write(input); + pw.flush(); + + InputStream processStdOutput = rp.getInputStream(); + Reader r = new InputStreamReader(processStdOutput); + BufferedReader br = new BufferedReader(r); + + StringBuilder resultBuilder = new StringBuilder(result); + while ((line = br.readLine()) != null) { + resultBuilder.append(line).append("\n"); + } + result = resultBuilder.toString(); + + // String result = new String(rp.getInputStream().readAllBytes()); + + if (rp.waitFor() != 0) { + throw new RuntimeException("Program returned with value != 0"); + } + return result; + } + + public static void makeProgram(Path workDirectory) throws IOException, InterruptedException { + ProcessBuilder builder = new ProcessBuilder("make"); + builder.directory(workDirectory.toFile()); + // builder.inheritIO(); + + Process p = builder.start(); + + if (p.waitFor() != 0) { + System.out.println("Could not make program !"); + throw new RuntimeException("Could not make program !"); + } + } +} diff --git a/src/test/java/Systemtests/TestLanguageFeaturesCombined.java b/src/test/java/Systemtests/TestLanguageFeaturesCombined.java new file mode 100644 index 0000000..715a8d1 --- /dev/null +++ b/src/test/java/Systemtests/TestLanguageFeaturesCombined.java @@ -0,0 +1,290 @@ +/* (C)2024 */ +package Systemtests; + +import static Systemtests.TestHelpers.getProgramOutput; +import static Systemtests.TestHelpers.makeProgram; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import CBuilder.Expression; +import CBuilder.ProgramBuilder; +import CBuilder.Reference; +import CBuilder.Statement; +import CBuilder.literals.IntLiteral; +import CBuilder.objects.*; +import CBuilder.objects.functions.Argument; +import CBuilder.objects.functions.Function; +import CBuilder.variables.Assignment; +import CBuilder.variables.VariableDeclaration; +import java.io.IOException; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TestLanguageFeaturesCombined { + @Test + void program(@TempDir Path workDirectory) throws IOException, InterruptedException { + String result; + String expected = + """ + 62 + 62 + -30 + -30 + -50 + 62 + a = b d = -30 + * + *

type(a) type(d) + * + *

idA = id(a) print(idA) + * + *

print(id(a)) id(print(d)) id(type(a)) + * + *

e = id(type(print(d))) e = print(-50) + * + *

type(type) + * + *

def printA(a): print(a) printA(idA) + * + *

class B: def print(self): print(self) + * + *

def __init__(self): self.b = 100 + * + *

class C(B): def __init__(self): + * + *

bObj = B() bObj.print() + * + *

print(bObj.b) + * + *

cObj = C() cOBJ.print() + * + *

print(cObj.b) ``` + */ + static void generateProgram(Path output) { + ProgramBuilder builder = new ProgramBuilder(); + + builder.addVariable(new VariableDeclaration("a")); // a + builder.addVariable(new VariableDeclaration("b")); // b + builder.addStatement(new Assignment(new Reference("a"), new Reference("b"))); // a = b + + // does cause build errors with -Wall + // builder.addVariable(new VariableDeclaration("c")); + // builder.addStatement(new Assignment(new VariableReference("c"), new + // VariableReference("c"))); + + builder.addVariable(new VariableDeclaration("d")); + builder.addStatement(new Assignment(new Reference("d"), new IntLiteral(-30))); // d = -30 + + builder.addStatement( + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("a")}))); // type(a) + builder.addStatement( + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("d")}))); // type(d) + + builder.addVariable(new VariableDeclaration("idA")); + builder.addStatement( + new Assignment( + new Reference("idA"), + new Call( + new Reference("id"), + List.of(new Expression[] {new Reference("a")})))); // idA = id(a) + builder.addStatement( + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("idA")}))); // print(idA) + + // call each built-in function in a nested context to make sure it correctly + // implements refCounts + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Call( + new Reference("id"), + List.of(new Expression[] {new Reference("a")})) + }))); // print(id(a)) + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("d")})) + }))); // id(print(d)) + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("a")})) + }))); // id(type(a)) + + // assign a nested function call to a variable + builder.addVariable(new VariableDeclaration("e")); // e + builder.addStatement( + new Call( + new Reference("id"), + List.of( + new Expression[] { + new Call( + new Reference("type"), + List.of( + new Expression[] { + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("d") + })) + })) + }))); // e = id(type(print(d))) + + // call a function on a literal and assign that to a variable + builder.addStatement( + new Assignment( + new Reference("e"), + new Call( + new Reference("print"), + List.of( + new Expression[] { + new IntLiteral(-50) + })))); // e = print(-50) + + builder.addStatement( + new Call( + new Reference("type"), + List.of(new Expression[] {new Reference("type")}))); // type(type) + + builder.addFunction( + new Function( + "printA", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of(new Expression[] {new Reference("a")})) + }), + List.of(new Argument[] {new Argument("a", 0)}), + List.of())); // def printA(a): + // print(a) + + builder.addStatement( + new Call( + new Reference("printA"), + List.of(new Expression[] {new Reference("idA")}))); // printA(idA) + + builder.addClass( + new MPyClass( + "ClassB", + new Reference("__MPyType_Object"), + List.of( + new Function[] { + new Function( + "print", + List.of( + new Statement[] { + new Call( + new Reference("print"), + List.of( + new Expression[] { + new Reference("self") + })) + }), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()), + new Function( + "__init__", + List.of( + new Statement[] { + new SuperCall(List.of()), + new AttributeAssignment( + new AttributeReference( + "b", new Reference("self")), + new IntLiteral(100)) + }), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>())); + + builder.addClass( + new MPyClass( + "C", + new Reference("ClassB"), + List.of( + new Function[] { + new Function( + "__init__", + List.of(new Statement[] {new SuperCall(List.of())}), + List.of(new Argument[] {new Argument("self", 0)}), + List.of()) + }), + new HashMap<>())); + + builder.addVariable(new VariableDeclaration("bObj")); + builder.addStatement( + new Assignment( + new Reference("bObj"), new Call(new Reference("ClassB"), List.of()))); + + builder.addStatement( + new Call(new AttributeReference("print", new Reference("bObj")), List.of())); + + // print(bObj.b) + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new AttributeReference("b", new Reference("bObj")) + }))); + + builder.addVariable(new VariableDeclaration("cObj")); + builder.addStatement( + new Assignment(new Reference("cObj"), new Call(new Reference("C"), List.of()))); + + builder.addStatement( + new Call(new AttributeReference("print", new Reference("cObj")), List.of())); + + builder.addStatement( + new Call( + new Reference("print"), + List.of( + new Expression[] { + new AttributeReference("b", new Reference("cObj")) + }))); + + builder.writeProgram(output); + } +}