117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package pointer
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestAlignForward(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
offset uintptr
|
|
alignment uintptr
|
|
}{
|
|
{"align 8 bytes", 1, 8},
|
|
{"align 16 bytes", 3, 16},
|
|
{"align 32 bytes", 7, 32},
|
|
{"align 64 bytes", 15, 64},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
value := int32(789)
|
|
pinned := Pin(&value)
|
|
defer pinned.Unpin()
|
|
|
|
// Add offset to misalign
|
|
misaligned := pinned.Add(tt.offset)
|
|
aligned := misaligned.AlignForward(tt.alignment)
|
|
|
|
// Check alignment
|
|
if aligned.Address()%tt.alignment != 0 {
|
|
t.Errorf("Address %d is not aligned to %d bytes", aligned.Address(), tt.alignment)
|
|
}
|
|
|
|
// Check it's forward aligned (greater or equal)
|
|
if aligned.Address() < misaligned.Address() {
|
|
t.Errorf("Forward aligned address %d should be >= original %d", aligned.Address(), misaligned.Address())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAlignBackward(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
offset uintptr
|
|
alignment uintptr
|
|
}{
|
|
{"align 8 bytes", 5, 8},
|
|
{"align 16 bytes", 10, 16},
|
|
{"align 32 bytes", 20, 32},
|
|
{"align 64 bytes", 40, 64},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
value := int32(321)
|
|
pinned := Pin(&value)
|
|
defer pinned.Unpin()
|
|
|
|
// Add offset to misalign
|
|
misaligned := pinned.Add(tt.offset)
|
|
aligned := misaligned.AlignBackward(tt.alignment)
|
|
|
|
// Check alignment
|
|
if aligned.Address()%tt.alignment != 0 {
|
|
t.Errorf("Address %d is not aligned to %d bytes", aligned.Address(), tt.alignment)
|
|
}
|
|
|
|
// Check it's backward aligned (less or equal)
|
|
if aligned.Address() > misaligned.Address() {
|
|
t.Errorf("Backward aligned address %d should be <= original %d", aligned.Address(), misaligned.Address())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNth(t *testing.T) {
|
|
// Test with int32 array
|
|
arr := []int32{10, 20, 30, 40, 50}
|
|
pinned := Pin(&arr[0])
|
|
defer pinned.Unpin()
|
|
|
|
for i := 0; i < len(arr); i++ {
|
|
value := Nth[int32](pinned, i)
|
|
if value != arr[i] {
|
|
t.Errorf("Index %d: expected %d, got %d", i, arr[i], value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNthFloat64(t *testing.T) {
|
|
// Test with a float64 array
|
|
arr := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
|
|
pinned := Pin(&arr[0])
|
|
defer pinned.Unpin()
|
|
|
|
for i := 0; i < len(arr); i++ {
|
|
value := Nth[float64](pinned, i)
|
|
if value != arr[i] {
|
|
t.Errorf("Index %d: expected %f, got %f", i, arr[i], value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPointerArithmeticChain(t *testing.T) {
|
|
value := int32(888)
|
|
pinned := Pin(&value)
|
|
defer pinned.Unpin()
|
|
|
|
// Test chaining operations
|
|
result := pinned.Add(16).Add(8).Sub(4)
|
|
expected := pinned.Address() + 16 + 8 - 4
|
|
|
|
if result.Address() != expected {
|
|
t.Errorf("Expected address %d, got %d", expected, result.Address())
|
|
}
|
|
}
|