go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/experiments/matchmaker/pkg/sim/lookup_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package sim
     9  
    10  import (
    11  	"math/rand"
    12  	"testing"
    13  
    14  	"go.charczuk.com/sdk/assert"
    15  	"go.charczuk.com/sdk/uuid"
    16  )
    17  
    18  func Test_Lookup_PopRandom(t *testing.T) {
    19  
    20  	rs := rand.NewSource(1234)
    21  	r := rand.New(rs)
    22  
    23  	lu := make(Lookup[uuid.UUID, *Player])
    24  	lu.Add(&Player{ID: uuid.V4()})
    25  	lu.Add(&Player{ID: uuid.V4()})
    26  	lu.Add(&Player{ID: uuid.V4()})
    27  	lu.Add(&Player{ID: uuid.V4()})
    28  
    29  	lu2 := make(Lookup[uuid.UUID, *Player])
    30  	v, ok := lu.PopRandom(r)
    31  	assert.ItsEqual(t, true, ok)
    32  	assert.ItsEqual(t, false, v.ID.IsZero())
    33  	lu2.Add(v)
    34  
    35  	v, ok = lu.PopRandom(r)
    36  	assert.ItsEqual(t, true, ok)
    37  	assert.ItsEqual(t, false, v.ID.IsZero())
    38  	lu2.Add(v)
    39  
    40  	v, ok = lu.PopRandom(r)
    41  	assert.ItsEqual(t, true, ok)
    42  	assert.ItsEqual(t, false, v.ID.IsZero())
    43  	lu2.Add(v)
    44  
    45  	v, ok = lu.PopRandom(r)
    46  	assert.ItsEqual(t, true, ok)
    47  	assert.ItsEqual(t, false, v.ID.IsZero())
    48  	lu2.Add(v)
    49  
    50  	v, ok = lu.PopRandom(r)
    51  	assert.ItsEqual(t, false, ok)
    52  	assert.ItsNil(t, v)
    53  
    54  	assert.ItsEqual(t, 0, len(lu))
    55  	assert.ItsEqual(t, 4, len(lu2))
    56  }