go-hep.org/x/hep@v0.38.1/groot/cmd/root-print/main_test.go (about) 1 // Copyright ©2018 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 main 6 7 import ( 8 "os" 9 "path/filepath" 10 "testing" 11 12 "go-hep.org/x/hep/groot" 13 "go-hep.org/x/hep/groot/riofs" 14 "go-hep.org/x/hep/hbook" 15 "go-hep.org/x/hep/hbook/rootcnv" 16 "gonum.org/v1/plot/cmpimg" 17 ) 18 19 func TestPrint(t *testing.T) { 20 dir, err := os.MkdirTemp("", "groot-root-print-") 21 if err != nil { 22 t.Fatalf("%+v", err) 23 } 24 defer os.RemoveAll(dir) 25 26 refname := filepath.Join(dir, "ref.root") 27 ref, err := groot.Create(refname) 28 if err != nil { 29 t.Fatalf("%+v", err) 30 } 31 defer ref.Close() 32 33 dir111, err := riofs.Dir(ref).Mkdir("dir-1/dir-11/dir-111") 34 if err != nil { 35 t.Fatalf("%+v", err) 36 } 37 38 dir121, err := riofs.Dir(ref).Mkdir("dir-1/dir-12/dir-121") 39 if err != nil { 40 t.Fatalf("%+v", err) 41 } 42 43 dir2, err := riofs.Dir(ref).Mkdir("dir-2") 44 if err != nil { 45 t.Fatalf("%+v", err) 46 } 47 48 h00 := hbook.NewH1D(10, 0, 10) 49 h00.Annotation()["name"] = "h00" 50 h00.Fill(5, 5) 51 err = ref.Put("h00", rootcnv.FromH1D(h00)) 52 if err != nil { 53 t.Fatalf("%+v", err) 54 } 55 56 h111 := hbook.NewH1D(10, 0, 10) 57 h111.Annotation()["name"] = "h111" 58 h111.Fill(5, 5) 59 err = dir111.Put("h111", rootcnv.FromH1D(h111)) 60 if err != nil { 61 t.Fatalf("%+v", err) 62 } 63 64 h121 := hbook.NewH2D(10, 0, 10, 10, 0, 10) 65 h121.Annotation()["name"] = "h121" 66 h121.Fill(5, 5, 5) 67 err = dir121.Put("h121", rootcnv.FromH2D(h121)) 68 if err != nil { 69 t.Fatalf("%+v", err) 70 } 71 72 h21 := hbook.NewH2D(10, 0, 10, 10, 0, 10) 73 h21.Annotation()["name"] = "h21" 74 h21.Fill(2, 1, 5) 75 err = dir2.Put("h21", rootcnv.FromH2D(h21)) 76 if err != nil { 77 t.Fatalf("%+v", err) 78 } 79 80 h22 := hbook.NewH2D(10, 0, 10, 10, 0, 10) 81 h22.Annotation()["name"] = "h22" 82 h22.Fill(2, 2, 5) 83 err = dir2.Put("h22", rootcnv.FromH2D(h22)) 84 if err != nil { 85 t.Fatalf("%+v", err) 86 } 87 88 g22 := hbook.NewS2DFrom([]float64{1, 2, 3}, []float64{11, 12, 13}) 89 g22.Annotation()["name"] = "g22" 90 err = dir2.Put("g22", rootcnv.FromS2D(g22)) 91 if err != nil { 92 t.Fatalf("%+v", err) 93 } 94 95 g23 := hbook.NewS2D([]hbook.Point2D{ 96 {X: 10, ErrX: hbook.Range{Min: 2, Max: 3}, Y: 10, ErrY: hbook.Range{Min: 2, Max: 3}}, 97 {X: 11, ErrX: hbook.Range{Min: 2, Max: 3}, Y: 11, ErrY: hbook.Range{Min: 2, Max: 3}}, 98 }...) 99 g23.Annotation()["name"] = "g23" 100 err = dir2.Put("g23", rootcnv.FromS2D(g23)) 101 if err != nil { 102 t.Fatalf("%+v", err) 103 } 104 105 err = ref.Close() 106 if err != nil { 107 t.Fatalf("%+v", err) 108 } 109 110 for _, tc := range []struct { 111 fname string 112 otype string 113 want []string 114 }{ 115 { 116 fname: refname, 117 otype: "png", 118 want: []string{ 119 "h00.png", 120 "h111.png", 121 "h121.png", 122 "h21.png", 123 "h22.png", 124 "g22.png", 125 "g23.png", 126 }, 127 }, 128 { 129 fname: refname + ":g.*", 130 otype: "png", 131 want: []string{ 132 "g22.png", 133 "g23.png", 134 }, 135 }, 136 { 137 fname: refname + ":dir", 138 otype: "png", 139 want: []string{ 140 "h111.png", 141 "h121.png", 142 "h21.png", 143 "h22.png", 144 "g22.png", 145 "g23.png", 146 }, 147 }, 148 { 149 fname: refname + ":dir-2", 150 otype: "png", 151 want: []string{ 152 "h21.png", 153 "h22.png", 154 "g22.png", 155 "g23.png", 156 }, 157 }, 158 { 159 fname: refname + ":dir-111", 160 otype: "png", 161 want: []string{ 162 "h111.png", 163 }, 164 }, 165 { 166 fname: refname + ":/dir-111", 167 otype: "png", 168 want: []string{ 169 "h111.png", 170 }, 171 }, 172 { 173 fname: refname + ":^dir-111", 174 otype: "png", 175 want: []string{}, 176 }, 177 { 178 fname: refname + ":^/dir-111", 179 otype: "png", 180 want: []string{}, 181 }, 182 } { 183 tname := tc.fname 184 tname = tname[len(dir)+1:] 185 t.Run(tname, func(t *testing.T) { 186 odir, err := os.MkdirTemp("", "groot-root-print-out-") 187 if err != nil { 188 t.Fatalf("%+v", err) 189 } 190 defer os.RemoveAll(odir) 191 192 const verbose = false 193 err = rootprint(odir, []string{tc.fname}, tc.otype, verbose) 194 if err != nil { 195 t.Fatalf("%+v", err) 196 } 197 198 files, err := filepath.Glob(filepath.Join(odir, "*."+tc.otype)) 199 if err != nil { 200 t.Fatalf("%+v", err) 201 } 202 203 if got, want := len(files), len(tc.want); got != want { 204 t.Fatalf("invalid number of files: got=%d, want=%d", got, want) 205 } 206 207 for _, name := range files { 208 got, err := os.ReadFile(name) 209 if err != nil { 210 t.Fatalf("could not read file %q: %+v", name, err) 211 } 212 if *cmpimg.GenerateTestData { 213 fname := filepath.Join("testdata", filepath.Base(name)) 214 err = os.WriteFile(fname, got, 0644) 215 if err != nil { 216 t.Fatalf("could not regenerate golden file %q: %+v", fname, err) 217 } 218 } 219 want, err := os.ReadFile(filepath.Join("testdata", filepath.Base(name))) 220 if err != nil { 221 t.Fatalf("could not read ref file %q: %+v", name, err) 222 } 223 ok, err := cmpimg.Equal(tc.otype, got, want) 224 if err != nil { 225 t.Fatalf("could not compare %q: %+v", name, err) 226 } 227 if !ok { 228 t.Fatalf("file %q does not compare equal", name) 229 } 230 } 231 }) 232 } 233 }