Skip to content

Commit

Permalink
Merge pull request lcompilers#1393 from Smit-create/test_import_c
Browse files Browse the repository at this point in the history
C: Enable import tests
  • Loading branch information
certik committed Jan 4, 2023
2 parents a18aa64 + 56a7e85 commit 9066fce
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 19 deletions.
25 changes: 17 additions & 8 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ macro(RUN)
set_tests_properties(${name} PROPERTIES WILL_FAIL TRUE)
endif()
elseif(KIND STREQUAL "c")
add_custom_command(
OUTPUT ${name}.c
COMMAND lpython --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
VERBATIM)
if (import_path)
add_custom_command(
OUTPUT ${name}.c
COMMAND lpython -I ${CMAKE_CURRENT_SOURCE_DIR}/${import_path} --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
VERBATIM)
else ()
add_custom_command(
OUTPUT ${name}.c
COMMAND lpython --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
VERBATIM)
endif()

add_executable(${name} ${name}.c ${RUN_EXTRAFILES})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(${name} lpython_rtlib)
Expand Down Expand Up @@ -270,10 +279,10 @@ RUN(NAME test_for_loop LABELS cpython llvm c)
RUN(NAME modules_01 LABELS cpython llvm c wasm wasm_x86)
RUN(NAME modules_02 LABELS cpython llvm wasm wasm_x86)
RUN(NAME test_import_01 LABELS cpython llvm c)
RUN(NAME test_import_02 LABELS cpython llvm)
RUN(NAME test_import_03 LABELS cpython llvm)
RUN(NAME test_import_02 LABELS cpython llvm c)
RUN(NAME test_import_03 LABELS cpython llvm c)
RUN(NAME test_import_04 IMPORT_PATH ..
LABELS cpython llvm)
LABELS cpython llvm c)
RUN(NAME test_math LABELS cpython llvm)
RUN(NAME test_numpy_01 LABELS cpython llvm c)
RUN(NAME test_numpy_02 LABELS cpython llvm c)
Expand Down
14 changes: 10 additions & 4 deletions integration_tests/test_import_02.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from test_import.import_module_01 import multiply, e
from test_import.import_module_02 import add, μ

print(e)
print(μ)
print(add(10, 20))
print(multiply(10, 20))

def f():
print(e)
print(μ)
print(add(10, 20))
print(multiply(10, 20))
assert add(10, 20) == 30
assert multiply(10, 20) == 200

f()
6 changes: 3 additions & 3 deletions integration_tests/test_import_04.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test_modules

def main0():
print(test_modules.sin(0.5))
print(test_modules.cos(0.5))
assert abs(test_modules.sin(0.5) + test_modules.cos(0.5) - 1.0) <= 1e-12
print(test_modules.sinx(0.5))
print(test_modules.cosx(0.5))
assert abs(test_modules.sinx(0.5) + test_modules.cosx(0.5) - 1.0) <= 1e-12

main0()
6 changes: 6 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ R"(#include <stdio.h>
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
std::string decl = self().convert_variable_decl(*v);
bool used_define_for_const = (ASR::is_a<ASR::Const_t>(*v->m_type) &&
v->m_intent == ASRUtils::intent_local);
if (used_define_for_const) {
contains += decl + "\n";
continue;
}
if (v->m_value) {
self().visit_expr(*v->m_value);
decl += " = " + src;
Expand Down
2 changes: 1 addition & 1 deletion test_modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .mathfn import sin, cos
from .mathfn import sinx, cosx
2 changes: 1 addition & 1 deletion test_modules/mathfn/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .sin import sin, cos
from .sin import sinx, cosx
4 changes: 2 additions & 2 deletions test_modules/mathfn/sin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ltypes import f64

def sin(x: f64) -> f64:
def sinx(x: f64) -> f64:
return x + 1.0

def cos(x: f64) -> f64:
def cosx(x: f64) -> f64:
return x - 1.0
13 changes: 13 additions & 0 deletions tests/reference/c-test_import_02-d2c54c4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "c-test_import_02-d2c54c4",
"cmd": "lpython --no-color --show-c {infile}",
"infile": "tests/../integration_tests/test_import_02.py",
"infile_hash": "cf2059987c7470ee617df897ed0d19906a9a8a94f05a5612570fce91",
"outfile": null,
"outfile_hash": null,
"stdout": "c-test_import_02-d2c54c4.stdout",
"stdout_hash": "f9b88f238f975b6430540713f230a55f96c2a161a1e4cc086beefb4a",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
76 changes: 76 additions & 0 deletions tests/reference/c-test_import_02-d2c54c4.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <inttypes.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

#define ASSERT(cond) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
exit(1); \
} \
}
#define ASSERT_MSG(cond, msg) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
printf("%s", "ERROR MESSAGE:\n"); \
printf("%s%s", msg, "\n"); \
exit(1); \
} \
}


struct dimension_descriptor
{
int32_t lower_bound, length;
};

// Implementations
#define e 2.71828182845904509e+00

int32_t multiply(int32_t x, int32_t y)
{
int32_t _lpython_return_variable;
_lpython_return_variable = x*y;
return _lpython_return_variable;
}

#define μ 1.45136923488338110e+00

int32_t add(int32_t x, int32_t y)
{
int32_t _lpython_return_variable;
_lpython_return_variable = x + y;
return _lpython_return_variable;
}

void f()
{
printf("%lf\n", e);
printf("%lf\n", μ);
printf("%d\n", add(10, 20));
printf("%d\n", multiply(10, 20));
ASSERT(add(10, 20) == 30);
ASSERT(multiply(10, 20) == 200);
}

void _lpython_main_program()
{
f();
}

int main(int argc, char* argv[])
{
_lpython_main_program();
return 0;
}
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ asr = true
filename = "../integration_tests/modules_02.py"
asr = true

[[test]]
filename = "../integration_tests/test_import_02.py"
c = true

[[test]]
filename = "../integration_tests/vec_01.py"
asr = true
Expand Down

0 comments on commit 9066fce

Please sign in to comment.