github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/logfile/log_entry_test.go (about)

     1  package logfile
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestEncodeEntry(t *testing.T) {
     9  	type args struct {
    10  		e *LogEntry
    11  	}
    12  	tests := []struct {
    13  		name  string
    14  		args  args
    15  		want  []byte
    16  		want1 int
    17  	}{
    18  		{
    19  			"nil", args{e: nil}, nil, 0,
    20  		},
    21  		{
    22  			"no-fields", args{e: &LogEntry{}}, []byte{28, 223, 68, 33, 0, 0, 0, 0}, 8,
    23  		},
    24  		{
    25  			"no-key-value", args{e: &LogEntry{ExpiredAt: 443434211}}, []byte{51, 97, 150, 123, 0, 0, 0, 198, 147, 242, 166, 3}, 12,
    26  		},
    27  		{
    28  			"with-key-value", args{e: &LogEntry{Key: []byte("kv"), Value: []byte("lotusdb"), ExpiredAt: 443434211}}, []byte{101, 208, 223, 156, 0, 4, 14, 198, 147, 242, 166, 3, 107, 118, 108, 111, 116, 117, 115, 100, 98}, 21,
    29  		},
    30  		{
    31  			"type-delete", args{e: &LogEntry{Key: []byte("kv"), Value: []byte("lotusdb"), ExpiredAt: 443434211, Type: TypeDelete}}, []byte{38, 27, 121, 27, 1, 4, 14, 198, 147, 242, 166, 3, 107, 118, 108, 111, 116, 117, 115, 100, 98}, 21,
    32  		},
    33  	}
    34  	for _, tt := range tests {
    35  		t.Run(tt.name, func(t *testing.T) {
    36  			got, got1 := EncodeEntry(tt.args.e)
    37  			if !reflect.DeepEqual(got, tt.want) {
    38  				t.Errorf("EncodeEntry() got = %v, want %v", got, tt.want)
    39  			}
    40  			if got1 != tt.want1 {
    41  				t.Errorf("EncodeEntry() got1 = %v, want %v", got1, tt.want1)
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  func Test_decodeHeader(t *testing.T) {
    48  	type args struct {
    49  		buf []byte
    50  	}
    51  	tests := []struct {
    52  		name  string
    53  		args  args
    54  		want  *entryHeader
    55  		want1 int64
    56  	}{
    57  		{
    58  			"nil", args{buf: nil}, nil, 0,
    59  		},
    60  		{
    61  			"no-enough-bytes", args{buf: []byte{1, 4, 3, 22}}, nil, 0,
    62  		},
    63  		{
    64  			"no-fields", args{buf: []byte{28, 223, 68, 33, 0, 0, 0, 0}}, &entryHeader{crc32: 558161692}, 8,
    65  		},
    66  		{
    67  			"normal", args{buf: []byte{101, 208, 223, 156, 0, 4, 14, 198, 147, 242, 166, 3}}, &entryHeader{crc32: 2631913573, typ: 0, kSize: 2, vSize: 7, expiredAt: 443434211}, 12,
    68  		},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			got, got1 := decodeHeader(tt.args.buf)
    73  			if !reflect.DeepEqual(got, tt.want) {
    74  				t.Errorf("decodeHeader() got = %v, want %v", got, tt.want)
    75  			}
    76  			if got1 != tt.want1 {
    77  				t.Errorf("decodeHeader() got1 = %v, want %v", got1, tt.want1)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func Test_getEntryCrc(t *testing.T) {
    84  	type args struct {
    85  		e *LogEntry
    86  		h []byte
    87  	}
    88  	tests := []struct {
    89  		name string
    90  		args args
    91  		want uint32
    92  	}{
    93  		{
    94  			"nil", args{e: nil, h: nil}, 0,
    95  		},
    96  		{
    97  			"no-fields", args{e: &LogEntry{}, h: []byte{0, 0, 0, 0}}, 558161692,
    98  		},
    99  		{
   100  			"normal", args{e: &LogEntry{Key: []byte("kv"), Value: []byte("lotusdb")}, h: []byte{0, 4, 14, 198, 147, 242, 166, 3}}, 2631913573,
   101  		},
   102  	}
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			if got := getEntryCrc(tt.args.e, tt.args.h); got != tt.want {
   106  				t.Errorf("getEntryCrc() = %v, want %v", got, tt.want)
   107  			}
   108  		})
   109  	}
   110  }