Merge branch 'main' of https://github.com/goplus/llgo into impl-stacksave
This commit is contained in:
147
_demo/go/gobuild/demo.go
Normal file
147
_demo/go/gobuild/demo.go
Normal file
@@ -0,0 +1,147 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/build"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("runtime.Compiler = %q\n", runtime.Compiler)
|
||||
|
||||
// Test 1: Check build.Default context
|
||||
ctx := build.Default
|
||||
fmt.Printf("build.Default.Compiler = %q\n", ctx.Compiler)
|
||||
if ctx.Compiler != "gc" {
|
||||
panic(fmt.Sprintf("expected build.Default.Compiler to be \"gc\", got %q", ctx.Compiler))
|
||||
}
|
||||
|
||||
if len(ctx.ToolTags) == 0 {
|
||||
panic("expected build.Default.ToolTags to be non-empty")
|
||||
}
|
||||
fmt.Printf("build.Default.ToolTags = %v\n", ctx.ToolTags)
|
||||
|
||||
if len(ctx.ReleaseTags) == 0 {
|
||||
panic("expected build.Default.ReleaseTags to be non-empty")
|
||||
}
|
||||
fmt.Printf("build.Default.ReleaseTags count = %d\n", len(ctx.ReleaseTags))
|
||||
|
||||
// Validate GOOS and GOARCH are set
|
||||
if ctx.GOOS == "" {
|
||||
panic("expected build.Default.GOOS to be non-empty")
|
||||
}
|
||||
if ctx.GOARCH == "" {
|
||||
panic("expected build.Default.GOARCH to be non-empty")
|
||||
}
|
||||
fmt.Printf("build.Default.GOOS = %q, GOARCH = %q\n", ctx.GOOS, ctx.GOARCH)
|
||||
|
||||
// Test 2: Import standard library package with FindOnly
|
||||
pkg, err := build.Import("fmt", "", build.FindOnly)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.Import(\"fmt\") failed: %v", err))
|
||||
}
|
||||
if pkg.ImportPath != "fmt" {
|
||||
panic(fmt.Sprintf("expected ImportPath \"fmt\", got %q", pkg.ImportPath))
|
||||
}
|
||||
if !pkg.Goroot {
|
||||
panic("expected fmt package to be in GOROOT")
|
||||
}
|
||||
fmt.Printf("build.Import(\"fmt\"): ImportPath=%s, Goroot=%v\n", pkg.ImportPath, pkg.Goroot)
|
||||
|
||||
// Test 3: Import nested standard library package
|
||||
osPkg, err := build.Import("os/exec", "", build.FindOnly)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.Import(\"os/exec\") failed: %v", err))
|
||||
}
|
||||
if osPkg.ImportPath != "os/exec" {
|
||||
panic(fmt.Sprintf("expected ImportPath \"os/exec\", got %q", osPkg.ImportPath))
|
||||
}
|
||||
if !osPkg.Goroot {
|
||||
panic("expected os/exec package to be in GOROOT")
|
||||
}
|
||||
fmt.Printf("build.Import(\"os/exec\"): ImportPath=%s, Goroot=%v\n", osPkg.ImportPath, osPkg.Goroot)
|
||||
|
||||
// Test 4: Import internal package (should succeed with FindOnly)
|
||||
internalPkg, err := build.Import("internal/cpu", "", build.FindOnly)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.Import(\"internal/cpu\") failed: %v", err))
|
||||
}
|
||||
if internalPkg.ImportPath != "internal/cpu" {
|
||||
panic(fmt.Sprintf("expected ImportPath \"internal/cpu\", got %q", internalPkg.ImportPath))
|
||||
}
|
||||
fmt.Printf("build.Import(\"internal/cpu\"): ImportPath=%s\n", internalPkg.ImportPath)
|
||||
|
||||
// Test 5: Import with srcDir parameter
|
||||
runtimePkg, err := build.Import("runtime", "", build.FindOnly)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.Import(\"runtime\") failed: %v", err))
|
||||
}
|
||||
if runtimePkg.ImportPath != "runtime" {
|
||||
panic(fmt.Sprintf("expected ImportPath \"runtime\", got %q", runtimePkg.ImportPath))
|
||||
}
|
||||
if runtimePkg.Dir == "" {
|
||||
panic("expected runtime package Dir to be non-empty")
|
||||
}
|
||||
fmt.Printf("build.Import(\"runtime\"): ImportPath=%s, Dir exists=%v\n", runtimePkg.ImportPath, runtimePkg.Dir != "")
|
||||
|
||||
// Test 6: ImportDir with current directory
|
||||
dirPkg, err := build.ImportDir(".", build.FindOnly)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.ImportDir(\".\") failed: %v", err))
|
||||
}
|
||||
// Note: Name might be empty with FindOnly mode as it doesn't read source files
|
||||
fmt.Printf("build.ImportDir(\".\"): Dir=%s, ImportPath=%s\n", dirPkg.Dir, dirPkg.ImportPath)
|
||||
|
||||
// Test 7: IsLocalImport with various paths
|
||||
testCases := []struct {
|
||||
path string
|
||||
expected bool
|
||||
}{
|
||||
{"./foo", true},
|
||||
{"../bar", true},
|
||||
{"./", true},
|
||||
{"fmt", false},
|
||||
{"github.com/user/repo", false},
|
||||
{"", false},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
result := build.IsLocalImport(tc.path)
|
||||
if result != tc.expected {
|
||||
panic(fmt.Sprintf("build.IsLocalImport(%q): expected %v, got %v", tc.path, tc.expected, result))
|
||||
}
|
||||
}
|
||||
fmt.Printf("build.IsLocalImport: all test cases passed\n")
|
||||
|
||||
// Test 8: Verify Context has expected fields
|
||||
if ctx.GOPATH == "" && ctx.GOROOT == "" {
|
||||
panic("expected either GOPATH or GOROOT to be set")
|
||||
}
|
||||
fmt.Printf("build.Default.GOROOT exists = %v\n", ctx.GOROOT != "")
|
||||
|
||||
// Test 9: Import with AllowBinary flag
|
||||
binaryPkg, err := build.Import("fmt", "", build.FindOnly|build.AllowBinary)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build.Import with AllowBinary failed: %v", err))
|
||||
}
|
||||
if binaryPkg.ImportPath != "fmt" {
|
||||
panic(fmt.Sprintf("expected ImportPath \"fmt\", got %q", binaryPkg.ImportPath))
|
||||
}
|
||||
fmt.Printf("build.Import(\"fmt\") with AllowBinary: success\n")
|
||||
|
||||
// Test 10: Verify compiler tag in build context
|
||||
hasCompilerTag := false
|
||||
for _, tag := range ctx.ReleaseTags {
|
||||
if strings.HasPrefix(tag, "go1.") {
|
||||
hasCompilerTag = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasCompilerTag {
|
||||
panic("expected at least one go1.x release tag")
|
||||
}
|
||||
fmt.Printf("build.Default.ReleaseTags: contains go1.x tags = %v\n", hasCompilerTag)
|
||||
|
||||
fmt.Printf("\nSuccess! All go/build public functions work correctly with llgo\n")
|
||||
fmt.Printf("Total tests passed: 10\n")
|
||||
}
|
||||
124
_demo/go/maphash/maphash.go
Normal file
124
_demo/go/maphash/maphash.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/maphash"
|
||||
)
|
||||
|
||||
func main() {
|
||||
testHashBasics()
|
||||
testMakeSeed()
|
||||
testSetSeed()
|
||||
testWriteMethods()
|
||||
testBytes()
|
||||
testString()
|
||||
}
|
||||
|
||||
func testHashBasics() {
|
||||
fmt.Println("=== Test Hash Basics ===")
|
||||
var h maphash.Hash
|
||||
n, err := h.WriteString("hello")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteString failed: %v", err))
|
||||
}
|
||||
if n != 5 {
|
||||
panic(fmt.Sprintf("WriteString returned %d, expected 5", n))
|
||||
}
|
||||
hash1 := h.Sum64()
|
||||
fmt.Printf("Hash of 'hello': 0x%x\n", hash1)
|
||||
|
||||
h.Reset()
|
||||
n, err = h.WriteString("world")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteString failed: %v", err))
|
||||
}
|
||||
hash2 := h.Sum64()
|
||||
fmt.Printf("Hash of 'world': 0x%x\n", hash2)
|
||||
|
||||
h.Reset()
|
||||
n, err = h.WriteString("hello")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteString failed: %v", err))
|
||||
}
|
||||
hash3 := h.Sum64()
|
||||
if hash1 != hash3 {
|
||||
panic(fmt.Sprintf("Hash mismatch: 0x%x != 0x%x", hash1, hash3))
|
||||
}
|
||||
fmt.Printf("Hash consistency verified: 0x%x == 0x%x\n", hash1, hash3)
|
||||
}
|
||||
|
||||
func testMakeSeed() {
|
||||
fmt.Println("\n=== Test MakeSeed ===")
|
||||
seed1 := maphash.MakeSeed()
|
||||
seed2 := maphash.MakeSeed()
|
||||
fmt.Printf("Seed 1: %v\n", seed1)
|
||||
fmt.Printf("Seed 2: %v\n", seed2)
|
||||
if seed1 == seed2 {
|
||||
fmt.Println("Warning: Seeds are identical (rare but possible)")
|
||||
}
|
||||
}
|
||||
|
||||
func testSetSeed() {
|
||||
fmt.Println("\n=== Test SetSeed ===")
|
||||
var h1, h2 maphash.Hash
|
||||
seed := maphash.MakeSeed()
|
||||
|
||||
h1.SetSeed(seed)
|
||||
_, err := h1.WriteString("test")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteString failed: %v", err))
|
||||
}
|
||||
hash1 := h1.Sum64()
|
||||
|
||||
h2.SetSeed(seed)
|
||||
_, err = h2.WriteString("test")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteString failed: %v", err))
|
||||
}
|
||||
hash2 := h2.Sum64()
|
||||
|
||||
if hash1 != hash2 {
|
||||
panic(fmt.Sprintf("Hashes with same seed should match: 0x%x != 0x%x", hash1, hash2))
|
||||
}
|
||||
fmt.Printf("Same seed produces same hash: 0x%x == 0x%x\n", hash1, hash2)
|
||||
}
|
||||
|
||||
func testWriteMethods() {
|
||||
fmt.Println("\n=== Test Write Methods ===")
|
||||
var h maphash.Hash
|
||||
|
||||
data := []byte("hello")
|
||||
n, err := h.Write(data)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Write failed: %v", err))
|
||||
}
|
||||
if n != len(data) {
|
||||
panic(fmt.Sprintf("Write returned %d, expected %d", n, len(data)))
|
||||
}
|
||||
hash1 := h.Sum64()
|
||||
fmt.Printf("Hash after Write: 0x%x\n", hash1)
|
||||
|
||||
h.Reset()
|
||||
err = h.WriteByte('A')
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("WriteByte failed: %v", err))
|
||||
}
|
||||
hash2 := h.Sum64()
|
||||
fmt.Printf("Hash after WriteByte('A'): 0x%x\n", hash2)
|
||||
}
|
||||
|
||||
func testBytes() {
|
||||
fmt.Println("\n=== Test Bytes Function ===")
|
||||
seed := maphash.MakeSeed()
|
||||
data := []byte("test data")
|
||||
hash := maphash.Bytes(seed, data)
|
||||
fmt.Printf("Bytes hash: 0x%x\n", hash)
|
||||
}
|
||||
|
||||
func testString() {
|
||||
fmt.Println("\n=== Test String Function ===")
|
||||
seed := maphash.MakeSeed()
|
||||
str := "test string"
|
||||
hash := maphash.String(seed, str)
|
||||
fmt.Printf("String hash: 0x%x\n", hash)
|
||||
}
|
||||
49
_demo/go/reflectindirect/reflect-indirect.go
Normal file
49
_demo/go/reflectindirect/reflect-indirect.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func main() {
|
||||
x := 42
|
||||
p := &x
|
||||
|
||||
// Test 1: Non-pointer value - should return same value
|
||||
v1 := reflect.Indirect(reflect.ValueOf(x))
|
||||
if !v1.IsValid() || v1.Interface() != 42 {
|
||||
panic("Non-pointer test failed: expected 42")
|
||||
}
|
||||
|
||||
// Test 2: Pointer - should dereference
|
||||
v2 := reflect.Indirect(reflect.ValueOf(p))
|
||||
if !v2.IsValid() || v2.Interface() != 42 {
|
||||
panic("Pointer dereference test failed: expected 42")
|
||||
}
|
||||
|
||||
// Test 3: Nil pointer - should return invalid Value
|
||||
var nilPtr *int
|
||||
v3 := reflect.Indirect(reflect.ValueOf(nilPtr))
|
||||
if v3.IsValid() {
|
||||
panic("Nil pointer test failed: expected invalid Value")
|
||||
}
|
||||
|
||||
// Test 4: Struct value - should return same value
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
person := Person{Name: "Alice", Age: 30}
|
||||
v4 := reflect.Indirect(reflect.ValueOf(person))
|
||||
if !v4.IsValid() || v4.Interface().(Person).Name != "Alice" || v4.Interface().(Person).Age != 30 {
|
||||
panic("Struct value test failed: expected Person{Name: Alice, Age: 30}")
|
||||
}
|
||||
|
||||
// Test 5: Struct pointer - should dereference
|
||||
personPtr := &Person{Name: "Bob", Age: 25}
|
||||
v5 := reflect.Indirect(reflect.ValueOf(personPtr))
|
||||
if !v5.IsValid() || v5.Interface().(Person).Name != "Bob" || v5.Interface().(Person).Age != 25 {
|
||||
panic("Struct pointer test failed: expected Person{Name: Bob, Age: 25}")
|
||||
}
|
||||
|
||||
println("PASS")
|
||||
}
|
||||
Reference in New Issue
Block a user