go-hep.org/x/hep@v0.38.1/groot/riofs/key_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 riofs
     6  
     7  import (
     8  	"errors"
     9  	"reflect"
    10  	"strings"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/groot/rbase"
    14  	"go-hep.org/x/hep/groot/rbytes"
    15  	"go-hep.org/x/hep/groot/rhist"
    16  	"go-hep.org/x/hep/groot/root"
    17  	"go-hep.org/x/hep/groot/rvers"
    18  )
    19  
    20  func TestKeyNewKeyFrom(t *testing.T) {
    21  	var (
    22  		werr = errors.New("riofs: invalid")
    23  	)
    24  	for _, tc := range []struct {
    25  		want *rbase.ObjString
    26  		wbuf *rbytes.WBuffer
    27  		err  error
    28  	}{
    29  		{
    30  			want: rbase.NewObjString("hello"),
    31  			wbuf: nil,
    32  		},
    33  		{
    34  			want: rbase.NewObjString("hello"),
    35  			wbuf: func() *rbytes.WBuffer {
    36  				wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    37  				wbuf.WriteString(strings.Repeat("=+=", 80))
    38  				return wbuf
    39  			}(),
    40  		},
    41  		{
    42  			want: rbase.NewObjString("hello"),
    43  			wbuf: func() *rbytes.WBuffer {
    44  				wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    45  				wbuf.WriteString(strings.Repeat("=+=", 80))
    46  				wbuf.SetErr(werr)
    47  				return wbuf
    48  			}(),
    49  			err: werr,
    50  		},
    51  		{
    52  			want: rbase.NewObjString(strings.Repeat("+", 512) + "hello"),
    53  			wbuf: nil,
    54  		},
    55  		{
    56  			want: rbase.NewObjString(strings.Repeat("+", 512) + "hello"),
    57  			wbuf: func() *rbytes.WBuffer {
    58  				wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    59  				wbuf.WriteString(strings.Repeat("=+=", 80))
    60  				return wbuf
    61  			}(),
    62  		},
    63  		{
    64  			want: rbase.NewObjString(strings.Repeat("+", 512) + "hello"),
    65  			wbuf: func() *rbytes.WBuffer {
    66  				wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    67  				wbuf.WriteString(strings.Repeat("=+=", 80))
    68  				wbuf.SetErr(werr)
    69  				return wbuf
    70  			}(),
    71  			err: werr,
    72  		},
    73  	} {
    74  		t.Run("", func(t *testing.T) {
    75  			var parent Directory
    76  			k, err := newTestKeyFrom(parent, tc.want, tc.wbuf)
    77  			switch {
    78  			case err == nil && tc.err == nil:
    79  				// ok
    80  			case err == nil && tc.err != nil:
    81  				t.Fatalf("expected an error (%v)", tc.err)
    82  			case err != nil && tc.err == nil:
    83  				t.Fatalf("could not generate key from tobjstring: %v", err)
    84  			case err != nil && tc.err != nil:
    85  				if got, want := err.Error(), tc.err.Error(); got != want {
    86  					t.Fatalf("error: got=%v, want=%v", got, want)
    87  				}
    88  				return
    89  			}
    90  
    91  			v, err := k.Object()
    92  			if err != nil {
    93  				t.Fatal(err)
    94  			}
    95  
    96  			if got := v.(*rbase.ObjString); !reflect.DeepEqual(got, tc.want) {
    97  				t.Fatalf("error:\ngot = %#v\nwant= %#v\n", got, tc.want)
    98  			}
    99  
   100  			otyp := k.ObjectType()
   101  			if otyp == nil {
   102  				t.Fatalf("could not retrieve key's payload's type")
   103  			}
   104  			if otyp != nil {
   105  				switch v := reflect.New(otyp).Elem().Interface(); v.(type) {
   106  				case root.ObjString:
   107  					// ok
   108  				default:
   109  					t.Fatalf("expected a root.ObjString (got %T)", v)
   110  				}
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestKeyObjectType(t *testing.T) {
   117  	for _, tc := range []struct {
   118  		name string
   119  		key  Key
   120  		typ  reflect.Type
   121  	}{
   122  		{
   123  			name: "TObjString",
   124  			key:  Key{class: "TObjString"},
   125  			typ:  reflect.TypeOf((*rbase.ObjString)(nil)),
   126  		},
   127  		{
   128  			name: "invalid",
   129  			key:  Key{class: "invalid"},
   130  			typ:  nil,
   131  		},
   132  		{
   133  			name: "TH1D",
   134  			key:  Key{class: "TH1D"},
   135  			typ:  reflect.TypeOf((*rhist.H1D)(nil)),
   136  		},
   137  	} {
   138  		t.Run(tc.name, func(t *testing.T) {
   139  			otyp := tc.key.ObjectType()
   140  			if otyp != tc.typ {
   141  				t.Fatalf("got: %+v, want=%+v", otyp, tc.typ)
   142  			}
   143  			if otyp == nil {
   144  				return
   145  			}
   146  
   147  			otyp2 := tc.key.ObjectType()
   148  			if otyp != otyp2 {
   149  				t.Fatalf("got: %+v, want=%+v", otyp2, otyp)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func newTestKeyFrom(dir Directory, obj root.Object, wbuf *rbytes.WBuffer) (Key, error) {
   156  	if wbuf == nil {
   157  		wbuf = rbytes.NewWBuffer(nil, nil, 0, nil)
   158  	}
   159  	beg := int(wbuf.Pos())
   160  	n, err := obj.(rbytes.Marshaler).MarshalROOT(wbuf)
   161  	if err != nil {
   162  		return Key{}, err
   163  	}
   164  	end := beg + n
   165  	data := wbuf.Bytes()[beg:end]
   166  
   167  	name := ""
   168  	title := ""
   169  	if obj, ok := obj.(root.Named); ok {
   170  		name = obj.Name()
   171  		title = obj.Title()
   172  	}
   173  
   174  	k := Key{
   175  		rvers:    rvers.Key,
   176  		objlen:   int32(n),
   177  		datetime: nowUTC(),
   178  		class:    obj.Class(),
   179  		name:     name,
   180  		title:    title,
   181  		buf:      data,
   182  		obj:      obj,
   183  		otyp:     reflect.TypeOf(obj),
   184  	}
   185  	return k, nil
   186  }