go-hep.org/x/hep@v0.38.1/hbook/yodacnv/yoda_test.go (about) 1 // Copyright ©2017 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 yodacnv_test 6 7 import ( 8 "bytes" 9 "reflect" 10 "testing" 11 12 "go-hep.org/x/hep/hbook" 13 "go-hep.org/x/hep/hbook/yodacnv" 14 ) 15 16 var ( 17 rdata []byte 18 h1 *hbook.H1D 19 h2 *hbook.H2D 20 p1 *hbook.P1D 21 s2 *hbook.S2D 22 ) 23 24 func TestReadWrite(t *testing.T) { 25 r := bytes.NewReader(rdata) 26 objs, err := yodacnv.Read(r) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 w := new(bytes.Buffer) 32 for _, v := range objs { 33 err = yodacnv.Write(w, v.(yodacnv.Marshaler)) 34 if err != nil { 35 t.Fatal(err) 36 } 37 } 38 39 if !reflect.DeepEqual(w.Bytes(), rdata) { 40 t.Fatalf("got:\n%s\nwant:\n%s\n", w.String(), string(rdata)) 41 } 42 } 43 44 func TestReadCounter(t *testing.T) { 45 r := bytes.NewReader([]byte(`BEGIN YODA_COUNTER /_EVTCOUNT 46 Path=/_EVTCOUNT 47 Title= 48 Type=Counter 49 # sumW sumW2 numEntries 50 3.255092e+09 1.059749e+15 10000 51 END YODA_COUNTER 52 `)) 53 54 objs, err := yodacnv.Read(r) 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 if len(objs) != 0 { 60 t.Fatalf("got %d values. want %d (COUNTER not implemented)", len(objs), 0) 61 } 62 } 63 64 func TestReadScatter1D(t *testing.T) { 65 r := bytes.NewReader([]byte(`BEGIN YODA_SCATTER1D /_XSEC 66 Path=/_XSEC 67 Title= 68 Type=Scatter1D 69 # xval xerr- xerr+ 70 2.966429e+04 2.828319e+02 2.828319e+02 71 END YODA_SCATTER1D 72 `)) 73 74 objs, err := yodacnv.Read(r) 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 if len(objs) != 0 { 80 t.Fatalf("got %d values. want %d (SCATTER1D not implemented)", len(objs), 0) 81 } 82 } 83 84 func init() { 85 86 add := func(o yodacnv.Marshaler) { 87 raw, err := o.MarshalYODA() 88 if err != nil { 89 panic(err) 90 } 91 rdata = append(rdata, raw...) 92 } 93 94 h1 = hbook.NewH1D(10, -4, 4) 95 h1.Annotation()["name"] = "histo-1d" 96 h1.Fill(1, 1) 97 h1.Fill(2, 1) 98 h1.Fill(-3, 1) 99 h1.Fill(-4, 1) 100 h1.Fill(0, 1) 101 h1.Fill(0, 1) 102 h1.Fill(10, 1) 103 h1.Fill(-10, 1) 104 105 add(h1) 106 107 h2 = hbook.NewH2D(5, -1, 1, 5, -2, +2) 108 h2.Annotation()["name"] = "histo-2d" 109 h2.Fill(+0.5, +1, 1) 110 h2.Fill(-0.5, +1, 1) 111 h2.Fill(+0.0, -1, 1) 112 113 add(h2) 114 115 p1 = hbook.NewP1D(10, -4, +4) 116 for i := range 10 { 117 v := float64(i) 118 p1.Fill(v, v*2, 1) 119 } 120 p1.Fill(-10, 10, 1) 121 122 add(p1) 123 124 s2 = hbook.NewS2DFromH1D(h1) 125 add(s2) 126 }