package pointer_test import ( "testing" "git.brut.systems/judah/xx/pointer" ) func TestPointer_AlignForward(t *testing.T) { tests := []struct { name string offset uintptr }{ {"align 8 bytes", 1}, {"align 16 bytes", 3}, {"align 32 bytes", 7}, {"align 64 bytes", 15}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { value := int32(789) pinned := pointer.Pin(&value) defer pinned.Unpin() // Add offset to misalign misaligned := pinned.Add(tt.offset) aligned := misaligned.AlignForward() // Check alignment if !aligned.Aligned() { t.Errorf("Address %d is not aligned", aligned.Address()) } // 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 TestPointer_AlignBackward(t *testing.T) { tests := []struct { name string offset uintptr }{ {"align 8 bytes", 5}, {"align 16 bytes", 10}, {"align 32 bytes", 20}, {"align 64 bytes", 40}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { value := int32(321) pinned := pointer.Pin(&value) defer pinned.Unpin() // Add offset to misalign misaligned := pinned.Add(tt.offset) aligned := misaligned.AlignBackward() // Check alignment if !aligned.Aligned() { t.Errorf("Address %d is not aligned", aligned.Address()) } // 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 TestPointer_Nth(t *testing.T) { // Test with int32 array arr := []int32{10, 20, 30, 40, 50} pinned := pointer.Pin(&arr[0]) defer pinned.Unpin() for i := 0; i < len(arr); i++ { value := pinned.Nth(i) if value != arr[i] { t.Errorf("Index %d: expected %d, got %d", i, arr[i], value) } } } func TestPointer_NthFloat64(t *testing.T) { // Test with a float64 array arr := []float64{1.1, 2.2, 3.3, 4.4, 5.5} pinned := pointer.Pin(&arr[0]) defer pinned.Unpin() for i := 0; i < len(arr); i++ { value := pinned.Nth(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 := pointer.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()) } } func TestPointer_Cast(t *testing.T) { value := int32(123) i32 := pointer.Pin(&value) defer i32.Unpin() f32 := pointer.Cast[float32](i32) f32.Store(3.14) if value == 123 { t.Errorf("Value should have been changed") } }