github.com/haraldrudell/parl@v0.4.176/preflect/int_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 preflect
     7  
     8  import (
     9  	"math"
    10  	"testing"
    11  	"unsafe"
    12  )
    13  
    14  func TestIntValue(t *testing.T) {
    15  	hasValueTrue := true
    16  	hasValueFalse := false
    17  	isSignedFalse := false
    18  	isSignedTrue := true
    19  	isPointerTrue := true
    20  	isPointerFalse := false
    21  	type args struct {
    22  		value any
    23  	}
    24  	tests := []struct {
    25  		name          string
    26  		args          args
    27  		wantU64       uint64
    28  		wantI64       int64
    29  		wantHasValue  bool
    30  		wantIsSigned  bool
    31  		wantIsPointer bool
    32  	}{
    33  		{"nil", args{nil}, 0, 0, hasValueTrue, isSignedFalse, isPointerTrue},
    34  		{"uint", args{uint(1)}, 1, 0, hasValueTrue, isSignedFalse, isPointerFalse},
    35  		{"int", args{-1}, math.MaxUint64, -1, hasValueTrue, isSignedTrue, isPointerFalse},
    36  		{"non-integer", args{false}, 0, 0, hasValueFalse, isSignedFalse, isPointerFalse},
    37  		{"pointer", args{&hasValueTrue}, uint64(uintptr(unsafe.Pointer(&hasValueTrue))), 0, hasValueTrue, isSignedFalse, isPointerTrue},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			gotU64, gotI64, gotHasValue, gotIsSigned, gotIsPointer := IntValue(tt.args.value)
    42  			if gotU64 != tt.wantU64 {
    43  				t.Errorf("IntValue() gotU64 = %v, want %v", gotU64, tt.wantU64)
    44  			}
    45  			if gotI64 != tt.wantI64 {
    46  				t.Errorf("IntValue() gotI64 = %v, want %v", gotI64, tt.wantI64)
    47  			}
    48  			if gotHasValue != tt.wantHasValue {
    49  				t.Errorf("IntValue() gotHasValue = %v, want %v", gotHasValue, tt.wantHasValue)
    50  			}
    51  			if gotIsSigned != tt.wantIsSigned {
    52  				t.Errorf("IntValue() gotIsSigned = %v, want %v", gotIsSigned, tt.wantIsSigned)
    53  			}
    54  			if gotIsPointer != tt.wantIsPointer {
    55  				t.Errorf("IntValue() gotIsPointer = %v, want %v", gotIsPointer, tt.wantIsPointer)
    56  			}
    57  		})
    58  	}
    59  }