Skip to content

Commit

Permalink
Test for all types of array members
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Jan 19, 2023
1 parent a7c1a07 commit d2c5b63
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
28 changes: 27 additions & 1 deletion integration_tests/structs_19.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class buffer_struct:

@dataclass
class buffer_struct_array:
buffer8: i8[100]
buffer1: i32[32]
buffer2: f32[32]
buffer4: f64[32]
buffer5: i16[64]
buffer6: i64[16]
buffer3: c32[16]
buffer7: c64[8]

@ccall
def get_buffer() -> CPtr:
Expand Down Expand Up @@ -87,9 +94,28 @@ def f_array():
print(pb.buffer1[15])

fill_buffer_array(b)
for i in range(100):
print(pb.buffer8[i])
assert pb.buffer8[i] == i8(i + 8)

for i in range(32):
print(pb.buffer1[i])
print(pb.buffer1[i], pb.buffer2[i], pb.buffer4[i])
assert pb.buffer1[i] == i32(i + 1)
assert pb.buffer2[i] == f32(i + 2)
assert pb.buffer4[i] == f64(i + 4)

for i in range(64):
print(pb.buffer5[i])
assert pb.buffer5[i] == i16(i + 5)

for i in range(16):
print(pb.buffer6[i], pb.buffer3[i])
assert pb.buffer6[i] == i64(i + 6)
assert pb.buffer3[i] == c32(i + 3)

for i in range(8):
print(pb.buffer7[i])
assert pb.buffer7[i] == c64(i + 7)


f()
Expand Down
22 changes: 22 additions & 0 deletions integration_tests/structs_19b.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ struct buffer_c {
};

struct buffer_c_array {
int8_t buffer8[100];
int32_t buffer1[32];
float buffer2[32];
double buffer4[32];
int16_t buffer5[64];
int64_t buffer6[16];
float complex buffer3[16];
double complex buffer7[8];
};

void fill_buffer(void* buffer_cptr) {
Expand All @@ -32,8 +39,23 @@ void fill_buffer(void* buffer_cptr) {

void fill_buffer_array(void* buffer_cptr) {
struct buffer_c_array* buffer_clink_ = (struct buffer_c_array*) buffer_cptr;
for( int i = 0; i < 128; i++ ) {
buffer_clink_->buffer8[i] = i + 8;
}
for( int i = 0; i < 32; i++ ) {
buffer_clink_->buffer1[i] = i + 1;
buffer_clink_->buffer2[i] = i + 2;
buffer_clink_->buffer4[i] = i + 4;
}
for( int i = 0; i < 64; i++ ) {
buffer_clink_->buffer5[i] = i + 5;
}
for( int i = 0; i < 16; i++ ) {
buffer_clink_->buffer6[i] = i + 6;
buffer_clink_->buffer3[i] = CMPLXF(i + 3, 0.0);
}
for( int i = 0; i < 8; i++ ) {
buffer_clink_->buffer7[i] = CMPLXL(i + 7, 0.0);
}
}

Expand Down
2 changes: 2 additions & 0 deletions integration_tests/structs_19b.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <inttypes.h>

void fill_buffer(void* buffer_cptr);
void fill_buffer_array(void* buffer_cptr);
void* get_buffer();
void* get_buffer_array();
12 changes: 10 additions & 2 deletions src/runtime/ltypes/ltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ def inner_func():

# C interoperation support

class c_float_complex(ctypes.Structure):
class c_complex(ctypes.Structure):
def __eq__(self, other):
if isinstance(other, complex):
return self.real == other.real and self.imag == other.imag
elif isinstance(other, (int, float)):
return self.real == other and self.imag == 0.0
return super().__eq__(other)

class c_float_complex(c_complex):
_fields_ = [("real", ctypes.c_float), ("imag", ctypes.c_float)]

class c_double_complex(ctypes.Structure):
class c_double_complex(c_complex):
_fields_ = [("real", ctypes.c_double), ("imag", ctypes.c_double)]

def convert_type_to_ctype(arg):
Expand Down

0 comments on commit d2c5b63

Please sign in to comment.