test: simple llgo tests

This commit is contained in:
Li Jie
2024-06-06 20:54:32 +08:00
parent 6049cf9047
commit 91e1fa6aff
6 changed files with 204 additions and 5 deletions

View File

@@ -12,18 +12,27 @@ on:
jobs:
test-macos:
runs-on: macos-latest
strategy:
matrix:
os: [macos-latest, macos-12, macos-13]
llvm: [17]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Update Homebrew
if: matrix.llvm == 17 # needed as long as LLVM 17 is still fresh
run: brew update
- name: Install LLVM ${{ matrix.llvm }}
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install llvm@${{ matrix.llvm }}
- name: Install LLVM ${{ matrix.llvm }} and bdw-gc
run: |
HOMEBREW_NO_AUTO_UPDATE=1 brew install llvm@${{ matrix.llvm }} bdw-gc
echo `brew --prefix llvm@${{ matrix.llvm }}`/bin >> $GITHUB_PATH
- name: Clang information
run: |
echo $PATH
which clang
clang --version
- name: Set up Go
uses: actions/setup-go@v5
@@ -36,6 +45,14 @@ jobs:
- name: Test
run: go test -v ./...
- name: Install
run: go install ./...
- name: LLGO tests
run: |
export LLGOROOT=$PWD
bash _test/run.sh
test-linux:
runs-on: ubuntu-20.04
strategy:
@@ -44,12 +61,19 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install LLVM ${{ matrix.llvm }}
- name: Install LLVM ${{ matrix.llvm }} and libgc-dev
run: |
echo 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{ matrix.llvm }} main' | sudo tee /etc/apt/sources.list.d/llvm.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install --no-install-recommends llvm-${{ matrix.llvm }}-dev
sudo apt-get install --no-install-recommends clang-${{ matrix.llvm }} llvm-${{ matrix.llvm }}-dev libgc-dev
echo /usr/lib/llvm-${{ matrix.llvm }}/bin >> $GITHUB_PATH
- name: Clang information
run: |
echo $PATH
which clang
clang --version
- name: Set up Go
uses: actions/setup-go@v5
@@ -62,6 +86,14 @@ jobs:
- name: Test
run: go test -v -coverprofile="coverage.txt" -covermode=atomic ./...
- name: Install
run: go install ./...
- name: LLGO tests
run: |
export LLGOROOT=$PWD
bash _test/run.sh
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:

101
_test/bdwgc.go Normal file
View File

