Skip to content

Commit

Permalink
Merge branch 'main' into init_decl
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Jun 20, 2022
2 parents a70f23b + d010d6b commit 98a0a2e
Show file tree
Hide file tree
Showing 44 changed files with 498 additions and 42 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ integration_tests/bindc_01
integration_tests/bindc_01.c
integration_tests/bindc_02
integration_tests/bindc_02.c
integration_tests/bindc_03
integration_tests/bindc_04
integration_tests/structs_01
integration_tests/structs_01.c
integration_tests/structs_02
integration_tests/structs_02.c
integration_tests/structs_03
integration_tests/structs_03.c
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ ctest
./run_tests.py
```

Also, run the integration tests:
Run integration tests:

```bash
cd integration_tests
./run_tests.sh
```

### Speed up Integration Test on Macs

Integration tests run slowly because Apple checks the hash of each
executable online before running. You can turn off that feature
in the Privacy tab of the Security and Privacy item of System
Preferences, Developer Tools, Terminal.app, "allow the apps below
to run software locally that does not meet the system's security
policy."

## Examples

You can run the following examples by hand in a terminal:
Expand All @@ -91,6 +100,7 @@ You can run the following examples by hand in a terminal:
./src/bin/lpython --show-asr examples/expr2.py
./src/bin/lpython --show-cpp examples/expr2.py
./src/bin/lpython --show-llvm examples/expr2.py
./src/bin/lpython --show-c examples/expr2.py
```

## Contributing
Expand Down
12 changes: 9 additions & 3 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ endmacro(RUN)
# Test zero and non-zero exit code and assert statements
RUN(NAME bindc_01 LABELS llvm c)
RUN(NAME bindc_02 LABELS llvm c)
RUN(NAME bindc_04 LABELS llvm)
RUN(NAME exit_01 LABELS cpython llvm c)
RUN(NAME exit_02 FAIL LABELS cpython llvm c)
RUN(NAME exit_01b LABELS cpython llvm c)
Expand Down Expand Up @@ -161,6 +162,8 @@ RUN(NAME test_c_interop_04 LABELS cpython llvm c
EXTRAFILES test_c_interop_04b.c)
RUN(NAME test_c_interop_05 LABELS llvm c
EXTRAFILES test_c_interop_05b.c)
RUN(NAME bindc_03 LABELS llvm
EXTRAFILES bindc_03b.c)
RUN(NAME test_generics_01 LABELS cpython llvm)
RUN(NAME test_cmath LABELS cpython llvm)
RUN(NAME test_complex LABELS cpython llvm)
Expand All @@ -170,9 +173,12 @@ RUN(NAME test_unary_minus LABELS cpython llvm)
RUN(NAME test_unary_plus LABELS cpython llvm)
RUN(NAME test_bool_binop LABELS cpython llvm)
RUN(NAME test_issue_518 LABELS cpython llvm)
RUN(NAME structs_01 LABELS cpython llvm)
RUN(NAME structs_02 LABELS llvm)
RUN(NAME structs_03 LABELS llvm)
RUN(NAME structs_01 LABELS cpython llvm c)
RUN(NAME structs_02 LABELS llvm c)
RUN(NAME structs_03 LABELS llvm c)
RUN(NAME test_str_to_int LABELS cpython llvm)
RUN(NAME test_platform LABELS cpython llvm)
RUN(NAME test_vars_01 LABELS cpython llvm)

# Just CPython
RUN(NAME test_builtin_bin LABELS cpython)
33 changes: 33 additions & 0 deletions integration_tests/bindc_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from ltypes import c_p_pointer, CPtr, pointer, i16, i32, Pointer, ccall, p_c_pointer

@ccall
def g(a: CPtr, value: i32) -> None:
pass

@ccall
def get_array(size: i32) -> CPtr:
pass

