github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/hashtable_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func v(i interface{}) Value {
     8  	return AsValue(i)
     9  }
    10  
    11  func TestHashTable(t *testing.T) {
    12  	var ht *hashTable
    13  	if !ht.full() {
    14  		t.Error("Expected nil hashTable to be full")
    15  	}
    16  	ht = ht.grow()
    17  	if ht.full() {
    18  		t.Error("Expected growed hashTable not to be full")
    19  	}
    20  	ht.set(v("hello"), v(123))
    21  	if ht.find(v("hello")) != v(123) {
    22  		t.Error("expected hello => 123")
    23  	}
    24  	if !ht.full() {
    25  		t.Error("should be full")
    26  	}
    27  }
    28  
    29  func TestMixedTable1To20(t *testing.T) {
    30  	var mt = new(mixedTable)
    31  	const n = 20
    32  	for i := 1; i <= n; i++ {
    33  		mt.insert(v(i), v(i+2))
    34  	}
    35  	for i := 1; i <= n; i++ {
    36  		mti := mt.get(v(i))
    37  		if mti != v(i+2) {
    38  			t.Errorf("Expected mt[%d] to be %d, got %v", i, i+2, mti)
    39  		}
    40  	}
    41  	asz := mt.array.size()
    42  	if asz != 32 {
    43  		t.Errorf("expected array size to be 32, got %d", asz)
    44  	}
    45  	alen := mt.array.getLen()
    46  	if alen != 20 {
    47  		t.Errorf("Expected array length to be 20, got %d", alen)
    48  	}
    49  	mtlen := mt.len()
    50  	if mtlen != 20 {
    51  		t.Errorf("Expected mt to have length 20, got %d", mtlen)
    52  	}
    53  }
    54  
    55  func TestMixedTableRemove(t *testing.T) {
    56  	mt := new(mixedTable)
    57  	for i := 1; i <= 5; i++ {
    58  		mt.insert(v(i), v(i))
    59  	}
    60  	mt.remove(v(5))
    61  	mt.remove(v(4))
    62  	alen := mt.array.getLen()
    63  	if alen != 3 {
    64  		t.Errorf("Expected array length to be 3, got %d", alen)
    65  	}
    66  	mtlen := mt.len()
    67  	if mtlen != 3 {
    68  		t.Errorf("Expected mt length to be 3, got %d", mtlen)
    69  	}
    70  }
    71  
    72  func Test_calculateArraySize(t *testing.T) {
    73  	type args struct {
    74  		idxCountByLen *[uintptrLen]uintptr
    75  	}
    76  	var counts = func(cs ...uintptr) *[uintptrLen]uintptr {
    77  		counts := new([uintptrLen]uintptr)
    78  		for i, c := range cs {
    79  			counts[i] = c
    80  		}
    81  		return counts
    82  	}
    83  
    84  	tests := []struct {
    85  		name string
    86  		args args
    87  		want uintptr
    88  	}{
    89  		{
    90  			name: "empty",
    91  			args: args{
    92  				idxCountByLen: counts(),
    93  			},
    94  			want: 0,
    95  		},
    96  		{
    97  			name: "0",
    98  			args: args{
    99  				idxCountByLen: counts(1),
   100  			},
   101  			want: 1,
   102  		},
   103  		{
   104  			name: "0 1",
   105  			args: args{
   106  				idxCountByLen: counts(1, 1),
   107  			},
   108  			want: 2,
   109  		},
   110  		{
   111  			name: "1",
   112  			args: args{
   113  				idxCountByLen: counts(0, 1),
   114  			},
   115  			want: 2,
   116  		},
   117  		{
   118  			name: "0 1 2",
   119  			args: args{
   120  				idxCountByLen: counts(1, 1, 1),
   121  			},
   122  			want: 4,
   123  		},
   124  		{
   125  			name: "0 1 2 3",
   126  			args: args{
   127  				idxCountByLen: counts(1, 1, 2),
   128  			},
   129  			want: 4,
   130  		},
   131  		{
   132  			name: "1 2 3 4",
   133  			args: args{
   134  				idxCountByLen: counts(0, 1, 2, 1),
   135  			},
   136  			want: 8,
   137  		},
   138  
   139  		{
   140  			name: "0..15",
   141  			args: args{
   142  				idxCountByLen: counts(1, 1, 2, 4, 8),
   143  			},
   144  			want: 16,
   145  		},
   146  		{
   147  			name: "0..16",
   148  			args: args{
   149  				idxCountByLen: counts(1, 1, 2, 4, 8, 1),
   150  			},
   151  			want: 32,
   152  		},
   153  		{
   154  			name: "3..18",
   155  			args: args{
   156  				idxCountByLen: counts(0, 0, 1, 4, 8, 3),
   157  			},
   158  			want: 32,
   159  		},
   160  		{
   161  			name: "10..49",
   162  			args: args{
   163  				idxCountByLen: counts(0, 0, 0, 0, 6, 16, 18),
   164  			},
   165  			want: 64,
   166  		},
   167  	}
   168  
   169  	for _, tt := range tests {
   170  		t.Run(tt.name, func(t *testing.T) {
   171  			if got := calculateArraySize(tt.args.idxCountByLen); got != tt.want {
   172  				t.Errorf("calculateArraySize() = %v, want %v", got, tt.want)
   173  			}
   174  		})
   175  	}
   176  }