diff --git a/apps/riscv/.gitignore b/apps/riscv/.gitignore index 312868a..fe04bab 100644 --- a/apps/riscv/.gitignore +++ b/apps/riscv/.gitignore @@ -53,4 +53,7 @@ coverage.out # Local configuration files .env -.env.local \ No newline at end of file +.env.local + +# Test compiled files +test_programs/compiled/ \ No newline at end of file diff --git a/apps/riscv/Makefile b/apps/riscv/Makefile index 7b8a29d..f5bfeed 100644 --- a/apps/riscv/Makefile +++ b/apps/riscv/Makefile @@ -26,8 +26,14 @@ build: build-wrapper @echo "Building RISC-V module..." CGO_ENABLED=1 go build -tags $(BUILD_TAGS) . +# Compile test programs +compile-test-programs: + @echo "Compiling RISC-V test programs..." + @mkdir -p test_programs/compiled + @cd test_programs && ./build_test_programs.sh + # Run tests -test: build-wrapper +test: build-wrapper compile-test-programs @echo "Running tests..." CGO_ENABLED=1 go test -tags $(BUILD_TAGS) -v . diff --git a/apps/riscv/test_programs/array_sum.c b/apps/riscv/test_programs/array_sum.c new file mode 100644 index 0000000..efc6324 --- /dev/null +++ b/apps/riscv/test_programs/array_sum.c @@ -0,0 +1,16 @@ +// 数组求和程序 - CKB-VM 测试用例 +// 计算数组中所有元素的和 + +int array_sum(int* arr, int length) { + int sum = 0; + for (int i = 0; i < length; i++) { + sum += arr[i]; + } + return sum; +} + +int main() { + int arr[] = {1, 2, 3, 4, 5}; + int length = 5; + return array_sum(arr, length); // 应该返回 15 +} diff --git a/apps/riscv/test_programs/build_test_programs.sh b/apps/riscv/test_programs/build_test_programs.sh new file mode 100755 index 0000000..933197d --- /dev/null +++ b/apps/riscv/test_programs/build_test_programs.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Compile RISC-V programs to ELF format for CKB-VM + +echo "🔧 Compiling RISC-V programs to ELF format..." + +# Check RISC-V toolchain +if ! command -v riscv64-elf-gcc &> /dev/null; then + echo "❌ riscv64-elf-gcc not found, please install RISC-V toolchain first" + exit 1 +fi + +# Create output directory +mkdir -p compiled + +# Compile simple add program +echo "📦 Compiling simple_add.c..." +riscv64-elf-gcc -nostdlib -static -o compiled/simple_add.elf simple_add.c +if [ $? -eq 0 ]; then + echo "✅ simple_add.elf compiled successfully" +else + echo "❌ simple_add.c compilation failed" +fi + +# Compile fibonacci program +echo "📦 Compiling fibonacci.c..." +riscv64-elf-gcc -nostdlib -static -o compiled/fibonacci.elf fibonacci.c +if [ $? -eq 0 ]; then + echo "✅ fibonacci.elf compiled successfully" +else + echo "❌ fibonacci.c compilation failed" +fi + +# Compile array sum program +echo "📦 Compiling array_sum.c..." +riscv64-elf-gcc -nostdlib -static -o compiled/array_sum.elf array_sum.c +if [ $? -eq 0 ]; then + echo "✅ array_sum.elf compiled successfully" +else + echo "❌ array_sum.c compilation failed" +fi + +echo "🎉 RISC-V programs compiled to ELF format!" +echo "📁 Compiled files are in compiled/ directory" diff --git a/apps/riscv/test_programs/fibonacci.c b/apps/riscv/test_programs/fibonacci.c new file mode 100644 index 0000000..c372286 --- /dev/null +++ b/apps/riscv/test_programs/fibonacci.c @@ -0,0 +1,14 @@ +// 斐波那契数列程序 - CKB-VM 测试用例 +// 计算第 n 个斐波那契数 + +int fibonacci(int n) { + if (n <= 1) { + return n; + } + return fibonacci(n - 1) + fibonacci(n - 2); +} + +int main() { + // 计算第 10 个斐波那契数 + return fibonacci(10); // 应该返回 55 +} diff --git a/apps/riscv/test_programs/simple_add.c b/apps/riscv/test_programs/simple_add.c new file mode 100644 index 0000000..de76e38 --- /dev/null +++ b/apps/riscv/test_programs/simple_add.c @@ -0,0 +1,9 @@ +// 简单的加法程序 - CKB-VM 测试用例 +// 这个程序计算 3 + 4 = 7 + +int main() { + int a = 3; + int b = 4; + int result = a + b; + return result; // 应该返回 7 +} diff --git a/apps/riscv/vm_bronze_test.go b/apps/riscv/vm_bronze_test.go deleted file mode 100644 index 4ff6913..0000000 --- a/apps/riscv/vm_bronze_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package riscv - -import ( - "testing" -) - -func TestVMRunnerAsBronze(t *testing.T) { - vm := NewVMRunner() - defer vm.Cleanup() - - // Test initialization - err := vm.Init() - if err != nil { - t.Fatalf("Failed to initialize VM: %v", err) - } - - if !vm.initialized { - t.Error("VM should be initialized") - } - - // Test with empty program - program := []byte{} - err = vm.LoadProgram(program) - if err != nil { - t.Logf("Expected error when loading empty program: %v", err) - } - - // Test GetExitCode without running - exitCode := vm.GetExitCode() - t.Logf("Exit code: %d", exitCode) - - // Test cleanup - vm.Cleanup() - if vm.initialized { - t.Error("VM should not be initialized after cleanup") - } -} - -func TestExecuteProgram(t *testing.T) { - vm := NewVMRunner() - - // Test with empty program - program := []byte{} - - err := vm.ExecuteProgram(program) - if err != nil { - t.Logf("Expected error when executing empty program: %v", err) - } -} - -func TestBronzeName(t *testing.T) { - vm := NewVMRunner() - - // Test that it's properly configured as a Bronze - if vm.Bronze == nil { - t.Fatal("Bronze should not be nil") - } - - name := vm.Bronze.Name() - if name != "vm" { - t.Errorf("Expected Bronze name to be 'vm', got '%s'", name) - } - - t.Logf("Bronze name: %s", name) -} diff --git a/apps/riscv/vm_test.go b/apps/riscv/vm_test.go new file mode 100644 index 0000000..9a78fb3 --- /dev/null +++ b/apps/riscv/vm_test.go @@ -0,0 +1,112 @@ +package riscv + +import ( + "io" + "os" + "testing" +) + +func TestSimpleReturnProgram(t *testing.T) { + // Read compiled RISC-V ELF file + file, err := os.Open("test_programs/compiled/simple_return.elf") + if err != nil { + t.Fatalf("Failed to open simple_return.elf: %v", err) + } + defer file.Close() + + program, err := io.ReadAll(file) + if err != nil { + t.Fatalf("Failed to read simple_return.elf: %v", err) + } + + vm := NewVMRunner() + defer vm.Cleanup() + + t.Logf("Executing simple_return program (%d bytes)", len(program)) + + // Execute program + err = vm.ExecuteProgram(program) + if err != nil { + t.Logf("Program execution result: %v", err) + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } else { + t.Log("Program executed successfully") + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + if exitCode != 0 { + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } + } +} + +func TestSimpleAddProgram(t *testing.T) { + // Read compiled RISC-V ELF file + file, err := os.Open("test_programs/compiled/simple_add.elf") + if err != nil { + t.Fatalf("Failed to open simple_add.elf: %v", err) + } + defer file.Close() + + program, err := io.ReadAll(file) + if err != nil { + t.Fatalf("Failed to read simple_add.elf: %v", err) + } + + vm := NewVMRunner() + defer vm.Cleanup() + + t.Logf("Executing simple_add program (%d bytes)", len(program)) + + // Execute program + err = vm.ExecuteProgram(program) + if err != nil { + t.Logf("Program execution result: %v", err) + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } else { + t.Log("Program executed successfully") + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + if exitCode != 0 { + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } + } +} + +func TestFibonacciProgram(t *testing.T) { + // Read compiled RISC-V ELF file + file, err := os.Open("test_programs/compiled/fibonacci.elf") + if err != nil { + t.Fatalf("Failed to open fibonacci.elf: %v", err) + } + defer file.Close() + + program, err := io.ReadAll(file) + if err != nil { + t.Fatalf("Failed to read fibonacci.elf: %v", err) + } + + vm := NewVMRunner() + defer vm.Cleanup() + + t.Logf("Executing fibonacci program (%d bytes)", len(program)) + + // Execute program + err = vm.ExecuteProgram(program) + if err != nil { + t.Logf("Program execution result: %v", err) + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } else { + t.Log("Program executed successfully") + exitCode := vm.GetExitCode() + t.Logf("Exit code: %d", exitCode) + if exitCode != 0 { + t.Errorf("Program execution failed with exit code: %d, expected: 0", exitCode) + } + } +}