github.com/dolthub/go-mysql-server@v0.18.0/sql/index_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  
     8  	"github.com/shopspring/decimal"
     9  	assert "github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestRoundUpFloat(t *testing.T) {
    13  	tests := []struct {
    14  		val interface{}
    15  		exp interface{}
    16  	}{
    17  		// float 32
    18  		{
    19  			val: float32(-math.MaxFloat32),
    20  			exp: float32(-math.MaxFloat32),
    21  		},
    22  		{
    23  			val: float32(-1.1),
    24  			exp: float32(-1.0),
    25  		},
    26  		{
    27  			val: float32(-0.9),
    28  			exp: float32(0.0),
    29  		},
    30  		{
    31  			val: float32(-0.5),
    32  			exp: float32(0.0),
    33  		},
    34  		{
    35  			val: float32(-0.1),
    36  			exp: float32(0.0),
    37  		},
    38  		{
    39  			val: float32(0.0),
    40  			exp: float32(0.0),
    41  		},
    42  		{
    43  			val: float32(0.1),
    44  			exp: float32(1.0),
    45  		},
    46  		{
    47  			val: float32(0.5),
    48  			exp: float32(1.0),
    49  		},
    50  		{
    51  			val: float32(0.9),
    52  			exp: float32(1.0),
    53  		},
    54  		{
    55  			val: float32(1.1),
    56  			exp: float32(2.0),
    57  		},
    58  		{
    59  			val: float32(math.MaxFloat32),
    60  			exp: float32(math.MaxFloat32),
    61  		},
    62  
    63  		// float64
    64  		{
    65  			val: -math.MaxFloat64,
    66  			exp: -math.MaxFloat64,
    67  		},
    68  		{
    69  			val: -1.1,
    70  			exp: -1.0,
    71  		},
    72  		{
    73  			val: -0.9,
    74  			exp: 0.0,
    75  		},
    76  		{
    77  			val: -0.5,
    78  			exp: 0.0,
    79  		},
    80  		{
    81  			val: -0.1,
    82  			exp: 0.0,
    83  		},
    84  		{
    85  			val: 0.0,
    86  			exp: 0.0,
    87  		},
    88  		{
    89  			val: 0.1,
    90  			exp: 1.0,
    91  		},
    92  		{
    93  			val: 0.5,
    94  			exp: 1.0,
    95  		},
    96  		{
    97  			val: 0.9,
    98  			exp: 1.0,
    99  		},
   100  		{
   101  			val: 1.1,
   102  			exp: 2.0,
   103  		},
   104  		{
   105  			val: math.MaxFloat64,
   106  			exp: math.MaxFloat64,
   107  		},
   108  
   109  		// decimal
   110  		{
   111  			val: decimal.NewFromFloat(-math.MaxFloat64),
   112  			exp: decimal.NewFromFloat(-math.MaxFloat64),
   113  		},
   114  		{
   115  			val: decimal.NewFromFloat(-1.1),
   116  			exp: decimal.NewFromFloat(-1.0),
   117  		},
   118  		{
   119  			val: decimal.NewFromFloat(-0.9),
   120  			exp: decimal.NewFromFloat(0.0),
   121  		},
   122  		{
   123  			val: decimal.NewFromFloat(-0.5),
   124  			exp: decimal.NewFromFloat(0.0),
   125  		},
   126  		{
   127  			val: decimal.NewFromFloat(-0.1),
   128  			exp: decimal.NewFromFloat(0.0),
   129  		},
   130  		{
   131  			val: decimal.NewFromFloat(0.0),
   132  			exp: decimal.NewFromFloat(0.0),
   133  		},
   134  		{
   135  			val: decimal.NewFromFloat(0.1),
   136  			exp: decimal.NewFromFloat(1.0),
   137  		},
   138  		{
   139  			val: decimal.NewFromFloat(0.5),
   140  			exp: decimal.NewFromFloat(1.0),
   141  		},
   142  		{
   143  			val: decimal.NewFromFloat(0.9),
   144  			exp: decimal.NewFromFloat(1.0),
   145  		},
   146  		{
   147  			val: decimal.NewFromFloat(1.1),
   148  			exp: decimal.NewFromFloat(2.0),
   149  		},
   150  		{
   151  			val: decimal.NewFromFloat(math.MaxFloat64),
   152  			exp: decimal.NewFromFloat(math.MaxFloat64),
   153  		},
   154  	}
   155  
   156  	for _, test := range tests {
   157  		t.Run(fmt.Sprintf("round up: %v", test.val), func(t *testing.T) {
   158  			res := ceil(test.val)
   159  			switch test.val.(type) {
   160  			case float32, float64:
   161  				assert.Equal(t, test.exp, res)
   162  			case decimal.Decimal:
   163  				assert.True(t, test.exp.(decimal.Decimal).Equals(res.(decimal.Decimal)))
   164  			}
   165  		})
   166  	}
   167  }
   168  
   169  func TestRoundDownFloat(t *testing.T) {
   170  	tests := []struct {
   171  		val interface{}
   172  		exp interface{}
   173  	}{
   174  		// float 32
   175  		{
   176  			val: float32(-math.MaxFloat32),
   177  			exp: float32(-math.MaxFloat32),
   178  		},
   179  		{
   180  			val: float32(-1.1),
   181  			exp: float32(-2.0),
   182  		},
   183  		{
   184  			val: float32(-0.9),
   185  			exp: float32(-1.0),
   186  		},
   187  		{
   188  			val: float32(-0.5),
   189  			exp: float32(-1.0),
   190  		},
   191  		{
   192  			val: float32(-0.1),
   193  			exp: float32(-1.0),
   194  		},
   195  		{
   196  			val: float32(0.0),
   197  			exp: float32(0.0),
   198  		},
   199  		{
   200  			val: float32(0.1),
   201  			exp: float32(0.0),
   202  		},
   203  		{
   204  			val: float32(0.5),
   205  			exp: float32(0.0),
   206  		},
   207  		{
   208  			val: float32(0.9),
   209  			exp: float32(0.0),
   210  		},
   211  		{
   212  			val: float32(1.1),
   213  			exp: float32(1.0),
   214  		},
   215  		{
   216  			val: float32(math.MaxFloat32),
   217  			exp: float32(math.MaxFloat32),
   218  		},
   219  
   220  		// float64
   221  		{
   222  			val: -math.MaxFloat64,
   223  			exp: -math.MaxFloat64,
   224  		},
   225  		{
   226  			val: -1.1,
   227  			exp: -2.0,
   228  		},
   229  		{
   230  			val: -0.9,
   231  			exp: -1.0,
   232  		},
   233  		{
   234  			val: -0.5,
   235  			exp: -1.0,
   236  		},
   237  		{
   238  			val: -0.1,
   239  			exp: -1.0,
   240  		},
   241  		{
   242  			val: 0.0,
   243  			exp: 0.0,
   244  		},
   245  		{
   246  			val: 0.1,
   247  			exp: 0.0,
   248  		},
   249  		{
   250  			val: 0.5,
   251  			exp: 0.0,
   252  		},
   253  		{
   254  			val: 0.9,
   255  			exp: 0.0,
   256  		},
   257  		{
   258  			val: 1.1,
   259  			exp: 1.0,
   260  		},
   261  		{
   262  			val: math.MaxFloat64,
   263  			exp: math.MaxFloat64,
   264  		},
   265  
   266  		// decimal
   267  		{
   268  			val: decimal.NewFromFloat(-math.MaxFloat64),
   269  			exp: decimal.NewFromFloat(-math.MaxFloat64),
   270  		},
   271  		{
   272  			val: decimal.NewFromFloat(-1.1),
   273  			exp: decimal.NewFromFloat(-2.0),
   274  		},
   275  		{
   276  			val: decimal.NewFromFloat(-0.9),
   277  			exp: decimal.NewFromFloat(-1.0),
   278  		},
   279  		{
   280  			val: decimal.NewFromFloat(-0.5),
   281  			exp: decimal.NewFromFloat(-1.0),
   282  		},
   283  		{
   284  			val: decimal.NewFromFloat(-0.1),
   285  			exp: decimal.NewFromFloat(-1.0),
   286  		},
   287  		{
   288  			val: decimal.NewFromFloat(0.0),
   289  			exp: decimal.NewFromFloat(0.0),
   290  		},
   291  		{
   292  			val: decimal.NewFromFloat(0.1),
   293  			exp: decimal.NewFromFloat(0.0),
   294  		},
   295  		{
   296  			val: decimal.NewFromFloat(0.5),
   297  			exp: decimal.NewFromFloat(0.0),
   298  		},
   299  		{
   300  			val: decimal.NewFromFloat(0.9),
   301  			exp: decimal.NewFromFloat(0.0),
   302  		},
   303  		{
   304  			val: decimal.NewFromFloat(1.1),
   305  			exp: decimal.NewFromFloat(1.0),
   306  		},
   307  		{
   308  			val: decimal.NewFromFloat(math.MaxFloat64),
   309  			exp: decimal.NewFromFloat(math.MaxFloat64),
   310  		},
   311  	}
   312  
   313  	for _, test := range tests {
   314  		t.Run(fmt.Sprintf("round down: %v", test.val), func(t *testing.T) {
   315  			res := floor(test.val)
   316  			switch test.val.(type) {
   317  			case float32, float64:
   318  				assert.Equal(t, test.exp, res)
   319  			case decimal.Decimal:
   320  				assert.True(t, test.exp.(decimal.Decimal).Equals(res.(decimal.Decimal)))
   321  			}
   322  		})
   323  	}
   324  }