github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/grid/types_test.go (about)

     1  // Copyright (c) 2015-2023 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package grid
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/tinylib/msgp/msgp"
    25  )
    26  
    27  func TestMarshalUnmarshalMSS(t *testing.T) {
    28  	v := MSS{"abc": "def", "ghi": "jkl"}
    29  	bts, err := v.MarshalMsg(nil)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	var v2 MSS
    34  	left, err := v2.UnmarshalMsg(bts)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if len(left) != 0 {
    39  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
    40  	}
    41  
    42  	left, err = msgp.Skip(bts)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if len(left) > 0 {
    47  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
    48  	}
    49  	if !reflect.DeepEqual(v, v2) {
    50  		t.Errorf("MSS: %v != %v", v, v2)
    51  	}
    52  }
    53  
    54  func TestMarshalUnmarshalMSSNil(t *testing.T) {
    55  	v := MSS(nil)
    56  	bts, err := v.MarshalMsg(nil)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	v2 := MSS(make(map[string]string, 1))
    61  	left, err := v2.UnmarshalMsg(bts)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if len(left) != 0 {
    66  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
    67  	}
    68  
    69  	left, err = msgp.Skip(bts)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	if len(left) > 0 {
    74  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
    75  	}
    76  	if !reflect.DeepEqual(v, v2) {
    77  		t.Errorf("MSS: %v != %v", v, v2)
    78  	}
    79  }
    80  
    81  func BenchmarkMarshalMsgMSS(b *testing.B) {
    82  	v := MSS{"abc": "def", "ghi": "jkl"}
    83  	b.ReportAllocs()
    84  	b.ResetTimer()
    85  	for i := 0; i < b.N; i++ {
    86  		v.MarshalMsg(nil)
    87  	}
    88  }
    89  
    90  func BenchmarkAppendMsgMSS(b *testing.B) {
    91  	v := MSS{"abc": "def", "ghi": "jkl"}
    92  	bts := make([]byte, 0, v.Msgsize())
    93  	bts, _ = v.MarshalMsg(bts[0:0])
    94  	b.SetBytes(int64(len(bts)))
    95  	b.ReportAllocs()
    96  	b.ResetTimer()
    97  	for i := 0; i < b.N; i++ {
    98  		bts, _ = v.MarshalMsg(bts[0:0])
    99  	}
   100  }
   101  
   102  func BenchmarkUnmarshalMSS(b *testing.B) {
   103  	v := MSS{"abc": "def", "ghi": "jkl"}
   104  	bts, _ := v.MarshalMsg(nil)
   105  	b.ReportAllocs()
   106  	b.SetBytes(int64(len(bts)))
   107  	b.ResetTimer()
   108  	for i := 0; i < b.N; i++ {
   109  		_, err := v.UnmarshalMsg(bts)
   110  		if err != nil {
   111  			b.Fatal(err)
   112  		}
   113  	}
   114  }
   115  
   116  func TestMarshalUnmarshalBytes(t *testing.T) {
   117  	v := Bytes([]byte("abc123123123"))
   118  	bts, err := v.MarshalMsg(nil)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	var v2 Bytes
   123  	left, err := v2.UnmarshalMsg(bts)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if len(left) != 0 {
   128  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
   129  	}
   130  
   131  	left, err = msgp.Skip(bts)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if len(left) > 0 {
   136  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
   137  	}
   138  	if !reflect.DeepEqual(v, v2) {
   139  		t.Errorf("MSS: %v != %v", v, v2)
   140  	}
   141  }
   142  
   143  func TestMarshalUnmarshalBytesNil(t *testing.T) {
   144  	v := Bytes(nil)
   145  	bts, err := v.MarshalMsg(nil)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	v2 := Bytes(make([]byte, 1))
   150  	left, err := v2.UnmarshalMsg(bts)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	if len(left) != 0 {
   155  		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
   156  	}
   157  
   158  	left, err = msgp.Skip(bts)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	if len(left) > 0 {
   163  		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
   164  	}
   165  	if !reflect.DeepEqual(v, v2) {
   166  		t.Errorf("MSS: %v != %v", v, v2)
   167  	}
   168  }