package scopes_test import ( tbase "testing" "git.sr.ht/~cco/go-scopes/common/ptr" "git.sr.ht/~cco/go-scopes/common/stack" "git.sr.ht/~cco/go-scopes/common/testing" "git.sr.ht/~cco/go-scopes/common/voc" ) func TestCommon(tb *tbase.T) { t := testing.SetUp(tb) t.Run("stack", StackTest) t.Run("voc", VocTest) t.Run("ptr-value", PtrValuePointerTest) t.Run("ptr-scalar", PtrScalarTest) t.Run("ptr-slice", PtrSliceTest) } func StackTest(t *testing.T) { var st stack.Stack[int] = stack.NewStack[int]() st.Push(3) st.Push(4) t.AssertEqual(st.Depth(), 2) st.Push(5) t.AssertEqual(st.Peek(0), 5) st.Replace(1, 99) st.Push(6) t.AssertEqual(st.Peek(2), 99) st.Push(7) st.Push(8) st.Push(9) t.AssertEqual(st.Peek(2), 7) st.Pop() t.AssertEqual(st.Pop(), 8) t.AssertEqual(st.Peek(st.Depth()-1), 3) } func VocTest(t *testing.T) { v1 := voc.NewVoc[int](nil) v1.Register("one", 1) m := v1.Lookup("one") t.AssertEqual(m.IsNothing(), false) t.AssertEqual(m.Value(), 1) m = v1.Lookup("two") t.AssertEqual(m.IsNothing(), true) } func PtrValuePointerTest(t *testing.T) { var v int var sp1 ptr.Ptr[int] = ptr.NewVP[int](&v) sp1.Append(11) t.AssertEqual(sp1.Value(), 11) t.AssertEqual(sp1.Next(), nil) sp2 := sp1.Copy() t.AssertEqual(sp2.Value(), 11) sp2.Set(42) t.AssertEqual(sp2.Value(), 42) t.AssertEqual(sp1.Value(), 11) sp3 := sp1.Clone() t.AssertEqual(sp3.Value(), 11) sp3.Set(46) t.AssertEqual(sp3.Value(), 46) t.AssertEqual(sp1.Value(), 46) sp4 := sp1.Reset() sp4.Append(33) t.AssertEqual(sp4.Value(), 33) } func PtrScalarTest(t *testing.T) { var sp1 ptr.Ptr[int] = ptr.NewScalar[int]() sp1.Append(11) t.AssertEqual(sp1.Value(), 11) t.AssertEqual(sp1.Next(), nil) sp2 := sp1.Copy() t.AssertEqual(sp2.Value(), 11) sp2.Set(42) t.AssertEqual(sp2.Value(), 42) t.AssertEqual(sp1.Value(), 11) sp3 := sp1.Clone() t.AssertEqual(sp3.Value(), 11) sp3.Set(46) t.AssertEqual(sp3.Value(), 46) t.AssertEqual(sp1.Value(), 46) } func PtrSliceTest(t *testing.T) { var sp1 ptr.Ptr[int] = ptr.NewSlice[int]() sp1.Append(11) t.AssertEqual(sp1.Value(), 11) t.AssertEqual(sp1.Next(), nil) sp1.Append(12) t.AssertEqual(sp1.Size(), 2) sp1.Reset() t.AssertEqual(sp1.Next().Value(), 11) t.AssertEqual(sp1.Value(), 11) sp1.Append(13) t.AssertEqual(sp1.Value(), 13) sp2 := sp1.Clone() t.AssertEqual(sp2.Value(), 13) sp2.Set(31) t.AssertEqual(sp2.Value(), 31) t.AssertEqual(sp1.Value(), 31) sp1.Append(14) t.AssertEqual(sp1.Size(), 4) t.AssertEqual(sp2.Size(), 4) t.AssertEqual(sp1.Value(), 14) t.AssertEqual(sp2.Value(), 31) sp3 := sp2.New() sp3.Next() t.AssertEqual(sp3.Value(), 11) sp3.Append(15) t.AssertEqual(sp1.Size(), 5) sp1.Next() t.AssertEqual(sp1.Value(), 15) t.AssertEqual(sp1.Size(), 5) sp1.Insert(99, 1) t.AssertEqual(sp1.Value(), 99) sp1.Next() t.AssertEqual(sp1.Value(), 12) sp1.Advance(-1) t.AssertEqual(sp1.Value(), 99) sp1.Reset() t.AssertEqual(sp1.Next().Value(), 11) }