github.com/blend/go-sdk@v1.20220411.3/validate/float32_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestFloat32Min(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	var verr error
    20  	var val float32 = 10.0
    21  	verr = Float32(&val).Min(1)()
    22  	assert.Nil(verr)
    23  
    24  	val = 10.0
    25  	verr = Float32(&val).Min(10)()
    26  	assert.Nil(verr)
    27  
    28  	verr = Float32(nil).Min(10)()
    29  	assert.NotNil(verr)
    30  	assert.Nil(ErrValue(verr))
    31  	assert.Equal(ErrFloat32Min, ErrCause(verr))
    32  
    33  	val = 1.0
    34  	verr = Float32(&val).Min(10)()
    35  	assert.NotNil(verr)
    36  	assert.NotNil(ErrValue(verr))
    37  	assert.Equal(ErrFloat32Min, ErrCause(verr))
    38  
    39  	val = 10.0
    40  	verr = Float32(&val).Min(10)()
    41  	assert.Nil(verr)
    42  }
    43  
    44  func TestFloat32Max(t *testing.T) {
    45  	assert := assert.New(t)
    46  
    47  	var verr error
    48  	var val float32 = 1.0
    49  	verr = Float32(&val).Max(10)()
    50  	assert.Nil(verr)
    51  
    52  	verr = Float32(nil).Max(10)()
    53  	assert.Nil(verr)
    54  
    55  	val = 10.0
    56  	verr = Float32(&val).Max(10)()
    57  	assert.Nil(verr)
    58  
    59  	val = 11.0
    60  	verr = Float32(&val).Max(10)()
    61  	assert.NotNil(verr)
    62  	assert.NotNil(ErrValue(verr))
    63  	assert.Equal(ErrFloat32Max, ErrCause(verr))
    64  }
    65  
    66  func TestFloat32Between(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	var verr error
    70  	var val float32 = 5.0
    71  	verr = Float32(&val).Between(1, 10)()
    72  	assert.Nil(verr)
    73  
    74  	verr = Float32(nil).Between(5, 10)()
    75  	assert.NotNil(verr)
    76  	assert.Nil(ErrValue(verr))
    77  	assert.Equal(ErrFloat32Min, ErrCause(verr))
    78  
    79  	val = 1.0
    80  	verr = Float32(&val).Between(5, 10)()
    81  	assert.NotNil(verr)
    82  	assert.Equal(1, ErrValue(verr))
    83  	assert.Equal(ErrFloat32Min, ErrCause(verr))
    84  
    85  	val = 5.0
    86  	verr = Float32(&val).Between(5, 10)()
    87  	assert.Nil(verr)
    88  
    89  	val = 10.0
    90  	verr = Float32(&val).Between(5, 10)()
    91  	assert.Nil(verr)
    92  
    93  	val = 11.0
    94  	verr = Float32(&val).Between(5, 10)()
    95  	assert.NotNil(verr)
    96  	assert.Equal(11, ErrValue(verr))
    97  	assert.Equal(ErrFloat32Max, ErrCause(verr))
    98  }
    99  
   100  func TestFloat32Positive(t *testing.T) {
   101  	assert := assert.New(t)
   102  
   103  	var verr error
   104  	var val float32 = 5.0
   105  	verr = Float32(&val).Positive()()
   106  	assert.Nil(verr)
   107  
   108  	verr = Float32(nil).Positive()()
   109  	assert.NotNil(verr)
   110  	assert.Nil(ErrValue(verr))
   111  	assert.Equal(ErrFloat32Positive, ErrCause(verr))
   112  
   113  	val = -5.0
   114  	verr = Float32(&val).Positive()()
   115  	assert.NotNil(verr)
   116  	assert.NotNil(ErrValue(verr))
   117  	assert.Equal(ErrFloat32Positive, ErrCause(verr))
   118  }
   119  
   120  func TestFloat32Negative(t *testing.T) {
   121  	assert := assert.New(t)
   122  
   123  	var verr error
   124  	var val float32 = -5.0
   125  	verr = Float32(&val).Negative()()
   126  	assert.Nil(verr)
   127  
   128  	verr = Float32(nil).Negative()()
   129  	assert.NotNil(verr)
   130  	assert.Nil(ErrValue(verr))
   131  	assert.Equal(ErrFloat32Negative, ErrCause(verr))
   132  
   133  	val = 5.0
   134  	verr = Float32(&val).Negative()()
   135  	assert.NotNil(verr)
   136  	assert.NotNil(ErrValue(verr))
   137  	assert.Equal(ErrFloat32Negative, ErrCause(verr))
   138  }
   139  
   140  func TestFloat32Epsilon(t *testing.T) {
   141  	assert := assert.New(t)
   142  
   143  	var verr error
   144  	var val float32 = 5.0
   145  	verr = Float32(&val).Epsilon(4.999999, DefaultEpsilon)()
   146  	assert.Nil(verr)
   147  
   148  	verr = Float32(nil).Epsilon(4.999999, DefaultEpsilon)()
   149  	assert.NotNil(verr)
   150  	assert.Nil(ErrValue(verr))
   151  	assert.Equal(ErrFloat32Epsilon, ErrCause(verr))
   152  
   153  	verr = Float32(&val).Epsilon(4.99, DefaultEpsilon)()
   154  	assert.NotNil(verr)
   155  	assert.Equal(5.0, ErrValue(verr))
   156  	assert.Equal(ErrFloat32Epsilon, ErrCause(verr))
   157  }
   158  
   159  func TestFloat32Zero(t *testing.T) {
   160  	assert := assert.New(t)
   161  
   162  	var verr error
   163  	var val float32 = 0.0
   164  	verr = Float32(&val).Zero()()
   165  	assert.Nil(verr)
   166  
   167  	verr = Float32(nil).Zero()()
   168  	assert.NotNil(verr)
   169  	assert.Nil(ErrValue(verr))
   170  	assert.Equal(ErrFloat32Zero, ErrCause(verr))
   171  
   172  	val = 5.0
   173  	verr = Float32(&val).Zero()()
   174  	assert.NotNil(verr)
   175  	assert.NotNil(ErrValue(verr))
   176  	assert.Equal(ErrFloat32Zero, ErrCause(verr))
   177  }
   178  
   179  func TestFloat32NotZero(t *testing.T) {
   180  	assert := assert.New(t)
   181  
   182  	var verr error
   183  	var val float32 = 5.0
   184  	verr = Float32(&val).NotZero()()
   185  	assert.Nil(verr)
   186  
   187  	verr = Float32(nil).NotZero()()
   188  	assert.NotNil(verr)
   189  	assert.Nil(ErrValue(verr))
   190  	assert.Equal(ErrFloat32NotZero, ErrCause(verr))
   191  
   192  	val = 0.0
   193  	verr = Float32(&val).NotZero()()
   194  	assert.NotNil(verr)
   195  	assert.NotNil(ErrValue(verr))
   196  	assert.Equal(ErrFloat32NotZero, ErrCause(verr))
   197  }