Skip to content

Commit

Permalink
llcppsymg:libs,cflags parse
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Oct 18, 2024
1 parent a227daf commit ce721f8
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 368 deletions.
269 changes: 269 additions & 0 deletions chore/_xtool/llcppsymg/_cmptest/config_test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package main

import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/goplus/llgo/chore/_xtool/llcppsymg/config"
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config/cfgparse"
)

func main() {
TestGetConf()
TestParseLibs()
TestGenDylibPaths()
TestParseCFlags()
TestGenHeaderFilePath()
}

func TestGetConf() {
Expand Down Expand Up @@ -61,3 +69,264 @@ func TestGetConf() {
fmt.Println()
}
}

func TestParseLibs() {
fmt.Println("=== Test ParseLibs ===")

testCases := []struct {
name string
input string
}{
{
name: "Lua library",
input: "-L/opt/homebrew/lib -llua -lm",
},
{
name: "SQLite library",
input: "-L/opt/homebrew/opt/sqlite/lib -lsqlite3",
},
{
name: "INIReader library",
input: "-L/opt/homebrew/Cellar/inih/58/lib -lINIReader",
},
{
name: "Multiple library paths",
input: "-L/opt/homebrew/lib -L/usr/lib -llua",
},
{
name: "No valid library",
input: "-L/opt/homebrew/lib",
},
}

for _, tc := range testCases {
fmt.Printf("Test case: %s\n", tc.name)
fmt.Printf("Input: %s\n", tc.input)

conf := cfgparse.ParseLibs(tc.input)

fmt.Println("Paths:", conf.Paths)
fmt.Println("Names:", conf.Names)
}
}

func TestGenDylibPaths() {
fmt.Println("=== Test GenDylibPaths ===")

tempDir := os.TempDir()
tempDefaultPath := filepath.Join(tempDir, "symblib")
affix := ".dylib"
if runtime.GOOS == "linux" {
affix = ".so"
}
err := os.MkdirAll(tempDefaultPath, 0755)
if err != nil {
fmt.Printf("Failed to create temp default path: %v\n", err)
return
}

dylib1 := filepath.Join(tempDir, "libsymb1"+affix)
dylib2 := filepath.Join(tempDir, "libsymb2"+affix)
defaultDylib3 := filepath.Join(tempDefaultPath, "libsymb3"+affix)

os.Create(dylib1)
os.Create(dylib2)
os.Create(defaultDylib3)
defer os.Remove(dylib1)
defer os.Remove(dylib2)
defer os.Remove(defaultDylib3)
defer os.Remove(tempDefaultPath)

testCase := []struct {
name string
conf *cfgparse.Libs
defaultPaths []string
want []string
}{
{
name: "existing dylib",
conf: &cfgparse.Libs{
Names: []string{"symb1"},
Paths: []string{tempDir},
},
defaultPaths: []string{},
want: []string{dylib1},
},
{
name: "existing dylibs",
conf: &cfgparse.Libs{
Names: []string{"symb1", "symb2"},
Paths: []string{tempDir},
},
defaultPaths: []string{},
want: []string{dylib1, dylib2},
},
{
name: "existint default paths",
conf: &cfgparse.Libs{
Names: []string{"symb1", "symb3"},
Paths: []string{tempDir},
},
defaultPaths: []string{tempDefaultPath},
want: []string{dylib1, defaultDylib3},
},
{
name: "existint default paths & not found",
conf: &cfgparse.Libs{
Names: []string{"symb1", "symb3", "math"},
Paths: []string{tempDir},
},
defaultPaths: []string{tempDefaultPath},
want: []string{dylib1, defaultDylib3},
},
{
name: "no existing dylib",
conf: &cfgparse.Libs{
Names: []string{"notexist"},
Paths: []string{tempDir},
},
want: []string{},
},
}
for _, tc := range testCase {
fmt.Printf("Test case: %s\n", tc.name)
paths, notFounds, err := tc.conf.GenDylibPaths(tc.defaultPaths)

if len(notFounds) > 0 {
fmt.Println("notFounds", notFounds)
}

if err != nil {
fmt.Printf("Error: %v\n", err)
}

for _, path := range paths {
found := false
for _, wantPath := range tc.want {
if path == wantPath {
found = true
fileName := filepath.Base(path)
if runtime.GOOS == "linux" {
fileName = strings.TrimSuffix(fileName, ".so")
} else {
fileName = strings.TrimSuffix(fileName, ".dylib")
}
fmt.Printf("Path %s is in the expected paths\n", fileName)
break
}
}
if !found {
fmt.Printf("Path %s is not in the expected paths\n", path)
}
}

}
}

func TestParseCFlags() {
fmt.Println("=== Test ParseCFlags ===")

testCases := []struct {
name string
input string
}{
{
name: "Single include path",
input: "-I/usr/include",
},
{
name: "Multiple include paths",
input: "-I/usr/include -I/opt/homebrew/include",
},
{
name: "Include paths mixed with other flags",
input: "-I/usr/include -DDEBUG -I/opt/local/include -Wall",
},
{
name: "Empty input",
input: "",
},
}

for _, tc := range testCases {
fmt.Printf("Test case: %s\n", tc.name)
fmt.Printf("Input: %s\n", tc.input)

conf := cfgparse.ParseCFlags(tc.input)

fmt.Println("Paths:", conf.Paths)
}
}

