go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

state_test.go (8234B)


      1 package state
      2 
      3 import (
      4 	"bytes"
      5 	"testing"
      6 )
      7 
      8 // Check creation 
      9 func TestNewState(t *testing.T) {
     10 	st := NewState(5)
     11 	if len(st.Flags) != 2 {
     12 		t.Fatalf("invalid state flag length: %v", len(st.Flags))
     13 	}
     14 	st = NewState(8)
     15 	if len(st.Flags) != 2 {
     16 		t.Fatalf("invalid state flag length: %v", len(st.Flags))
     17 	}
     18 	st = NewState(17)
     19 	if len(st.Flags) != 4 {
     20 		t.Fatalf("invalid state flag length: %v", len(st.Flags))
     21 	}
     22 	v := st.FlagBitSize()
     23 	x := uint32(17+8)
     24 	if v != x {
     25 		t.Fatalf("expected %d, get %d", x, v)
     26 	}
     27 	v = uint32(st.FlagByteSize())
     28 	x = 4
     29 	if v != x {
     30 		t.Fatalf("expected %d, get %d", x, v)
     31 	}
     32 	if !IsWriteableFlag(8) {
     33 		t.Fatal("expected true")
     34 	}
     35 }
     36 
     37 func TestStateflags(t *testing.T) {
     38 	st := NewState(9)
     39 	v := st.GetFlag(2)
     40 	if v {
     41 		t.Fatalf("Expected bit 2 not to be set")
     42 	}
     43 	v = st.SetFlag(2)
     44 	if !v {
     45 		t.Fatalf("Expected change to be set for bit 2")
     46 	}
     47 	v = st.GetFlag(2)
     48 	if !v {
     49 		t.Fatalf("Expected bit 2 to be set")
     50 	}
     51 	v = st.SetFlag(10)
     52 	if !v {
     53 		t.Fatalf("Expected change to be set for bit 10")
     54 	}
     55 	v = st.GetFlag(10)
     56 	if !v {
     57 		t.Fatalf("Expected bit 10 to be set")
     58 	}
     59 	v = st.ResetFlag(2)
     60 	if !v {
     61 		t.Fatalf("Expected change to be set for bit 10")
     62 	}
     63 	v = st.GetFlag(2)
     64 	if v {
     65 		t.Fatalf("Expected bit 2 not to be set")
     66 	}
     67 	v = st.GetFlag(10)
     68 	if !v {
     69 		t.Fatalf("Expected bit 10 to be set")
     70 	}
     71 	v = st.SetFlag(10)
     72 	if v {
     73 		t.Fatalf("Expected change not to be set for bit 10")
     74 	}
     75 	v = st.SetFlag(2)
     76 	v = st.SetFlag(16)
     77 	if !bytes.Equal(st.Flags[:3], []byte{0x04, 0x04, 0x01}) {
     78 		t.Fatalf("Expected 0x040401, got %v", st.Flags[:3])
     79 	}
     80 	defer func() {
     81 		if r := recover(); r == nil {
     82 			t.Fatal("did not panic")
     83 		}
     84 	}()
     85 	v = st.SetFlag(42)
     86 }
     87 
     88 func TestStateFlagReset(t *testing.T) {
     89 	st := NewState(9)
     90 	v := st.SetFlag(10)
     91 	v = st.SetFlag(11)
     92 	v = st.ResetFlag(10)
     93 	if !v {
     94 		t.Fatal("expected true")
     95 	}
     96 	v = st.ResetFlag(10)
     97 	if v {
     98 		t.Fatal("expected false")
     99 	}
    100 	defer func() {
    101 		if r := recover(); r == nil {
    102 			t.Fatal("did not panic")
    103 		}
    104 	}()
    105 	v = st.ResetFlag(42)
    106 }
    107 
    108 func TestStateFlagGetOutOfRange(t *testing.T) {
    109 	st := NewState(1)
    110 	defer func() {
    111 		if r := recover(); r == nil {
    112 			t.Fatal("did not panic")
    113 		}
    114 	}()
    115 	st.GetFlag(9)
    116 }
    117 
    118 func TestStateFlagFromSlice(t *testing.T) {
    119 	st := NewState(15)
    120 	st.SetFlag(2)
    121 	v := st.GetIndex([]byte{})
    122 	if v {
    123 		t.Fatalf("Expected no match on empty compare")
    124 	}
    125 	v = st.GetIndex([]byte{0x01})
    126 	if v {
    127 		t.Fatalf("Expected 0x01 not to match")
    128 	}
    129 	v = st.GetIndex([]byte{0x04})
    130 	if !v {
    131 		t.Fatalf("Expected 0x04 to match")
    132 	}
    133 	st.SetFlag(12)
    134 	v = st.GetIndex([]byte{0x04})
    135 	if !v {
    136 		t.Fatalf("Expected 0x04 to match")
    137 	}
    138 	v = st.GetIndex([]byte{0x00, 0x10})
    139 	if !v {
    140 		t.Fatalf("Expected 0x1000 to match")
    141 	}
    142 	v = st.ResetFlag(2)
    143 	v = st.GetIndex([]byte{0x00, 0x10})
    144 	if !v {
    145 		t.Fatalf("Expected 0x1000 to matck")
    146 	}
    147 }
    148 
    149 func TestStateNavigate(t *testing.T) {
    150 	st := NewState(0)
    151 	err := st.Down("foo")
    152 	if err != nil {
    153 		t.Fatal(err)
    154 	}
    155 	err = st.Down("bar")
    156 	if err != nil {
    157 		t.Fatal(err)
    158 	}
    159 	err = st.Down("baz")
    160 	if err != nil {
    161 		t.Fatal(err)
    162 	}
    163 
    164 	s, i := st.Where()
    165 	if s != "baz" {
    166 		t.Fatalf("expected baz, got %s", s)
    167 	}
    168 	if i != 0 {
    169 		t.Fatalf("expected idx 0, got %v", i)
    170 	}
    171 	r := st.Depth()
    172 	if r != 2 {
    173 		t.Fatalf("expected depth 3, got %v", r)
    174 	}
    175 
    176 	s, err = st.Up()
    177 	if err != nil {
    178 		t.Fatal(err)
    179 	}
    180 	if s != "bar" {
    181 		t.Fatalf("expected bar, got %s", s)
    182 	}
    183 	s, i = st.Where()
    184 	if s != "bar" {
    185 		t.Fatalf("expected bar, got %s", s)
    186 	}
    187 	if i != 0 {
    188 		t.Fatalf("expected idx 0, got %v", i)
    189 	}
    190 
    191 	i, err = st.Next()
    192 	if err != nil {
    193 		t.Fatal(err)
    194 	}
    195 	if i != 1 {
    196 		t.Fatalf("expected idx 1, got %v", i)
    197 	}
    198 	i, err = st.Next()
    199 	if err != nil {
    200 		t.Fatal(err)
    201 	}
    202 	if i != 2 {
    203 		t.Fatalf("expected idx 2, got %v", i)
    204 	}
    205 	if err != nil {
    206 		t.Fatal(err)
    207 	}
    208 
    209 	s, i = st.Where()
    210 	if s != "bar" {
    211 		t.Fatalf("expected baz, got %s", s)
    212 	}
    213 	if i != 2 {
    214 		t.Fatalf("expected idx 2, got %v", i)
    215 	}
    216 
    217 	s, err = st.Up()
    218 	if err != nil {
    219 		t.Fatal(err)
    220 	}
    221 	if s != "foo" {
    222 		t.Fatalf("expected foo, got %s", s)
    223 	}
    224 	s, i = st.Where()
    225 	if s != "foo" {
    226 		t.Fatalf("expected foo, got %s", s)
    227 	}
    228 	if i != 0 {
    229 		t.Fatalf("expected idx 0, got %v", i)
    230 	}
    231 }
    232 
    233 func TestStateFlagMatch(t *testing.T) {
    234 	st := NewState(2)
    235 	st.SetFlag(8)
    236 	v := st.MatchFlag(8, true)
    237 	if !v {
    238 		t.Fatalf("unexpected flag")
    239 	}
    240 	v = st.MatchFlag(8, false)
    241 	if v {
    242 		t.Fatalf("unexpected flag")
    243 	}
    244 
    245 	v = st.MatchFlag(9, true)
    246 	if v {
    247 		t.Fatalf("unexpected flag")
    248 	}
    249 	v = st.MatchFlag(9, false)
    250 	if !v {
    251 		t.Fatalf("unexpected flag")
    252 	}
    253 }
    254 
    255 func TestStateMovementNoRoot(t *testing.T) {
    256 	var err error
    257 	st := NewState(0)
    258 	_, err = st.Next()
    259 	if err == nil {
    260 		t.Fatal("expected error")
    261 	}
    262 	_, err = st.Previous()
    263 	if err == nil {
    264 		t.Fatal("expected error")
    265 	}
    266 	vl, vr := st.Sides()
    267 	if vl {
    268 		t.Fatal("expected false")
    269 	}
    270 	if vr {
    271 		t.Fatal("expected false")
    272 	}
    273 	_, err = st.Top()
    274 	if err == nil {
    275 		t.Fatal("expected error")
    276 	}
    277 	_, err = st.Up()
    278 	if err == nil {
    279 		t.Fatal("expected error")
    280 	}
    281 	v := st.Depth()
    282 	if v != -1 {
    283 		t.Fatalf("expected -1, got %d", v)
    284 	}
    285 }
    286 
    287 func TestStateInput(t *testing.T) {
    288 	var err error
    289 	var wrongInput [257]byte
    290 	st := NewState(0)
    291 	_, err = st.GetInput()
    292 	if err == nil {
    293 		t.Fatal("expected error")
    294 	}
    295 	err = st.SetInput(wrongInput[:])
    296 	if err == nil {
    297 		t.Fatal("expected error")
    298 	}
    299 	b := []byte("foo")
    300 	err = st.SetInput(b)
    301 	if err != nil {
    302 		t.Fatal(err)
    303 	}
    304 	v, err := st.GetInput()
    305 	if err != nil {
    306 		t.Fatal(err)
    307 	}
    308 	if !bytes.Equal(v, b) {
    309 		t.Fatalf("expected %x, got %x", b, v)
    310 	}
    311 }
    312 
    313 func TestStateMovement(t *testing.T) {
    314 	var x uint16
    315 	st := NewState(0)
    316 	err := st.Down("foo")
    317 	if err != nil {
    318 		t.Fatal(err)
    319 	}
    320 	r, err := st.Top()
    321 	if err != nil {
    322 		t.Fatal(err)
    323 	}
    324 	if !r {
    325 		t.Fatal("expected true")
    326 	}
    327 	err = st.Down("bar")
    328 	if err != nil {
    329 		t.Fatal(err)
    330 	}
    331 	err = st.Down("baz")
    332 	if err != nil {
    333 		t.Fatal(err)
    334 	}
    335 	v := st.Depth()
    336 	if v != 2 {
    337 		t.Fatalf("expected 1, got %d", v)
    338 	}
    339 	s, err := st.Up()
    340 	if err != nil {
    341 		t.Fatal(err)
    342 	}
    343 	if s != "bar" {
    344 		t.Fatalf("expected 'bar', got '%s'", s)
    345 	}
    346 	v = st.Depth()
    347 	if v != 1 {
    348 		t.Fatalf("expected 1, got %d", v)
    349 	}
    350 	vr, vl := st.Sides()
    351 	if !vr {
    352 		t.Fatal("expected true")
    353 	}
    354 	if vl {
    355 		t.Fatal("expected false")
    356 	}
    357 	_, err = st.Previous()
    358 	if err == nil {
    359 		t.Fatal("expected error")
    360 	}
    361 	x, err = st.Next()
    362 	if err != nil {
    363 		t.Fatal(err)
    364 	}
    365 	if x != 1 {
    366 		t.Fatalf("expected 1, got %d", x)
    367 	}
    368 	vr, vl = st.Sides()
    369 	if !vr {
    370 		t.Fatal("expected true")
    371 	}
    372 	if !vl {
    373 		t.Fatal("expected true")
    374 	}
    375 	x, err = st.Next()
    376 	if err != nil {
    377 		t.Fatal(err)
    378 	}
    379 	if x != 2 {
    380 		t.Fatalf("expected 2, got %d", x)
    381 	}
    382 	_, err = st.Next()
    383 	if err != nil {
    384 		t.Fatal(err)
    385 	}
    386 	s, x = st.Where()
    387 	if s != "bar" {
    388 		t.Fatalf("expected 'baz', got '%s'", s)
    389 	}
    390 	if x != 3 {
    391 		t.Fatalf("expected 3, got '%d'", x)
    392 	}
    393 	x, err = st.Previous()
    394 	if x != 2 {
    395 		t.Fatalf("expected 2, got '%d'", x)
    396 	}
    397 	vl, vr = st.Sides()
    398 	if !vr {
    399 		t.Fatal("expected true")
    400 	}
    401 	if !vl {
    402 		t.Fatal("expected true")
    403 	}
    404 }
    405 
    406 func TestStateMaxMovement(t *testing.T) {
    407 	MaxLevel = 3
    408 	st := NewState(0)
    409 	st.Down("inky")	
    410 	st.Down("pinky")	
    411 	st.Down("blinky")
    412 	st.Down("clyde")
    413 	defer func() {
    414 		if r := recover(); r == nil {
    415 			t.Fatal("did not panic")
    416 		}
    417 	}()
    418 	st.Down("sue")
    419 }
    420 
    421 func TestStateReset(t *testing.T) {
    422 	st := NewState(3)
    423 	st.SetFlag(2)
    424 	st.SetFlag(9)
    425 	st.Down("foo")
    426 	st.Down("bar")
    427 	st.Down("baz")
    428 	st.Next()
    429 	st.Next()
    430 	st.SetInput([]byte("xyzzy"))
    431 
    432 	r, err := st.Top()
    433 	if r {
    434 		t.Fatal("expected false")
    435 	}
    436 
    437 	// check that cloneempty doesnt affect the position of the original
    438 	o := st.CloneEmpty()
    439 	r, err = o.Top()
    440 	if err == nil {
    441 		t.Fatal("expected error")
    442 	}
    443 	r, err = st.Top()
    444 	if r {
    445 		t.Fatal("expected false")
    446 	}
    447 
    448 	err = st.Restart()
    449 	if err != nil {
    450 		t.Fatal(err)
    451 	}
    452 	r, err = st.Top()
    453 	if err != nil {
    454 		t.Fatal(err)
    455 	}
    456 	if !r {
    457 		t.Fatal("expected true")
    458 	}
    459 	if st.GetFlag(2) {
    460 		t.Fatal("expected not set")
    461 	}
    462 	s, v := st.Where()
    463 	if s != "foo" {
    464 		t.Fatalf("expected 'foo', got '%s'", s)
    465 	}
    466 	if v > 0 {
    467 		t.Fatalf("expected 0, got %d", v)
    468 	}
    469 }
    470 
    471 func TestStateLanguage(t *testing.T) {
    472 	st := NewState(0)
    473 	if st.Language != nil {
    474 		t.Fatal("expected language not set")
    475 	}
    476 	err := st.SetLanguage("nor")
    477 	if err != nil {
    478 		t.Fatal(err)
    479 	}
    480 	if st.Language == nil {
    481 		t.Fatal("expected language set")
    482 	}
    483 }