github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/format_test.go (about)

     1  // Copyright 2022 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package sstable
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestTableFormat_RoundTrip(t *testing.T) {
    14  	tcs := []struct {
    15  		name    string
    16  		magic   string
    17  		version uint32
    18  		want    TableFormat
    19  		wantErr string
    20  	}{
    21  		// Valid cases.
    22  		{
    23  			name:    "LevelDB",
    24  			magic:   levelDBMagic,
    25  			version: 0,
    26  			want:    TableFormatLevelDB,
    27  		},
    28  		{
    29  			name:    "RocksDBv2",
    30  			magic:   rocksDBMagic,
    31  			version: 2,
    32  			want:    TableFormatRocksDBv2,
    33  		},
    34  		{
    35  			name:    "PebbleDBv1",
    36  			magic:   bitalostableDBMagic,
    37  			version: 1,
    38  			want:    TableFormatPebblev1,
    39  		},
    40  		{
    41  			name:    "PebbleDBv2",
    42  			magic:   bitalostableDBMagic,
    43  			version: 2,
    44  			want:    TableFormatPebblev2,
    45  		},
    46  		// Invalid cases.
    47  		{
    48  			name:    "Invalid RocksDB version",
    49  			magic:   rocksDBMagic,
    50  			version: 1,
    51  			wantErr: "bitalostable/table: unsupported rocksdb format version 1",
    52  		},
    53  		{
    54  			name:    "Invalid PebbleDB version",
    55  			magic:   bitalostableDBMagic,
    56  			version: 3,
    57  			wantErr: "bitalostable/table: unsupported bitalostable format version 3",
    58  		},
    59  		{
    60  			name:    "Unknown magic string",
    61  			magic:   "foo",
    62  			wantErr: "bitalostable/table: invalid table (bad magic number)",
    63  		},
    64  	}
    65  
    66  	for _, tc := range tcs {
    67  		t.Run(tc.name, func(t *testing.T) {
    68  			// Tuple -> TableFormat.
    69  			f, err := ParseTableFormat([]byte(tc.magic), tc.version)
    70  			if tc.wantErr != "" {
    71  				require.Error(t, err)
    72  				require.Equal(t, tc.wantErr, err.Error())
    73  				return
    74  			}
    75  			require.NoError(t, err)
    76  			require.Equal(t, tc.want, f)
    77  
    78  			// TableFormat -> Tuple.
    79  			s, v := f.AsTuple()
    80  			require.Equal(t, tc.magic, s)
    81  			require.Equal(t, tc.version, v)
    82  		})
    83  	}
    84  }