github.com/matrixorigin/matrixone@v1.2.0/pkg/common/assertx/float64_test.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package assertx
    16  
    17  import "testing"
    18  
    19  func TestInEpsilonF64(t *testing.T) {
    20  	type args struct {
    21  		want float64
    22  		got  float64
    23  	}
    24  	tests := []struct {
    25  		name string
    26  		args args
    27  		want bool
    28  	}{
    29  
    30  		{
    31  			name: "Test 1 - difference is epsilon",
    32  			args: args{
    33  				want: 2.0,
    34  				got:  2.0 + float64EqualityThreshold,
    35  			},
    36  			want: false,
    37  		},
    38  		{
    39  			name: "Test 2.a - difference is less than epsilon",
    40  			args: args{
    41  				want: 2.0,
    42  				got:  2.0 + 0.5*float64EqualityThreshold,
    43  			},
    44  			want: true,
    45  		},
    46  		{
    47  			name: "Test 2.b - difference is more than epsilon",
    48  			args: args{
    49  				want: 2.0,
    50  				got:  2.0 + 2*float64EqualityThreshold,
    51  			},
    52  			want: false,
    53  		},
    54  		{
    55  			name: "Test 2.c - difference is -ve of epsilon",
    56  			args: args{
    57  				want: 2.0,
    58  				got:  2.0 - float64EqualityThreshold,
    59  			},
    60  			want: false,
    61  		},
    62  		{
    63  			name: "Test 3 - till 9th digit is same, 10th digit is different",
    64  			args: args{
    65  				want: 1.732050800_0,
    66  				got:  1.732050800_9,
    67  			},
    68  			want: true,
    69  		},
    70  		{
    71  			name: "Test 4 - 9th digit is different",
    72  			args: args{
    73  				want: 1.73205080_0,
    74  				got:  1.73205080_9,
    75  			},
    76  			want: false,
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			if got := InEpsilonF64(tt.args.want, tt.args.got); got != tt.want {
    82  				t.Errorf("%s InEpsilonF64() = %v, want %v", tt.name, got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestInEpsilonF64Slice(t *testing.T) {
    89  	type args struct {
    90  		want []float64
    91  		got  []float64
    92  	}
    93  	tests := []struct {
    94  		name string
    95  		args args
    96  		want bool
    97  	}{
    98  		{
    99  			name: "Test 1 - difference is epsilon",
   100  			args: args{
   101  				want: []float64{2.0, 3.0},
   102  				got:  []float64{2.0 + float64EqualityThreshold, 3.0 + float64EqualityThreshold},
   103  			},
   104  			want: false,
   105  		},
   106  		{
   107  			name: "Test 2 - till 9th digit is same, 10th digit is different)",
   108  			args: args{
   109  				want: []float64{1.732050800_0},
   110  				got:  []float64{1.732050800_9},
   111  			},
   112  			want: true,
   113  		},
   114  		{
   115  			name: "Test 3 - 9th digit is different",
   116  			args: args{
   117  				want: []float64{1.73205080_0},
   118  				got:  []float64{1.73205080_9},
   119  			},
   120  			want: false,
   121  		},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			if got := InEpsilonF64Slice(tt.args.want, tt.args.got); got != tt.want {
   126  				t.Errorf("InEpsilonF64Slice() = %v, want %v", got, tt.want)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestInEpsilonF64Slices(t *testing.T) {
   133  	type args struct {
   134  		want [][]float64
   135  		got  [][]float64
   136  	}
   137  	tests := []struct {
   138  		name string
   139  		args args
   140  		want bool
   141  	}{
   142  		{
   143  			name: "Test 1 - difference is epsilon",
   144  			args: args{
   145  				want: [][]float64{{2.0, 3.0}, {4.0}},
   146  				got:  [][]float64{{2.0 + float64EqualityThreshold, 3.0 + float64EqualityThreshold}, {4.0}},
   147  			},
   148  			want: false,
   149  		},
   150  		{
   151  			name: "Test 2 - difference is less than epsilon (next neg-power of epsilon & epsilon/2)",
   152  			args: args{
   153  				want: [][]float64{{2.0, 3.0}, {4.0}},
   154  				got:  [][]float64{{2.0 + 1e-1*float64EqualityThreshold, 3.0 + float64EqualityThreshold/2}, {4.0}},
   155  			},
   156  			want: true,
   157  		},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			if got := InEpsilonF64Slices(tt.args.want, tt.args.got); got != tt.want {
   162  				t.Errorf("InEpsilonF64Slices() = %v, want %v", got, tt.want)
   163  			}
   164  		})
   165  	}
   166  }