go-hep.org/x/hep@v0.38.1/groot/rphys/rw_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 rphys 6 7 import ( 8 "io" 9 "reflect" 10 "testing" 11 12 "go-hep.org/x/hep/groot/internal/rtests" 13 "go-hep.org/x/hep/groot/rbase" 14 "go-hep.org/x/hep/groot/rbytes" 15 "go-hep.org/x/hep/groot/rtypes" 16 ) 17 18 func TestWRBuffer(t *testing.T) { 19 for _, tc := range []struct { 20 name string 21 want rtests.ROOTer 22 cmp func(a, b rtests.ROOTer) bool 23 }{ 24 { 25 name: "TFeldmanCousins", 26 want: &FeldmanCousins{ 27 obj: rbase.Object{ID: 0x0, Bits: 0x3000000}, 28 CL: 1, 29 UpLim: 2, 30 LoLim: 3, 31 Nobs: 4, 32 Nbkg: 5, 33 MuMin: 6, 34 MuMax: 7, 35 MuStep: 8, 36 NMuStep: 9, 37 NMax: 10, 38 Quick: 11, 39 }, 40 }, 41 { 42 name: "TLorentzVector", 43 want: &LorentzVector{ 44 obj: rbase.Object{ID: 0x0, Bits: 0x3000000}, 45 p: Vector3{ 46 obj: rbase.Object{ID: 0x0, Bits: 0x3000000}, 47 x: 1, y: 2, z: 3, 48 }, 49 e: 4, 50 }, 51 }, 52 { 53 name: "TVector2", 54 want: &Vector2{ 55 obj: rbase.Object{ID: 0x0, Bits: 0x3000000}, 56 x: 1, y: 2, 57 }, 58 }, 59 { 60 name: "TVector3", 61 want: &Vector3{ 62 obj: rbase.Object{ID: 0x0, Bits: 0x3000000}, 63 x: 1, y: 2, z: 3, 64 }, 65 }, 66 } { 67 t.Run(tc.name, func(t *testing.T) { 68 { 69 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 70 wbuf.SetErr(io.EOF) 71 _, err := tc.want.MarshalROOT(wbuf) 72 if err == nil { 73 t.Fatalf("expected an error") 74 } 75 if err != io.EOF { 76 t.Fatalf("got=%v, want=%v", err, io.EOF) 77 } 78 } 79 wbuf := rbytes.NewWBuffer(nil, nil, 0, nil) 80 _, err := tc.want.MarshalROOT(wbuf) 81 if err != nil { 82 t.Fatalf("could not marshal ROOT: %v", err) 83 } 84 85 rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil) 86 class := tc.want.Class() 87 obj := rtypes.Factory.Get(class)().Interface().(rbytes.Unmarshaler) 88 { 89 rbuf.SetErr(io.EOF) 90 err = obj.UnmarshalROOT(rbuf) 91 if err == nil { 92 t.Fatalf("expected an error") 93 } 94 if err != io.EOF { 95 t.Fatalf("got=%v, want=%v", err, io.EOF) 96 } 97 rbuf.SetErr(nil) 98 } 99 err = obj.UnmarshalROOT(rbuf) 100 if err != nil { 101 t.Fatalf("could not unmarshal ROOT: %v", err) 102 } 103 104 switch tc.cmp { 105 case nil: 106 if !reflect.DeepEqual(obj, tc.want) { 107 t.Fatalf("error\ngot= %+v\nwant=%+v\n", obj, tc.want) 108 } 109 default: 110 obj := obj.(rtests.ROOTer) 111 if !tc.cmp(obj, tc.want) { 112 t.Fatalf("error\ngot= %+v\nwant=%+v\n", obj, tc.want) 113 } 114 } 115 }) 116 } 117 }