@ccallable
def f(q_void: CPtr) -> None:
i: i32
el: i32
q: Pointer[i32[:]]
c_p_pointer(q_void, q)
for i in range(10):
q2: CPtr
p_c_pointer(pointer(q[i]), q2)
g(q2, i * i)
# TODO: Use q[i] directly in the assert.
el = q[i]
print(el)
assert el == i * i

def run():
a: CPtr
size: i32
size = 10
a = get_array(size)
f(a)

run()
10 changes: 10 additions & 0 deletions integration_tests/bindc_03b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "bindc_03b.h"
#include <stdlib.h>

void g(int32_t* x, int32_t value) {
*x = value;
}

void* get_array(int32_t size) {
return malloc(size * sizeof(int32_t));
}
10 changes: 10 additions & 0 deletions integration_tests/bindc_03b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef BINDC_03B
#define BINDC_03B

#include <stdint.h>

void g(int32_t* x, int32_t value);

void* get_array(int32_t size);

#endif // BINDC_03B
21 changes: 21 additions & 0 deletions integration_tests/bindc_04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ltypes import pointer, i16, Pointer

# Testing Global Pointers
x: Pointer[i16[:]]

def f():
yptr1: Pointer[i16[:]]
y: i16[2]
y[0] = 1
y[1] = 2
yptr1 = pointer(y)
assert yptr1[0] == 1
assert yptr1[1] == 2
x = pointer(y)

def check():
f()
assert x[0] == 1
assert x[1] == 2

check()
7 changes: 7 additions & 0 deletions integration_tests/structs_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def f(a: A):
print(a.x)
print(a.y)

def change_struct(a: A):
a.x = a.x + 1
a.y = a.y + 1

def g():
x: A
x = A(3.25, 3)
Expand All @@ -21,5 +25,8 @@ def g():
f(x)
assert x.x == 5
assert x.y == 5.5
change_struct(x)
assert x.x == 6
assert x.y == 6.5

g()
18 changes: 17 additions & 1 deletion integration_tests/test_c_interop_02.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import ccall, f32, f64, i32, i64
from ltypes import ccall, f32, f64, i8, i16, i32, i64

@ccall
def f_f64_f64(x: f64) -> f64:
Expand All @@ -16,6 +16,14 @@ def f_i64_i64(x: i64) -> i64:
def f_i32_i32(x: i32) -> i32:
pass

@ccall
def f_i16_i16(x: i16) -> i16:
pass

@ccall
def f_i8_i8(x: i8) -> i8:
pass

def test_c_callbacks():
xf64: f64
xf64 = 3.3
Expand All @@ -33,4 +41,12 @@ def test_c_callbacks():
xi32 = 3
assert f_i32_i32(xi32) == 4

xi16: i16
xi16 = 3
assert f_i16_i16(xi16) == 4

xi8: i8
xi8 = 3
assert f_i8_i8(xi8) == 4

