vise/state/state_test.go

220 lines
3.7 KiB
Go
Raw Normal View History

2023-03-31 11:52:04 +02:00
package state
import (
2023-04-01 00:25:05 +02:00
"bytes"
2023-03-31 11:52:04 +02:00
"testing"
)
2023-04-01 00:17:44 +02:00
// Check creation
func TestNewState(t *testing.T) {
2023-03-31 14:06:59 +02:00
st := NewState(5)
2023-04-13 01:38:33 +02:00
if len(st.Flags) != 2 {
t.Fatalf("invalid state flag length: %v", len(st.Flags))
2023-03-31 11:52:04 +02:00
}
2023-03-31 14:06:59 +02:00
st = NewState(8)
2023-04-13 01:38:33 +02:00
if len(st.Flags) != 2 {
t.Fatalf("invalid state flag length: %v", len(st.Flags))
2023-03-31 11:52:04 +02:00
}
2023-03-31 14:06:59 +02:00
st = NewState(17)
2023-04-13 01:38:33 +02:00
if len(st.Flags) != 4 {
t.Fatalf("invalid state flag length: %v", len(st.Flags))
2023-04-01 00:17:44 +02:00
}
}
2023-04-13 00:42:36 +02:00
func TestStateflags(t *testing.T) {
st := NewState(9)
2023-04-01 00:17:44 +02:00
v, err := st.GetFlag(2)
if err != nil {
t.Error(err)
}
if v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected bit 2 not to be set")
2023-04-01 00:17:44 +02:00
}
v, err = st.SetFlag(2)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected change to be set for bit 2")
2023-04-01 00:17:44 +02:00
}
v, err = st.GetFlag(2)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected bit 2 to be set")
2023-04-01 00:17:44 +02:00
}
v, err = st.SetFlag(10)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected change to be set for bit 10")
2023-04-01 00:17:44 +02:00
}
v, err = st.GetFlag(10)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected bit 10 to be set")
2023-04-01 00:17:44 +02:00
}
v, err = st.ResetFlag(2)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected change to be set for bit 10")
2023-04-01 00:17:44 +02:00
}
v, err = st.GetFlag(2)
if err != nil {
t.Error(err)
}
if v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected bit 2 not to be set")
2023-04-01 00:17:44 +02:00
}
v, err = st.GetFlag(10)
if err != nil {
t.Error(err)
}
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected bit 10 to be set")
2023-04-01 00:17:44 +02:00
}
v, err = st.SetFlag(10)
if err != nil {
t.Error(err)
}
if v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected change not to be set for bit 10")
2023-03-31 11:52:04 +02:00
}
2023-04-01 00:25:05 +02:00
v, err = st.SetFlag(2)
if err != nil {
t.Error(err)
}
2023-04-01 09:36:26 +02:00
v, err = st.SetFlag(16)
2023-04-01 00:25:05 +02:00
if err != nil {
t.Error(err)
}
2023-04-01 09:36:26 +02:00
v, err = st.SetFlag(17)
if err == nil {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected out of range for bit index 17")
2023-04-01 09:36:26 +02:00
}
2023-04-13 01:38:33 +02:00
if !bytes.Equal(st.Flags[:3], []byte{0x04, 0x04, 0x01}) {
t.Fatalf("Expected 0x040401, got %v", st.Flags[:3])
2023-04-01 09:36:26 +02:00
}
}
func TestStateFlagFromSlice(t *testing.T) {
st := NewState(15)
_, _= st.SetFlag(2)
v := st.GetIndex([]byte{})
if v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected no match on empty compare")
2023-04-01 09:36:26 +02:00
}
v = st.GetIndex([]byte{0x01})
if v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected 0x01 not to match")
2023-04-01 09:36:26 +02:00
}
v = st.GetIndex([]byte{0x04})
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected 0x04 to match")
2023-04-01 09:36:26 +02:00
}
_, _= st.SetFlag(12)
v = st.GetIndex([]byte{0x04})
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected 0x04 to match")
2023-04-01 09:36:26 +02:00
}
v = st.GetIndex([]byte{0x00, 0x10})
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected 0x1000 to match")
2023-04-01 09:36:26 +02:00
}
v, _ = st.ResetFlag(2)
v = st.GetIndex([]byte{0x00, 0x10})
if !v {
2023-04-08 17:09:10 +02:00
t.Fatalf("Expected 0x1000 to matck")
2023-04-01 00:25:05 +02:00
}
2023-03-31 11:52:04 +02:00
}
2023-04-08 17:09:10 +02:00
func TestStateNavigate(t *testing.T) {
st := NewState(0)
err := st.Down("foo")
2023-03-31 13:56:11 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 13:56:11 +02:00
}
2023-04-08 17:09:10 +02:00
err = st.Down("bar")
2023-03-31 13:56:11 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 13:56:11 +02:00
}
2023-04-08 17:09:10 +02:00
err = st.Down("baz")
2023-03-31 14:06:59 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 14:06:59 +02:00
}
2023-03-31 15:04:08 +02:00
2023-04-08 17:09:10 +02:00
s, i := st.Where()
if s != "baz" {
t.Fatalf("expected baz, got %s", s)
2023-03-31 15:04:08 +02:00
}
2023-04-08 17:09:10 +02:00
if i != 0 {
t.Fatalf("expected idx 0, got %v", i)
2023-03-31 15:04:08 +02:00
}
2023-04-08 17:09:10 +02:00
r := st.Depth()
if r != 2 {
t.Fatalf("expected depth 3, got %v", r)
2023-03-31 15:04:08 +02:00
}
2023-04-08 17:09:10 +02:00
s, err = st.Up()
2023-03-31 15:04:08 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 15:04:08 +02:00
}
2023-04-08 17:09:10 +02:00
if s != "bar" {
t.Fatalf("expected bar, got %s", s)
2023-03-31 15:04:08 +02:00
}
2023-04-08 17:09:10 +02:00
s, i = st.Where()
if s != "bar" {
t.Fatalf("expected bar, got %s", s)
}
if i != 0 {
t.Fatalf("expected idx 0, got %v", i)
}
2023-03-31 21:42:13 +02:00
2023-04-08 17:09:10 +02:00
i, err = st.Next()
2023-03-31 21:42:13 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 21:42:13 +02:00
}
2023-04-08 17:09:10 +02:00
if i != 1 {
t.Fatalf("expected idx 1, got %v", i)
2023-03-31 21:42:13 +02:00
}
2023-04-08 17:09:10 +02:00
i, err = st.Next()
2023-03-31 21:42:13 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 21:42:13 +02:00
}
2023-04-08 17:09:10 +02:00
if i != 2 {
t.Fatalf("expected idx 2, got %v", i)
2023-03-31 21:42:13 +02:00
}
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 21:42:13 +02:00
}
2023-03-31 22:08:06 +02:00
2023-04-08 17:09:10 +02:00
s, i = st.Where()
if s != "bar" {
t.Fatalf("expected baz, got %s", s)
2023-03-31 22:08:06 +02:00
}
2023-04-08 17:09:10 +02:00
if i != 2 {
t.Fatalf("expected idx 2, got %v", i)
2023-03-31 22:08:06 +02:00
}
2023-04-08 17:09:10 +02:00
s, err = st.Up()
2023-03-31 22:08:06 +02:00
if err != nil {
2023-04-08 17:09:10 +02:00
t.Fatal(err)
2023-03-31 22:08:06 +02:00
}
2023-04-08 17:09:10 +02:00
if s != "foo" {
t.Fatalf("expected foo, got %s", s)
2023-03-31 22:08:06 +02:00
}
2023-04-08 17:09:10 +02:00
s, i = st.Where()
if s != "foo" {
t.Fatalf("expected foo, got %s", s)
2023-03-31 22:08:06 +02:00
}
2023-04-08 17:09:10 +02:00
if i != 0 {
t.Fatalf("expected idx 0, got %v", i)
2023-03-31 22:08:06 +02:00
}
}