go-hep.org/x/hep@v0.38.1/geo/gdml/gdml_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 gdml
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/xml"
    10  	"os"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func TestReadSchema(t *testing.T) {
    16  	t.Skip("boo")
    17  
    18  	for _, tc := range []struct {
    19  		fname string
    20  		err   error
    21  		want  Schema
    22  	}{
    23  		{
    24  			fname: "testdata/test.gdml",
    25  		},
    26  	} {
    27  		t.Run(tc.fname, func(t *testing.T) {
    28  			raw, err := os.ReadFile(tc.fname)
    29  			if err != nil {
    30  				t.Fatal(err)
    31  			}
    32  			var schema Schema
    33  			dec := xml.NewDecoder(bytes.NewReader(raw))
    34  			err = dec.Decode(&schema)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  
    39  			if !reflect.DeepEqual(schema, tc.want) {
    40  				t.Fatal(err)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestReadConstant(t *testing.T) {
    47  	for _, tc := range []struct {
    48  		raw  string
    49  		want Constant
    50  	}{
    51  		{
    52  			raw:  `<constant name="length" value="6.25"/>`,
    53  			want: Constant{Name: "length", Value: 6.25},
    54  		},
    55  		{
    56  			raw:  `<constant name="mass" value="6"/>`,
    57  			want: Constant{Name: "mass", Value: 6},
    58  		},
    59  	} {
    60  		t.Run(tc.want.Name, func(t *testing.T) {
    61  			var v Constant
    62  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
    63  			if err != nil {
    64  				t.Fatal(err)
    65  			}
    66  
    67  			if !reflect.DeepEqual(v, tc.want) {
    68  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestReadQuantity(t *testing.T) {
    75  	for _, tc := range []struct {
    76  		raw  string
    77  		want Quantity
    78  	}{
    79  		{
    80  			raw:  `<quantity  name="W_Density" type="density" value="1" unit="g/cm3"/>`,
    81  			want: Quantity{Name: "W_Density", Type: "density", Value: 1, Unit: "g/cm3"},
    82  		},
    83  	} {
    84  		t.Run(tc.want.Name, func(t *testing.T) {
    85  			var v Quantity
    86  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
    87  			if err != nil {
    88  				t.Fatal(err)
    89  			}
    90  
    91  			if !reflect.DeepEqual(v, tc.want) {
    92  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestReadVariable(t *testing.T) {
    99  	for _, tc := range []struct {
   100  		raw  string
   101  		want Variable
   102  	}{
   103  		{
   104  			raw:  `<variable name="x" value="6"/>`,
   105  			want: Variable{Name: "x", Value: "6"},
   106  		},
   107  		{
   108  			raw:  `<variable name="y" value="x/2"/>`,
   109  			want: Variable{Name: "y", Value: "x/2"},
   110  		},
   111  	} {
   112  		t.Run(tc.want.Name, func(t *testing.T) {
   113  			var v Variable
   114  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   115  			if err != nil {
   116  				t.Fatal(err)
   117  			}
   118  
   119  			if !reflect.DeepEqual(v, tc.want) {
   120  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func TestReadPosition(t *testing.T) {
   127  	for _, tc := range []struct {
   128  		raw  string
   129  		want Position
   130  	}{
   131  		{
   132  			raw:  `<position name="box_position" x="25.0" y="50.0" z="75.0" unit="cm"/>`,
   133  			want: Position{Name: "box_position", X: "25.0", Y: "50.0", Z: "75.0", Unit: "cm"},
   134  		},
   135  	} {
   136  		t.Run(tc.want.Name, func(t *testing.T) {
   137  			var v Position
   138  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   139  			if err != nil {
   140  				t.Fatal(err)
   141  			}
   142  
   143  			if !reflect.DeepEqual(v, tc.want) {
   144  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   145  			}
   146  		})
   147  	}
   148  }
   149  
   150  func TestReadRotation(t *testing.T) {
   151  	for _, tc := range []struct {
   152  		raw  string
   153  		want Rotation
   154  	}{
   155  		{
   156  			raw:  `<rotation name="identity" x="0" y="0" z="0" unit="deg"/>`,
   157  			want: Rotation{Name: "identity", X: "0", Y: "0", Z: "0", Unit: "deg"},
   158  		},
   159  		{
   160  			raw:  `<rotation name="rot-z" x="0" y="0" z="30" unit="deg"/>`,
   161  			want: Rotation{Name: "rot-z", X: "0", Y: "0", Z: "30", Unit: "deg"},
   162  		},
   163  		{
   164  			raw:  `<rotation name="rot-x" x="10" unit="deg"/>`,
   165  			want: Rotation{Name: "rot-x", X: "10", Y: "", Z: "", Unit: "deg"},
   166  		},
   167  	} {
   168  		t.Run(tc.want.Name, func(t *testing.T) {
   169  			var v Rotation
   170  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   171  			if err != nil {
   172  				t.Fatal(err)
   173  			}
   174  
   175  			if !reflect.DeepEqual(v, tc.want) {
   176  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestReadScale(t *testing.T) {
   183  	for _, tc := range []struct {
   184  		raw  string
   185  		want Scale
   186  	}{
   187  		{
   188  			raw:  `<scale name="identity" x="1" y="1" z="1"/>`,
   189  			want: Scale{Name: "identity", X: "1", Y: "1", Z: "1"},
   190  		},
   191  		{
   192  			raw:  `<scale name="reflection" x="-1" y="-1" z="1"/>`,
   193  			want: Scale{Name: "reflection", X: "-1", Y: "-1", Z: "1"},
   194  		},
   195  	} {
   196  		t.Run(tc.want.Name, func(t *testing.T) {
   197  			var v Scale
   198  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   199  			if err != nil {
   200  				t.Fatal(err)
   201  			}
   202  
   203  			if !reflect.DeepEqual(v, tc.want) {
   204  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   205  			}
   206  		})
   207  	}
   208  }
   209  
   210  func TestReadMatrix(t *testing.T) {
   211  	for _, tc := range []struct {
   212  		raw  string
   213  		want Matrix
   214  	}{
   215  		{
   216  			raw:  `<matrix name="m1" coldim="3" values="0.4 9 126 8.5 7 21 34.6 7 9"/>`,
   217  			want: Matrix{Name: "m1", Cols: 3, Values: "0.4 9 126 8.5 7 21 34.6 7 9"},
   218  		},
   219  		{
   220  			// FIXME(sbinet): customize Matrix.UnmarshalXML to post-process lines into []float64 or []string
   221  			raw: `<matrix name="m2" coldim="3" values=" 0.4 9 126
   222   8.5 7 21
   223  34.6 7  9"/>`,
   224  			want: Matrix{Name: "m2", Cols: 3, Values: " 0.4 9 126\n 8.5 7 21\n34.6 7  9"},
   225  		},
   226  		{
   227  			raw:  `<matrix name="m3" coldim="1" values="1 2 3 4"/>`,
   228  			want: Matrix{Name: "m3", Cols: 1, Values: "1 2 3 4"},
   229  		},
   230  	} {
   231  		t.Run(tc.want.Name, func(t *testing.T) {
   232  			var v Matrix
   233  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   234  			if err != nil {
   235  				t.Fatal(err)
   236  			}
   237  
   238  			if !reflect.DeepEqual(v, tc.want) {
   239  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   240  			}
   241  		})
   242  	}
   243  }
   244  
   245  func TestReadIsotope(t *testing.T) {
   246  	for _, tc := range []struct {
   247  		raw  string
   248  		want Isotope
   249  	}{
   250  		{
   251  			raw: `
   252  			<isotope name="U235" Z="92" N="235">
   253  				<atom type="A" value="235.01"/>
   254  			</isotope>`,
   255  			want: Isotope{Name: "U235", Z: 92, N: 235, Atom: Atom{Type: "A", Value: 235.01}},
   256  		},
   257  		{
   258  			raw: `
   259  			<isotope name="U238" Z="92" N="238">
   260  				<atom type="A" value="235.03"/>
   261  			</isotope>`,
   262  			want: Isotope{Name: "U238", Z: 92, N: 238, Atom: Atom{Type: "A", Value: 235.03}},
   263  		},
   264  	} {
   265  		t.Run(tc.want.Name, func(t *testing.T) {
   266  			var v Isotope
   267  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   268  			if err != nil {
   269  				t.Fatal(err)
   270  			}
   271  
   272  			if !reflect.DeepEqual(v, tc.want) {
   273  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   274  			}
   275  		})
   276  	}
   277  }
   278  
   279  func TestReadElement(t *testing.T) {
   280  	for _, tc := range []struct {
   281  		raw  string
   282  		want Element
   283  	}{
   284  		{
   285  			raw: `
   286  			<element name="Oxygen" Z="8" formula="O">
   287  				<atom value="16"/>
   288  			</element>`,
   289  			want: Element{Name: "Oxygen", Z: 8, Formula: "O", Atom: Atom{Type: "", Value: 16}},
   290  		},
   291  		{
   292  			raw: `
   293  			<element name="enriched_uranium">
   294  				<fraction ref="U235" n="0.9" />
   295  				<fraction ref="U238" n="0.1" />
   296  			</element>`,
   297  			want: Element{
   298  				Name: "enriched_uranium",
   299  				Fractions: []Fraction{
   300  					{Ref: "U235", N: 0.9},
   301  					{Ref: "U238", N: 0.1},
   302  				},
   303  			},
   304  		},
   305  	} {
   306  		t.Run(tc.want.Name, func(t *testing.T) {
   307  			var v Element
   308  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   309  			if err != nil {
   310  				t.Fatal(err)
   311  			}
   312  
   313  			if !reflect.DeepEqual(v, tc.want) {
   314  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   315  			}
   316  		})
   317  	}
   318  }
   319  
   320  func TestReadMaterial(t *testing.T) {
   321  	for _, tc := range []struct {
   322  		raw  string
   323  		want Material
   324  	}{
   325  		{
   326  			raw: `
   327  			<material name="Al" Z="13.0">
   328  				<D    value="2.70"/>
   329  				<atom value="26.98"/>
   330  			</material>`,
   331  			want: Material{Name: "Al", Z: 13.0, Density: Density{Value: 2.7}, Atom: Atom{Value: 26.98}},
   332  		},
   333  		{
   334  			raw: `
   335  			<material name="Water" formula="H2O">
   336  				<D    value="1.0"/>
   337  				<composite n="2" ref="Hydrogen"/>
   338  				<composite n="1" ref="Oxygen"/>
   339  			</material>`,
   340  			want: Material{
   341  				Name:    "Water",
   342  				Formula: "H2O",
   343  				Density: Density{Value: 1},
   344  				Composites: []Composite{
   345  					{N: 2, Ref: "Hydrogen"},
   346  					{N: 1, Ref: "Oxygen"},
   347  				},
   348  			},
   349  		},
   350  		{
   351  			raw: `
   352  			<material name="Air" formula="air">
   353  				<D    value="0.0012899999999999999"/>
   354  				<fraction n="0.7" ref="Nitrogen"/>
   355  				<fraction n="0.3" ref="Oxygen"/>
   356  			</material>`,
   357  			want: Material{
   358  				Name:    "Air",
   359  				Formula: "air",
   360  				Density: Density{Value: 0.0012899999999999999},
   361  				Fractions: []Fraction{
   362  					{N: 0.7, Ref: "Nitrogen"},
   363  					{N: 0.3, Ref: "Oxygen"},
   364  				},
   365  			},
   366  		},
   367  	} {
   368  		t.Run(tc.want.Name, func(t *testing.T) {
   369  			var v Material
   370  			err := xml.NewDecoder(bytes.NewReader([]byte(tc.raw))).Decode(&v)
   371  			if err != nil {
   372  				t.Fatal(err)
   373  			}
   374  
   375  			if !reflect.DeepEqual(v, tc.want) {
   376  				t.Fatalf("got = %#v\nwant= %#v", v, tc.want)
   377  			}
   378  		})
   379  	}
   380  }