github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xnumber/xnumber_test.go (about)

     1  package xnumber
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Aoi-hosizora/ahlib/xtesting"
     6  	"log"
     7  	"math"
     8  	"reflect"
     9  	"strconv"
    10  	"testing"
    11  )
    12  
    13  func TestAccuracy(t *testing.T) {
    14  	acc := NewAccuracy(1e-2)
    15  
    16  	for _, tc := range []struct {
    17  		giveFn func(a, b float64) bool
    18  		giveA  float64
    19  		giveB  float64
    20  		want   bool
    21  	}{
    22  		{EqualInAccuracy, 0.333, 0.332, false}, // 0.333 - 0.332 = 0.001 != 0.001
    23  		{EqualInAccuracy, 0.333, 0.333, true},
    24  		{EqualInAccuracy, 0.333, 0.334, false},
    25  		{EqualInAccuracy, 0.3333, 0.3332, true}, // 0.3333 - 0.3332 = 0.0001 != 0.0001
    26  		{EqualInAccuracy, 0.3333, 0.3333, true},
    27  		{EqualInAccuracy, 0.3333, 0.3334, true},
    28  		{acc.Equal, 0.33, 0.32, false},
    29  		{acc.Equal, 0.33, 0.33, true},
    30  		{acc.Equal, 0.33, 0.34, false},
    31  		{acc.Equal, 0.333, 0.332, true},
    32  		{acc.Equal, 0.333, 0.333, true},
    33  		{acc.Equal, 0.333, 0.334, true},
    34  
    35  		{NotEqualInAccuracy, 0.333, 0.332, true},
    36  		{NotEqualInAccuracy, 0.333, 0.333, false},
    37  		{NotEqualInAccuracy, 0.333, 0.334, true},
    38  		{NotEqualInAccuracy, 0.3333, 0.3332, false},
    39  		{NotEqualInAccuracy, 0.3333, 0.3333, false},
    40  		{NotEqualInAccuracy, 0.3333, 0.3334, false},
    41  		{acc.NotEqual, 0.33, 0.32, true},
    42  		{acc.NotEqual, 0.33, 0.33, false},
    43  		{acc.NotEqual, 0.33, 0.34, true},
    44  		{acc.NotEqual, 0.333, 0.332, false},
    45  		{acc.NotEqual, 0.333, 0.333, false},
    46  		{acc.NotEqual, 0.333, 0.334, false},
    47  
    48  		{GreaterInAccuracy, 0.333, 0.332, true},
    49  		{GreaterInAccuracy, 0.333, 0.333, false},
    50  		{GreaterInAccuracy, 0.333, 0.334, false},
    51  		{GreaterInAccuracy, 0.3333, 0.3332, false},
    52  		{GreaterInAccuracy, 0.3333, 0.3333, false},
    53  		{GreaterInAccuracy, 0.3333, 0.3334, false},
    54  		{acc.Greater, 0.33, 0.32, true},
    55  		{acc.Greater, 0.33, 0.33, false},
    56  		{acc.Greater, 0.33, 0.34, false},
    57  		{acc.Greater, 0.333, 0.332, false},
    58  		{acc.Greater, 0.333, 0.333, false},
    59  		{acc.Greater, 0.333, 0.334, false},
    60  
    61  		{GreaterOrEqualInAccuracy, 0.333, 0.332, true},
    62  		{GreaterOrEqualInAccuracy, 0.333, 0.333, true},
    63  		{GreaterOrEqualInAccuracy, 0.333, 0.334, false},
    64  		{GreaterOrEqualInAccuracy, 0.3333, 0.3332, true},
    65  		{GreaterOrEqualInAccuracy, 0.3333, 0.3333, true},
    66  		{GreaterOrEqualInAccuracy, 0.3333, 0.3334, true},
    67  		{acc.GreaterOrEqual, 0.33, 0.32, true},
    68  		{acc.GreaterOrEqual, 0.33, 0.33, true},
    69  		{acc.GreaterOrEqual, 0.33, 0.34, false},
    70  		{acc.GreaterOrEqual, 0.333, 0.332, true},
    71  		{acc.GreaterOrEqual, 0.333, 0.333, true},
    72  		{acc.GreaterOrEqual, 0.333, 0.334, true},
    73  
    74  		{LessInAccuracy, 0.333, 0.332, false},
    75  		{LessInAccuracy, 0.333, 0.333, false},
    76  		{LessInAccuracy, 0.333, 0.334, true},
    77  		{LessInAccuracy, 0.3333, 0.3332, false},
    78  		{LessInAccuracy, 0.3333, 0.3333, false},
    79  		{LessInAccuracy, 0.3333, 0.3334, false},
    80  		{acc.Less, 0.33, 0.32, false},
    81  		{acc.Less, 0.33, 0.33, false},
    82  		{acc.Less, 0.33, 0.34, true},
    83  		{acc.Less, 0.333, 0.332, false},
    84  		{acc.Less, 0.333, 0.333, false},
    85  		{acc.Less, 0.333, 0.334, false},
    86  
    87  		{LessOrEqualInAccuracy, 0.333, 0.332, false},
    88  		{LessOrEqualInAccuracy, 0.333, 0.333, true},
    89  		{LessOrEqualInAccuracy, 0.333, 0.334, true},
    90  		{LessOrEqualInAccuracy, 0.3333, 0.3332, true},
    91  		{LessOrEqualInAccuracy, 0.3333, 0.3333, true},
    92  		{LessOrEqualInAccuracy, 0.3333, 0.3334, true},
    93  		{acc.LessOrEqual, 0.33, 0.32, false},
    94  		{acc.LessOrEqual, 0.33, 0.33, true},
    95  		{acc.LessOrEqual, 0.33, 0.34, true},
    96  		{acc.LessOrEqual, 0.333, 0.332, true},
    97  		{acc.LessOrEqual, 0.333, 0.333, true},
    98  		{acc.LessOrEqual, 0.333, 0.334, true},
    99  	} {
   100  		xtesting.Equal(t, tc.giveFn(tc.giveA, tc.giveB), tc.want)
   101  	}
   102  }
   103  
   104  func TestFormatByteSize(t *testing.T) {
   105  	for _, tc := range []struct {
   106  		give float64
   107  		want string
   108  	}{
   109  		{-1025, "-1.00KB"},
   110  		{-5, "-5B"},
   111  		{0, "0B"},
   112  		{1023, "1023B"},
   113  		{1024, "1.00KB"},
   114  		{1030, "1.01KB"},
   115  		{1536, "1.50KB"},
   116  		{2048, "2.00KB"},
   117  		{1024 * 1024, "1.00MB"},
   118  		{2.51 * 1024 * 1024, "2.51MB"},
   119  		{1024 * 1024 * 1024, "1.00GB"},
   120  		{2.51 * 1024 * 1024 * 1024, "2.51GB"},
   121  		{1024 * 1024 * 1024 * 1024, "1.00TB"},
   122  		{1.1 * 1024 * 1024 * 1024 * 1024, "1.10TB"},
   123  	} {
   124  		xtesting.Equal(t, FormatByteSize(tc.give), tc.want)
   125  	}
   126  }
   127  
   128  func TestBool(t *testing.T) {
   129  	xtesting.Equal(t, Bool(true), 1)
   130  	xtesting.Equal(t, Bool(false), 0)
   131  }
   132  
   133  func TestIntBitLength(t *testing.T) {
   134  	xtesting.Equal(t, IntBitLength(), strconv.IntSize)
   135  }
   136  
   137  func TestFastrand(t *testing.T) {
   138  	for i := 0; i < 5; i++ {
   139  		log.Println(FastrandUint32())
   140  	}
   141  	for i := 0; i < 5; i++ {
   142  		log.Println(FastrandUint64())
   143  	}
   144  	for i := 0; i < 5; i++ {
   145  		log.Println(FastrandInt32())
   146  	}
   147  	for i := 0; i < 5; i++ {
   148  		log.Println(FastrandInt64())
   149  	}
   150  }
   151  
   152  func TestIsPowerOfTwo(t *testing.T) {
   153  	for _, tc := range []struct {
   154  		give int
   155  		want bool
   156  	}{
   157  		{0, true},
   158  		{1, true},
   159  		{2, true},
   160  		{3, false},
   161  		{4, true},
   162  		{5, false},
   163  		{1023, false},
   164  		{1024, true},
   165  		{2047, false},
   166  		{2048, true},
   167  		{65535, false},
   168  		{65536, true},
   169  		{1073741823, false},
   170  		{1073741824, true},
   171  		{2147483647, false},
   172  	} {
   173  		t.Run(strconv.Itoa(tc.give), func(t *testing.T) {
   174  			xtesting.Equal(t, IsPowerOfTwo(tc.give), tc.want)
   175  		})
   176  	}
   177  }
   178  
   179  func TestMinMax(t *testing.T) {
   180  	for _, tc := range []struct {
   181  		give interface{}
   182  		want interface{}
   183  	}{
   184  		{MinInt8, math.MinInt8},
   185  		{MinInt16, math.MinInt16},
   186  		{MinInt32, math.MinInt32},
   187  		{MinInt64, math.MinInt64},
   188  		{MinUint8, 0},
   189  		{MinUint16, 0},
   190  		{MinUint32, 0},
   191  		{MinUint64, 0},
   192  
   193  		{MaxInt8, math.MaxInt8},
   194  		{MaxInt16, math.MaxInt16},
   195  		{MaxInt32, math.MaxInt32},
   196  		{MaxInt64, math.MaxInt64},
   197  		{MaxUint8, 0xff},
   198  		{MaxUint16, 0xffff},
   199  		{MaxUint32, 0xffffffff},
   200  		{MaxUint64, uint64(0xffffffffffffffff)},
   201  	} {
   202  		xtesting.EqualValue(t, tc.give, tc.want)
   203  	}
   204  
   205  	xtesting.True(t, EqualInAccuracy(float64(MaxFloat32), math.MaxFloat32))
   206  	xtesting.True(t, EqualInAccuracy(float64(SmallestNonzeroFloat32), math.SmallestNonzeroFloat32))
   207  	xtesting.True(t, EqualInAccuracy(MaxFloat64, math.MaxFloat64))
   208  	xtesting.True(t, EqualInAccuracy(SmallestNonzeroFloat64, math.SmallestNonzeroFloat64))
   209  }
   210  
   211  func TestOverflowWhen(t *testing.T) {
   212  	// signed add (positive+positive)
   213  	xtesting.True(t, OverflowWhenAddInt8(MaxInt8, 1))
   214  	xtesting.True(t, OverflowWhenAddInt16(MaxInt16, 1))
   215  	xtesting.True(t, OverflowWhenAddInt32(MaxInt32, 1))
   216  	xtesting.True(t, OverflowWhenAddInt64(MaxInt64, 1))
   217  	xtesting.False(t, OverflowWhenAddInt8(MaxInt8-1, 1))
   218  	xtesting.False(t, OverflowWhenAddInt16(MaxInt16-1, 1))
   219  	xtesting.False(t, OverflowWhenAddInt32(MaxInt32-1, 1))
   220  	xtesting.False(t, OverflowWhenAddInt64(MaxInt64-1, 1))
   221  
   222  	// signed add (negative+negative)
   223  	xtesting.True(t, OverflowWhenAddInt8(MinInt8, -1))
   224  	xtesting.True(t, OverflowWhenAddInt16(MinInt16, -1))
   225  	xtesting.True(t, OverflowWhenAddInt32(MinInt32, -1))
   226  	xtesting.True(t, OverflowWhenAddInt64(MinInt64, -1))
   227  	xtesting.False(t, OverflowWhenAddInt8(MinInt8+1, -1))
   228  	xtesting.False(t, OverflowWhenAddInt16(MinInt16+1, -1))
   229  	xtesting.False(t, OverflowWhenAddInt32(MinInt32+1, -1))
   230  	xtesting.False(t, OverflowWhenAddInt64(MinInt64+1, -1))
   231  
   232  	// signed subtract (negative-positive)
   233  	xtesting.True(t, OverflowWhenSubtractInt8(MinInt8, 1))
   234  	xtesting.True(t, OverflowWhenSubtractInt16(MinInt16, 1))
   235  	xtesting.True(t, OverflowWhenSubtractInt32(MinInt32, 1))
   236  	xtesting.True(t, OverflowWhenSubtractInt64(MinInt64, 1))
   237  	xtesting.False(t, OverflowWhenSubtractInt8(MinInt8+1, 1))
   238  	xtesting.False(t, OverflowWhenSubtractInt16(MinInt16+1, 1))
   239  	xtesting.False(t, OverflowWhenSubtractInt32(MinInt32+1, 1))
   240  	xtesting.False(t, OverflowWhenSubtractInt64(MinInt64+1, 1))
   241  
   242  	// signed subtract (positive-negative)
   243  	xtesting.True(t, OverflowWhenSubtractInt8(MaxInt8, -1))
   244  	xtesting.True(t, OverflowWhenSubtractInt16(MaxInt16, -1))
   245  	xtesting.True(t, OverflowWhenSubtractInt32(MaxInt32, -1))
   246  	xtesting.True(t, OverflowWhenSubtractInt64(MaxInt64, -1))
   247  	xtesting.False(t, OverflowWhenSubtractInt8(MaxInt8-1, -1))
   248  	xtesting.False(t, OverflowWhenSubtractInt16(MaxInt16-1, -1))
   249  	xtesting.False(t, OverflowWhenSubtractInt32(MaxInt32-1, -1))
   250  	xtesting.False(t, OverflowWhenSubtractInt64(MaxInt64-1, -1))
   251  
   252  	// unsigned add (positive+positive)
   253  	xtesting.True(t, OverflowWhenAddUint8(MaxUint8, 1))
   254  	xtesting.True(t, OverflowWhenAddUint16(MaxUint16, 1))
   255  	xtesting.True(t, OverflowWhenAddUint32(MaxUint32, 1))
   256  	xtesting.True(t, OverflowWhenAddUint64(MaxUint64, 1))
   257  	xtesting.False(t, OverflowWhenAddUint8(MaxUint8-1, 1))
   258  	xtesting.False(t, OverflowWhenAddUint16(MaxUint16-1, 1))
   259  	xtesting.False(t, OverflowWhenAddUint32(MaxUint32-1, 1))
   260  	xtesting.False(t, OverflowWhenAddUint64(MaxUint64-1, 1))
   261  
   262  	// unsigned subtract (positive-positive)
   263  	xtesting.True(t, OverflowWhenSubtractUint8(1, 2))
   264  	xtesting.True(t, OverflowWhenSubtractUint16(1, 2))
   265  	xtesting.True(t, OverflowWhenSubtractUint32(1, 2))
   266  	xtesting.True(t, OverflowWhenSubtractUint64(1, 2))
   267  	xtesting.False(t, OverflowWhenSubtractUint8(2, 1))
   268  	xtesting.False(t, OverflowWhenSubtractUint16(2, 1))
   269  	xtesting.False(t, OverflowWhenSubtractUint32(2, 1))
   270  	xtesting.False(t, OverflowWhenSubtractUint64(2, 1))
   271  
   272  	originBitLength := intBitLength
   273  	defer func() { intBitLength = originBitLength }()
   274  	intBitLength = 32
   275  	xtesting.True(t, OverflowWhenAddInt(int(MaxInt32), 1))
   276  	xtesting.False(t, OverflowWhenAddInt(int(MaxInt32-1), 1))
   277  	xtesting.True(t, OverflowWhenAddInt(int(MinInt32), -1))
   278  	xtesting.False(t, OverflowWhenAddInt(int(MinInt32+1), -1))
   279  	xtesting.True(t, OverflowWhenSubtractInt(int(MinInt32), 1))
   280  	xtesting.False(t, OverflowWhenSubtractInt(int(MinInt32+1), 1))
   281  	xtesting.True(t, OverflowWhenSubtractInt(int(MaxInt32), -1))
   282  	xtesting.False(t, OverflowWhenSubtractInt(int(MaxInt32-1), -1))
   283  	xtesting.True(t, OverflowWhenAddUint(uint(MaxUint32), 1))
   284  	xtesting.False(t, OverflowWhenAddUint(uint(MaxUint32-1), 1))
   285  	xtesting.True(t, OverflowWhenSubtractUint(uint(1), 2))
   286  	xtesting.False(t, OverflowWhenSubtractUint(uint(2), 1))
   287  	intBitLength = 64
   288  	xtesting.True(t, OverflowWhenAddInt(int(MaxInt64), 1))
   289  	xtesting.False(t, OverflowWhenAddInt(int(MaxInt64-1), 1))
   290  	xtesting.True(t, OverflowWhenAddInt(int(MinInt64), -1))
   291  	xtesting.False(t, OverflowWhenAddInt(int(MinInt64+1), -1))
   292  	xtesting.True(t, OverflowWhenSubtractInt(int(MinInt64), 1))
   293  	xtesting.False(t, OverflowWhenSubtractInt(int(MinInt64+1), 1))
   294  	xtesting.True(t, OverflowWhenSubtractInt(int(MaxInt64), -1))
   295  	xtesting.False(t, OverflowWhenSubtractInt(int(MaxInt64-1), -1))
   296  	xtesting.True(t, OverflowWhenAddUint(uint(MaxUint64), 1))
   297  	xtesting.False(t, OverflowWhenAddUint(uint(MaxUint64-1), 1))
   298  	xtesting.True(t, OverflowWhenSubtractUint(uint(1), 2))
   299  	xtesting.False(t, OverflowWhenSubtractUint(uint(2), 1))
   300  }
   301  
   302  func TestParse(t *testing.T) {
   303  	i, _ := ParseInt("9223372036854775807", 10)
   304  	xtesting.Equal(t, i, 9223372036854775807)
   305  	i8, _ := ParseInt8("127", 10)
   306  	xtesting.Equal(t, i8, int8(127))
   307  	i16, _ := ParseInt16("32767", 10)
   308  	xtesting.Equal(t, i16, int16(32767))
   309  	i32, _ := ParseInt32("2147483647", 10)
   310  	xtesting.Equal(t, i32, int32(2147483647))
   311  	i64, _ := ParseInt64("9223372036854775807", 10)
   312  	xtesting.Equal(t, i64, int64(9223372036854775807))
   313  
   314  	u, _ := ParseUint("18446744073709551615", 10)
   315  	xtesting.Equal(t, u, uint(18446744073709551615))
   316  	u8, _ := ParseUint8("255", 10)
   317  	xtesting.Equal(t, u8, uint8(255))
   318  	u16, _ := ParseUint16("65535", 10)
   319  	xtesting.Equal(t, u16, uint16(65535))
   320  	u32, _ := ParseUint32("4294967295", 10)
   321  	xtesting.Equal(t, u32, uint32(4294967295))
   322  	u64, _ := ParseUint64("18446744073709551615", 10)
   323  	xtesting.Equal(t, u64, uint64(18446744073709551615))
   324  
   325  	f32, _ := ParseFloat32("0.5")
   326  	xtesting.Equal(t, f32, float32(0.5))
   327  	f64, _ := ParseFloat64("0.5")
   328  	xtesting.Equal(t, f64, 0.5)
   329  
   330  	_, err := ParseInt8("a", 10) // no number
   331  	xtesting.NotNil(t, err)
   332  	_, err = ParseInt8("a", 11) // success
   333  	xtesting.Nil(t, err)
   334  	_, err = ParseInt32("2147483648", 10) // overflow
   335  	xtesting.NotNil(t, err)
   336  	_, err = ParseInt64("10", 37) // base err
   337  	xtesting.NotNil(t, err)
   338  }
   339  
   340  func TestParseOr(t *testing.T) {
   341  	xtesting.Equal(t, ParseIntOr("9223372036854775807", 10, 0), 9223372036854775807)
   342  	xtesting.Equal(t, ParseInt8Or("127", 10, 0), int8(127))
   343  	xtesting.Equal(t, ParseInt16Or("32767", 10, 0), int16(32767))
   344  	xtesting.Equal(t, ParseInt32Or("2147483647", 10, 0), int32(2147483647))
   345  	xtesting.Equal(t, ParseInt64Or("9223372036854775807", 10, 0), int64(9223372036854775807))
   346  	xtesting.Equal(t, ParseUintOr("18446744073709551615", 10, 0), uint(18446744073709551615))
   347  	xtesting.Equal(t, ParseUint8Or("255", 10, 0), uint8(255))
   348  	xtesting.Equal(t, ParseUint16Or("65535", 10, 0), uint16(65535))
   349  	xtesting.Equal(t, ParseUint32Or("4294967295", 10, 0), uint32(4294967295))
   350  	xtesting.Equal(t, ParseUint64Or("18446744073709551615", 10, 0), uint64(18446744073709551615))
   351  	xtesting.Equal(t, ParseFloat32Or("0.5", 0), float32(0.5))
   352  	xtesting.Equal(t, ParseFloat64Or("0.5", 0), 0.5)
   353  
   354  	xtesting.Equal(t, ParseIntOr("", 10, 9223372036854775807), 9223372036854775807)
   355  	xtesting.Equal(t, ParseInt8Or("", 10, 127), int8(127))
   356  	xtesting.Equal(t, ParseInt16Or("", 10, 32767), int16(32767))
   357  	xtesting.Equal(t, ParseInt32Or("", 10, 2147483647), int32(2147483647))
   358  	xtesting.Equal(t, ParseInt64Or("", 10, 9223372036854775807), int64(9223372036854775807))
   359  	xtesting.Equal(t, ParseUintOr("", 10, 18446744073709551615), uint(18446744073709551615))
   360  	xtesting.Equal(t, ParseUint8Or("", 10, 255), uint8(255))
   361  	xtesting.Equal(t, ParseUint16Or("", 10, 65535), uint16(65535))
   362  	xtesting.Equal(t, ParseUint32Or("", 10, 4294967295), uint32(4294967295))
   363  	xtesting.Equal(t, ParseUint64Or("", 10, 18446744073709551615), uint64(18446744073709551615))
   364  	xtesting.Equal(t, ParseFloat32Or("", 0.5), float32(0.5))
   365  	xtesting.Equal(t, ParseFloat64Or("", 0.5), 0.5)
   366  }
   367  
   368  func TestAtoX(t *testing.T) {
   369  	i, _ := Atoi("9223372036854775807")
   370  	xtesting.Equal(t, i, 9223372036854775807)
   371  	i8, _ := Atoi8("127")
   372  	xtesting.Equal(t, i8, int8(127))
   373  	i16, _ := Atoi16("32767")
   374  	xtesting.Equal(t, i16, int16(32767))
   375  	u32, _ := Atou32("4294967295")
   376  	xtesting.Equal(t, u32, uint32(4294967295))
   377  	i64, _ := Atoi64("9223372036854775807")
   378  	xtesting.Equal(t, i64, int64(9223372036854775807))
   379  
   380  	u, _ := Atou("18446744073709551615")
   381  	xtesting.Equal(t, u, uint(18446744073709551615))
   382  	u8, _ := Atou8("255")
   383  	xtesting.Equal(t, u8, uint8(255))
   384  	u16, _ := Atou16("65535")
   385  	xtesting.Equal(t, u16, uint16(65535))
   386  	i32, _ := Atoi32("2147483647")
   387  	xtesting.Equal(t, i32, int32(2147483647))
   388  	u64, _ := Atou64("18446744073709551615")
   389  	xtesting.Equal(t, u64, uint64(18446744073709551615))
   390  
   391  	f32, _ := Atof32("0.5")
   392  	xtesting.Equal(t, f32, float32(0.5))
   393  	f64, _ := Atof64("0.5")
   394  	xtesting.Equal(t, f64, 0.5)
   395  }
   396  
   397  func TestAtoXOr(t *testing.T) {
   398  	xtesting.Equal(t, AtoiOr("9223372036854775807", 0), 9223372036854775807)
   399  	xtesting.Equal(t, Atoi8Or("127", 0), int8(127))
   400  	xtesting.Equal(t, Atoi16Or("32767", 0), int16(32767))
   401  	xtesting.Equal(t, Atoi32Or("2147483647", 0), int32(2147483647))
   402  	xtesting.Equal(t, Atoi64Or("9223372036854775807", 0), int64(9223372036854775807))
   403  	xtesting.Equal(t, AtouOr("18446744073709551615", 0), uint(18446744073709551615))
   404  	xtesting.Equal(t, Atou8Or("255", 0), uint8(255))
   405  	xtesting.Equal(t, Atou16Or("65535", 0), uint16(65535))
   406  	xtesting.Equal(t, Atou32Or("4294967295", 0), uint32(4294967295))
   407  	xtesting.Equal(t, Atou64Or("18446744073709551615", 0), uint64(18446744073709551615))
   408  	xtesting.Equal(t, Atof32Or("0.5", 0), float32(0.5))
   409  	xtesting.Equal(t, Atof64Or("0.5", 0), 0.5)
   410  
   411  	xtesting.Equal(t, AtoiOr("", 9223372036854775807), 9223372036854775807)
   412  	xtesting.Equal(t, Atoi8Or("", 127), int8(127))
   413  	xtesting.Equal(t, Atoi16Or("", 32767), int16(32767))
   414  	xtesting.Equal(t, Atoi32Or("", 2147483647), int32(2147483647))
   415  	xtesting.Equal(t, Atoi64Or("", 9223372036854775807), int64(9223372036854775807))
   416  	xtesting.Equal(t, AtouOr("", 18446744073709551615), uint(18446744073709551615))
   417  	xtesting.Equal(t, Atou8Or("", 255), uint8(255))
   418  	xtesting.Equal(t, Atou16Or("", 65535), uint16(65535))
   419  	xtesting.Equal(t, Atou32Or("", 4294967295), uint32(4294967295))
   420  	xtesting.Equal(t, Atou64Or("", 18446744073709551615), uint64(18446744073709551615))
   421  	xtesting.Equal(t, Atof32Or("", 0.5), float32(0.5))
   422  	xtesting.Equal(t, Atof64Or("", 0.5), 0.5)
   423  }
   424  
   425  func TestFormat(t *testing.T) {
   426  	xtesting.Equal(t, FormatInt(9223372036854775807, 10), "9223372036854775807")
   427  	xtesting.Equal(t, FormatInt8(127, 10), "127")
   428  	xtesting.Equal(t, FormatInt16(32767, 10), "32767")
   429  	xtesting.Equal(t, FormatInt32(2147483647, 10), "2147483647")
   430  	xtesting.Equal(t, FormatInt64(9223372036854775807, 10), "9223372036854775807")
   431  	xtesting.Equal(t, FormatUint(18446744073709551615, 10), "18446744073709551615")
   432  	xtesting.Equal(t, FormatUint8(255, 10), "255")
   433  	xtesting.Equal(t, FormatUint16(65535, 10), "65535")
   434  	xtesting.Equal(t, FormatUint32(4294967295, 10), "4294967295")
   435  	xtesting.Equal(t, FormatUint64(18446744073709551615, 10), "18446744073709551615")
   436  	xtesting.Equal(t, FormatFloat32(0.5, 'f', -1), "0.5")
   437  	xtesting.Equal(t, FormatFloat64(0.5, 'f', -1), "0.5")
   438  	xtesting.Equal(t, FormatFloat32(0.5555, 'e', 2), "5.55e-01")
   439  	xtesting.Equal(t, FormatFloat64(0.5555, 'e', 2), "5.55e-01")
   440  }
   441  
   442  func TestXtoa(t *testing.T) {
   443  	xtesting.Equal(t, Itoa(9223372036854775807), "9223372036854775807")
   444  	xtesting.Equal(t, I8toa(127), "127")
   445  	xtesting.Equal(t, I16toa(32767), "32767")
   446  	xtesting.Equal(t, I32toa(2147483647), "2147483647")
   447  	xtesting.Equal(t, I64toa(9223372036854775807), "9223372036854775807")
   448  	xtesting.Equal(t, Utoa(18446744073709551615), "18446744073709551615")
   449  	xtesting.Equal(t, U8toa(255), "255")
   450  	xtesting.Equal(t, U16toa(65535), "65535")
   451  	xtesting.Equal(t, U32toa(4294967295), "4294967295")
   452  	xtesting.Equal(t, U64toa(18446744073709551615), "18446744073709551615")
   453  	xtesting.Equal(t, F32toa(0.5), "0.5")
   454  	xtesting.Equal(t, F64toa(0.5), "0.5")
   455  }
   456  
   457  func TestRange(t *testing.T) {
   458  	for _, tc := range []struct {
   459  		giveFrom int8
   460  		giveTo   int8
   461  		giveStep int8
   462  		want     []int8
   463  	}{
   464  		{1, 1, 1, nil},
   465  		{1, 1, -1, nil},
   466  		{1, 10, 0, nil},
   467  		{10, 1, 0, nil},
   468  		{1, 10, -1, nil},
   469  		{10, 1, 1, nil},
   470  		{1, 2, 1, []int8{1}},
   471  		{1, 0, -1, []int8{1}},
   472  		{1, 2, 100, []int8{1}},
   473  		{1, 0, -100, []int8{1}},
   474  
   475  		{0, 10, 1, []int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
   476  		{1, 10, 1, []int8{1, 2, 3, 4, 5, 6, 7, 8, 9}},
   477  		{0, 10, 2, []int8{0, 2, 4, 6, 8}},
   478  		{1, 10, 2, []int8{1, 3, 5, 7, 9}},
   479  		{0, 10, 3, []int8{0, 3, 6, 9}},
   480  		{1, 10, 3, []int8{1, 4, 7}},
   481  		{0, 10, 4, []int8{0, 4, 8}},
   482  		{1, 10, 4, []int8{1, 5, 9}},
   483  		{0, 10, 5, []int8{0, 5}},
   484  		{1, 10, 5, []int8{1, 6}},
   485  		{0, 10, 6, []int8{0, 6}},
   486  		{1, 10, 6, []int8{1, 7}},
   487  		{0, 10, 9, []int8{0, 9}},
   488  		{1, 10, 9, []int8{1}},
   489  		{0, 10, 10, []int8{0}},
   490  		{1, 10, 10, []int8{1}},
   491  		{0, 10, 100, []int8{0}},
   492  		{1, 10, 100, []int8{1}},
   493  
   494  		{11, 0, -1, []int8{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
   495  		{10, 0, -1, []int8{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
   496  		{11, 0, -2, []int8{11, 9, 7, 5, 3, 1}},
   497  		{10, 0, -2, []int8{10, 8, 6, 4, 2}},
   498  		{11, 0, -3, []int8{11, 8, 5, 2}},
   499  		{10, 0, -3, []int8{10, 7, 4, 1}},
   500  		{11, 0, -4, []int8{11, 7, 3}},
   501  		{10, 0, -4, []int8{10, 6, 2}},
   502  		{11, 0, -5, []int8{11, 6, 1}},
   503  		{10, 0, -5, []int8{10, 5}},
   504  		{11, 0, -6, []int8{11, 5}},
   505  		{10, 0, -6, []int8{10, 4}},
   506  		{11, 0, -9, []int8{11, 2}},
   507  		{10, 0, -9, []int8{10, 1}},
   508  		{11, 0, -10, []int8{11, 1}},
   509  		{10, 0, -10, []int8{10}},
   510  		{11, 0, -100, []int8{11}},
   511  		{10, 0, -100, []int8{10}},
   512  	} {
   513  		t.Run(fmt.Sprintf("%d_%d_%d", tc.giveFrom, tc.giveTo, tc.giveStep), func(t *testing.T) {
   514  			intSlice := IntRange(int(tc.giveFrom), int(tc.giveTo), int(tc.giveStep))
   515  			int8Slice := Int8Range(tc.giveFrom, tc.giveTo, tc.giveStep)
   516  			int16Slice := Int16Range(int16(tc.giveFrom), int16(tc.giveTo), int16(tc.giveStep))
   517  			int32Slice := Int32Range(int32(tc.giveFrom), int32(tc.giveTo), int32(tc.giveStep))
   518  			int64Slice := Int64Range(int64(tc.giveFrom), int64(tc.giveTo), int64(tc.giveStep))
   519  			for _, slice := range []interface{}{intSlice, int8Slice, int16Slice, int32Slice, int64Slice} {
   520  				val := reflect.ValueOf(slice)
   521  				xtesting.Equal(t, val.Len(), len(tc.want))
   522  				for i := 0; i < val.Len(); i++ {
   523  					xtesting.Equal(t, int8(val.Index(i).Int()), tc.want[i])
   524  				}
   525  			}
   526  
   527  			if tc.giveFrom >= 0 && tc.giveTo >= 0 {
   528  				flag := int8(+1)
   529  				if tc.giveStep < 0 {
   530  					flag = int8(-1)
   531  				}
   532  				uintSlice := UintRange(uint(tc.giveFrom), uint(tc.giveTo), uint(flag*tc.giveStep), flag == -1)
   533  				uint8Slice := Uint8Range(uint8(tc.giveFrom), uint8(tc.giveTo), uint8(flag*tc.giveStep), flag == -1)
   534  				uint16Slice := Uint16Range(uint16(tc.giveFrom), uint16(tc.giveTo), uint16(flag*tc.giveStep), flag == -1)
   535  				uint32Slice := Uint32Range(uint32(tc.giveFrom), uint32(tc.giveTo), uint32(flag*tc.giveStep), flag == -1)
   536  				uint64Slice := Uint64Range(uint64(tc.giveFrom), uint64(tc.giveTo), uint64(flag*tc.giveStep), flag == -1)
   537  				for _, slice := range []interface{}{uintSlice, uint8Slice, uint16Slice, uint32Slice, uint64Slice} {
   538  					val := reflect.ValueOf(slice)
   539  					xtesting.Equal(t, val.Len(), len(tc.want))
   540  					for i := 0; i < val.Len(); i++ {
   541  						xtesting.Equal(t, int8(val.Index(i).Uint()), tc.want[i])
   542  					}
   543  				}
   544  			}
   545  		})
   546  	}
   547  
   548  	t.Run("overflow", func(t *testing.T) {
   549  		xtesting.Equal(t, IntRange(int(MaxInt64-3), int(MaxInt64), 2), []int{int(MaxInt64 - 3), int(MaxInt64 - 1)})
   550  		xtesting.Equal(t, Int8Range(MaxInt8-3, MaxInt8, 2), []int8{MaxInt8 - 3, MaxInt8 - 1})
   551  		xtesting.Equal(t, Int16Range(MaxInt16-3, MaxInt16, 2), []int16{MaxInt16 - 3, MaxInt16 - 1})
   552  		xtesting.Equal(t, Int32Range(MaxInt32-3, MaxInt32, 2), []int32{MaxInt32 - 3, MaxInt32 - 1})
   553  		xtesting.Equal(t, Int64Range(MaxInt64-3, MaxInt64, 2), []int64{MaxInt64 - 3, MaxInt64 - 1})
   554  
   555  		xtesting.Equal(t, IntRange(int(MinInt64+3), int(MinInt64), -2), []int{int(MinInt64 + 3), int(MinInt64 + 1)})
   556  		xtesting.Equal(t, Int8Range(MinInt8+3, MinInt8, -2), []int8{MinInt8 + 3, MinInt8 + 1})
   557  		xtesting.Equal(t, Int16Range(MinInt16+3, MinInt16, -2), []int16{MinInt16 + 3, MinInt16 + 1})
   558  		xtesting.Equal(t, Int32Range(MinInt32+3, MinInt32, -2), []int32{MinInt32 + 3, MinInt32 + 1})
   559  		xtesting.Equal(t, Int64Range(MinInt64+3, MinInt64, -2), []int64{MinInt64 + 3, MinInt64 + 1})
   560  
   561  		xtesting.Equal(t, UintRange(uint(MaxUint64-3), uint(MaxUint64), 2), []uint{uint(MaxUint64 - 3), uint(MaxUint64 - 1)})
   562  		xtesting.Equal(t, Uint8Range(MaxUint8-3, MaxUint8, 2), []uint8{MaxUint8 - 3, MaxUint8 - 1})
   563  		xtesting.Equal(t, Uint16Range(MaxUint16-3, MaxUint16, 2), []uint16{MaxUint16 - 3, MaxUint16 - 1})
   564  		xtesting.Equal(t, Uint32Range(MaxUint32-3, MaxUint32, 2), []uint32{MaxUint32 - 3, MaxUint32 - 1})
   565  		xtesting.Equal(t, Uint64Range(MaxUint64-3, MaxUint64, 2), []uint64{MaxUint64 - 3, MaxUint64 - 1})
   566  
   567  		xtesting.Equal(t, UintRange(3, 0, 2, true), []uint{3, 1})
   568  		xtesting.Equal(t, Uint8Range(3, 0, 2, true), []uint8{3, 1})
   569  		xtesting.Equal(t, Uint16Range(3, 0, 2, true), []uint16{3, 1})
   570  		xtesting.Equal(t, Uint32Range(3, 0, 2, true), []uint32{3, 1})
   571  		xtesting.Equal(t, Uint64Range(3, 0, 2, true), []uint64{3, 1})
   572  	})
   573  }
   574  
   575  func TestReverseSlice(t *testing.T) {
   576  	for _, tc := range []struct {
   577  		give []int8
   578  		want []int8
   579  	}{
   580  		{[]int8{}, []int8{}},
   581  		{[]int8{0}, []int8{0}},
   582  		{[]int8{1, 2}, []int8{2, 1}},
   583  		{[]int8{1, 2, 3}, []int8{3, 2, 1}},
   584  		{[]int8{0, 0, 0, 0}, []int8{0, 0, 0, 0}},
   585  		{[]int8{1, 1, 3, 2, 2}, []int8{2, 2, 3, 1, 1}},
   586  		{[]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, []int8{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
   587  		{[]int8{0, 1, 4, 7, 2, 5, 8, 3, 6, 9}, []int8{9, 6, 3, 8, 5, 2, 7, 4, 1, 0}},
   588  	} {
   589  		t.Run(fmt.Sprintf("%v", tc.give), func(t *testing.T) {
   590  			l := len(tc.give)
   591  			intSlice, int8Slice, int16Slice, int32Slice, int64Slice := make([]int, l), make([]int8, l), make([]int16, l), make([]int32, l), make([]int64, l)
   592  			uintSlice, uint8Slice, uint16Slice, uint32Slice, uint64Slice := make([]uint, l), make([]uint8, l), make([]uint16, l), make([]uint32, l), make([]uint64, l)
   593  			for _, obj := range []struct {
   594  				slice    interface{}
   595  				f        interface{}
   596  				unsigned bool
   597  			}{
   598  				{intSlice, ReverseIntSlice, false},
   599  				{int8Slice, ReverseInt8Slice, false},
   600  				{int16Slice, ReverseInt16Slice, false},
   601  				{int32Slice, ReverseInt32Slice, false},
   602  				{int64Slice, ReverseInt64Slice, false},
   603  				{uintSlice, ReverseUintSlice, true},
   604  				{uint8Slice, ReverseUint8Slice, true},
   605  				{uint16Slice, ReverseUint16Slice, true},
   606  				{uint32Slice, ReverseUint32Slice, true},
   607  				{uint64Slice, ReverseUint64Slice, true},
   608  			} {
   609  				val := reflect.ValueOf(obj.slice)
   610  				for idx := 0; idx < val.Len(); idx++ {
   611  					if !obj.unsigned {
   612  						val.Index(idx).SetInt(int64(tc.give[idx]))
   613  					} else {
   614  						val.Index(idx).SetUint(uint64(tc.give[idx]))
   615  					}
   616  				}
   617  				reflect.ValueOf(obj.f).Call([]reflect.Value{val})
   618  				xtesting.Equal(t, val.Len(), len(tc.want))
   619  				for i := 0; i < val.Len(); i++ {
   620  					if !obj.unsigned {
   621  						xtesting.Equal(t, int8(val.Index(i).Int()), tc.want[i])
   622  					} else {
   623  						xtesting.Equal(t, int8(val.Index(i).Uint()), tc.want[i])
   624  					}
   625  				}
   626  			}
   627  		})
   628  	}
   629  }