github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/graph/set/uid/uid_test.go (about)

     1  // Copyright ©2020 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package uid implements unique ID provision for graphs.
     6  package uid
     7  
     8  import (
     9  	"math"
    10  	"testing"
    11  
    12  	"golang.org/x/exp/rand"
    13  )
    14  
    15  func TestSetChurn(t *testing.T) {
    16  	rnd := rand.New(rand.NewSource(1))
    17  
    18  	set := NewSet()
    19  
    20  	// Iterate over a number of ID allocations,
    21  	// occasionally deleting IDs from the store.
    22  	seen := make(map[int64]bool)
    23  	for k := 0; k < 2; k++ {
    24  		for i := 0; i < 1e4; i++ {
    25  			id := set.NewID()
    26  			if seen[id] {
    27  				t.Fatalf("NewID returned already used ID")
    28  			}
    29  			set.Use(id)
    30  			seen[id] = true
    31  			if rnd.Float64() < 0.01 {
    32  				j := rnd.Intn(10)
    33  				for id := range seen {
    34  					set.Release(id)
    35  					delete(seen, id)
    36  					j--
    37  					if j <= 0 {
    38  						break
    39  					}
    40  				}
    41  			}
    42  		}
    43  
    44  		// Kick the set into scavenging mode.
    45  		set.Use(math.MaxInt64)
    46  	}
    47  }