test_c_callbacks()
8 changes: 8 additions & 0 deletions integration_tests/test_c_interop_02b.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ int64_t f_i64_i64(int64_t x) {
int32_t f_i32_i32(int32_t x) {
return x+1;
}

int16_t f_i16_i16(int16_t x) {
return x+1;
}

int8_t f_i8_i8(int8_t x) {
return x+1;
}
2 changes: 2 additions & 0 deletions integration_tests/test_c_interop_02b.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ double f_f64_f64(double x);
float f_f32_f32(float x);
int64_t f_i64_i64(int64_t x);
int32_t f_i32_i32(int32_t x);
int16_t f_i16_i16(int16_t x);
int8_t f_i8_i8 (int8_t x);


#endif // TEST_C_INTEROP_02B
8 changes: 8 additions & 0 deletions integration_tests/test_c_interop_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def f_pf32_i32(x: CPtr) -> f32:
def f_pf64_i32(x: CPtr) -> f64:
pass

@ccall
def f_pvoid_pvoid(x: CPtr) -> CPtr:
pass

def test_c_callbacks():
xi32: i32
xi32 = 3
Expand All @@ -25,23 +29,27 @@ def test_c_callbacks():
p_c_pointer(pointer(xi32, i32), p)
print(pointer(xi32, i32), p)
assert f_pi32_i32(p) == 4
assert f_pi32_i32(f_pvoid_pvoid(p)) == 4

xi64: i64
xi64 = 3
p_c_pointer(pointer(xi64, i64), p)
print(pointer(xi64, i64), p)
assert f_pi64_i32(p) == 4
assert f_pi64_i32(f_pvoid_pvoid(p)) == 4

xf32: f32
xf32 = 3.3
p_c_pointer(pointer(xf32, f32), p)
print(pointer(xf32, f32), p)
assert abs(f_pf32_i32(p)-4.3) < 1e-6
assert abs(f_pf32_i32(f_pvoid_pvoid(p))-4.3) < 1e-6

xf64: f64
xf64 = 3.3
p_c_pointer(pointer(xf64, f64), p)
print(pointer(xf64, f64), p)
assert abs(f_pf64_i32(p)-4.3) < 1e-12
assert abs(f_pf64_i32(f_pvoid_pvoid(p))-4.3) < 1e-12

test_c_callbacks()
4 changes: 4 additions & 0 deletions integration_tests/test_c_interop_03b.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ float f_pf32_i32(float *x) {
double f_pf64_i32(double *x) {
return *x+1;
}

void* f_pvoid_pvoid(void *x) {
return x;
}
1 change: 1 addition & 0 deletions integration_tests/test_c_interop_03b.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ int32_t f_pi32_i32(int32_t *x);
int64_t f_pi64_i32(int64_t *x);
float f_pf32_i32(float *x);
double f_pf64_i32(double *x);
void* f_pvoid_pvoid(void *x);


#endif // TEST_C_INTEROP_03B
9 changes: 9 additions & 0 deletions integration_tests/test_platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from platform import python_implementation

def test_platform1():
s: str
s = python_implementation()
print("Python Implementation:", s)
assert s == "CPython" or s == "LPython"

test_platform1()
10 changes: 10 additions & 0 deletions integration_tests/test_str_to_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ltypes import i32

def f():
i: i32
i = int("314")
assert i == 314
i = int("-314")
assert i == -314

f()
8 changes: 8 additions & 0 deletions integration_tests/test_vars_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from test_vars_01b import test_name_other_module

def test_name():
print(__name__)
assert __name__ == "__main__"

test_name()
test_name_other_module()
3 changes: 3 additions & 0 deletions integration_tests/test_vars_01b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_name_other_module():
print(__name__)
assert __name__ != "__main__"
11 changes: 6 additions & 5 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int emit_asr(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -205,7 +205,7 @@ int emit_cpp(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -245,7 +245,7 @@ int emit_c(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -288,7 +288,7 @@ int emit_llvm(const std::string &infile,
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -352,7 +352,7 @@ int compile_python_to_object_file(
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, true,
compiler_options.symtab_only);
compiler_options.disable_main, compiler_options.symtab_only);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -645,6 +645,7 @@ int main(int argc, char *argv[])
app.add_flag("--indent", compiler_options.indent, "Indented print ASR/AST");
app.add_flag("--tree", compiler_options.tree, "Tree structure print ASR/AST");
app.add_option("--pass", arg_pass, "Apply the ASR pass and show ASR (implies --show-asr)");
app.add_flag("--disable-main", compiler_options.disable_main, "Do not generate any code for the `main` function");
app.add_flag("--symtab-only", compiler_options.symtab_only, "Only create symbol tables in ASR (skip executable stmt)");
app.add_flag("--time-report", time_report, "Show compilation time report");
app.add_flag("--static", static_link, "Create a static executable");
Expand Down
Loading

0 comments on commit 98a0a2e

Please sign in to comment.