github.com/goplus/llgo@v0.8.3/cl/compile_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_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/goplus/llgo/cl/cltest"
    23  	"github.com/goplus/llgo/ssa"
    24  )
    25  
    26  func testCompile(t *testing.T, src, expected string) {
    27  	t.Helper()
    28  	cltest.TestCompileEx(t, src, "foo.go", expected)
    29  }
    30  
    31  func TestFromTestpy(t *testing.T) {
    32  	cltest.FromDir(t, "", "./_testpy", false)
    33  }
    34  
    35  func TestFromTestlibc(t *testing.T) {
    36  	cltest.FromDir(t, "", "./_testlibc", true)
    37  }
    38  
    39  func TestFromTestrt(t *testing.T) {
    40  	cltest.FromDir(t, "", "./_testrt", true)
    41  }
    42  
    43  func TestFromTestdata(t *testing.T) {
    44  	cltest.FromDir(t, "", "./_testdata", false)
    45  }
    46  
    47  func TestSqlite(t *testing.T) {
    48  	cltest.Pkg(t, "github.com/goplus/llgo/c/sqlite", "../c/sqlite/llgo_autogen.ll")
    49  }
    50  
    51  func TestFromTestpymath(t *testing.T) {
    52  	cltest.Pkg(t, ssa.PkgPython+"/math", "../py/math/llgo_autogen.ll")
    53  }
    54  
    55  func TestPython(t *testing.T) {
    56  	cltest.Pkg(t, ssa.PkgPython, "../py/llgo_autogen.ll")
    57  }
    58  
    59  func TestRuntime(t *testing.T) {
    60  	cltest.Pkg(t, ssa.PkgRuntime, "../internal/runtime/llgo_autogen.ll")
    61  }
    62  
    63  func TestVar(t *testing.T) {
    64  	testCompile(t, `package foo
    65  
    66  var a int
    67  `, `; ModuleID = 'foo'
    68  source_filename = "foo"
    69  
    70  @foo.a = global ptr null
    71  @"foo.init$guard" = global ptr null
    72  
    73  define void @foo.init() {
    74  _llgo_0:
    75    %0 = load i1, ptr @"foo.init$guard", align 1
    76    br i1 %0, label %_llgo_2, label %_llgo_1
    77  
    78  _llgo_1:                                          ; preds = %_llgo_0
    79    store i1 true, ptr @"foo.init$guard", align 1
    80    br label %_llgo_2
    81  
    82  _llgo_2:                                          ; preds = %_llgo_1, %_llgo_0
    83    ret void
    84  }
    85  `)
    86  }
    87  
    88  func TestBasicFunc(t *testing.T) {
    89  	testCompile(t, `package foo
    90  
    91  func fn(a int, b float64) int {
    92  	return 1
    93  }
    94  `, `; ModuleID = 'foo'
    95  source_filename = "foo"
    96  
    97  @"foo.init$guard" = global ptr null
    98  
    99  define i64 @foo.fn(i64 %0, double %1) {
   100  _llgo_0:
   101    ret i64 1
   102  }
   103  
   104  define void @foo.init() {
   105  _llgo_0:
   106    %0 = load i1, ptr @"foo.init$guard", align 1
   107    br i1 %0, label %_llgo_2, label %_llgo_1
   108  
   109  _llgo_1:                                          ; preds = %_llgo_0
   110    store i1 true, ptr @"foo.init$guard", align 1
   111    br label %_llgo_2
   112  
   113  _llgo_2:                                          ; preds = %_llgo_1, %_llgo_0
   114    ret void
   115  }
   116  `)
   117  }