fs_test.go (3320B)
1 package persist 2 3 import ( 4 "bytes" 5 "context" 6 "reflect" 7 "testing" 8 9 "git.defalsify.org/vise.git/cache" 10 "git.defalsify.org/vise.git/state" 11 "git.defalsify.org/vise.git/vm" 12 "git.defalsify.org/vise.git/db/mem" 13 ) 14 15 func TestSerializeState(t *testing.T) { 16 st := state.NewState(12) 17 st.Down("foo") 18 st.Down("bar") 19 st.Down("baz") 20 st.Next() 21 st.Next() 22 23 b := vm.NewLine(nil, vm.LOAD, []string{"foo"}, []byte{42}, nil) 24 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 25 st.SetCode(b) 26 27 ca := cache.NewCache().WithCacheSize(1024) 28 ca.Add("inky", "pinky", 13) 29 ca.Add("blinky", "clyde", 42) 30 31 ctx := context.Background() 32 store := mem.NewMemDb() 33 store.Connect(ctx, "") 34 pr := NewPersister(store).WithContext(context.Background()).WithSession("xyzzy").WithContent(st, ca) 35 v, err := pr.Serialize() 36 if err != nil { 37 t.Error(err) 38 } 39 40 prnew := NewPersister(store).WithSession("xyzzy") 41 err = prnew.Deserialize(v) 42 if err != nil { 43 t.Fatal(err) 44 } 45 stNew := prnew.GetState() 46 stOld := pr.GetState() 47 caNew := prnew.GetMemory() 48 caOld := pr.GetMemory() 49 50 if !reflect.DeepEqual(stNew.ExecPath, stOld.ExecPath) { 51 t.Fatalf("expected %s, got %s", stNew.ExecPath, stOld.ExecPath) 52 } 53 if !bytes.Equal(stNew.Code, stOld.Code) { 54 t.Fatalf("expected %x, got %x", stNew.Code, stOld.Code) 55 } 56 if stNew.BitSize != stOld.BitSize { 57 t.Fatalf("expected %v, got %v", stNew.BitSize, stOld.BitSize) 58 } 59 if stNew.SizeIdx != stOld.SizeIdx { 60 t.Fatalf("expected %v, got %v", stNew.SizeIdx, stOld.SizeIdx) 61 } 62 if !reflect.DeepEqual(caNew, caOld) { 63 t.Fatalf("expected %v, got %v", caNew, caOld) 64 } 65 } 66 67 func TestSaveLoad(t *testing.T) { 68 st := state.NewState(12) 69 st.Down("foo") 70 st.Down("bar") 71 st.Down("baz") 72 st.Next() 73 st.Next() 74 75 b := vm.NewLine(nil, vm.LOAD, []string{"foo"}, []byte{42}, nil) 76 b = vm.NewLine(b, vm.HALT, nil, nil, nil) 77 st.SetCode(b) 78 79 ca := cache.NewCache().WithCacheSize(1024) 80 ca.Add("inky", "pinky", 13) 81 ca.Add("blinky", "clyde", 42) 82 83 ctx := context.Background() 84 store := mem.NewMemDb() 85 store.Connect(ctx, "") 86 pr := NewPersister(store).WithContent(st, ca) 87 err := pr.Save("xyzzy") 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 prnew := NewPersister(store) 93 err = prnew.Load("xyzzy") 94 if err != nil { 95 t.Fatal(err) 96 } 97 stNew := prnew.GetState() 98 stOld := pr.GetState() 99 caNew := prnew.GetMemory() 100 caOld := pr.GetMemory() 101 102 if !reflect.DeepEqual(stNew.ExecPath, stOld.ExecPath) { 103 t.Fatalf("expected %s, got %s", stNew.ExecPath, stOld.ExecPath) 104 } 105 if !bytes.Equal(stNew.Code, stOld.Code) { 106 t.Fatalf("expected %x, got %x", stNew.Code, stOld.Code) 107 } 108 if stNew.BitSize != stOld.BitSize { 109 t.Fatalf("expected %v, got %v", stNew.BitSize, stOld.BitSize) 110 } 111 if stNew.SizeIdx != stOld.SizeIdx { 112 t.Fatalf("expected %v, got %v", stNew.SizeIdx, stOld.SizeIdx) 113 } 114 if !reflect.DeepEqual(caNew, caOld) { 115 t.Fatalf("expected %v, got %v", caNew, caOld) 116 } 117 } 118 119 func TestSaveLoadFlags(t *testing.T) { 120 ctx := context.Background() 121 st := state.NewState(2) 122 st.SetFlag(8) 123 ca := cache.NewCache() 124 125 store := mem.NewMemDb() 126 store.Connect(ctx, "") 127 pr := NewPersister(store).WithContent(st, ca) 128 err := pr.Save("xyzzy") 129 if err != nil { 130 t.Fatal(err) 131 } 132 133 prnew := NewPersister(store) 134 135 err = prnew.Load("xyzzy") 136 if err != nil { 137 t.Fatal(err) 138 } 139 stnew := prnew.GetState() 140 if !stnew.GetFlag(8) { 141 t.Fatalf("expected flag 8 set") 142 } 143 }