go-hep.org/x/hep@v0.38.1/groot/exp/rntup/rntup_test.go (about) 1 // Copyright ©2020 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 rntup 6 7 import ( 8 "reflect" 9 "testing" 10 11 "go-hep.org/x/hep/groot/internal/rtests" 12 "go-hep.org/x/hep/groot/rbytes" 13 "go-hep.org/x/hep/groot/riofs" 14 ) 15 16 func TestNTuple(t *testing.T) { 17 for _, tc := range []struct { 18 want rtests.ROOTer 19 }{ 20 { 21 want: &NTuple{1, 2, span{1, 2, 3}, span{4, 5, 6}, 7}, 22 }, 23 } { 24 t.Run("", func(t *testing.T) { 25 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 26 _, err := tc.want.MarshalROOT(wbuf) 27 if err != nil { 28 t.Fatalf("could not marshal: %+v", err) 29 } 30 31 rt := reflect.Indirect(reflect.ValueOf(tc.want)).Type() 32 got := reflect.New(rt).Interface().(rtests.ROOTer) 33 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 34 35 err = got.UnmarshalROOT(rbuf) 36 if err != nil { 37 t.Fatalf("could not unmarshal: %+v", err) 38 } 39 40 if got, want := got, tc.want; !reflect.DeepEqual(got, want) { 41 t.Fatalf("invalid r/w round-trip:\ngot= %#v\nwant=%#v", got, want) 42 } 43 }) 44 } 45 } 46 47 func TestReadNTuple(t *testing.T) { 48 f, err := riofs.Open("../../testdata/ntpl001_staff.root") 49 if err != nil { 50 t.Fatalf("could not open file: +%v", err) 51 } 52 defer f.Close() 53 54 obj, err := f.Get("Staff") 55 if err != nil { 56 t.Fatalf("error: %+v", err) 57 } 58 59 nt, ok := obj.(*NTuple) 60 if !ok { 61 t.Fatalf("%q not an NTuple: %T", "Staff", obj) 62 } 63 64 want := NTuple{ 65 rvers: 0x0, 66 size: 0x30, 67 header: span{ 68 seek: 854, 69 nbytes: 537, 70 length: 2495, 71 }, 72 footer: span{ 73 seek: 72369, 74 nbytes: 285, 75 length: 804, 76 }, 77 reserved: 0, 78 } 79 80 if got, want := *nt, want; got != want { 81 t.Fatalf("error:\ngot= %#v\nwant=%#v", got, want) 82 } 83 84 if got, want := nt.String(), want.String(); got != want { 85 t.Fatalf("error:\ngot= %v\nwant=%v", got, want) 86 } 87 }