github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/misc/cgo/test/issue3261.go (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cgotest
     6  
     7  /*
     8  // libgcc on ARM might be compiled as thumb code, but our 5l
     9  // can't handle that, so we have to disable this test on arm.
    10  #ifdef __ARMEL__
    11  #include <stdio.h>
    12  int vabs(int x) {
    13  	puts("testLibgcc is disabled on ARM because 5l cannot handle thumb library.");
    14  	return (x < 0) ? -x : x;
    15  }
    16  #else
    17  int __absvsi2(int); // dummy prototype for libgcc function
    18  // we shouldn't name the function abs, as gcc might use
    19  // the builtin one.
    20  int vabs(int x) { return __absvsi2(x); }
    21  #endif
    22  */
    23  import "C"
    24  
    25  import "testing"
    26  
    27  func testLibgcc(t *testing.T) {
    28  	var table = []struct {
    29  		in, out C.int
    30  	}{
    31  		{0, 0},
    32  		{1, 1},
    33  		{-42, 42},
    34  		{1000300, 1000300},
    35  		{1 - 1<<31, 1<<31 - 1},
    36  	}
    37  	for _, v := range table {
    38  		if o := C.vabs(v.in); o != v.out {
    39  			t.Fatalf("abs(%d) got %d, should be %d", v.in, o, v.out)
    40  			return
    41  		}
    42  	}
    43  }