github.com/richardwilkes/toolbox@v1.121.0/xmath/num/int128_test.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package num_test
    11  
    12  import (
    13  	"encoding/json"
    14  	"math"
    15  	"math/big"
    16  	"strconv"
    17  	"testing"
    18  
    19  	"github.com/richardwilkes/toolbox/check"
    20  	"gopkg.in/yaml.v3"
    21  
    22  	"github.com/richardwilkes/toolbox/xmath/num"
    23  )
    24  
    25  const (
    26  	indexFmt               = "index %d"
    27  	maxInt64PlusOneAsStr   = "9223372036854775808"
    28  	minInt64MinusOneAsStr  = "-9223372036854775809"
    29  	maxInt128AsStr         = "170141183460469231731687303715884105727"
    30  	maxInt128PlusOneAsStr  = "170141183460469231731687303715884105728"
    31  	minInt128AsStr         = "-170141183460469231731687303715884105728"
    32  	minInt128MinusOneAsStr = "-170141183460469231731687303715884105729"
    33  )
    34  
    35  var table = []*info{
    36  	{
    37  		IsInt64:  true,
    38  		IsInt128: true,
    39  	},
    40  	{
    41  		Int64:    -1,
    42  		IsInt64:  true,
    43  		IsInt128: true,
    44  	},
    45  	{
    46  		Int64:    1,
    47  		IsInt64:  true,
    48  		IsInt128: true,
    49  	},
    50  	{
    51  		ValueAsStr: "18446744073712590000",
    52  		IsInt128:   true,
    53  	},
    54  	{
    55  		ValueAsStr: "-18446744073712590000",
    56  		IsInt128:   true,
    57  	},
    58  	{
    59  		Int64:    math.MaxInt64,
    60  		IsInt64:  true,
    61  		IsInt128: true,
    62  	},
    63  	{
    64  		Int64:    math.MinInt64,
    65  		IsInt64:  true,
    66  		IsInt128: true,
    67  	},
    68  	{
    69  		ValueAsStr: maxInt64PlusOneAsStr,
    70  		IsInt64:    false,
    71  		IsInt128:   true,
    72  	},
    73  	{
    74  		ValueAsStr: minInt64MinusOneAsStr,
    75  		IsInt64:    false,
    76  		IsInt128:   true,
    77  	},
    78  	{
    79  		ValueAsStr: maxInt128AsStr,
    80  		IsInt64:    false,
    81  		IsInt128:   true,
    82  	},
    83  	{
    84  		ValueAsStr: minInt128AsStr,
    85  		IsInt64:    false,
    86  		IsInt128:   true,
    87  	},
    88  	{
    89  		ValueAsStr:              maxInt128PlusOneAsStr,
    90  		ExpectedConversionAsStr: maxInt128AsStr,
    91  		IsInt64:                 false,
    92  		IsInt128:                false,
    93  	},
    94  	{
    95  		ValueAsStr:              minInt128MinusOneAsStr,
    96  		ExpectedConversionAsStr: minInt128AsStr,
    97  		IsInt64:                 false,
    98  		IsInt128:                false,
    99  	},
   100  }
   101  
   102  type info struct {
   103  	ValueAsStr              string
   104  	ExpectedConversionAsStr string
   105  	Int64                   int64
   106  	IsInt64                 bool
   107  	IsInt128                bool
   108  }
   109  
   110  func init() {
   111  	for _, d := range table {
   112  		if d.IsInt64 {
   113  			d.ValueAsStr = strconv.FormatInt(d.Int64, 10)
   114  		}
   115  		if d.ExpectedConversionAsStr == "" {
   116  			d.ExpectedConversionAsStr = d.ValueAsStr
   117  		}
   118  	}
   119  }
   120  
   121  func bigIntFromStr(t *testing.T, one *info, index int) *big.Int {
   122  	t.Helper()
   123  	b, ok := new(big.Int).SetString(one.ValueAsStr, 10)
   124  	check.True(t, ok, indexFmt, index)
   125  	check.Equal(t, one.ValueAsStr, b.String(), indexFmt, index)
   126  	return b
   127  }
   128  
   129  func TestInt128FromInt64(t *testing.T) {
   130  	for i, one := range table {
   131  		if one.IsInt64 {
   132  			check.Equal(t, one.ExpectedConversionAsStr, num.Int128From64(one.Int64).String(), indexFmt, i)
   133  		}
   134  	}
   135  }
   136  
   137  func TestInt128FromBigInt(t *testing.T) {
   138  	for i, one := range table {
   139  		check.Equal(t, one.ExpectedConversionAsStr, num.Int128FromBigInt(bigIntFromStr(t, one, i)).String(), indexFmt, i)
   140  	}
   141  }
   142  
   143  func TestInt128AsBigInt(t *testing.T) {
   144  	for i, one := range table {
   145  		if one.IsInt128 {
   146  			check.Equal(t, one.ValueAsStr, num.Int128FromBigInt(bigIntFromStr(t, one, i)).AsBigInt().String(), indexFmt, i)
   147  		}
   148  	}
   149  }
   150  
   151  func TestInt128AsInt64(t *testing.T) {
   152  	for i, one := range table {
   153  		if one.IsInt64 {
   154  			check.Equal(t, one.Int64, num.Int128From64(one.Int64).AsInt64(), indexFmt, i)
   155  		}
   156  	}
   157  }
   158  
   159  func TestInt128IsInt64(t *testing.T) {
   160  	for i, one := range table {
   161  		if one.IsInt128 {
   162  			check.Equal(t, one.IsInt64, num.Int128FromBigInt(bigIntFromStr(t, one, i)).IsInt64(), indexFmt, i)
   163  		}
   164  	}
   165  }
   166  
   167  func TestInt128Sign(t *testing.T) {
   168  	for i, one := range table {
   169  		if one.IsInt128 {
   170  			var sign int
   171  			switch {
   172  			case one.ValueAsStr[0] == '-':
   173  				sign = -1
   174  			case one.ValueAsStr == "0":
   175  				sign = 0
   176  			default:
   177  				sign = 1
   178  			}
   179  			check.Equal(t, sign, num.Int128FromBigInt(bigIntFromStr(t, one, i)).Sign(), indexFmt, i)
   180  		}
   181  	}
   182  }
   183  
   184  func TestInt128Inc(t *testing.T) {
   185  	big1 := new(big.Int).SetInt64(1)
   186  	for i, one := range table {
   187  		if one.IsInt128 {
   188  			b := bigIntFromStr(t, one, i)
   189  			v := num.Int128FromBigInt(b)
   190  			if v == num.MaxInt128 {
   191  				check.Equal(t, num.MinInt128, v.Inc(), indexFmt, i)
   192  			} else {
   193  				b.Add(b, big1)
   194  				check.Equal(t, b.String(), v.Inc().AsBigInt().String(), indexFmt, i)
   195  			}
   196  		}
   197  	}
   198  }
   199  
   200  func TestInt128Dec(t *testing.T) {
   201  	big1 := new(big.Int).SetInt64(1)
   202  	for i, one := range table {
   203  		if one.IsInt128 {
   204  			b := bigIntFromStr(t, one, i)
   205  			v := num.Int128FromBigInt(b)
   206  			if v == num.MinInt128 {
   207  				check.Equal(t, num.MaxInt128, v.Dec(), indexFmt, i)
   208  			} else {
   209  				b.Sub(b, big1)
   210  				check.Equal(t, b.String(), v.Dec().AsBigInt().String(), indexFmt, i)
   211  			}
   212  		}
   213  	}
   214  }
   215  
   216  func TestInt128Add(t *testing.T) {
   217  	check.Equal(t, num.Int128From64(0), num.Int128From64(0).Add(num.Int128From64(0)))
   218  	check.Equal(t, num.Int128From64(-3), num.Int128From64(-2).Add(num.Int128From64(-1)))
   219  	check.Equal(t, num.Int128From64(0), num.Int128From64(1).Add(num.Int128From64(-1)))
   220  	check.Equal(t, num.Int128From64(-1), num.Int128From64(-2).Add(num.Int128From64(1)))
   221  	check.Equal(t, num.Int128From64(120), num.Int128From64(22).Add(num.Int128From64(98)))
   222  	check.Equal(t, num.Int128FromComponents(1, 0), num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).Add(num.Int128From64(1)))
   223  	check.Equal(t, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF), num.Int128FromComponents(1, 0).Add(num.Int128From64(-1)))
   224  	check.Equal(t, num.MinInt128, num.MaxInt128.Add(num.Int128From64(1)))
   225  }
   226  
   227  func TestInt128Sub(t *testing.T) {
   228  	check.Equal(t, num.Int128From64(0), num.Int128From64(0).Sub(num.Int128From64(0)))
   229  	check.Equal(t, num.Int128From64(-1), num.Int128From64(-2).Sub(num.Int128From64(-1)))
   230  	check.Equal(t, num.Int128From64(-1), num.Int128From64(1).Sub(num.Int128From64(2)))
   231  	check.Equal(t, num.Int128From64(1), num.Int128From64(-1).Sub(num.Int128From64(-2)))
   232  	check.Equal(t, num.Int128From64(2), num.Int128From64(1).Sub(num.Int128From64(-1)))
   233  	check.Equal(t, num.Int128From64(-2), num.Int128From64(-1).Sub(num.Int128From64(1)))
   234  	check.Equal(t, num.Int128From64(-3), num.Int128From64(-2).Sub(num.Int128From64(1)))
   235  	check.Equal(t, num.Int128From64(-76), num.Int128From64(22).Sub(num.Int128From64(98)))
   236  	check.Equal(t, num.Int128FromComponents(1, 0), num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).Sub(num.Int128From64(-1)))
   237  	check.Equal(t, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF), num.Int128FromComponents(1, 0).Sub(num.Int128From64(1)))
   238  	check.Equal(t, num.MaxInt128, num.MinInt128.Sub(num.Int128From64(1)))
   239  	check.Equal(t, num.MinInt128, num.MaxInt128.Sub(num.Int128From64(-1)))
   240  	check.Equal(t, num.Int128FromComponents(0x8000000000000000, 1), num.MinInt128.Sub(num.Int128From64(-1)))
   241  }
   242  
   243  func TestInt128Neg(t *testing.T) {
   244  	check.Equal(t, num.Int128From64(0), num.Int128From64(0).Neg())
   245  	check.Equal(t, num.Int128From64(1), num.Int128From64(-1).Neg())
   246  	check.Equal(t, num.Int128From64(-1), num.Int128From64(1).Neg())
   247  	check.Equal(t, num.Int128FromComponents(0xFFFFFFFFFFFFFFFF, 1), num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).Neg())
   248  	check.Equal(t, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF), num.Int128FromComponents(0xFFFFFFFFFFFFFFFF, 1).Neg())
   249  	check.Equal(t, num.Int128FromComponents(0x8000000000000000, 1), num.MaxInt128.Neg())
   250  	check.Equal(t, num.MinInt128, num.MinInt128.Neg())
   251  	check.Equal(t, num.Int128FromComponents(0xFFFFFFFFFFFFFFFF, 0), num.Int128FromComponents(1, 0).Neg())
   252  }
   253  
   254  func TestInt128Abs(t *testing.T) {
   255  	check.Equal(t, num.Int128From64(0), num.Int128From64(0).Abs())
   256  	check.Equal(t, num.Int128From64(1), num.Int128From64(-1).Abs())
   257  	check.Equal(t, num.Int128From64(1), num.Int128From64(1).Abs())
   258  	check.Equal(t, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF), num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).Abs())
   259  	check.Equal(t, num.Int128FromComponents(1, 0), num.Int128FromComponents(0xFFFFFFFFFFFFFFFF, 0).Abs())
   260  	check.Equal(t, num.MaxInt128, num.MaxInt128.Abs())
   261  	check.Equal(t, num.MinInt128, num.MinInt128.Abs())
   262  }
   263  
   264  func TestInt128AbsUint128(t *testing.T) {
   265  	check.Equal(t, num.Uint128From64(0), num.Int128From64(0).AbsUint128())
   266  	check.Equal(t, num.Uint128From64(1), num.Int128From64(-1).AbsUint128())
   267  	check.Equal(t, num.Uint128From64(1), num.Int128From64(1).AbsUint128())
   268  	check.Equal(t, num.Uint128FromComponents(0, 0xFFFFFFFFFFFFFFFF), num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).AbsUint128())
   269  	check.Equal(t, num.Uint128FromComponents(1, 0), num.Int128FromComponents(0xFFFFFFFFFFFFFFFF, 0).AbsUint128())
   270  	check.Equal(t, num.Uint128FromComponents(0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF), num.MaxInt128.AbsUint128())
   271  	check.Equal(t, num.Uint128FromComponents(0x8000000000000000, 0), num.MinInt128.AbsUint128())
   272  }
   273  
   274  func TestInt128Cmp(t *testing.T) {
   275  	check.Equal(t, 0, num.Int128From64(0).Cmp(num.Int128From64(0)))
   276  	check.Equal(t, -1, num.Int128From64(-2).Cmp(num.Int128From64(-1)))
   277  	check.Equal(t, -1, num.Int128From64(1).Cmp(num.Int128From64(2)))
   278  	check.Equal(t, 1, num.Int128From64(-1).Cmp(num.Int128From64(-2)))
   279  	check.Equal(t, 1, num.Int128From64(1).Cmp(num.Int128From64(-1)))
   280  	check.Equal(t, -1, num.Int128From64(-1).Cmp(num.Int128From64(1)))
   281  	check.Equal(t, -1, num.Int128From64(-2).Cmp(num.Int128From64(1)))
   282  	check.Equal(t, -1, num.Int128From64(22).Cmp(num.Int128From64(98)))
   283  	check.Equal(t, 1, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).Cmp(num.Int128From64(-1)))
   284  	check.Equal(t, 1, num.Int128FromComponents(1, 0).Cmp(num.Int128From64(1)))
   285  	check.Equal(t, -1, num.MinInt128.Cmp(num.Int128From64(1)))
   286  	check.Equal(t, 1, num.MaxInt128.Cmp(num.Int128From64(-1)))
   287  	check.Equal(t, -1, num.MinInt128.Cmp(num.MaxInt128))
   288  	check.Equal(t, 1, num.MaxInt128.Cmp(num.MinInt128))
   289  	check.Equal(t, 0, num.MaxInt128.Cmp(num.MaxInt128)) //nolint:gocritic // Yes, we meant to compare the same value
   290  	check.Equal(t, 0, num.MinInt128.Cmp(num.MinInt128)) //nolint:gocritic // Yes, we meant to compare the same value
   291  }
   292  
   293  func TestInt128GreaterThan(t *testing.T) {
   294  	check.Equal(t, false, num.Int128From64(0).GreaterThan(num.Int128From64(0)))
   295  	check.Equal(t, false, num.Int128From64(-2).GreaterThan(num.Int128From64(-1)))
   296  	check.Equal(t, false, num.Int128From64(1).GreaterThan(num.Int128From64(2)))
   297  	check.Equal(t, false, num.Int128From64(-1).GreaterThan(num.Int128From64(1)))
   298  	check.Equal(t, false, num.Int128From64(-2).GreaterThan(num.Int128From64(1)))
   299  	check.Equal(t, false, num.Int128From64(22).GreaterThan(num.Int128From64(98)))
   300  	check.Equal(t, false, num.MinInt128.GreaterThan(num.Int128From64(1)))
   301  	check.Equal(t, false, num.MinInt128.GreaterThan(num.MaxInt128))
   302  	check.Equal(t, false, num.MaxInt128.GreaterThan(num.MaxInt128))
   303  	check.Equal(t, false, num.MinInt128.GreaterThan(num.MinInt128))
   304  	check.Equal(t, true, num.Int128From64(-1).GreaterThan(num.Int128From64(-2)))
   305  	check.Equal(t, true, num.Int128From64(1).GreaterThan(num.Int128From64(-1)))
   306  	check.Equal(t, true, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).GreaterThan(num.Int128From64(-1)))
   307  	check.Equal(t, true, num.Int128FromComponents(1, 0).GreaterThan(num.Int128From64(1)))
   308  	check.Equal(t, true, num.MaxInt128.GreaterThan(num.Int128From64(-1)))
   309  	check.Equal(t, true, num.MaxInt128.GreaterThan(num.MinInt128))
   310  }
   311  
   312  func TestInt128GreaterOrEqualTo(t *testing.T) {
   313  	check.Equal(t, true, num.Int128From64(0).GreaterThanOrEqual(num.Int128From64(0)))
   314  	check.Equal(t, false, num.Int128From64(-2).GreaterThanOrEqual(num.Int128From64(-1)))
   315  	check.Equal(t, false, num.Int128From64(1).GreaterThanOrEqual(num.Int128From64(2)))
   316  	check.Equal(t, false, num.Int128From64(-1).GreaterThanOrEqual(num.Int128From64(1)))
   317  	check.Equal(t, false, num.Int128From64(-2).GreaterThanOrEqual(num.Int128From64(1)))
   318  	check.Equal(t, false, num.Int128From64(22).GreaterThanOrEqual(num.Int128From64(98)))
   319  	check.Equal(t, false, num.MinInt128.GreaterThanOrEqual(num.Int128From64(1)))
   320  	check.Equal(t, false, num.MinInt128.GreaterThanOrEqual(num.MaxInt128))
   321  	check.Equal(t, true, num.MaxInt128.GreaterThanOrEqual(num.MaxInt128))
   322  	check.Equal(t, true, num.MinInt128.GreaterThanOrEqual(num.MinInt128))
   323  	check.Equal(t, true, num.Int128From64(-1).GreaterThanOrEqual(num.Int128From64(-2)))
   324  	check.Equal(t, true, num.Int128From64(1).GreaterThanOrEqual(num.Int128From64(-1)))
   325  	check.Equal(t, true, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).GreaterThanOrEqual(num.Int128From64(-1)))
   326  	check.Equal(t, true, num.Int128FromComponents(1, 0).GreaterThanOrEqual(num.Int128From64(1)))
   327  	check.Equal(t, true, num.MaxInt128.GreaterThanOrEqual(num.Int128From64(-1)))
   328  	check.Equal(t, true, num.MaxInt128.GreaterThanOrEqual(num.MinInt128))
   329  }
   330  
   331  func TestInt128LessThan(t *testing.T) {
   332  	check.Equal(t, false, num.Int128From64(0).LessThan(num.Int128From64(0)))
   333  	check.Equal(t, true, num.Int128From64(-2).LessThan(num.Int128From64(-1)))
   334  	check.Equal(t, true, num.Int128From64(1).LessThan(num.Int128From64(2)))
   335  	check.Equal(t, true, num.Int128From64(-1).LessThan(num.Int128From64(1)))
   336  	check.Equal(t, true, num.Int128From64(-2).LessThan(num.Int128From64(1)))
   337  	check.Equal(t, true, num.Int128From64(22).LessThan(num.Int128From64(98)))
   338  	check.Equal(t, true, num.MinInt128.LessThan(num.Int128From64(1)))
   339  	check.Equal(t, true, num.MinInt128.LessThan(num.MaxInt128))
   340  	check.Equal(t, false, num.MaxInt128.LessThan(num.MaxInt128))
   341  	check.Equal(t, false, num.MinInt128.LessThan(num.MinInt128))
   342  	check.Equal(t, false, num.Int128From64(-1).LessThan(num.Int128From64(-2)))
   343  	check.Equal(t, false, num.Int128From64(1).LessThan(num.Int128From64(-1)))
   344  	check.Equal(t, false, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).LessThan(num.Int128From64(-1)))
   345  	check.Equal(t, false, num.Int128FromComponents(1, 0).LessThan(num.Int128From64(1)))
   346  	check.Equal(t, false, num.MaxInt128.LessThan(num.Int128From64(-1)))
   347  	check.Equal(t, false, num.MaxInt128.LessThan(num.MinInt128))
   348  }
   349  
   350  func TestInt128LessOrEqualTo(t *testing.T) {
   351  	check.Equal(t, true, num.Int128From64(0).LessThanOrEqual(num.Int128From64(0)))
   352  	check.Equal(t, true, num.Int128From64(-2).LessThanOrEqual(num.Int128From64(-1)))
   353  	check.Equal(t, true, num.Int128From64(1).LessThanOrEqual(num.Int128From64(2)))
   354  	check.Equal(t, true, num.Int128From64(-1).LessThanOrEqual(num.Int128From64(1)))
   355  	check.Equal(t, true, num.Int128From64(-2).LessThanOrEqual(num.Int128From64(1)))
   356  	check.Equal(t, true, num.Int128From64(22).LessThanOrEqual(num.Int128From64(98)))
   357  	check.Equal(t, true, num.MinInt128.LessThanOrEqual(num.Int128From64(1)))
   358  	check.Equal(t, true, num.MinInt128.LessThanOrEqual(num.MaxInt128))
   359  	check.Equal(t, true, num.MaxInt128.LessThanOrEqual(num.MaxInt128))
   360  	check.Equal(t, true, num.MinInt128.LessThanOrEqual(num.MinInt128))
   361  	check.Equal(t, false, num.Int128From64(-1).LessThanOrEqual(num.Int128From64(-2)))
   362  	check.Equal(t, false, num.Int128From64(1).LessThanOrEqual(num.Int128From64(-1)))
   363  	check.Equal(t, false, num.Int128FromComponents(0, 0xFFFFFFFFFFFFFFFF).LessThanOrEqual(num.Int128From64(-1)))
   364  	check.Equal(t, false, num.Int128FromComponents(1, 0).LessThanOrEqual(num.Int128From64(1)))
   365  	check.Equal(t, false, num.MaxInt128.LessThanOrEqual(num.Int128From64(-1)))
   366  	check.Equal(t, false, num.MaxInt128.LessThanOrEqual(num.MinInt128))
   367  }
   368  
   369  func TestInt128Mul(t *testing.T) {
   370  	bigMax64 := new(big.Int).SetInt64(math.MaxInt64)
   371  	bigMin64 := new(big.Int).SetInt64(math.MinInt64)
   372  	check.Equal(t, num.Int128From64(0), num.Int128From64(0).Mul(num.Int128From64(0)))
   373  	check.Equal(t, num.Int128From64(4), num.Int128From64(-2).Mul(num.Int128From64(-2)))
   374  	check.Equal(t, num.Int128From64(-4), num.Int128From64(-2).Mul(num.Int128From64(2)))
   375  	check.Equal(t, num.Int128From64(0), num.Int128From64(1).Mul(num.Int128From64(0)))
   376  	check.Equal(t, num.Int128From64(1176), num.Int128From64(12).Mul(num.Int128From64(98)))
   377  	check.Equal(t, num.Int128FromBigInt(new(big.Int).Mul(bigMax64, bigMax64)), num.Int128From64(math.MaxInt64).Mul(num.Int128From64(math.MaxInt64)))
   378  	check.Equal(t, num.Int128FromBigInt(new(big.Int).Mul(bigMin64, bigMin64)), num.Int128From64(math.MinInt64).Mul(num.Int128From64(math.MinInt64)))
   379  	check.Equal(t, num.Int128FromBigInt(new(big.Int).Mul(bigMin64, bigMax64)), num.Int128From64(math.MinInt64).Mul(num.Int128From64(math.MaxInt64)))
   380  }
   381  
   382  func TestInt128Div(t *testing.T) {
   383  	left, _ := new(big.Int).SetString("-170141183460469231731687303715884105728", 10)
   384  	result, _ := new(big.Int).SetString("-17014118346046923173168730371588410", 10)
   385  	check.Equal(t, num.Int128From64(0), num.Int128From64(1).Div(num.Int128From64(2)))
   386  	check.Equal(t, num.Int128From64(3), num.Int128From64(11).Div(num.Int128From64(3)))
   387  	check.Equal(t, num.Int128From64(4), num.Int128From64(12).Div(num.Int128From64(3)))
   388  	check.Equal(t, num.Int128From64(-3), num.Int128From64(11).Div(num.Int128From64(-3)))
   389  	check.Equal(t, num.Int128From64(-4), num.Int128From64(12).Div(num.Int128From64(-3)))
   390  	check.Equal(t, num.Int128From64(1), num.Int128From64(10).Div(num.Int128From64(10)))
   391  	check.Equal(t, num.Int128From64(-1), num.Int128From64(10).Div(num.Int128From64(-10)))
   392  	check.Equal(t, num.Int128From64(1), num.Int128FromComponents(1, 0).Div(num.Int128FromComponents(1, 0)))
   393  	check.Equal(t, num.Int128From64(2), num.Int128FromComponents(246, 0).Div(num.Int128FromComponents(123, 0)))
   394  	check.Equal(t, num.Int128From64(2), num.Int128FromComponents(246, 0).Div(num.Int128FromComponents(122, 0)))
   395  	check.Equal(t, num.Int128FromBigInt(result), num.Int128FromBigInt(left).Div(num.Int128From64(10000)))
   396  }
   397  
   398  func TestInt128Json(t *testing.T) {
   399  	for i, one := range table {
   400  		if !one.IsInt128 {
   401  			continue
   402  		}
   403  		in := num.Int128FromStringNoCheck(one.ValueAsStr)
   404  		data, err := json.Marshal(in)
   405  		check.NoError(t, err, indexFmt, i)
   406  		var out num.Int128
   407  		check.NoError(t, json.Unmarshal(data, &out), indexFmt, i)
   408  		check.Equal(t, in, out, indexFmt, i)
   409  	}
   410  }
   411  
   412  func TestInt128Yaml(t *testing.T) {
   413  	for i, one := range table {
   414  		if !one.IsInt128 {
   415  			continue
   416  		}
   417  		in := num.Int128FromStringNoCheck(one.ValueAsStr)
   418  		data, err := yaml.Marshal(in)
   419  		check.NoError(t, err, indexFmt, i)
   420  		var out num.Int128
   421  		check.NoError(t, yaml.Unmarshal(data, &out), indexFmt, i)
   422  		check.Equal(t, in, out, indexFmt, i)
   423  	}
   424  }