func TestGenHeaderFilePath() {
fmt.Println("=== Test GenHeaderFilePath ===")

tempDir := os.TempDir()
temDir2 := filepath.Join(tempDir, "include")
tempFile1 := filepath.Join(tempDir, "test1.h")
tempFile2 := filepath.Join(tempDir, "test2.h")
tempFile3 := filepath.Join(temDir2, "test3.h")
os.MkdirAll(temDir2, 0755)
os.Create(tempFile1)
os.Create(tempFile2)
os.Create(tempFile3)
defer os.Remove(tempFile1)
defer os.Remove(tempFile2)
defer os.Remove(tempFile3)
defer os.Remove(temDir2)

testCases := []struct {
name string
cflags string
files []string
}{
{
name: "Valid files",
cflags: "-I" + tempDir,
files: []string{"test1.h", "test2.h"},
},
{
name: "Mixed existing and non-existing files",
cflags: "-I" + tempDir,
files: []string{"test1.h", "nonexistent.h"},
},
{
name: "Multiple include paths",
cflags: "-I" + tempDir + " -I" + temDir2,
files: []string{"test1.h", "test2.h", "test3.h"},
},
{
name: "No existing files",
cflags: "-I" + tempDir,
files: []string{"nonexistent1.h", "nonexistent2.h"},
},
{
name: "Empty file list",
cflags: "-I/usr/include",
files: []string{},
},
}

for _, tc := range testCases {
fmt.Printf("Test case: %s\n", tc.name)
fmt.Printf("Input files: %v\n", tc.files)

cflag := cfgparse.ParseCFlags(tc.cflags)
result, notFounds, err := cflag.GenHeaderFilePaths(tc.files)

if err != nil {
fmt.Printf("Error: %v\n", err)
}
if len(notFounds) > 0 {
fmt.Println("notFounds", notFounds)
}
if result != nil {
relativeResult := make([]string, len(result))
for i, path := range result {
relativeResult[i] = filepath.Base(path)
}
fmt.Printf("Output: %v\n", relativeResult)
}
fmt.Println()
}
}
73 changes: 73 additions & 0 deletions chore/_xtool/llcppsymg/_cmptest/config_test/llgo.expect
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,79 @@ Cplusplus: false
=== Test case: Invalid JSON ===
Error: failed to parse config

=== Test ParseLibs ===
Test case: Lua library
Input: -L/opt/homebrew/lib -llua -lm
Paths: [/opt/homebrew/lib]
Names: [lua m]
Test case: SQLite library
Input: -L/opt/homebrew/opt/sqlite/lib -lsqlite3
Paths: [/opt/homebrew/opt/sqlite/lib]
Names: [sqlite3]
Test case: INIReader library
Input: -L/opt/homebrew/Cellar/inih/58/lib -lINIReader
Paths: [/opt/homebrew/Cellar/inih/58/lib]
Names: [INIReader]
Test case: Multiple library paths
Input: -L/opt/homebrew/lib -L/usr/lib -llua
Paths: [/opt/homebrew/lib /usr/lib]
Names: [lua]
Test case: No valid library
Input: -L/opt/homebrew/lib
Paths: [/opt/homebrew/lib]
Names: []
=== Test GenDylibPaths ===
Test case: existing dylib
Path libsymb1 is in the expected paths
Test case: existing dylibs
Path libsymb1 is in the expected paths
Path libsymb2 is in the expected paths
Test case: existint default paths
Path libsymb1 is in the expected paths
Path libsymb3 is in the expected paths
Test case: existint default paths & not found
notFounds [math]
Path libsymb1 is in the expected paths
Path libsymb3 is in the expected paths
Test case: no existing dylib
notFounds [notexist]
Error: failed to find any libraries
=== Test ParseCFlags ===
Test case: Single include path
Input: -I/usr/include
Paths: [/usr/include]
Test case: Multiple include paths
Input: -I/usr/include -I/opt/homebrew/include
Paths: [/usr/include /opt/homebrew/include]
Test case: Include paths mixed with other flags
Input: -I/usr/include -DDEBUG -I/opt/local/include -Wall
Paths: [/usr/include /opt/local/include]
Test case: Empty input
Input:
Paths: []
=== Test GenHeaderFilePath ===
Test case: Valid files
Input files: [test1.h test2.h]
Output: [test1.h test2.h]

Test case: Mixed existing and non-existing files
Input files: [test1.h nonexistent.h]
notFounds [nonexistent.h]
Output: [test1.h]

Test case: Multiple include paths
Input files: [test1.h test2.h test3.h]
Output: [test1.h test2.h test3.h]

Test case: No existing files
Input files: [nonexistent1.h nonexistent2.h]
Error: failed to find any header files
notFounds [nonexistent1.h nonexistent2.h]

Test case: Empty file list
Input files: []
Error: failed to find any header files


#stderr

Expand Down
18 changes: 0 additions & 18 deletions chore/_xtool/llcppsymg/_cmptest/parse_test/llgo.expect
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
#stdout
=== Test GenHeaderFilePath ===
Test case: Valid files
Input files: [test1.h test2.h]
Output: [test1.h test2.h]

Test case: Mixed existing and non-existing files
Input files: [test1.h nonexistent.h]
Error: some files not found or inaccessible: [file not found: nonexistent.h]
Output: [test1.h]

Test case: No existing files
Input files: [nonexistent1.h nonexistent2.h]
Error: some files not found or inaccessible: [file not found: nonexistent1.h file not found: nonexistent2.h]

Test case: Empty file list
Input files: []
Error: no valid header files

=== Test NewSymbolProcessor ===
Before: No prefixes After: Prefixes: [lua_ luaL_]

Expand Down
Loading

0 comments on commit ce721f8

Please sign in to comment.