github.com/MikyChow/arbitrum-go-ethereum@v0.0.0-20230306102812-078da49636de/metrics/chunked_associative_array_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Ported from
     8  // https://github.com/dropwizard/metrics/blob/release/4.2.x/metrics-core/src/main/java/com/codahale/metrics/ChunkedAssociativeLongArray.java
     9  
    10  func TestChunkedAssociativeArray_Put(t *testing.T) {
    11  	array := NewChunkedAssociativeArray(3)
    12  	// Test that time cannot go backwards
    13  	array.Put(7, 7)
    14  	expectedStringBefore := "[(7: 7) ]"
    15  	if array.String() != expectedStringBefore {
    16  		t.Errorf("initial array string incorrect: %s", array.String())
    17  	}
    18  }
    19  
    20  func TestChunkedAssociativeArray_ValuesEmpty(t *testing.T) {
    21  	array := NewChunkedAssociativeArray(3)
    22  
    23  	values := array.Values()
    24  	if len(values) != 1 {
    25  		t.Fatalf("unexpected length of values: %d", len(values))
    26  	}
    27  	if values[0] != 0 {
    28  		t.Errorf("unexpected value in empty values: %d", values[0])
    29  	}
    30  }
    31  
    32  func TestChunkedAssociativeArray_Trim(t *testing.T) {
    33  	array := NewChunkedAssociativeArray(3)
    34  	array.Put(-7, 7)
    35  	array.Put(-5, 7)
    36  	array.Put(-4, 7)
    37  	array.Put(-3, 3)
    38  	array.Put(-2, 1)
    39  	array.Put(0, 5)
    40  	array.Put(3, 0)
    41  	array.Put(9, 8)
    42  	array.Put(15, 0)
    43  	array.Put(19, 5)
    44  	array.Put(21, 5)
    45  	array.Put(34, -9)
    46  	array.Put(109, 5)
    47  
    48  	expectedStringBefore := "[(-7: 7) (-5: 7) (-4: 7) ]->[(-3: 3) (-2: 1) (0: 5) ]->[(3: 0) (9: 8) (15: 0) ]->[(19: 5) (21: 5) (34: -9) ]->[(109: 5) ]"
    49  	if array.String() != expectedStringBefore {
    50  		t.Errorf("initial array string incorrect: %s", array.String())
    51  	}
    52  	valuesBefore := array.Values()
    53  	expectedValuesBefore := []int64{7, 7, 7, 3, 1, 5, 0, 8, 0, 5, 5, -9, 5}
    54  	if len(valuesBefore) != len(expectedValuesBefore) {
    55  		t.Errorf("initial values returned incorrect length: %d", len(valuesBefore))
    56  	} else {
    57  		for i, value := range valuesBefore {
    58  			if value != expectedValuesBefore[i] {
    59  				t.Errorf("unexpected value %d at index %d", value, i)
    60  			}
    61  		}
    62  	}
    63  	if array.Size() != 13 {
    64  		t.Errorf("initial array size incorrect: %d", array.Size())
    65  	}
    66  
    67  	array.Trim(-2, 20)
    68  
    69  	expectedStringAfter := "[(-2: 1) (0: 5) ]->[(3: 0) (9: 8) (15: 0) ]->[(19: 5) ]"
    70  	if array.String() != expectedStringAfter {
    71  		t.Errorf("array string incorrect: %s", array.String())
    72  	}
    73  	valuesAfter := array.Values()
    74  	expectedValuesAfter := []int64{1, 5, 0, 8, 0, 5}
    75  	if len(valuesAfter) != len(expectedValuesAfter) {
    76  		t.Errorf("values returned incorrect length: %d", len(valuesAfter))
    77  	} else {
    78  		for i, value := range valuesAfter {
    79  			if value != expectedValuesAfter[i] {
    80  				t.Errorf("unexpected value %d at index %d", value, i)
    81  			}
    82  		}
    83  	}
    84  	if array.Size() != 6 {
    85  		t.Errorf("array size incorrect: %d", array.Size())
    86  	}
    87  
    88  	array.Trim(-2, 16)
    89  	expectedStringAfter2 := "[(-2: 1) (0: 5) ]->[(3: 0) (9: 8) (15: 0) ]"
    90  	if array.String() != expectedStringAfter2 {
    91  		t.Errorf("array string incorrect: %s", array.String())
    92  	}
    93  
    94  	initialCacheCount := array.chunksCache.Len()
    95  
    96  	// Have AllocateChunk take from cache
    97  	array.Put(200, 555)
    98  	expectedStringAfter3 := "[(-2: 1) (0: 5) ]->[(3: 0) (9: 8) (15: 0) ]->[(200: 555) ]"
    99  	if array.String() != expectedStringAfter3 {
   100  		t.Errorf("array string incorrect: %s", array.String())
   101  	}
   102  
   103  	if array.chunksCache.Len() >= initialCacheCount {
   104  		t.Error("cache not used when allocating chunk")
   105  	}
   106  }
   107  
   108  func TestAssociativeArrayChunk_IsFirstElementEmptyOrGreaterEqualThanKey(t *testing.T) {
   109  	chunk := NewAssociativeArrayChunk(3)
   110  
   111  	if !chunk.IsFirstElementEmptyOrGreaterEqualThanKey(5) {
   112  		t.Error("empty test failed")
   113  	}
   114  
   115  	chunk.keys = []int64{41, 42, 43}
   116  	chunk.startIndex = 1
   117  
   118  	if chunk.IsFirstElementEmptyOrGreaterEqualThanKey(43) {
   119  		t.Error("element less than key test failed")
   120  	}
   121  	if !chunk.IsFirstElementEmptyOrGreaterEqualThanKey(42) {
   122  		t.Error("element greater than or equal to key test failed")
   123  	}
   124  }
   125  
   126  func TestAssociativeArrayChunk_IsLastElementIsLessThanKey(t *testing.T) {
   127  	chunk := NewAssociativeArrayChunk(3)
   128  
   129  	if !chunk.IsLastElementEmptyOrLessThanKey(5) {
   130  		t.Error("empty test failed")
   131  	}
   132  
   133  	chunk.keys = []int64{41, 42, 43}
   134  	chunk.cursor = 2
   135  
   136  	if !chunk.IsLastElementEmptyOrLessThanKey(43) {
   137  		t.Error("element less than key test failed")
   138  	}
   139  	if chunk.IsLastElementEmptyOrLessThanKey(42) {
   140  		t.Error("element greater than or equal to key test failed")
   141  	}
   142  }
   143  
   144  func TestAssociativeArrayChunk_FindFirstIndexOfGreaterEqualElements(t *testing.T) {
   145  	chunk := NewAssociativeArrayChunk(7)
   146  
   147  	if chunk.FindFirstIndexOfGreaterEqualElements(5) != 0 {
   148  		t.Error("empty test failed")
   149  	}
   150  
   151  	chunk.keys = []int64{41, 42, 43, 44, 45, 46, 48}
   152  	chunk.startIndex = 1
   153  	chunk.cursor = 6
   154  
   155  	if chunk.FindFirstIndexOfGreaterEqualElements(41) != chunk.startIndex {
   156  		t.Error("minKey less than first element test failed")
   157  	}
   158  	if chunk.FindFirstIndexOfGreaterEqualElements(42) != chunk.startIndex {
   159  		t.Error("minKey greater than or equal to first element test failed")
   160  	}
   161  	if chunk.FindFirstIndexOfGreaterEqualElements(43) != 2 {
   162  		t.Error("2nd element test failed")
   163  	}
   164  	if chunk.FindFirstIndexOfGreaterEqualElements(46) != 5 {
   165  		t.Error("last element test failed")
   166  	}
   167  	if chunk.FindFirstIndexOfGreaterEqualElements(49) != 6 {
   168  		t.Error("past last element test failed")
   169  	}
   170  }