go-hep.org/x/hep@v0.38.1/groot/riofs/memfile_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  	"os"
     9  	"path/filepath"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"go-hep.org/x/hep/groot/rbase"
    14  	"go-hep.org/x/hep/groot/root"
    15  )
    16  
    17  func TestRMemFile(t *testing.T) {
    18  	dir, err := os.MkdirTemp("", "riofs-")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer os.RemoveAll(dir)
    23  
    24  	fname := filepath.Join(dir, "objstring.root")
    25  
    26  	w, err := Create(fname)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	var (
    32  		kname = "my-key"
    33  		want  = rbase.NewObjString("Hello World from Go-HEP!")
    34  	)
    35  
    36  	err = w.Put(kname, want)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	if got, want := len(w.Keys()), 1; got != want {
    42  		t.Fatalf("invalid number of keys. got=%d, want=%d", got, want)
    43  	}
    44  
    45  	err = w.Close()
    46  	if err != nil {
    47  		t.Fatalf("error closing file: %v", err)
    48  	}
    49  
    50  	raw, err := os.ReadFile(fname)
    51  	if err != nil {
    52  		t.Fatalf("error reading file: %v", err)
    53  	}
    54  
    55  	r, err := NewReader(RMemFile(raw))
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	defer r.Close()
    60  
    61  	rgot, err := r.Get(kname)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	if got := rgot.(root.ObjString); !reflect.DeepEqual(got, want) {
    67  		t.Fatalf("error reading back objstring.\ngot = %#v\nwant = %#v", got, want)
    68  	}
    69  
    70  	err = r.Close()
    71  	if err != nil {
    72  		t.Fatalf("error closing file: %v", err)
    73  	}
    74  }