gitee.com/quant1x/num@v0.3.2/asm/c2goasm/test/clib_amd64_test.go (about)

     1  /*
     2   * Minio Cloud Storage, (C) 2017 Minio, Inc.
     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 c2goasmtest
    18  
    19  import (
    20  	"testing"
    21  	"unsafe"
    22  )
    23  
    24  func testClibFloor32(t *testing.T, fl, expected float32) {
    25  
    26  	got := _ClibFloor32(fl)
    27  	if expected != got {
    28  		t.Errorf("testClibFloor32(): \nexpected %v\ngot      %v", expected, got)
    29  	}
    30  }
    31  
    32  func testClibFloor64(t *testing.T, fl, expected float64) {
    33  
    34  	got := _ClibFloor64(fl)
    35  	if expected != got {
    36  		t.Errorf("testClibFloor64(): \nexpected %v\ngot      %v", expected, got)
    37  	}
    38  }
    39  
    40  func TestClibFloor(t *testing.T) {
    41  
    42  	testClibFloor32(t, 2.1, 2.0)
    43  	testClibFloor32(t, 1.9, 1.0)
    44  	testClibFloor32(t, 1.5, 1.0)
    45  	testClibFloor32(t, 1.1, 1.0)
    46  	testClibFloor32(t, 1.0, 1.0)
    47  	testClibFloor32(t, 1.0-1e-6, 0.0)
    48  	testClibFloor32(t, 0.0-1e-6, -1.0)
    49  
    50  	testClibFloor64(t, 2.1, 2.0)
    51  	testClibFloor64(t, 1.9, 1.0)
    52  	testClibFloor64(t, 1.5, 1.0)
    53  	testClibFloor64(t, 1.1, 1.0)
    54  	testClibFloor64(t, 1.0, 1.0)
    55  	testClibFloor64(t, 1.0-1e-6, 0.0)
    56  	testClibFloor64(t, 0.0-1e-6, -1.0)
    57  
    58  }
    59  
    60  func TestClibMemcpy(t *testing.T) {
    61  
    62  	src := make([]byte, 256)
    63  	zero := make([]byte, 256)
    64  	dst := make([]byte, 256)
    65  
    66  	for i, _ := range src {
    67  		src[i] = byte(i)
    68  	}
    69  
    70  	for count := 0; count < 256; count++ {
    71  
    72  		copy(dst[:], zero[:])
    73  
    74  		ptr := _ClibMemcpy(unsafe.Pointer(&dst[0]), unsafe.Pointer(&src[0]), uint(count))
    75  		if unsafe.Pointer(&dst[0]) != ptr {
    76  			t.Errorf("TestClibMemcpy(): \nexpected %v\ngot      %v", unsafe.Pointer(&dst[0]), ptr)
    77  		}
    78  
    79  		i := 0
    80  		for ; i < count; i++ {
    81  			if dst[i] != src[i] {
    82  				t.Errorf("TestClibMemcpy(): \nexpected %d\ngot      %d", src[i], dst[i])
    83  			}
    84  		}
    85  		for ; i < len(dst); i++ {
    86  			if dst[i] != 0 {
    87  				t.Errorf("TestClibMemcpy(): \nexpected %d\ngot      %d", 0, dst[i])
    88  			}
    89  		}
    90  	}
    91  }
    92  
    93  func TestClibMemset(t *testing.T) {
    94  
    95  	init := make([]byte, 256)
    96  	dst := make([]byte, 256)
    97  
    98  	for i, _ := range init {
    99  		init[i] = byte(i)
   100  	}
   101  
   102  	for count := 0; count < 256; count++ {
   103  
   104  		copy(dst[:], init[:])
   105  
   106  		ptr := _ClibMemset(unsafe.Pointer(&dst[0]), count, uint(count))
   107  		if unsafe.Pointer(&dst[0]) != ptr {
   108  			t.Errorf("TestClibMemcpy(): \nexpected %v\ngot      %v", unsafe.Pointer(&dst[0]), ptr)
   109  		}
   110  
   111  		i := 0
   112  		for ; i < count; i++ {
   113  			if dst[i] != byte(count) {
   114  				t.Errorf("1-TestClibMemcpy(%d): \nexpected %d\ngot      %d", i, count, dst[i])
   115  			}
   116  		}
   117  		for ; i < len(dst); i++ {
   118  			if dst[i] != init[i] {
   119  				t.Errorf("2-TestClibMemcpy(%d): \nexpected %d\ngot      %d", i, init[i], dst[i])
   120  			}
   121  		}
   122  	}
   123  }