github.com/haraldrudell/parl@v0.4.176/ints/int-properties_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package ints provide manipulation of integer types.
     7  package ints
     8  
     9  import (
    10  	"math"
    11  	"strconv"
    12  	"testing"
    13  	"unsafe"
    14  
    15  	"golang.org/x/exp/constraints"
    16  )
    17  
    18  func TestIntProperties(t *testing.T) {
    19  	//t.Fail()
    20  
    21  	const cIsUnsigned, cIsSigned = false, true
    22  	const cMinIsZero = 0
    23  	type IntPropertiesFunc func() (isSigned bool, maxPositive uint64, maxNegative int64, sizeof int)
    24  	var typs = []struct {
    25  		basicTypeName     string
    26  		intPropertiesFunc IntPropertiesFunc
    27  		isSigned          bool
    28  		maxPositive       uint64
    29  		maxNegative       int64
    30  		sizeof            int
    31  	}{
    32  		{"uint8", func() (bool, uint64, int64, int) { return IntProperties(uint8(0)) }, cIsUnsigned, math.MaxUint8, cMinIsZero, int(unsafe.Sizeof(uint8(0)))},
    33  		{"int8", func() (bool, uint64, int64, int) { return IntProperties(int8(0)) }, cIsSigned, math.MaxInt8, math.MinInt8, int(unsafe.Sizeof(int8(0)))},
    34  		{"uint64", func() (bool, uint64, int64, int) { return IntProperties(uint64(0)) }, cIsUnsigned, math.MaxUint64, cMinIsZero, int(unsafe.Sizeof(uint64(0)))},
    35  		{"int64", func() (bool, uint64, int64, int) { return IntProperties(int64(0)) }, cIsSigned, math.MaxInt64, math.MinInt64, int(unsafe.Sizeof(int64(0)))},
    36  		{"uintptr", func() (bool, uint64, int64, int) { return IntProperties(uintptr(0)) }, cIsUnsigned, math.MaxUint64, cMinIsZero, int(unsafe.Sizeof(uintptr(0)))},
    37  	}
    38  	for _, typ := range typs {
    39  		var isSigned, maxPositive, maxNegative, sizeof = typ.intPropertiesFunc()
    40  		t.Logf("%s maxPositive: %s maxNegative: %s", typ.basicTypeName, signedHexadecimal(maxPositive), signedHexadecimal(maxNegative))
    41  		if isSigned != typ.isSigned {
    42  			t.Errorf("%s isSigned %t", typ.basicTypeName, isSigned)
    43  		}
    44  		if maxPositive != typ.maxPositive {
    45  			t.Errorf("%s maxPositive %d exp %d", typ.basicTypeName, maxPositive, typ.maxPositive)
    46  		}
    47  		if maxNegative != typ.maxNegative {
    48  			t.Errorf("%s maxNegative %d exp %d", typ.basicTypeName, maxNegative, typ.maxNegative)
    49  		}
    50  		if sizeof != typ.sizeof {
    51  			t.Errorf("%s sizeof %d exp %d", typ.basicTypeName, sizeof, typ.sizeof)
    52  		}
    53  	}
    54  }
    55  
    56  // signedHexadecimal returns a human-readable hexadecimal string
    57  //   - positive → "0xffff_ffff_ffff_f800"
    58  //   - negative → ""
    59  func signedHexadecimal[T constraints.Integer](integer T) (hexString string) {
    60  	var u64 uint64
    61  	var isNegative bool
    62  	if isNegative = integer < 0; isNegative {
    63  		u64 = uint64(-integer)
    64  	} else {
    65  		u64 = uint64(integer)
    66  	}
    67  	hexString = "0x" + strconv.FormatUint(u64, 16)
    68  	if isNegative {
    69  		hexString = "-" + hexString
    70  	}
    71  	for i := len(hexString) - 4; i > 2; i -= 4 {
    72  		hexString = hexString[:i] + "_" + hexString[i:]
    73  	}
    74  	return
    75  }