@@ -0,0 +1,101 @@
package main
import (
"unsafe"
"github.com/goplus/llgo/_test/testing"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/internal/runtime/bdwgc"
)
// ------ Test malloc ------
func TestMalloc(t *testing.T) {
pn := (*int)(bdwgc.Malloc(unsafe.Sizeof(int(0))))
*pn = 1 << 30
c.Printf(c.Str("value: %d, %x, %p, %p\n"), *pn, *pn, pn, &pn)
pl := (*int64)(bdwgc.Realloc(c.Pointer(pn), unsafe.Sizeof(int64(0))))
*pl = 1 << 60
c.Printf(c.Str("value: %lld, %llx, %p, %p\n"), *pl, *pl, pl, &pl)
bdwgc.Free(c.Pointer(pl))
}
// ------ Test finalizer ------
const (
RETURN_VALUE_FREED = 1 << 31
)
var called uint = 0
func setReturnValueFreed(pobj c.Pointer, clientData c.Pointer) {
called |= RETURN_VALUE_FREED
c.Printf(c.Str("called: %x\n"), called)
}
func setLoopValueFreed(pobj c.Pointer, clientData c.Pointer) {
pmask := (*uint)(clientData)
called |= *pmask
c.Printf(c.Str("called: %x\n"), called)
}
func isCalled(mask uint) bool {
return called&mask != 0
}
func returnValue() *int {
pn := bdwgc.Malloc(unsafe.Sizeof(int(0)))
bdwgc.RegisterFinalizer(pn, setReturnValueFreed, nil, nil, nil)
return (*int)(pn)
}
func callFunc() {
pn := returnValue()
*pn = 1 << 30
c.Printf(c.Str("value: %d, %x, %p, %p\n"), *pn, *pn, pn, &pn)
bdwgc.Gcollect()
check(!isCalled(RETURN_VALUE_FREED), c.Str("finalizer should not be called"))
}
func loop() {
for i := 0; i < 5; i++ {
p := bdwgc.Malloc(unsafe.Sizeof(int(0)))
pn := (*int)(p)
*pn = i
c.Printf(c.Str("value: %d, %x, %p, %p\n"), *pn, *pn, pn, &pn)
pflag := (*uint)(c.Malloc(unsafe.Sizeof(uint(0))))
*pflag = 1 << i
bdwgc.RegisterFinalizer(p, setLoopValueFreed, c.Pointer(pflag), nil, nil)
bdwgc.Gcollect()
check(!isCalled(1<<i), c.Str("finalizer should not be called"))
for j := 0; j < i; j++ {
check(isCalled(1<<j), c.Str("finalizers of previous objects should be called"))
}
}
}
// Clear stack to avoid reference
func clearStack() {
p := c.Alloca(128)
c.Memset(p, 0, 128)
}
func check(b bool, msg *c.Char) {
if !b {
c.Printf(c.Str("check failed: %s\n"), msg)
panic("check failed")
}
}
func TestFinalizer(t *testing.T) {
bdwgc.Init()
callFunc()
clearStack()
bdwgc.Gcollect()
check(isCalled(RETURN_VALUE_FREED), c.Str("finalizer should be called"))
loop()
}

31
_test/main.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"github.com/goplus/llgo/_test/testing"
"github.com/goplus/llgo/c"
)
type TestCase struct {
Name string
F func(*testing.T)
}
func main() {
tests := []TestCase{
{"TestMalloc", TestMalloc},
{"TestFinalizer", TestFinalizer},
}
if c.Argc == 1 {
for _, test := range tests {
c.Printf(c.Str("%s\n"), c.AllocaCStr(test.Name))
}
return
}
c.Fprintf(c.Stderr, c.Str("arg: %s\n"), c.Index(c.Argv, 1))
idx := int(c.Atoi(c.Index(c.Argv, 1)))
if idx < 0 || idx >= len(tests) {
c.Printf(c.Str("invalid test index %d"), idx)
panic("invalid test index")
}
tests[idx].F(nil)
}

28
_test/run.sh Normal file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
set -e
testcmd=/tmp/test
llgo build -o $testcmd ./_test
cases=$($testcmd)
total=$(echo "$cases" | wc -l | tr -d ' ')
succ=0
for idx in $(seq 1 $((total))); do
case=$(echo "$cases" | sed -n "${idx}p")
case_id=$(echo "$case" | cut -d',' -f1)
case_name=$(echo "$case" | cut -d',' -f2)
echo "=== Test case: $case_name"
set +e
out=$("$testcmd" "$((idx-1))" 2>&1)
exit_code=$?
set -e
if [ "${exit_code:-0}" -ne 0 ]; then
echo "failed: $out"
else
succ=$((succ+1))
echo "passed"
fi
done
echo "=== Done"
echo "$succ/$total tests passed"
if [ "$total" -ne "$succ" ]; then
exit 1
fi

4
_test/testing/testing.go Normal file
View File

@@ -0,0 +1,4 @@
package testing
type T struct {
}

3
c/c.go
View File

@@ -160,3 +160,6 @@ func GetoptLong(argc Int, argv **Char, optstring *Char, longopts *Option, longin
func GetoptLongOnly(argc Int, argv **Char, optstring *Char, longopts *Option, longindex *Int) Int
// -----------------------------------------------------------------------------
//go:linkname Atoi C.atoi
func Atoi(s *Char) Int