github.com/wasilibs/nottinygc@v0.7.2-0.20240312114022-d59c9478ef51/intmap_test.go (about)

     1  // Copyright wasilibs authors
     2  // SPDX-License-Identifier: MIT
     3  
     4  package nottinygc
     5  
     6  import "testing"
     7  
     8  func TestIntMapBasic(t *testing.T) {
     9  	m := newIntMap()
    10  	_, ok := m.get(5)
    11  	if ok {
    12  		t.Fatal("expected not ok in empty map")
    13  	}
    14  
    15  	m.put(5, 10)
    16  	v, ok := m.get(5)
    17  	if !ok {
    18  		t.Fatal("expected ok in map")
    19  	}
    20  	if v != 10 {
    21  		t.Fatal("expected 10 in map")
    22  	}
    23  }
    24  
    25  func TestIntMapNoResize(t *testing.T) {
    26  	top := int(0.75 * 512)
    27  	m := newIntMap()
    28  	for i := 0; i < top; i++ {
    29  		m.put(uintptr(i), uintptr(i))
    30  	}
    31  
    32  	if len(m.buckets) != 512 {
    33  		t.Fatal("expected 512 buckets")
    34  	}
    35  
    36  	for i := 0; i < top; i++ {
    37  		v, ok := m.get(uintptr(i))
    38  		if !ok {
    39  			t.Fatalf("expected %d to be in map", i)
    40  		}
    41  		if v != uintptr(i) {
    42  			t.Fatalf("expected %d to have value %d in map, got %d", i, i, v)
    43  		}
    44  	}
    45  	_, ok := m.get(uintptr(top))
    46  	if ok {
    47  		t.Fatal("expected not ok in map")
    48  	}
    49  }
    50  
    51  func TestIntMapResize(t *testing.T) {
    52  	top := 512
    53  	m := newIntMap()
    54  	for i := 0; i < top; i++ {
    55  		m.put(uintptr(i), uintptr(i))
    56  	}
    57  
    58  	if len(m.buckets) != 1024 {
    59  		t.Fatal("expected 1024 buckets")
    60  	}
    61  
    62  	for i := 0; i < top; i++ {
    63  		v, ok := m.get(uintptr(i))
    64  		if !ok {
    65  			t.Fatalf("expected %d to be in map", i)
    66  		}
    67  		if v != uintptr(i) {
    68  			t.Fatalf("expected %d to have value %d in map, got %d", i, i, v)
    69  		}
    70  	}
    71  	_, ok := m.get(uintptr(top))
    72  	if ok {
    73  		t.Fatal("expected not ok in map")
    74  	}
    75  }