go-hep.org/x/hep@v0.38.1/slha/slha_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 slha_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"os"
    11  	"reflect"
    12  	"strings"
    13  	"testing"
    14  
    15  	"go-hep.org/x/hep/slha"
    16  )
    17  
    18  func TestDecode(t *testing.T) {
    19  	const fname = "testdata/sps1a.spc"
    20  	f, err := os.Open(fname)
    21  	if err != nil {
    22  		t.Fatalf("error opening file [%s]: %v\n", fname, err)
    23  	}
    24  	defer f.Close()
    25  
    26  	data, err := slha.Decode(f)
    27  	if err != nil {
    28  		t.Fatalf("error decoding file [%s]: %v\n", fname, err)
    29  	}
    30  	if data == nil {
    31  		t.Fatalf("got nil SLHA data\n")
    32  	}
    33  }
    34  
    35  func TestEncode(t *testing.T) {
    36  	const fname = "testdata/sps1a.spc"
    37  	f, err := os.Open(fname)
    38  	if err != nil {
    39  		t.Fatalf("error opening file [%s]: %v\n", fname, err)
    40  	}
    41  	defer f.Close()
    42  
    43  	data, err := slha.Decode(f)
    44  	if err != nil {
    45  		t.Fatalf("error decoding file [%s]: %v\n", fname, err)
    46  	}
    47  
    48  	const ofname = "testdata/write.out"
    49  	out, err := os.Create(ofname)
    50  	if err != nil {
    51  		t.Fatalf("error creating file [%s]: %v\n", ofname, err)
    52  	}
    53  	defer out.Close()
    54  	defer os.Remove(ofname)
    55  
    56  	err = slha.Encode(out, data)
    57  	if err != nil {
    58  		t.Fatalf("error encoding file [%s]: %v\n", ofname, err)
    59  	}
    60  
    61  	err = out.Close()
    62  	if err != nil {
    63  		t.Fatalf("error closing file [%s]: %v\n", ofname, err)
    64  	}
    65  }
    66  
    67  func TestRW(t *testing.T) {
    68  	for _, fname := range []string{
    69  		"testdata/sps1a.spc",
    70  		"testdata/ex1-snowmass-point-1a.slha",
    71  		"testdata/slha1.txt",
    72  		"testdata/slha2.txt",
    73  	} {
    74  		f, err := os.Open(fname)
    75  		if err != nil {
    76  			t.Fatalf("error opening file [%s]: %v\n", fname, err)
    77  		}
    78  		defer f.Close()
    79  
    80  		wdata, err := slha.Decode(f)
    81  		if err != nil {
    82  			t.Fatalf("error decoding file [%s]: %v\n", fname, err)
    83  		}
    84  		f.Close()
    85  
    86  		ofname := fname + ".out"
    87  		f, err = os.Create(ofname)
    88  		if err != nil {
    89  			t.Fatalf("error creating file [%s]: %v\n", ofname, err)
    90  		}
    91  		defer f.Close()
    92  
    93  		err = slha.Encode(f, wdata)
    94  		if err != nil {
    95  			t.Fatalf("error encoding file [%s]: %v\n", ofname, err)
    96  		}
    97  
    98  		err = f.Close()
    99  		if err != nil {
   100  			t.Fatalf("error closing file [%s]: %v\n", ofname, err)
   101  		}
   102  
   103  		f, err = os.Open(ofname)
   104  		if err != nil {
   105  			t.Fatalf("error re-opening file [%s]: %v\n", ofname, err)
   106  		}
   107  		defer f.Close()
   108  
   109  		rdata, err := slha.Decode(f)
   110  		if err != nil {
   111  			t.Fatalf("error re-decoding file [%s]: %v\n", ofname, err)
   112  		}
   113  		f.Close()
   114  
   115  		if ok, str := compareSLHA(*rdata, *wdata); !ok {
   116  			t.Fatalf("error - SLHA data differ - file %s\n%s\n", ofname, str)
   117  		} else {
   118  			os.Remove(ofname)
   119  		}
   120  	}
   121  }
   122  
   123  func compareSLHA(a, b slha.SLHA) (bool, string) {
   124  	str := make([]string, 0)
   125  	ok := true
   126  	if len(a.Blocks) != len(b.Blocks) {
   127  		str = append(str,
   128  			fmt.Sprintf("ref - #block: %d\n", len(a.Blocks)),
   129  			fmt.Sprintf("chk - #block: %d\n", len(b.Blocks)),
   130  		)
   131  		ok = false
   132  	} else {
   133  		for i := range a.Blocks {
   134  			ablock := &a.Blocks[i]
   135  			bblock := &b.Blocks[i]
   136  
   137  			if math.IsNaN(ablock.Q) {
   138  				ablock.Q = -999
   139  			}
   140  			if math.IsNaN(bblock.Q) {
   141  				bblock.Q = -999
   142  			}
   143  
   144  			if ablock.Name != bblock.Name {
   145  				ok = false
   146  				str = append(str,
   147  					fmt.Sprintf("ref - block[%d] n=%q\n", i, ablock.Name),
   148  					fmt.Sprintf("chk - block[%d] n=%q\n", i, bblock.Name),
   149  				)
   150  			}
   151  
   152  			if ablock.Comment != bblock.Comment {
   153  				ok = false
   154  				str = append(str,
   155  					fmt.Sprintf("ref - block[%d] n=%q comm=%q\n", i, ablock.Name, ablock.Comment),
   156  					fmt.Sprintf("chk - block[%d] n=%q comm=%q\n", i, bblock.Name, bblock.Comment),
   157  				)
   158  
   159  			}
   160  
   161  			if ablock.Q != bblock.Q {
   162  				ok = false
   163  				str = append(str,
   164  					fmt.Sprintf("ref - block[%d] n=%q Q=%v\n", i, ablock.Name, ablock.Q),
   165  					fmt.Sprintf("chk - block[%d] n=%q Q=%v\n", i, bblock.Name, bblock.Q),
   166  				)
   167  			}
   168  
   169  			if len(ablock.Data) != len(bblock.Data) {
   170  				str = append(str,
   171  					fmt.Sprintf("ref - block[%d] n=%q #entries=%d\n", i, ablock.Name, len(ablock.Data)),
   172  					fmt.Sprintf("chk - block[%d] n=%q #entries=%d\n", i, bblock.Name, len(bblock.Data)),
   173  				)
   174  				ok = false
   175  			} else {
   176  				for j := range ablock.Data {
   177  					aa := &ablock.Data[j]
   178  					bb := &bblock.Data[j]
   179  
   180  					if !reflect.DeepEqual(aa.Index, bb.Index) {
   181  						ok = false
   182  						str = append(str,
   183  							fmt.Sprintf("ref - block[%d][%d] index=%v\n", i, j, aa.Index.Index()),
   184  							fmt.Sprintf("chk - block[%d][%d] index=%v\n", i, j, bb.Index.Index()),
   185  						)
   186  					}
   187  
   188  					va := aa.Value.Interface()
   189  					vb := bb.Value.Interface()
   190  					if !reflect.DeepEqual(va, vb) {
   191  						ok = false
   192  						str = append(str,
   193  							fmt.Sprintf("ref - block[%d][%d] v=%#v\n", i, j, va),
   194  							fmt.Sprintf("chk - block[%d][%d] v=%#v\n", i, j, vb),
   195  						)
   196  
   197  					}
   198  
   199  					ca := aa.Value.Comment()
   200  					cb := bb.Value.Comment()
   201  					if !reflect.DeepEqual(ca, cb) {
   202  						ok = false
   203  						str = append(str,
   204  							fmt.Sprintf("ref - block[%d][%d] comm=%q\n", i, j, ca),
   205  							fmt.Sprintf("chk - block[%d][%d] comm=%q\n", i, j, cb),
   206  						)
   207  
   208  					}
   209  
   210  				}
   211  			}
   212  		}
   213  	}
   214  
   215  	if len(a.Particles) != len(b.Particles) {
   216  		str = append(str,
   217  			fmt.Sprintf("ref - #parts: %d\n", len(a.Particles)),
   218  			fmt.Sprintf("chk - #parts: %d\n", len(b.Particles)),
   219  		)
   220  		ok = false
   221  	} else {
   222  		for i := range a.Particles {
   223  			apart := &a.Particles[i]
   224  			bpart := &b.Particles[i]
   225  
   226  			if apart.PdgID != bpart.PdgID {
   227  				ok = false
   228  				str = append(str,
   229  					fmt.Sprintf("ref - part[%d] pdgid=%d\n", i, apart.PdgID),
   230  					fmt.Sprintf("chk - part[%d] pdgid=%q\n", i, bpart.PdgID),
   231  				)
   232  			}
   233  
   234  			if math.IsNaN(apart.Width) {
   235  				apart.Width = -999
   236  			}
   237  			if math.IsNaN(bpart.Width) {
   238  				bpart.Width = -999
   239  			}
   240  
   241  			if math.IsNaN(apart.Mass) {
   242  				apart.Mass = -999
   243  			}
   244  			if math.IsNaN(bpart.Mass) {
   245  				bpart.Mass = -999
   246  			}
   247  
   248  			if !reflect.DeepEqual(apart, bpart) {
   249  				ok = false
   250  				str = append(str,
   251  					fmt.Sprintf("ref - part[%d] pdgid=%d\n", i, apart.PdgID),
   252  					fmt.Sprintf("chk - part[%d] pdgid=%d\n", i, bpart.PdgID),
   253  				)
   254  			}
   255  		}
   256  	}
   257  
   258  	return ok, strings.Join(str, "\n")
   259  }