Source file src/runtime/conv_wasmx_test.go

     1  // Copyright 2020 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  //go:build wasm || wasm32
     6  
     7  package runtime_test
     8  
     9  import (
    10  	"testing"
    11  )
    12  
    13  var res int64
    14  var ures uint64
    15  
    16  // TODO: This test probably should be in a different place.
    17  
    18  func TestFloatTruncation(t *testing.T) {
    19  	testdata := []struct {
    20  		input      float64
    21  		convInt64  int64
    22  		convUInt64 uint64
    23  		overflow   bool
    24  	}{
    25  		// max +- 1
    26  		{
    27  			input:      0x7fffffffffffffff,
    28  			convInt64:  0x7fffffffffffffff,
    29  			convUInt64: 0x8000000000000000,
    30  		},
    31  		// For out-of-bounds conversion, the result is implementation-dependent.
    32  		// This test verifies the implementation of wasm architecture, which is,
    33  		// saturating to the min/max value.
    34  		{
    35  			input:      0x8000000000000000,
    36  			convInt64:  0x7fffffffffffffff,
    37  			convUInt64: 0x8000000000000000,
    38  		},
    39  		{
    40  			input:      0x7ffffffffffffffe,
    41  			convInt64:  0x7fffffffffffffff,
    42  			convUInt64: 0x8000000000000000,
    43  		},
    44  		// neg max +- 1
    45  		{
    46  			input:      -0x8000000000000000,
    47  			convInt64:  -0x8000000000000000,
    48  			convUInt64: 0,
    49  		},
    50  		{
    51  			input:      -0x8000000000000001,
    52  			convInt64:  -0x8000000000000000,
    53  			convUInt64: 0,
    54  		},
    55  		{
    56  			input:      -0x7fffffffffffffff,
    57  			convInt64:  -0x8000000000000000,
    58  			convUInt64: 0,
    59  		},
    60  		// trunc point +- 1
    61  		{
    62  			input:      0x7ffffffffffffdff,
    63  			convInt64:  0x7ffffffffffffc00,
    64  			convUInt64: 0x7ffffffffffffc00,
    65  		},
    66  		{
    67  			input:      0x7ffffffffffffe00,
    68  			convInt64:  0x7fffffffffffffff,
    69  			convUInt64: 0x8000000000000000,
    70  		},
    71  		{
    72  			input:      0x7ffffffffffffdfe,
    73  			convInt64:  0x7ffffffffffffc00,
    74  			convUInt64: 0x7ffffffffffffc00,
    75  		},
    76  		// neg trunc point +- 1
    77  		{
    78  			input:      -0x7ffffffffffffdff,
    79  			convInt64:  -0x7ffffffffffffc00,
    80  			convUInt64: 0,
    81  		},
    82  		{
    83  			input:      -0x7ffffffffffffe00,
    84  			convInt64:  -0x8000000000000000,
    85  			convUInt64: 0,
    86  		},
    87  		{
    88  			input:      -0x7ffffffffffffdfe,
    89  			convInt64:  -0x7ffffffffffffc00,
    90  			convUInt64: 0,
    91  		},
    92  		// umax +- 1
    93  		{
    94  			input:      0xffffffffffffffff,
    95  			convInt64:  0x7fffffffffffffff,
    96  			convUInt64: 0xffffffffffffffff,
    97  		},
    98  		{
    99  			input:      0x10000000000000000,
   100  			convInt64:  0x7fffffffffffffff,
   101  			convUInt64: 0xffffffffffffffff,
   102  		},
   103  		{
   104  			input:      0xfffffffffffffffe,
   105  			convInt64:  0x7fffffffffffffff,
   106  			convUInt64: 0xffffffffffffffff,
   107  		},
   108  		// umax trunc +- 1
   109  		{
   110  			input:      0xfffffffffffffbff,
   111  			convInt64:  0x7fffffffffffffff,
   112  			convUInt64: 0xfffffffffffff800,
   113  		},
   114  		{
   115  			input:      0xfffffffffffffc00,
   116  			convInt64:  0x7fffffffffffffff,
   117  			convUInt64: 0xffffffffffffffff,
   118  		},
   119  		{
   120  			input:      0xfffffffffffffbfe,
   121  			convInt64:  0x7fffffffffffffff,
   122  			convUInt64: 0xfffffffffffff800,
   123  		},
   124  	}
   125  	for _, item := range testdata {
   126  		if got, want := int64(item.input), item.convInt64; got != want {
   127  			t.Errorf("int64(%f): got %x, want %x", item.input, got, want)
   128  		}
   129  		if got, want := uint64(item.input), item.convUInt64; got != want {
   130  			t.Errorf("uint64(%f): got %x, want %x", item.input, got, want)
   131  		}
   132  	}
   133  }
   134  

View as plain text