go-hep.org/x/hep@v0.38.1/groot/rbase/ref_test.go (about)

     1  // Copyright ©2020 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 rbase
     6  
     7  import (
     8  	"testing"
     9  
    10  	"go-hep.org/x/hep/groot/rbytes"
    11  )
    12  
    13  func TestRef(t *testing.T) {
    14  	ref := Ref{pid: &gPID}
    15  	if obj := ref.Object(); obj != nil {
    16  		t.Fatalf("invalid referenced object")
    17  	}
    18  	if got, want := ref.UID(), uint32(0); got != want {
    19  		t.Fatalf("invalid UID: got=%d, want=%d", got, want)
    20  	}
    21  
    22  	obj := NewObject()
    23  	obj.ID = 42
    24  	gPID.objs[obj.UID()] = obj
    25  
    26  	ref.obj = *obj
    27  	if ptr := ref.Object(); ptr != obj {
    28  		t.Fatalf("invalid referenced object: got=%v, want=%v", ptr, obj)
    29  	}
    30  	if got, want := ref.UID(), obj.ID; got != want {
    31  		t.Fatalf("invalid UID: got=%d, want=%d", got, want)
    32  	}
    33  
    34  	obj.SetBit(kIsReferenced)
    35  
    36  	wbuf := rbytes.NewWBuffer(nil, nil, 0, nil)
    37  	wbuf.WriteObject(obj)
    38  	err := wbuf.Err()
    39  	if err != nil {
    40  		t.Fatalf("could not marshal ROOT: %+v", err)
    41  	}
    42  
    43  	rbuf := rbytes.NewRBuffer(wbuf.Bytes(), nil, 0, nil)
    44  	var got Ref
    45  	err = got.UnmarshalROOT(rbuf)
    46  	if err != nil {
    47  		t.Fatalf("could not unmarshal ROOT: %+v", err)
    48  	}
    49  
    50  	if got.obj.ID != obj.ID {
    51  		t.Fatalf("invalid unmarshaled ref: got=%d, want=%d", got.obj.ID, obj.ID)
    52  	}
    53  }