github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/table_test.go (about) 1 package runtime 2 3 import "testing" 4 5 func TestTable_Remove(t *testing.T) { 6 tbl := NewTable() 7 for i := 1; i <= 5; i++ { 8 tbl.Set(v(i), v(i)) 9 } 10 tbl.Set(v(5), NilValue) 11 tbl.Set(v(4), NilValue) 12 tlen := tbl.Len() 13 if tlen != 3 { 14 t.Errorf("Expected table length to be 3, got %d", tlen) 15 } 16 } 17 18 func TestTable_FloatKey(t *testing.T) { 19 tbl := NewTable() 20 for i := 1; i <= 5; i++ { 21 tbl.Set(v(i), v(i)) 22 } 23 tbl.Set(v(float64(500)), v("hi")) 24 val := tbl.Get(v(500)) 25 if val != v("hi") { 26 t.Errorf(`Expected "hi", got %v`, val) 27 } 28 } 29 30 func TestTable_Next(t *testing.T) { 31 tbl := NewTable() 32 tbl.Set(v(1), v("x")) 33 tbl.Set(v(2), v("y")) 34 k1, v1, ok1 := tbl.Next(NilValue) 35 if !ok1 { 36 t.Fatal("Next failed at first step") 37 } 38 k2, v2, ok2 := tbl.Next(k1) 39 if !ok2 { 40 t.Fatal("Next failed at second step") 41 } 42 k3, _, ok3 := tbl.Next(k2) 43 if !ok3 { 44 t.Error("Expected ok3 to be true") 45 } 46 if k3 != NilValue { 47 t.Errorf("Expected k3 to be nil, got %v", k3) 48 } 49 if k1 != v(1) { 50 k1, v1, k2, v2 = k2, v2, k1, v1 51 } 52 if !(k1 == v(1) && v1 == v("x") && k2 == v(2) && v2 == v("y")) { 53 t.Errorf("Expected (1, x) and (2, y) to be the items, got (%v, %v) and (%v, %v)", k1, v1, k2, v2) 54 } 55 }