gonum.org/v1/gonum@v0.14.0/num/quat/abs_test.go (about)

     1  // Copyright ©2018 The Gonum 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 quat
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  )
    11  
    12  var absTests = []struct {
    13  	q    Number
    14  	want float64
    15  }{
    16  	{q: Number{}, want: 0},
    17  	{q: NaN(), want: nan},
    18  	{q: Inf(), want: inf},
    19  	{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}, want: 2},
    20  	{q: Number{Real: -1, Imag: 1, Jmag: -1, Kmag: 1}, want: 2},
    21  	{q: Number{Real: 1, Imag: 2, Jmag: 3, Kmag: 4}, want: math.Sqrt(1 + 4 + 9 + 16)},
    22  	{q: Number{Real: -1, Imag: -2, Jmag: -3, Kmag: -4}, want: math.Sqrt(1 + 4 + 9 + 16)},
    23  }
    24  
    25  func TestAbs(t *testing.T) {
    26  	t.Parallel()
    27  	for _, test := range absTests {
    28  		got := Abs(test.q)
    29  		if math.IsNaN(test.want) {
    30  			if !math.IsNaN(got) {
    31  				t.Errorf("unexpected result for Abs(%v): got:%v want:%v", test.q, got, test.want)
    32  			}
    33  			continue
    34  		}
    35  		if got != test.want {
    36  			t.Errorf("unexpected result for Abs(%v): got:%v want:%v", test.q, got, test.want)
    37  		}
    38  	}
    39  }