package bitmap_test import ( "fmt" "math/rand" "os" "strings" "testing" "bitmap" ) func TestBitVectorSize(t *testing.T) { type testCase struct { width, height uint32 expectSize int } for _, tc := range []testCase{ { width: 10, height: 10, expectSize: 4, }, { width: 1, height: 1, expectSize: 1, }, { width: 32, height: 32, expectSize: 32, }, } { t.Run(fmt.Sprintf("w=%d h=%d s=%d", tc.width, tc.height, tc.expectSize), func(t *testing.T) { bm := bitmap.NewBitmap(tc.width, tc.height) l := len(bitmap.Value(bm)) if tc.expectSize != l { t.Errorf("expected bitmap size: %d, got: %d", tc.expectSize, l) } }) } } func TestSetPixel(t *testing.T) { type coord struct { x, y int } type testCase struct { width, height uint32 pixels []coord bits []int } asMap := func(bits []int) map[int]bool { result := make(map[int]bool) for i := range bits { if _, ok := result[bits[i]]; ok { t.Fatalf("source bits duplicate at idx=%d", i) } else { result[bits[i]] = true } } return result } asUint32 := func(v bool) uint32 { return map[bool]uint32{true: 1, false: 0}[v] } for i, tc := range []testCase{ { width: 5, height: 5, pixels: []coord{{0, 0}}, bits: []int{0}, }, { width: 5, height: 5, pixels: []coord{{1, 0}}, bits: []int{1}, }, { width: 5, height: 5, pixels: []coord{{2, 0}}, bits: []int{2}, }, { width: 5, height: 5, pixels: []coord{{0, 1}}, bits: []int{5}, }, { width: 5, height: 5, pixels: []coord{{4, 4}}, bits: []int{24}, }, { width: 8, height: 8, pixels: []coord{{7, 7}}, bits: []int{63}, }, } { t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) { bm := bitmap.NewBitmap(tc.width, tc.height) for _, c := range tc.pixels { if bm.IsSet(c.x, c.y) { t.Errorf("expected pixel to be clear at x=%d y=%d", c.x, c.y) } bm.Set(c.x, c.y) if !bm.IsSet(c.x, c.y) { t.Errorf("expected pixel to be set at x=%d y=%d", c.x, c.y) } } bitVector := bitmap.Value(bm) bitNum := 0 expected := asMap(tc.bits) for bi := range bitVector { for ; bitNum < (bi+1)*32; bitNum++ { if (bitVector[bi]>>(bitNum%32))&1 != asUint32(expected[bitNum]) { t.Errorf("expected: bit #%d to be %t, got %v", bitNum, expected[bitNum], uint32(1<<(bitNum%32))) } } } }) } } func TestClear(t *testing.T) { var bm = bitmap.NewBitmap(10, 10) for range 50 { bm.Set(rand.Intn(10), rand.Intn(10)) } var acc uint32 for _, holder := range bitmap.Value(bm) { acc |= holder } if acc == 0 { t.Errorf("some pixels should be set") } bm.Clear() acc = 0 for _, holder := range bitmap.Value(bm) { acc |= holder } if acc != 0 { t.Errorf("all pixels should be clear") } } func TestCircle5x5(t *testing.T) { type testCase struct { x, y int r float64 filled bool } bm := bitmap.NewBitmap(80, 80) for i, tc := range []testCase{ {3, 3, 0.9, false}, } { file := fmt.Sprintf("assets_test/circle_case_%02d.txt", i) _ = file t.Run(file, func(t *testing.T) { bm.CircleAdjacent(tc.x, tc.y, tc.r) b, err := os.ReadFile(file) if err != nil { t.Fatal(err) } expect := strings.TrimSpace(string(b)) if expect != bm.String() { t.Errorf("expect:\n%s\ngot:\n%s\n", expect, bm.String()) } }) } } func TestCircle(t *testing.T) { var size int = 40 var bm1 = bitmap.NewBitmap(uint32(size), uint32(size)) bm1.Circle(size/2+5, size/2-5, float64(size/2)-3) fmt.Println(bm1) var bm2 = bitmap.NewBitmap(uint32(size), uint32(size)) bm2.CircleAdjacent(size/2+20, size/2, float64(size/2)-5) }