github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/values/values_test.go (about) 1 package values 2 3 import ( 4 "testing" 5 6 "github.com/bananabytelabs/wazero/internal/gojs/goos" 7 "github.com/bananabytelabs/wazero/internal/testing/require" 8 ) 9 10 func Test_Values(t *testing.T) { 11 t.Parallel() 12 13 vs := NewValues() 14 15 err := require.CapturePanic(func() { 16 _ = vs.Get(goos.NextID) 17 }) 18 require.Contains(t, err.Error(), "is out of range 0") 19 20 v1 := "foo" 21 id1 := vs.Increment(v1) 22 v2 := "bar" 23 id2 := vs.Increment(v2) 24 25 require.Equal(t, goos.NextID, id1) 26 require.Equal(t, v1, vs.Get(id1)) 27 28 // Second value should be at a sequential position 29 require.Equal(t, id1+1, id2) 30 require.Equal(t, v2, vs.Get(id2)) 31 32 // Incrementing the ref count should return the same ID 33 require.Equal(t, id1, vs.Increment(v1)) 34 require.Equal(t, v1, vs.Get(id1)) 35 36 // Decrement and we should still get the value 37 vs.Decrement(id1) 38 require.Equal(t, v1, vs.Get(id1)) 39 40 // Decrement again, and we should panic, as go should never attempt to 41 // get a value it already decremented to zero. 42 vs.Decrement(id1) 43 err = require.CapturePanic(func() { 44 _ = vs.Get(id1) 45 }) 46 require.Contains(t, err.Error(), "was nil") 47 48 // Since the ID is no longer in use, we should be able to revive it. 49 require.Equal(t, id1, vs.Increment(v1)) 50 require.Equal(t, v1, vs.Get(id1)) 51 }