github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/array_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  /* Ensure nextSize() gives linear amortized cost of realloc/copy */
     8  func TestGrowth(t *testing.T) {
     9  	var currentSize = 0
    10  	var copyCost int64 = 0
    11  
    12  	// Make sure it hits math.MaxInt32, if we insist:
    13  	for currentSize != MAX_ARRAY_LENGTH {
    14  		nextSize := Oversize(1+currentSize, NUM_BYTES_OBJECT_REF)
    15  		assert2(nextSize > currentSize, "%v -> %v", currentSize, nextSize)
    16  		if currentSize > 0 {
    17  			copyCost += int64(currentSize)
    18  			copyCostPerElement := float64(copyCost) / float64(currentSize)
    19  			assert2(copyCostPerElement < 10, "cost %v", copyCostPerElement)
    20  		}
    21  		currentSize = nextSize
    22  	}
    23  }