github.com/mnlphlp/aoc22@v0.0.0-20230330151331-c1dc4bff1b9b/day23/grid/grid_test.go (about)

     1  package grid
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/mnlphlp/aoc22/util"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  const testInput = `....#..
    14  ..###.#
    15  #...#.#
    16  .#...##
    17  #.###..
    18  ##.#.##
    19  .#..#..`
    20  
    21  func TestInsert(t *testing.T) {
    22  	grid := ParseInput(testInput)
    23  	testPos := []util.Pos2{}
    24  	for i := 0; i < 10; i++ {
    25  		testPos = append(testPos, util.Pos2{rand.Intn(200) - 100, rand.Intn(100) - 100})
    26  	}
    27  	fmt.Println(testPos)
    28  	for i, pos := range testPos {
    29  		grid = grid.Insert(pos)
    30  		for _, test := range testPos[:i+1] {
    31  			require.True(t, grid.Contains(test), "Error when testing %v after inserting %v", test, pos)
    32  		}
    33  	}
    34  }
    35  
    36  func TestInsert2(t *testing.T) {
    37  	g := Grid{}
    38  	a := util.Pos2{-3, 9}
    39  	b := util.Pos2{-4, -1}
    40  	g = g.Insert(a)
    41  	require.True(t, g.Contains(a))
    42  	g = g.Insert(b)
    43  	require.True(t, g.Contains(a))
    44  	require.True(t, g.Contains(b))
    45  }
    46  
    47  func TestContains(t *testing.T) {
    48  	grid := ParseInput(testInput)
    49  	testPos := []util.Pos2{
    50  		{0, 2},
    51  		{2, 1},
    52  		{3, 1},
    53  	}
    54  	for _, pos := range testPos {
    55  		require.True(t, grid.Contains(pos), "%v not found in grid", pos)
    56  	}
    57  }
    58  
    59  func TestHasNeighbors(t *testing.T) {
    60  	g := Grid{}
    61  	for i := 0; i < 100; i++ {
    62  		x, y := rand.Intn(200), rand.Intn(200)
    63  		dir := DirectionGroups[rand.Intn(200)%4][rand.Intn(200)%3]
    64  		p := util.Pos2{x, y}
    65  		g = g.Insert(p)
    66  		n := util.Pos2{x + dir.X, y + dir.Y}
    67  		g = g.Insert(n)
    68  		assert.True(t, g.HasNeighbor(p), "%v not registered as neighbor %v", n, p)
    69  	}
    70  }