go-hep.org/x/hep@v0.38.1/groot/rbytes/rw_test.go (about) 1 // Copyright ©2019 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rbytes_test 6 7 import ( 8 "reflect" 9 "strings" 10 "testing" 11 12 "go-hep.org/x/hep/groot/rbase" 13 "go-hep.org/x/hep/groot/rbytes" 14 "go-hep.org/x/hep/groot/rdict" 15 "go-hep.org/x/hep/groot/rmeta" 16 "go-hep.org/x/hep/groot/root" 17 ) 18 19 func TestReadWriteObjectAny(t *testing.T) { 20 var ( 21 v1 = rbase.NewNamed("name-1", "title-1") 22 v2 = rbase.NewNamed("name-2", "title-2") 23 v3 *rbase.Named 24 ) 25 26 for _, tc := range []struct { 27 name string 28 vs []*rbase.Named 29 }{ 30 {"1213", []*rbase.Named{v1, v2, v1, v3}}, 31 {"1123", []*rbase.Named{v1, v1, v2, v3}}, 32 {"2113", []*rbase.Named{v2, v1, v1, v3}}, 33 {"2131", []*rbase.Named{v2, v1, v3, v1}}, 34 35 {"12213", []*rbase.Named{v1, v2, v2, v1, v3}}, 36 {"12123", []*rbase.Named{v1, v2, v1, v2, v3}}, 37 {"11223", []*rbase.Named{v1, v1, v2, v2, v3}}, 38 {"21213", []*rbase.Named{v2, v1, v2, v1, v3}}, 39 {"22131", []*rbase.Named{v2, v2, v1, v3, v1}}, 40 } { 41 t.Run(tc.name, func(t *testing.T) { 42 43 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 44 for i, v := range tc.vs { 45 wbuf.WriteObjectAny(v) 46 if err := wbuf.Err(); err != nil { 47 t.Fatalf("could not write named[%d]=%v: %+v", i, v, err) 48 } 49 } 50 if err := wbuf.Err(); err != nil { 51 t.Fatalf("could not fill wbuffer: %+v", err) 52 } 53 54 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 55 for i := range tc.vs { 56 var v *rbase.Named 57 obj := rbuf.ReadObjectAny() 58 if err := rbuf.Err(); err != nil { 59 t.Fatalf("could not read object[%d]: %+v", i, err) 60 } 61 62 if obj != nil { 63 v = obj.(*rbase.Named) 64 } 65 66 if got, want := v, tc.vs[i]; !reflect.DeepEqual(got, want) { 67 t.Fatalf("invalid named[%d] value:\ngot = %v\nwant= %v\n", i, got, want) 68 } 69 } 70 }) 71 } 72 } 73 74 func TestRWStrings(t *testing.T) { 75 want := []string{"", "x", "", "xx", "", "xxx"} 76 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 77 for i, str := range want { 78 wbuf.WriteString(str) 79 if err := wbuf.Err(); err != nil { 80 t.Errorf("could not write string #%d: %+v", i, err) 81 } 82 } 83 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 84 for i := range want { 85 got := rbuf.ReadString() 86 if got != want[i] { 87 t.Errorf("invalid string at %d: got=%q, want=%q", i, got, want[i]) 88 } 89 } 90 } 91 92 func TestRWStdVecStrings(t *testing.T) { 93 want := [][]string{ 94 {"", "x", "", "xx", "", "xxx"}, 95 {"x", "", "xx", "", "xxx"}, 96 {"", "x", "", "xx", "", "xxx"}, 97 {"x", "", "xx", "", "xxx", strings.Repeat("1!", 256)}, 98 } 99 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 100 for i, str := range want { 101 wbuf.WriteStdVectorStrs(str) 102 if err := wbuf.Err(); err != nil { 103 t.Errorf("could not write string #%d: %+v", i, err) 104 } 105 } 106 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 107 for i := range want { 108 var got []string 109 rbuf.ReadStdVectorStrs(&got) 110 if got, want := got, want[i]; !reflect.DeepEqual(got, want) { 111 t.Errorf("invalid string at %d: got=%q, want=%q", i, got, want) 112 } 113 } 114 } 115 116 func TestRWFloat16(t *testing.T) { 117 makeElm := func(title string) rbytes.StreamerElement { 118 elm := rdict.Element{ 119 Name: *rbase.NewNamed("f16", title), 120 Type: rmeta.Float16, 121 }.New() 122 return &elm 123 } 124 125 for _, tc := range []struct { 126 name string 127 v root.Float16 128 want root.Float16 129 }{ 130 { 131 name: "", 132 v: 42, 133 want: 42, 134 }, 135 { 136 name: "[0,42]", 137 v: 42, 138 want: 42, 139 }, 140 { 141 name: "[43,44]", 142 v: 42, 143 want: 43, 144 }, 145 { 146 name: "[0,41]", 147 v: 42, 148 want: 41, 149 }, 150 { 151 name: "[10,10,2]", 152 v: 14, 153 want: 14, 154 }, 155 { 156 name: "[-10,-10,3]", 157 v: -10, 158 want: -10, 159 }, 160 { 161 name: "[10,10,20]", 162 v: 10, 163 want: 10, 164 }, 165 } { 166 t.Run(tc.name, func(t *testing.T) { 167 var elm rbytes.StreamerElement 168 if tc.name != "" { 169 elm = makeElm(tc.name) 170 } 171 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 172 wbuf.WriteArrayF16([]root.Float16{tc.v}, elm) 173 if err := wbuf.Err(); err != nil { 174 t.Fatalf("could not write f16=%v: %+v", tc.v, err) 175 } 176 177 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 178 got := make([]root.Float16, 1) 179 rbuf.ReadArrayF16(got, elm) 180 if err := rbuf.Err(); err != nil { 181 t.Fatalf("could not read f16=%v: %+v", tc.v, err) 182 } 183 184 if got, want := got[0], tc.want; got != want { 185 t.Fatalf("invalid r/w round-trip: got=%v, want=%v", got, want) 186 } 187 }) 188 } 189 } 190 191 func TestRWDouble32(t *testing.T) { 192 makeElm := func(title string) rbytes.StreamerElement { 193 elm := rdict.Element{ 194 Name: *rbase.NewNamed("d32", title), 195 Type: rmeta.Double32, 196 }.New() 197 return &elm 198 } 199 200 for _, tc := range []struct { 201 name string 202 v root.Double32 203 want root.Double32 204 }{ 205 { 206 name: "", 207 v: 42, 208 want: 42, 209 }, 210 { 211 name: "[0,42]", 212 v: 42, 213 want: 42, 214 }, 215 { 216 name: "[43,44]", 217 v: 42, 218 want: 43, 219 }, 220 { 221 name: "[0,41]", 222 v: 42, 223 want: 41, 224 }, 225 { 226 name: "[10,10,2]", 227 v: 14, 228 want: 14, 229 }, 230 { 231 name: "[-10,-10,3]", 232 v: -10, 233 want: -10, 234 }, 235 { 236 name: "[10,10,20]", 237 v: 10, 238 want: 10, 239 }, 240 } { 241 t.Run(tc.name, func(t *testing.T) { 242 var elm rbytes.StreamerElement 243 if tc.name != "" { 244 elm = makeElm(tc.name) 245 } 246 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 247 wbuf.WriteArrayD32([]root.Double32{tc.v}, elm) 248 if err := wbuf.Err(); err != nil { 249 t.Fatalf("could not write d32=%v: %+v", tc.v, err) 250 } 251 252 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 253 got := make([]root.Double32, 1) 254 rbuf.ReadArrayD32(got, elm) 255 if err := rbuf.Err(); err != nil { 256 t.Fatalf("could not read d32=%v: %+v", tc.v, err) 257 } 258 259 if got, want := got[0], tc.want; got != want { 260 t.Fatalf("invalid r/w round-trip: got=%v, want=%v", got, want) 261 } 262 }) 263 } 264 }