github.com/apache/arrow/go/v16@v16.1.0/arrow/float16/float16_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package float16
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestFloat16(t *testing.T) {
    27  	cases := map[Num]float32{
    28  		{bits: 0x3c00}: 1,
    29  		{bits: 0x4000}: 2,
    30  		{bits: 0xc000}: -2,
    31  		{bits: 0x0000}: 0,
    32  		{bits: 0x5b8f}: 241.875,
    33  		{bits: 0xdb8f}: -241.875,
    34  		{bits: 0x48c8}: 9.5625,
    35  		{bits: 0xc8c8}: -9.5625,
    36  	}
    37  	for k, v := range cases {
    38  		f := k.Float32()
    39  		assert.Equal(t, v, f, "float32 values should be the same")
    40  		i := New(v)
    41  		assert.Equal(t, k.bits, i.bits, "float16 values should be the same")
    42  		assert.Equal(t, k.Uint16(), i.Uint16(), "float16 values should be the same")
    43  		assert.Equal(t, k.String(), fmt.Sprintf("%v", v), "string representation differ")
    44  	}
    45  }
    46  
    47  func TestAdd(t *testing.T) {
    48  	for _, tc := range []struct {
    49  		n    Num
    50  		rhs  Num
    51  		want Num
    52  	}{
    53  		{Num{bits: 0x0000}, Num{bits: 0x0000}, Num{bits: 0x0000}}, // 0 + 0 = 0
    54  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, Num{bits: 0x4200}}, // 1 + 2 = 3
    55  		{Num{bits: 0x4248}, Num{bits: 0x3245}, Num{bits: 0x42AC}}, // 3.141 + 0.196 = 3.336
    56  	} {
    57  		t.Run("add", func(t *testing.T) {
    58  			n := tc.n.Add(tc.rhs)
    59  			if got, want := n, tc.want; got != want {
    60  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestSub(t *testing.T) {
    67  	for _, tc := range []struct {
    68  		n    Num
    69  		rhs  Num
    70  		want Num
    71  	}{
    72  		{Num{bits: 0x0000}, Num{bits: 0x0000}, Num{bits: 0x0000}}, // 0 - 0 = 0
    73  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, Num{bits: 0xBC00}}, // 1 - 2 = -1
    74  		{Num{bits: 0x4248}, Num{bits: 0x3245}, Num{bits: 0x41E3}}, // 3.141 - 0.196 = 2.944
    75  	} {
    76  		t.Run("sub", func(t *testing.T) {
    77  			n := tc.n.Sub(tc.rhs)
    78  			if got, want := n, tc.want; got != want {
    79  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestMul(t *testing.T) {
    86  	for _, tc := range []struct {
    87  		n    Num
    88  		rhs  Num
    89  		want Num
    90  	}{
    91  		{Num{bits: 0x0000}, Num{bits: 0x0000}, Num{bits: 0x0000}}, // 0 * 0 = 0
    92  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, Num{bits: 0x4000}}, // 1 * 2 = 2
    93  		{Num{bits: 0x4248}, Num{bits: 0x3245}, Num{bits: 0x38EC}}, // 3.141 * 0.196 = 0.6153
    94  	} {
    95  		t.Run("mul", func(t *testing.T) {
    96  			n := tc.n.Mul(tc.rhs)
    97  			if got, want := n, tc.want; got != want {
    98  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  func TestDiv(t *testing.T) {
   105  	for _, tc := range []struct {
   106  		n    Num
   107  		rhs  Num
   108  		want Num
   109  	}{
   110  		{Num{bits: 0x0000}, Num{bits: 0x3c00}, Num{bits: 0x0000}}, // 0 / 1 = 0
   111  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, Num{bits: 0x3800}}, // 1 / 2 = 0.5
   112  		{Num{bits: 0x4248}, Num{bits: 0x3245}, Num{bits: 0x4C01}}, // 3.141 * 0.196 = 16.02
   113  	} {
   114  		t.Run("div", func(t *testing.T) {
   115  			n := tc.n.Div(tc.rhs)
   116  			if got, want := n, tc.want; got != want {
   117  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func TestGreater(t *testing.T) {
   124  	for _, tc := range []struct {
   125  		n    Num
   126  		rhs  Num
   127  		want bool
   128  	}{
   129  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, false}, // 1 > 2 = false
   130  		{Num{bits: 0x4900}, Num{bits: 0x4900}, false}, // 10 == 10 = false
   131  		{Num{bits: 0x4248}, Num{bits: 0x3245}, true},  // 3.141 > 0.196 = true
   132  	} {
   133  		t.Run("greater", func(t *testing.T) {
   134  			n := tc.n.Greater(tc.rhs)
   135  			if got, want := n, tc.want; got != want {
   136  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func TestLess(t *testing.T) {
   143  	for _, tc := range []struct {
   144  		n    Num
   145  		rhs  Num
   146  		want bool
   147  	}{
   148  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, true},  // 1 < 2 = true
   149  		{Num{bits: 0x4900}, Num{bits: 0x4900}, false}, // 10 == 10 = false
   150  		{Num{bits: 0x4248}, Num{bits: 0x3245}, false}, // 3.141 < 0.196 = false
   151  	} {
   152  		t.Run("less", func(t *testing.T) {
   153  			n := tc.n.Less(tc.rhs)
   154  			if got, want := n, tc.want; got != want {
   155  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func TestCmp(t *testing.T) {
   162  	for _, tc := range []struct {
   163  		n    Num
   164  		rhs  Num
   165  		want int
   166  	}{
   167  		{Num{bits: 0x3c00}, Num{bits: 0x4000}, -1}, // cmp(1, 2) = -1
   168  		{Num{bits: 0x4900}, Num{bits: 0x4900}, 0},  // cmp(10, 10) = 0
   169  		{Num{bits: 0x4248}, Num{bits: 0x3245}, 1},  // cmp(3.141, 0.196) = 1
   170  	} {
   171  		t.Run("cmp", func(t *testing.T) {
   172  			n := tc.n.Cmp(tc.rhs)
   173  			if got, want := n, tc.want; got != want {
   174  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   175  			}
   176  		})
   177  	}
   178  }
   179  
   180  func TestMax(t *testing.T) {
   181  	for _, tc := range []struct {
   182  		n    Num
   183  		rhs  []Num
   184  		want Num
   185  	}{
   186  		{Num{bits: 0x3c00}, []Num{{bits: 0x4000}, {bits: 0x4580}, {bits: 0x3C00}, {bits: 0x4247}}, Num{bits: 0x4580}}, // max(2, 5.5, 1, 3.14) = 5.5
   187  		{Num{bits: 0x4248}, []Num{{bits: 0xC000}, {bits: 0xC580}, {bits: 0x3C00}, {bits: 0x4247}}, Num{bits: 0x4248}}, // max(-2, -5.5, 1, 3.14) = 3.14
   188  	} {
   189  		t.Run("max", func(t *testing.T) {
   190  			n := Max(tc.n, tc.rhs...)
   191  			if got, want := n, tc.want; got != want {
   192  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   193  			}
   194  		})
   195  	}
   196  }
   197  
   198  func TestMin(t *testing.T) {
   199  	for _, tc := range []struct {
   200  		n    Num
   201  		rhs  []Num
   202  		want Num
   203  	}{
   204  		{Num{bits: 0x3c00}, []Num{{bits: 0x4000}, {bits: 0x4580}, {bits: 0x3C00}, {bits: 0x4247}}, Num{bits: 0x3C00}}, // min(2, 5.5, 1, 3.14) = 1
   205  		{Num{bits: 0x4248}, []Num{{bits: 0x4000}, {bits: 0xC580}, {bits: 0xBC00}, {bits: 0x4247}}, Num{bits: 0xC580}}, // min(2, -5.5, -1, 3.14) = -5.5
   206  	} {
   207  		t.Run("min", func(t *testing.T) {
   208  			n := Min(tc.n, tc.rhs...)
   209  			if got, want := n, tc.want; got != want {
   210  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   211  			}
   212  		})
   213  	}
   214  }
   215  
   216  func TestAbs(t *testing.T) {
   217  	for _, tc := range []struct {
   218  		n    Num
   219  		want Num
   220  	}{
   221  		{Num{bits: 0x4580}, Num{bits: 0x4580}}, // 5.5
   222  		{Num{bits: 0x0000}, Num{bits: 0x0000}}, // 0
   223  		{Num{bits: 0xC580}, Num{bits: 0x4580}}, // -5.5
   224  	} {
   225  		t.Run("abs", func(t *testing.T) {
   226  			n := tc.n.Abs()
   227  			if got, want := n, tc.want; got != want {
   228  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   229  			}
   230  		})
   231  	}
   232  }
   233  
   234  func TestSign(t *testing.T) {
   235  	for _, tc := range []struct {
   236  		n    Num
   237  		want int
   238  	}{
   239  		{Num{bits: 0x4580}, 1},  // 5.5
   240  		{Num{bits: 0x0000}, 0},  // 0
   241  		{Num{bits: 0x8000}, 0},  // -0
   242  		{Num{bits: 0xC580}, -1}, // -5.5
   243  	} {
   244  		t.Run("sign", func(t *testing.T) {
   245  			n := tc.n.Sign()
   246  			if got, want := n, tc.want; got != want {
   247  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   248  			}
   249  		})
   250  	}
   251  }
   252  
   253  func TestSignbit(t *testing.T) {
   254  	for _, tc := range []struct {
   255  		n    Num
   256  		want bool
   257  	}{
   258  		{Num{bits: 0x4580}, false}, // 5.5
   259  		{Num{bits: 0x0000}, false}, // 0
   260  		{Num{bits: 0x8000}, true},  // -0
   261  		{Num{bits: 0xC580}, true},  // -5.5
   262  	} {
   263  		t.Run("signbit", func(t *testing.T) {
   264  			n := tc.n.Signbit()
   265  			if got, want := n, tc.want; got != want {
   266  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   267  			}
   268  		})
   269  	}
   270  }
   271  
   272  func TestIsNaN(t *testing.T) {
   273  	for _, tc := range []struct {
   274  		n    Num
   275  		want bool
   276  	}{
   277  		{NaN(), true},
   278  		{NaN().Negate(), true},
   279  		{Inf(), false},
   280  		{Inf().Negate(), false},
   281  		{Num{bits: 0x7c01}, true}, // nan
   282  		{Num{bits: 0xfc01}, true}, // -nan
   283  		{Num{bits: 0x7e00}, true}, // nan
   284  		{Num{bits: 0xfe00}, true}, // -nan
   285  	} {
   286  		t.Run("isnan", func(t *testing.T) {
   287  			n := tc.n.IsNaN()
   288  			if got, want := n, tc.want; got != want {
   289  				t.Fatalf("invalid value. got=%v, want=%v", got, want)
   290  			}
   291  		})
   292  	}
   293  }