Files
llgo/cl/builtin_test.go

167 lines
3.5 KiB
Go
Raw Normal View History

2024-04-26 04:44:49 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cl
import (
2024-04-29 11:58:48 +08:00
"go/constant"
2024-04-26 05:39:15 +08:00
"go/types"
2024-04-26 04:44:49 +08:00
"testing"
2024-04-26 05:39:15 +08:00
llssa "github.com/goplus/llgo/ssa"
"golang.org/x/tools/go/ssa"
2024-04-26 04:44:49 +08:00
)
2024-05-03 18:35:14 +08:00
func TestErrCompileInstrOrValue(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("compileInstrOrValue: no error?")
}
}()
ctx := &context{
bvals: make(map[ssa.Value]llssa.Expr),
}
ctx.compileInstrOrValue(nil, &ssa.Call{}, true)
}
2024-05-02 00:04:37 +08:00
func TestErrAdvance(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("advance: no error?")
}
}()
var ctx context
ctx.advance(nil, nil)
}
2024-04-29 18:55:09 +08:00
func TestErrAlloca(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("alloca: no error?")
}
}()
2024-05-02 00:04:37 +08:00
var ctx context
ctx.alloca(nil, nil)
2024-04-29 18:55:09 +08:00
}
2024-04-29 13:59:06 +08:00
func TestCStrNoArgs(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("cstr: no error?")
}
}()
cstr(nil, nil)
}
func TestCStrNonconst(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("cstr: no error?")
}
}()
cstr(nil, []ssa.Value{&ssa.Parameter{}})
}
2024-04-29 10:06:47 +08:00
func TestPkgNoInit(t *testing.T) {
pkg := types.NewPackage("foo", "foo")
ctx := &context{
goTyps: pkg,
loaded: make(map[*types.Package]*pkgInfo),
}
if ctx.pkgNoInit(pkg) {
t.Fatal("pkgNoInit?")
}
}
func TestPkgKind(t *testing.T) {
2024-04-29 11:34:59 +08:00
if v := pkgKind("noinit"); v != PkgNoInit {
2024-04-29 10:06:47 +08:00
t.Fatal("pkgKind:", v)
}
2024-04-29 11:34:59 +08:00
if v := pkgKind(""); v != PkgLLGo {
2024-04-29 10:06:47 +08:00
t.Fatal("pkgKind:", v)
}
}
2024-04-29 11:58:48 +08:00
func TestPkgKindOf(t *testing.T) {
if v := PkgKindOf(types.Unsafe); v != PkgDeclOnly {
t.Fatal("PkgKindOf unsafe:", v)
}
pkg := types.NewPackage("foo", "foo")
pkg.Scope().Insert(
types.NewConst(
0, pkg, "LLGoPackage", types.Typ[types.String],
constant.MakeString("noinit")),
)
if v := PkgKindOf(pkg); v != PkgNoInit {
t.Fatal("PkgKindOf foo:", v)
}
}
2024-04-28 22:38:04 +08:00
func TestIsAny(t *testing.T) {
if isAny(types.Typ[types.UntypedInt]) {
t.Fatal("isAny?")
}
}
func TestIntVal(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("intVal: no error?")
}
}()
intVal(&ssa.Parameter{})
}
2024-04-26 04:44:49 +08:00
func TestIgnoreName(t *testing.T) {
if !ignoreName("runtime.foo") || !ignoreName("runtime/foo") || !ignoreName("internal/abi") {
t.Fatal("ignoreName failed")
}
}
2024-04-26 05:39:15 +08:00
func TestErrImport(t *testing.T) {
2024-04-26 05:39:15 +08:00
var ctx context
pkg := types.NewPackage("foo", "foo")
2024-04-29 09:51:32 +08:00
ctx.importPkg(pkg, nil)
2024-04-26 05:39:15 +08:00
}
func TestErrInitLinkname(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("initLinkname: no error?")
}
}()
var ctx context
2024-05-01 20:33:31 +08:00
ctx.initLinkname("foo", "//go:linkname Printf printf", false)
}
func TestErrVarOf(t *testing.T) {
2024-04-26 05:39:15 +08:00
defer func() {
if r := recover(); r == nil {
t.Fatal("varOf: no error?")
}
}()
prog := llssa.NewProgram(nil)
pkg := prog.NewPackage("foo", "foo")
pkgTypes := types.NewPackage("foo", "foo")
ctx := &context{
pkg: pkg,
goTyps: pkgTypes,
}
ssaPkg := &ssa.Package{Pkg: pkgTypes}
g := &ssa.Global{Pkg: ssaPkg}
ctx.varOf(g)
}