github.com/goplus/llgo@v0.8.3/cl/builtin_test.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cl
    18  
    19  import (
    20  	"go/ast"
    21  	"go/constant"
    22  	"go/types"
    23  	"testing"
    24  
    25  	llssa "github.com/goplus/llgo/ssa"
    26  	"golang.org/x/tools/go/ssa"
    27  )
    28  
    29  func TestRecvTypeName(t *testing.T) {
    30  	if ret := recvTypeName(&ast.IndexExpr{
    31  		X:     &ast.Ident{Name: "Pointer"},
    32  		Index: &ast.Ident{Name: "T"},
    33  	}); ret != "Pointer" {
    34  		t.Fatal("recvTypeName IndexExpr:", ret)
    35  	}
    36  	if ret := recvTypeName(&ast.IndexListExpr{
    37  		X:       &ast.Ident{Name: "Pointer"},
    38  		Indices: []ast.Expr{&ast.Ident{Name: "T"}},
    39  	}); ret != "Pointer" {
    40  		t.Fatal("recvTypeName IndexListExpr:", ret)
    41  	}
    42  	defer func() {
    43  		if r := recover(); r == nil {
    44  			t.Fatal("recvTypeName: no error?")
    45  		}
    46  	}()
    47  	recvTypeName(&ast.BadExpr{})
    48  }
    49  
    50  /*
    51  func TestErrCompileValue(t *testing.T) {
    52  	defer func() {
    53  		if r := recover(); r != "can't use llgo instruction as a value" {
    54  			t.Fatal("TestErrCompileValue:", r)
    55  		}
    56  	}()
    57  	pkg := types.NewPackage("foo", "foo")
    58  	ctx := &context{
    59  		goTyps: pkg,
    60  		link: map[string]string{
    61  			"foo.": "llgo.unreachable",
    62  		},
    63  	}
    64  	ctx.compileValue(nil, &ssa.Function{
    65  		Pkg:       &ssa.Package{Pkg: pkg},
    66  		Signature: types.NewSignatureType(nil, nil, nil, nil, nil, false),
    67  	})
    68  }
    69  */
    70  
    71  func TestErrCompileInstrOrValue(t *testing.T) {
    72  	defer func() {
    73  		if r := recover(); r == nil {
    74  			t.Fatal("compileInstrOrValue: no error?")
    75  		}
    76  	}()
    77  	ctx := &context{
    78  		bvals: make(map[ssa.Value]llssa.Expr),
    79  	}
    80  	ctx.compileInstrOrValue(nil, &ssa.Call{}, true)
    81  }
    82  
    83  func TestErrAdvance(t *testing.T) {
    84  	defer func() {
    85  		if r := recover(); r == nil {
    86  			t.Fatal("advance: no error?")
    87  		}
    88  	}()
    89  	var ctx context
    90  	ctx.advance(nil, nil)
    91  }
    92  
    93  func TestErrAlloca(t *testing.T) {
    94  	defer func() {
    95  		if r := recover(); r == nil {
    96  			t.Fatal("alloca: no error?")
    97  		}
    98  	}()
    99  	var ctx context
   100  	ctx.alloca(nil, nil)
   101  }
   102  
   103  func TestErrAllocaCStr(t *testing.T) {
   104  	defer func() {
   105  		if r := recover(); r == nil {
   106  			t.Fatal("allocaCStr: no error?")
   107  		}
   108  	}()
   109  	var ctx context
   110  	ctx.allocaCStr(nil, nil)
   111  }
   112  
   113  func TestCStrNoArgs(t *testing.T) {
   114  	defer func() {
   115  		if r := recover(); r == nil {
   116  			t.Fatal("cstr: no error?")
   117  		}
   118  	}()
   119  	cstr(nil, nil)
   120  }
   121  
   122  func TestCStrNonconst(t *testing.T) {
   123  	defer func() {
   124  		if r := recover(); r == nil {
   125  			t.Fatal("cstr: no error?")
   126  		}
   127  	}()
   128  	cstr(nil, []ssa.Value{&ssa.Parameter{}})
   129  }
   130  
   131  func TestPkgNoInit(t *testing.T) {
   132  	pkg := types.NewPackage("foo", "foo")
   133  	ctx := &context{
   134  		goTyps: pkg,
   135  		loaded: make(map[*types.Package]*pkgInfo),
   136  	}
   137  	if ctx.pkgNoInit(pkg) {
   138  		t.Fatal("pkgNoInit?")
   139  	}
   140  }
   141  
   142  func TestPkgKind(t *testing.T) {
   143  	if v, _ := pkgKind("link: hello.a"); v != PkgLinkExtern {
   144  		t.Fatal("pkgKind:", v)
   145  	}
   146  	if v, _ := pkgKind("noinit"); v != PkgNoInit {
   147  		t.Fatal("pkgKind:", v)
   148  	}
   149  	if v, _ := pkgKind(""); v != PkgLLGo {
   150  		t.Fatal("pkgKind:", v)
   151  	}
   152  }
   153  
   154  func TestPkgKindOf(t *testing.T) {
   155  	if v, _ := PkgKindOf(types.Unsafe); v != PkgDeclOnly {
   156  		t.Fatal("PkgKindOf unsafe:", v)
   157  	}
   158  	pkg := types.NewPackage("foo", "foo")
   159  	pkg.Scope().Insert(
   160  		types.NewConst(
   161  			0, pkg, "LLGoPackage", types.Typ[types.String],
   162  			constant.MakeString("noinit")),
   163  	)
   164  	if v, _ := PkgKindOf(pkg); v != PkgNoInit {
   165  		t.Fatal("PkgKindOf foo:", v)
   166  	}
   167  }
   168  
   169  func TestIsAny(t *testing.T) {
   170  	if isAny(types.Typ[types.UntypedInt]) {
   171  		t.Fatal("isAny?")
   172  	}
   173  }
   174  
   175  func TestIntVal(t *testing.T) {
   176  	defer func() {
   177  		if r := recover(); r == nil {
   178  			t.Fatal("intVal: no error?")
   179  		}
   180  	}()
   181  	intVal(&ssa.Parameter{})
   182  }
   183  
   184  func TestIgnoreName(t *testing.T) {
   185  	if !ignoreName("runtime.foo") || !ignoreName("runtime/foo") || !ignoreName("internal/abi") {
   186  		t.Fatal("ignoreName failed")
   187  	}
   188  }
   189  
   190  func TestErrImport(t *testing.T) {
   191  	var ctx context
   192  	pkg := types.NewPackage("foo", "foo")
   193  	ctx.importPkg(pkg, nil)
   194  }
   195  
   196  func TestErrInitLinkname(t *testing.T) {
   197  	var ctx context
   198  	ctx.initLinkname("//llgo:link abc", func(name string) (string, bool, bool) {
   199  		return "", false, false
   200  	})
   201  	ctx.initLinkname("//go:linkname Printf printf", func(name string) (string, bool, bool) {
   202  		return "", false, false
   203  	})
   204  	defer func() {
   205  		if r := recover(); r == nil {
   206  			t.Fatal("initLinkname: no error?")
   207  		}
   208  	}()
   209  	ctx.initLinkname("//go:linkname Printf printf", func(name string) (string, bool, bool) {
   210  		return "foo.Printf", false, name == "Printf"
   211  	})
   212  }
   213  
   214  func TestErrVarOf(t *testing.T) {
   215  	defer func() {
   216  		if r := recover(); r == nil {
   217  			t.Fatal("varOf: no error?")
   218  		}
   219  	}()
   220  	prog := llssa.NewProgram(nil)
   221  	pkg := prog.NewPackage("foo", "foo")
   222  	pkgTypes := types.NewPackage("foo", "foo")
   223  	ctx := &context{
   224  		pkg:    pkg,
   225  		goTyps: pkgTypes,
   226  	}
   227  	ssaPkg := &ssa.Package{Pkg: pkgTypes}
   228  	g := &ssa.Global{Pkg: ssaPkg}
   229  	ctx.varOf(nil, g)
   230  }