full test params passing of exported functions

This commit is contained in:
Li Jie
2025-09-12 10:08:01 +08:00
parent 50d1d2e19a
commit cc65ee18b5
9 changed files with 565 additions and 42 deletions

View File

@@ -6,6 +6,17 @@ import (
C "github.com/goplus/llgo/_demo/go/export/c"
)
// assert helper function for testing
func assert[T comparable](got, expected T, message string) {
if got != expected {
println("ASSERTION FAILED:", message)
println(" Expected:", expected)
println(" Got: ", got)
panic("assertion failed: " + message)
}
println("✓", message)
}
// Small struct
type SmallStruct struct {
ID int8 `json:"id"`
@@ -36,6 +47,17 @@ type Node struct {
type MyInt int
type MyString string
// Function types for callbacks
//
//llgo:type C
type IntCallback func(int) int
//llgo:type C
type StringCallback func(string) string
//llgo:type C
type VoidCallback func()
// Complex struct with mixed arrays and slices
type ComplexData struct {
Matrix [3][4]int32 `json:"matrix"` // 2D array
@@ -124,10 +146,15 @@ func CreateNode(data int) *Node {
}
//export LinkNodes
func LinkNodes(first, second *Node) {
if first != nil {
func LinkNodes(first, second *Node) int {
if first != nil && second != nil {
first.Next = second
return first.Data + second.Data // Return sum for verification
}
if first != nil {
return first.Data + 1000 // Return data + offset if only first exists
}
return 2000 // Return fixed value if both are nil
}
//export TraverseNodes
@@ -193,12 +220,12 @@ func ProcessUint64(x uint64) uint64 {
//export ProcessInt
func ProcessInt(x int) int {
return x + 100
return x * 11
}
//export ProcessUint
func ProcessUint(x uint) uint {
return x + 200
return x * 21
}
//export ProcessUintptr
@@ -374,17 +401,53 @@ func ProcessIntChannel(ch chan int) int {
}
}
// Functions with function callbacks
//export ProcessWithIntCallback
func ProcessWithIntCallback(x int, callback IntCallback) int {
if callback != nil {
return callback(x)
}
return x
}
//export ProcessWithStringCallback
func ProcessWithStringCallback(s string, callback StringCallback) string {
if callback != nil {
return callback(s)
}
return s
}
//export ProcessWithVoidCallback
func ProcessWithVoidCallback(callback VoidCallback) int {
if callback != nil {
callback()
return 123 // Return non-zero to indicate callback was called
}
return 456 // Return different value if callback is nil
}
//export ProcessThreeUnnamedParams
func ProcessThreeUnnamedParams(a int, s string, b bool) float64 {
result := float64(a) + float64(len(s))
if b {
result *= 1.5
}
return result
}
// Functions with interface
//export ProcessInterface
func ProcessInterface(i interface{}) int {
switch v := i.(type) {
case int:
return v
return v + 100
case string:
return len(v)
return len(v) * 10
default:
return 0
return 999 // Non-zero default to avoid false positives
}
}
@@ -416,7 +479,16 @@ func ThreeParams(a int32, b float64, c bool) float64 {
//export MultipleParams
func MultipleParams(a int8, b uint16, c int32, d uint64, e float32, f float64, g string, h bool) string {
return g + "_processed"
result := g + "_" + string(rune('A'+a)) + string(rune('0'+b%10)) + string(rune('0'+c%10))
if h {
result += "_true"
}
return result + "_" + string(rune('0'+int(d%10))) + "_" + string(rune('0'+int(e)%10)) + "_" + string(rune('0'+int(f)%10))
}
//export NoParamNames
func NoParamNames(int8, int16, bool) int32 {
return 789 // Return non-zero value for testing, params are unnamed by design
}
// Functions returning no value
@@ -463,27 +535,52 @@ func main() {
// Test small struct
small := CreateSmallStruct(5, true)
assert(small.ID, int8(5), "CreateSmallStruct ID should be 5")
assert(small.Flag, true, "CreateSmallStruct Flag should be true")
println("Small struct:", small.ID, small.Flag)
processed := ProcessSmallStruct(small)
assert(processed.ID, int8(6), "ProcessSmallStruct should increment ID to 6")
assert(processed.Flag, false, "ProcessSmallStruct should flip Flag to false")
println("Processed small:", processed.ID, processed.Flag)
// Test large struct
large := CreateLargeStruct(12345, "test")
assert(large.ID, int64(12345), "CreateLargeStruct ID should be 12345")
assert(large.Name, "test", "CreateLargeStruct Name should be 'test'")
println("Large struct ID:", large.ID, "Name:", large.Name)
total := ProcessLargeStruct(large)
// Expected calculation:
// ID: 12345, Name len: 4, Values: 1+2+3+4+5+6+7+8+9+10=55, Children len: 2
// Extra1: 12345, Extra2: 67890, Extra3: 3, Extra4: +1000, Extra5: 4096
expectedTotal := int64(12345 + 4 + 55 + 2 + 12345 + 67890 + 3 + 1000 + 4096)
assert(total, expectedTotal, "ProcessLargeStruct total should match expected calculation")
println("Large struct total:", total)
// Test self-referential struct
node1 := CreateNode(100)
node2 := CreateNode(200)
LinkNodes(node1, node2)
linkResult := LinkNodes(node1, node2)
assert(linkResult, 300, "LinkNodes should return sum of node data (100 + 200)")
count := TraverseNodes(node1)
assert(count, 2, "TraverseNodes should count 2 linked nodes")
println("Node count:", count)
// Test basic types
// Test basic types with assertions
assert(ProcessBool(true), false, "ProcessBool(true) should return false")
assert(ProcessInt8(10), int8(11), "ProcessInt8(10) should return 11")
f32Result := ProcessFloat32(3.14)
// Float comparison with tolerance
if f32Result < 4.7 || f32Result > 4.72 {
println("ASSERTION FAILED: ProcessFloat32(3.14) should return ~4.71, got:", f32Result)
panic("float assertion failed")
}
println("✓ ProcessFloat32(3.14) returns ~4.71")
assert(ProcessString("hello"), "processed_hello", "ProcessString should prepend 'processed_'")
println("Bool:", ProcessBool(true))
println("Int8:", ProcessInt8(10))
println("Float32:", ProcessFloat32(3.14))
@@ -491,43 +588,66 @@ func main() {
// Test named types
myInt := ProcessMyInt(MyInt(42))
assert(myInt, MyInt(420), "ProcessMyInt(42) should return 420")
println("MyInt:", int(myInt))
myStr := ProcessMyString(MyString("world"))
assert(myStr, MyString("modified_world"), "ProcessMyString should prepend 'modified_'")
println("MyString:", string(myStr))
// Test collections
arr := [5]int{1, 2, 3, 4, 5}
println("Array sum:", ProcessIntArray(arr))
arrSum := ProcessIntArray(arr)
assert(arrSum, 15, "ProcessIntArray([1,2,3,4,5]) should return 15")
println("Array sum:", arrSum)
slice := []int{10, 20, 30}
println("Slice sum:", ProcessIntSlice(slice))
sliceSum := ProcessIntSlice(slice)
assert(sliceSum, 60, "ProcessIntSlice([10,20,30]) should return 60")
println("Slice sum:", sliceSum)
m := make(map[string]int)
m["a"] = 100
m["b"] = 200
println("Map sum:", ProcessStringMap(m))
mapSum := ProcessStringMap(m)
assert(mapSum, 300, "ProcessStringMap({'a':100,'b':200}) should return 300")
println("Map sum:", mapSum)
// Test multidimensional arrays
matrix2d := CreateMatrix2D()
println("Matrix2D sum:", ProcessMatrix2D(matrix2d))
matrix2dSum := ProcessMatrix2D(matrix2d)
assert(matrix2dSum, int32(78), "ProcessMatrix2D should return 78 (sum of 1+2+...+12)")
println("Matrix2D sum:", matrix2dSum)
matrix3d := CreateMatrix3D()
println("Matrix3D sum:", ProcessMatrix3D(matrix3d))
matrix3dSum := ProcessMatrix3D(matrix3d)
assert(matrix3dSum, uint32(300), "ProcessMatrix3D should return 300")
println("Matrix3D sum:", matrix3dSum)
grid5x4 := CreateGrid5x4()
println("Grid5x4 sum:", ProcessGrid5x4(grid5x4))
gridSum := ProcessGrid5x4(grid5x4)
assert(gridSum, 115.0, "ProcessGrid5x4 should return 115.0")
println("Grid5x4 sum:", gridSum)
// Test complex data with multidimensional arrays
complexData := CreateComplexData()
println("ComplexData matrix sum:", ProcessComplexData(complexData))
complexSum := ProcessComplexData(complexData)
assert(complexSum, int32(78), "ProcessComplexData should return 78")
println("ComplexData matrix sum:", complexSum)
// Test various parameter counts
assert(NoParams(), 42, "NoParams should return 42")
assert(OneParam(5), 10, "OneParam(5) should return 10")
assert(TwoParams(65, "_test"), "A_test", "TwoParams should return 'A_test'")
assert(ThreeParams(10, 2.5, true), 25.0, "ThreeParams should return 25.0")
assert(NoParamNames(1, 2, false), int32(789), "NoParamNames should return 789")
println("NoParams:", NoParams())
println("OneParam:", OneParam(5))
println("TwoParams:", TwoParams(65, "_test"))
println("ThreeParams:", ThreeParams(10, 2.5, true))
println("MultipleParams:", MultipleParams(1, 2, 3, 4, 5.0, 6.0, "result", true))
println("NoParamNames:", NoParamNames(1, 2, false))
// Test XType from c package
xtype := CreateXType(42, "test", 3.14, true)
@@ -541,5 +661,14 @@ func main() {
println("Ptr XType:", ptrX.ID, ptrX.Name, ptrX.Value, ptrX.Flag)
}
// Test callback functions
intResult := ProcessWithIntCallback(10, func(x int) int { return x * 3 })
println("IntCallback result:", intResult)
stringResult := ProcessWithStringCallback("hello", func(s string) string { return s + "_callback" })
println("StringCallback result:", stringResult)
ProcessWithVoidCallback(func() { println("VoidCallback executed") })
NoReturn("demo completed")
}

View File

@@ -84,6 +84,12 @@ typedef intptr_t main_MyInt;
typedef GoString main_MyString;
typedef intptr_t (*main_IntCallback)(intptr_t);
typedef GoString (*main_StringCallback)(GoString);
typedef void (*main_VoidCallback)(void);
GoString
Concat(GoString a, GoString b);
@@ -129,12 +135,15 @@ CreateXType(int32_t id, GoString name, double value, _Bool flag);
void
HelloWorld(void);
void
intptr_t
LinkNodes(main_Node* first, main_Node* second);
GoString
MultipleParams(int8_t a, uint16_t b, int32_t c, uint64_t d, float e, double f, GoString g, _Bool h);
int32_t
NoParamNames(int8_t, int16_t, _Bool);
intptr_t
NoParams(void);
@@ -175,7 +184,7 @@ int8_t
ProcessInt8(int8_t x);
intptr_t
ProcessIntArray(intptr_t arr[5]);
ProcessIntArray(intptr_t* arr);
intptr_t
ProcessIntChannel(GoChan ch);
@@ -216,6 +225,9 @@ ProcessString(GoString s);
intptr_t
ProcessStringMap(GoMap m);
double
ProcessThreeUnnamedParams(intptr_t a, GoString s, _Bool b);
uintptr_t
ProcessUint(uintptr_t x);
@@ -237,6 +249,15 @@ ProcessUintptr(uintptr_t x);
void*
ProcessUnsafePointer(void* p);
intptr_t
ProcessWithIntCallback(intptr_t x, main_IntCallback callback);
GoString
ProcessWithStringCallback(GoString s, main_StringCallback callback);
intptr_t
ProcessWithVoidCallback(main_VoidCallback callback);
C_XType
ProcessXType(C_XType x);

View File

@@ -205,14 +205,53 @@ echo ""
# echo ""
# Test 3: Go export demo execution
print_status "=== Test 3: Running Go export demo ==="
if go run export.go > /tmp/go_export_output.log 2>&1; then
print_status "Go export demo execution succeeded"
# Check if output contains expected success indicators
if grep -q "✓" /tmp/go_export_output.log; then
SUCCESS_COUNT=$(grep -c "✓" /tmp/go_export_output.log)
print_status "All $SUCCESS_COUNT assertions passed in Go export demo"
else
print_warning "No assertion markers found in Go export demo output"
fi
# Show key output lines
print_status "Go export demo output summary:"
if grep -q "ASSERTION FAILED" /tmp/go_export_output.log; then
print_error "Found assertion failures in Go export demo"
grep "ASSERTION FAILED" /tmp/go_export_output.log
else
print_status " ✅ No assertion failures detected"
echo " 📊 First few lines of output:"
head -5 /tmp/go_export_output.log | sed 's/^/ /'
echo " 📊 Last few lines of output:"
tail -5 /tmp/go_export_output.log | sed 's/^/ /'
fi
else
print_error "Go export demo execution failed"
print_error "Error output:"
cat /tmp/go_export_output.log | sed 's/^/ /'
fi
# Cleanup temporary file
rm -f /tmp/go_export_output.log
echo ""
# Final summary
print_status "=== Test Summary ==="
if [[ -f "libexport.a" ]] && [[ -f "libexport.h" ]]; then
print_status "All tests completed successfully:"
print_status " ✅ Go export demo execution with assertions"
print_status " ✅ C header generation (c-archive and c-shared modes)"
print_status " ✅ C demo compilation and execution"
print_status " ✅ Cross-platform symbol renaming"
print_status " ✅ Init function export and calling"
print_status " ✅ Function callback types with proper typedef syntax"
print_status " ✅ Multidimensional array parameter handling"
print_status ""
print_status "Final files available:"
print_status " - libexport.a (static library)"

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
#include "../libexport.h"
int main() {
@@ -43,26 +44,59 @@ int main() {
// Test self-referential struct
main_Node* node1 = CreateNode(100);
main_Node* node2 = CreateNode(200);
LinkNodes(node1, node2);
int link_result = LinkNodes(node1, node2);
assert(link_result == 300); // LinkNodes returns 100 + 200 = 300
printf("LinkNodes result: %d\n", link_result);
int count = TraverseNodes(node1);
assert(count == 2); // Should traverse 2 nodes
printf("Node count: %d\n", count);
// Test basic types
printf("Bool: %d\n", ProcessBool(1)); // 1 for true
// Test basic types with assertions
assert(ProcessBool(1) == 0); // ProcessBool(true) returns !true = false
printf("Bool: %d\n", ProcessBool(1));
assert(ProcessInt8(10) == 11); // ProcessInt8(x) returns x + 1
printf("Int8: %d\n", ProcessInt8(10));
assert(ProcessUint8(10) == 11); // ProcessUint8(x) returns x + 1
printf("Uint8: %d\n", ProcessUint8(10));
assert(ProcessInt16(10) == 20); // ProcessInt16(x) returns x * 2
printf("Int16: %d\n", ProcessInt16(10));
assert(ProcessUint16(10) == 20); // ProcessUint16(x) returns x * 2
printf("Uint16: %d\n", ProcessUint16(10));
assert(ProcessInt32(10) == 30); // ProcessInt32(x) returns x * 3
printf("Int32: %d\n", ProcessInt32(10));
assert(ProcessUint32(10) == 30); // ProcessUint32(x) returns x * 3
printf("Uint32: %u\n", ProcessUint32(10));
assert(ProcessInt64(10) == 40); // ProcessInt64(x) returns x * 4
printf("Int64: %" PRId64 "\n", ProcessInt64(10));
assert(ProcessUint64(10) == 40); // ProcessUint64(x) returns x * 4
printf("Uint64: %" PRIu64 "\n", ProcessUint64(10));
assert(ProcessInt(10) == 110); // ProcessInt(x) returns x * 11
printf("Int: %ld\n", ProcessInt(10));
assert(ProcessUint(10) == 210); // ProcessUint(x) returns x * 21
printf("Uint: %lu\n", ProcessUint(10));
assert(ProcessUintptr(0x1000) == 4396); // ProcessUintptr(x) returns x + 300 = 4096 + 300
printf("Uintptr: %lu\n", ProcessUintptr(0x1000));
printf("Float32: %f\n", ProcessFloat32(3.14f));
printf("Float64: %f\n", ProcessFloat64(3.14));
// Float comparisons with tolerance
float f32_result = ProcessFloat32(3.14f);
assert(f32_result > 4.7f && f32_result < 4.72f); // ProcessFloat32(x) returns x * 1.5 ≈ 4.71
printf("Float32: %f\n", f32_result);
double f64_result = ProcessFloat64(3.14);
assert(f64_result > 7.84 && f64_result < 7.86); // ProcessFloat64(x) returns x * 2.5 ≈ 7.85
printf("Float64: %f\n", f64_result);
// Test unsafe pointer
int test_val = 42;
@@ -85,9 +119,30 @@ int main() {
printf("Interface test skipped (complex in C)\n");
// Test various parameter counts
assert(NoParams() == 42); // NoParams() always returns 42
printf("NoParams: %ld\n", NoParams());
assert(OneParam(5) == 10); // OneParam(x) returns x * 2
printf("OneParam: %ld\n", OneParam(5));
assert(ThreeParams(10, 2.5, 1) == 25.0); // ThreeParams calculates result
printf("ThreeParams: %f\n", ThreeParams(10, 2.5, 1)); // 1 for true
// Test ProcessThreeUnnamedParams - now uses all parameters
GoString test_str = {"hello", 5};
double unnamed_result = ProcessThreeUnnamedParams(10, test_str, 1);
assert(unnamed_result == 22.5); // (10 + 5) * 1.5 = 22.5
printf("ProcessThreeUnnamedParams: %f\n", unnamed_result);
// Test ProcessWithVoidCallback - now returns int
int void_callback_result = ProcessWithVoidCallback(NULL);
assert(void_callback_result == 456); // Returns 456 when callback is nil
printf("ProcessWithVoidCallback(NULL): %d\n", void_callback_result);
// Test NoParamNames - function with unnamed parameters
int32_t no_names_result = NoParamNames(5, 10, 0);
assert(no_names_result == 789); // Returns fixed value 789
printf("NoParamNames: %d\n", no_names_result);
// Test XType from c package - create GoString for name parameter
GoString xname = {"test_x", 6}; // name and length
@@ -116,6 +171,7 @@ int main() {
{9, 10, 11, 12}
};
int32_t matrix_sum = ProcessMatrix2D(test_matrix);
assert(matrix_sum == 78); // Sum of 1+2+3+...+12 = 78
printf("Matrix2D sum: %d\n", matrix_sum);
// Create a test 3D cube [2][3][4]uint8
@@ -129,6 +185,7 @@ int main() {
}
}
uint32_t cube_sum = ProcessMatrix3D(test_cube);
assert(cube_sum == 300); // Sum of 1+2+3+...+24 = 300
printf("Matrix3D (cube) sum: %u\n", cube_sum);
// Create a test 5x4 grid [5][4]double
@@ -141,6 +198,7 @@ int main() {
}
}
double grid_sum = ProcessGrid5x4(test_grid);
assert(grid_sum == 115.0); // Sum of 1.0+1.5+2.0+...+10.5 = 115.0
printf("Grid5x4 sum: %f\n", grid_sum);
// Test functions that return multidimensional arrays (as multi-level pointers)